blob: 92731335af9f9053924ef3a6ce04c21ace3c0928 [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 Patel5e005d82009-08-31 22:00:15 +0000238/// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
239bool DIDescriptor::isLexicalBlock() const {
240 assert (!isNull() && "Invalid descriptor!");
241 unsigned Tag = getTag();
242
243 return Tag == dwarf::DW_TAG_lexical_block;
244}
245
Devang Patel6ceea332009-08-31 18:49:10 +0000246//===----------------------------------------------------------------------===//
247// Simple Descriptor Constructors and other Methods
248//===----------------------------------------------------------------------===//
249
250DIType::DIType(MDNode *N) : DIDescriptor(N) {
251 if (!N) return;
252 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
253 DbgNode = 0;
254 }
255}
256
Devang Patel68afdc32009-01-05 18:33:01 +0000257unsigned DIArray::getNumElements() const {
Devang Patele4b27562009-08-28 23:24:31 +0000258 assert (DbgNode && "Invalid DIArray");
259 return DbgNode->getNumElements();
Devang Patel68afdc32009-01-05 18:33:01 +0000260}
Chris Lattnera45664f2008-11-10 02:56:27 +0000261
Devang Patelc4999d72009-07-22 18:23:44 +0000262/// replaceAllUsesWith - Replace all uses of debug info referenced by
263/// this descriptor. After this completes, the current debug info value
264/// is erased.
265void DIDerivedType::replaceAllUsesWith(DIDescriptor &D) {
266 if (isNull())
267 return;
268
Devang Patel6930f4f2009-07-22 18:56:16 +0000269 assert (!D.isNull() && "Can not replace with null");
Devang Patele4b27562009-08-28 23:24:31 +0000270 DbgNode->replaceAllUsesWith(D.getNode());
271 delete DbgNode;
Devang Patelc4999d72009-07-22 18:23:44 +0000272}
273
Devang Patelb79b5352009-01-19 23:21:49 +0000274/// Verify - Verify that a compile unit is well formed.
275bool DICompileUnit::Verify() const {
276 if (isNull())
277 return false;
Bill Wendling0582ae92009-03-13 04:39:26 +0000278 std::string Res;
279 if (getFilename(Res).empty())
280 return false;
Devang Patelb79b5352009-01-19 23:21:49 +0000281 // It is possible that directory and produce string is empty.
Bill Wendling0582ae92009-03-13 04:39:26 +0000282 return true;
Devang Patelb79b5352009-01-19 23:21:49 +0000283}
284
285/// Verify - Verify that a type descriptor is well formed.
286bool DIType::Verify() const {
287 if (isNull())
288 return false;
289 if (getContext().isNull())
290 return false;
291
292 DICompileUnit CU = getCompileUnit();
293 if (!CU.isNull() && !CU.Verify())
294 return false;
295 return true;
296}
297
298/// Verify - Verify that a composite type descriptor is well formed.
299bool DICompositeType::Verify() const {
300 if (isNull())
301 return false;
302 if (getContext().isNull())
303 return false;
304
305 DICompileUnit CU = getCompileUnit();
306 if (!CU.isNull() && !CU.Verify())
307 return false;
308 return true;
309}
310
311/// Verify - Verify that a subprogram descriptor is well formed.
312bool DISubprogram::Verify() const {
313 if (isNull())
314 return false;
315
316 if (getContext().isNull())
317 return false;
318
319 DICompileUnit CU = getCompileUnit();
320 if (!CU.Verify())
321 return false;
322
323 DICompositeType Ty = getType();
324 if (!Ty.isNull() && !Ty.Verify())
325 return false;
326 return true;
327}
328
329/// Verify - Verify that a global variable descriptor is well formed.
330bool DIGlobalVariable::Verify() const {
331 if (isNull())
332 return false;
333
334 if (getContext().isNull())
335 return false;
336
337 DICompileUnit CU = getCompileUnit();
Chris Lattnere3f6cea2009-05-05 04:55:56 +0000338 if (!CU.isNull() && !CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000339 return false;
340
341 DIType Ty = getType();
342 if (!Ty.Verify())
343 return false;
344
345 if (!getGlobal())
346 return false;
347
348 return true;
349}
350
351/// Verify - Verify that a variable descriptor is well formed.
352bool DIVariable::Verify() const {
353 if (isNull())
354 return false;
355
356 if (getContext().isNull())
357 return false;
358
359 DIType Ty = getType();
360 if (!Ty.Verify())
361 return false;
362
Devang Patelb79b5352009-01-19 23:21:49 +0000363 return true;
364}
365
Devang Patel36375ee2009-02-17 21:23:59 +0000366/// getOriginalTypeSize - If this type is derived from a base type then
367/// return base type size.
368uint64_t DIDerivedType::getOriginalTypeSize() const {
369 if (getTag() != dwarf::DW_TAG_member)
370 return getSizeInBits();
371 DIType BT = getTypeDerivedFrom();
372 if (BT.getTag() != dwarf::DW_TAG_base_type)
373 return getSizeInBits();
374 return BT.getSizeInBits();
375}
Devang Patelb79b5352009-01-19 23:21:49 +0000376
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000377/// describes - Return true if this subprogram provides debugging
378/// information for the function F.
379bool DISubprogram::describes(const Function *F) {
380 assert (F && "Invalid function");
381 std::string Name;
382 getLinkageName(Name);
383 if (Name.empty())
384 getName(Name);
Daniel Dunbar460f6562009-07-26 09:48:23 +0000385 if (F->getName() == Name)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000386 return true;
387 return false;
388}
389
Chris Lattnera45664f2008-11-10 02:56:27 +0000390//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000391// DIDescriptor: dump routines for all descriptors.
392//===----------------------------------------------------------------------===//
393
394
395/// dump - Print descriptor.
396void DIDescriptor::dump() const {
Devang Patele4b27562009-08-28 23:24:31 +0000397 errs() << "[" << dwarf::TagString(getTag()) << "] ";
398 errs().write_hex((intptr_t)DbgNode) << ']';
Devang Patel7136a652009-07-01 22:10:23 +0000399}
400
401/// dump - Print compile unit.
402void DICompileUnit::dump() const {
403 if (getLanguage())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000404 errs() << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000405
406 std::string Res1, Res2;
Chris Lattnera81d29b2009-08-23 07:33:14 +0000407 errs() << " [" << getDirectory(Res1) << "/" << getFilename(Res2) << " ]";
Devang Patel7136a652009-07-01 22:10:23 +0000408}
409
410/// dump - Print type.
411void DIType::dump() const {
412 if (isNull()) return;
413
414 std::string Res;
415 if (!getName(Res).empty())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000416 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000417
418 unsigned Tag = getTag();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000419 errs() << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000420
421 // TODO : Print context
422 getCompileUnit().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000423 errs() << " ["
424 << getLineNumber() << ", "
425 << getSizeInBits() << ", "
426 << getAlignInBits() << ", "
427 << getOffsetInBits()
428 << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000429
430 if (isPrivate())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000431 errs() << " [private] ";
Devang Patel7136a652009-07-01 22:10:23 +0000432 else if (isProtected())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000433 errs() << " [protected] ";
Devang Patel7136a652009-07-01 22:10:23 +0000434
435 if (isForwardDecl())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000436 errs() << " [fwd] ";
Devang Patel7136a652009-07-01 22:10:23 +0000437
Devang Patel6ceea332009-08-31 18:49:10 +0000438 if (isBasicType())
Devang Patele4b27562009-08-28 23:24:31 +0000439 DIBasicType(DbgNode).dump();
Devang Patel6ceea332009-08-31 18:49:10 +0000440 else if (isDerivedType())
Devang Patele4b27562009-08-28 23:24:31 +0000441 DIDerivedType(DbgNode).dump();
Devang Patel6ceea332009-08-31 18:49:10 +0000442 else if (isCompositeType())
Devang Patele4b27562009-08-28 23:24:31 +0000443 DICompositeType(DbgNode).dump();
Devang Patel7136a652009-07-01 22:10:23 +0000444 else {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000445 errs() << "Invalid DIType\n";
Devang Patel7136a652009-07-01 22:10:23 +0000446 return;
447 }
448
Chris Lattnera81d29b2009-08-23 07:33:14 +0000449 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000450}
451
452/// dump - Print basic type.
453void DIBasicType::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000454 errs() << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000455}
456
457/// dump - Print derived type.
458void DIDerivedType::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000459 errs() << "\n\t Derived From: "; getTypeDerivedFrom().dump();
Devang Patel7136a652009-07-01 22:10:23 +0000460}
461
462/// dump - Print composite type.
463void DICompositeType::dump() const {
464 DIArray A = getTypeArray();
465 if (A.isNull())
466 return;
Chris Lattnera81d29b2009-08-23 07:33:14 +0000467 errs() << " [" << A.getNumElements() << " elements]";
Devang Patel7136a652009-07-01 22:10:23 +0000468}
469
470/// dump - Print global.
471void DIGlobal::dump() const {
472 std::string Res;
473 if (!getName(Res).empty())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000474 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000475
476 unsigned Tag = getTag();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000477 errs() << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000478
479 // TODO : Print context
480 getCompileUnit().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000481 errs() << " [" << getLineNumber() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000482
483 if (isLocalToUnit())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000484 errs() << " [local] ";
Devang Patel7136a652009-07-01 22:10:23 +0000485
486 if (isDefinition())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000487 errs() << " [def] ";
Devang Patel7136a652009-07-01 22:10:23 +0000488
Devang Patel6ceea332009-08-31 18:49:10 +0000489 if (isGlobalVariable())
Devang Patele4b27562009-08-28 23:24:31 +0000490 DIGlobalVariable(DbgNode).dump();
Devang Patel7136a652009-07-01 22:10:23 +0000491
Chris Lattnera81d29b2009-08-23 07:33:14 +0000492 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000493}
494
495/// dump - Print subprogram.
496void DISubprogram::dump() const {
497 DIGlobal::dump();
498}
499
500/// dump - Print global variable.
501void DIGlobalVariable::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000502 errs() << " [";
503 getGlobal()->dump();
504 errs() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000505}
506
507/// dump - Print variable.
508void DIVariable::dump() const {
509 std::string Res;
510 if (!getName(Res).empty())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000511 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000512
513 getCompileUnit().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000514 errs() << " [" << getLineNumber() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000515 getType().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000516 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000517}
518
519//===----------------------------------------------------------------------===//
Chris Lattnera45664f2008-11-10 02:56:27 +0000520// DIFactory: Basic Helpers
521//===----------------------------------------------------------------------===//
522
Bill Wendlingdc817b62009-05-14 18:26:15 +0000523DIFactory::DIFactory(Module &m)
Owen Anderson99035272009-07-07 17:12:53 +0000524 : M(m), VMContext(M.getContext()), StopPointFn(0), FuncStartFn(0),
525 RegionStartFn(0), RegionEndFn(0),
Bill Wendlingdc817b62009-05-14 18:26:15 +0000526 DeclareFn(0) {
Owen Andersond7f2a6c2009-08-05 23:16:16 +0000527 EmptyStructPtr = PointerType::getUnqual(StructType::get(VMContext));
Chris Lattner497a7a82008-11-10 04:10:34 +0000528}
529
Chris Lattnera45664f2008-11-10 02:56:27 +0000530Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000531 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000532 "Tag too large for debug encoding!");
Owen Anderson1d0be152009-08-13 21:58:54 +0000533 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000534}
535
Chris Lattnera45664f2008-11-10 02:56:27 +0000536//===----------------------------------------------------------------------===//
537// DIFactory: Primary Constructors
538//===----------------------------------------------------------------------===//
539
Chris Lattnera45664f2008-11-10 02:56:27 +0000540/// GetOrCreateArray - Create an descriptor for an array of descriptors.
541/// This implicitly uniques the arrays created.
542DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
Devang Patele4b27562009-08-28 23:24:31 +0000543 SmallVector<Value*, 16> Elts;
Chris Lattnera45664f2008-11-10 02:56:27 +0000544
Devang Patele4b27562009-08-28 23:24:31 +0000545 if (NumTys == 0)
546 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
547 else
548 for (unsigned i = 0; i != NumTys; ++i)
549 Elts.push_back(Tys[i].getNode());
Devang Patel82459882009-08-26 05:01:18 +0000550
Devang Patele4b27562009-08-28 23:24:31 +0000551 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
Chris Lattnera45664f2008-11-10 02:56:27 +0000552}
553
554/// GetOrCreateSubrange - Create a descriptor for a value range. This
555/// implicitly uniques the values returned.
556DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
Devang Patele4b27562009-08-28 23:24:31 +0000557 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000558 GetTagConstant(dwarf::DW_TAG_subrange_type),
Owen Anderson1d0be152009-08-13 21:58:54 +0000559 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
560 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
Chris Lattnera45664f2008-11-10 02:56:27 +0000561 };
562
Devang Patele4b27562009-08-28 23:24:31 +0000563 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000564}
565
566
567
568/// CreateCompileUnit - Create a new descriptor for the specified compile
569/// unit. Note that this does not unique compile units within the module.
570DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
571 const std::string &Filename,
572 const std::string &Directory,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000573 const std::string &Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000574 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000575 bool isOptimized,
Devang Patel13319ce2009-02-17 22:43:44 +0000576 const char *Flags,
577 unsigned RunTimeVer) {
Devang Patele4b27562009-08-28 23:24:31 +0000578 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000579 GetTagConstant(dwarf::DW_TAG_compile_unit),
Devang Patele4b27562009-08-28 23:24:31 +0000580 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Owen Anderson1d0be152009-08-13 21:58:54 +0000581 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
Devang Patele4b27562009-08-28 23:24:31 +0000582 MDString::get(VMContext, Filename),
583 MDString::get(VMContext, Directory),
584 MDString::get(VMContext, Producer),
Owen Anderson1d0be152009-08-13 21:58:54 +0000585 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
586 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patele4b27562009-08-28 23:24:31 +0000587 MDString::get(VMContext, Flags),
Owen Anderson1d0be152009-08-13 21:58:54 +0000588 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000589 };
Devang Patele4b27562009-08-28 23:24:31 +0000590
591 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000592}
593
594/// CreateEnumerator - Create a single enumerator value.
595DIEnumerator DIFactory::CreateEnumerator(const std::string &Name, uint64_t Val){
Devang Patele4b27562009-08-28 23:24:31 +0000596 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000597 GetTagConstant(dwarf::DW_TAG_enumerator),
Devang Patele4b27562009-08-28 23:24:31 +0000598 MDString::get(VMContext, Name),
Owen Anderson1d0be152009-08-13 21:58:54 +0000599 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000600 };
Devang Patele4b27562009-08-28 23:24:31 +0000601 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000602}
603
604
605/// CreateBasicType - Create a basic type like int, float, etc.
606DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Bill Wendling0582ae92009-03-13 04:39:26 +0000607 const std::string &Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000608 DICompileUnit CompileUnit,
609 unsigned LineNumber,
610 uint64_t SizeInBits,
611 uint64_t AlignInBits,
612 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000613 unsigned Encoding) {
Devang Patele4b27562009-08-28 23:24:31 +0000614 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000615 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patele4b27562009-08-28 23:24:31 +0000616 Context.getNode(),
617 MDString::get(VMContext, Name),
618 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000619 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
620 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
621 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
622 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
623 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
624 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000625 };
Devang Patele4b27562009-08-28 23:24:31 +0000626 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000627}
628
629/// CreateDerivedType - Create a derived type like const qualified type,
630/// pointer, typedef, etc.
631DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
632 DIDescriptor Context,
633 const std::string &Name,
634 DICompileUnit CompileUnit,
635 unsigned LineNumber,
636 uint64_t SizeInBits,
637 uint64_t AlignInBits,
638 uint64_t OffsetInBits,
639 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000640 DIType DerivedFrom) {
Devang Patele4b27562009-08-28 23:24:31 +0000641 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000642 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000643 Context.getNode(),
644 MDString::get(VMContext, Name),
645 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000646 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
647 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
648 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
649 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
650 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patele4b27562009-08-28 23:24:31 +0000651 DerivedFrom.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000652 };
Devang Patele4b27562009-08-28 23:24:31 +0000653 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000654}
655
656/// CreateCompositeType - Create a composite type like array, struct, etc.
657DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
658 DIDescriptor Context,
659 const std::string &Name,
660 DICompileUnit CompileUnit,
661 unsigned LineNumber,
662 uint64_t SizeInBits,
663 uint64_t AlignInBits,
664 uint64_t OffsetInBits,
665 unsigned Flags,
666 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000667 DIArray Elements,
668 unsigned RuntimeLang) {
Owen Andersone277fed2009-07-07 16:31:25 +0000669
Devang Patele4b27562009-08-28 23:24:31 +0000670 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000671 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000672 Context.getNode(),
673 MDString::get(VMContext, Name),
674 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000675 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
676 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
677 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
678 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
679 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patele4b27562009-08-28 23:24:31 +0000680 DerivedFrom.getNode(),
681 Elements.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000682 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
Chris Lattnera45664f2008-11-10 02:56:27 +0000683 };
Devang Patele4b27562009-08-28 23:24:31 +0000684 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
Chris Lattnera45664f2008-11-10 02:56:27 +0000685}
686
687
688/// CreateSubprogram - Create a new descriptor for the specified subprogram.
689/// See comments in DISubprogram for descriptions of these fields. This
690/// method does not unique the generated descriptors.
691DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
692 const std::string &Name,
693 const std::string &DisplayName,
694 const std::string &LinkageName,
695 DICompileUnit CompileUnit,
696 unsigned LineNo, DIType Type,
697 bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000698 bool isDefinition) {
Devang Patel854967e2008-12-17 22:39:29 +0000699
Devang Patele4b27562009-08-28 23:24:31 +0000700 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000701 GetTagConstant(dwarf::DW_TAG_subprogram),
Devang Patele4b27562009-08-28 23:24:31 +0000702 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
703 Context.getNode(),
704 MDString::get(VMContext, Name),
705 MDString::get(VMContext, DisplayName),
706 MDString::get(VMContext, LinkageName),
707 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000708 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000709 Type.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000710 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
711 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition)
Chris Lattnera45664f2008-11-10 02:56:27 +0000712 };
Devang Patele4b27562009-08-28 23:24:31 +0000713 return DISubprogram(MDNode::get(VMContext, &Elts[0], 11));
Chris Lattnera45664f2008-11-10 02:56:27 +0000714}
715
716/// CreateGlobalVariable - Create a new descriptor for the specified global.
717DIGlobalVariable
718DIFactory::CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
719 const std::string &DisplayName,
720 const std::string &LinkageName,
721 DICompileUnit CompileUnit,
722 unsigned LineNo, DIType Type,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000723 bool isDefinition, llvm::GlobalVariable *Val) {
Devang Patele4b27562009-08-28 23:24:31 +0000724 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000725 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patele4b27562009-08-28 23:24:31 +0000726 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
727 Context.getNode(),
728 MDString::get(VMContext, Name),
729 MDString::get(VMContext, DisplayName),
730 MDString::get(VMContext, LinkageName),
731 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000732 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000733 Type.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000734 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
735 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
Devang Patele4b27562009-08-28 23:24:31 +0000736 Val
Chris Lattnera45664f2008-11-10 02:56:27 +0000737 };
Devang Patele4b27562009-08-28 23:24:31 +0000738
739 Value *const *Vs = &Elts[0];
740 MDNode *Node = MDNode::get(VMContext,Vs, 12);
741
742 // Create a named metadata so that we do not lose this mdnode.
743 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
744 NMD->addElement(Node);
745
746 return DIGlobalVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +0000747}
748
749
750/// CreateVariable - Create a new descriptor for the specified variable.
751DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
752 const std::string &Name,
753 DICompileUnit CompileUnit, unsigned LineNo,
Devang Pateldd9db662009-01-30 18:20:31 +0000754 DIType Type) {
Devang Patele4b27562009-08-28 23:24:31 +0000755 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000756 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000757 Context.getNode(),
758 MDString::get(VMContext, Name),
759 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000760 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000761 Type.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000762 };
Devang Patele4b27562009-08-28 23:24:31 +0000763 return DIVariable(MDNode::get(VMContext, &Elts[0], 6));
Chris Lattnera45664f2008-11-10 02:56:27 +0000764}
765
766
767/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +0000768/// specified parent VMContext.
Devang Patel5e005d82009-08-31 22:00:15 +0000769DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context) {
Devang Patele4b27562009-08-28 23:24:31 +0000770 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000771 GetTagConstant(dwarf::DW_TAG_lexical_block),
Devang Patele4b27562009-08-28 23:24:31 +0000772 Context.getNode()
Chris Lattnera45664f2008-11-10 02:56:27 +0000773 };
Devang Patel5e005d82009-08-31 22:00:15 +0000774 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 2));
Chris Lattnera45664f2008-11-10 02:56:27 +0000775}
776
777
778//===----------------------------------------------------------------------===//
779// DIFactory: Routines for inserting code into a function
780//===----------------------------------------------------------------------===//
781
782/// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
783/// inserting it at the end of the specified basic block.
784void DIFactory::InsertStopPoint(DICompileUnit CU, unsigned LineNo,
785 unsigned ColNo, BasicBlock *BB) {
786
787 // Lazily construct llvm.dbg.stoppoint function.
788 if (!StopPointFn)
789 StopPointFn = llvm::Intrinsic::getDeclaration(&M,
790 llvm::Intrinsic::dbg_stoppoint);
791
792 // Invoke llvm.dbg.stoppoint
793 Value *Args[] = {
Owen Anderson1d0be152009-08-13 21:58:54 +0000794 ConstantInt::get(llvm::Type::getInt32Ty(VMContext), LineNo),
795 ConstantInt::get(llvm::Type::getInt32Ty(VMContext), ColNo),
Devang Patele4b27562009-08-28 23:24:31 +0000796 CU.getNode()
Chris Lattnera45664f2008-11-10 02:56:27 +0000797 };
798 CallInst::Create(StopPointFn, Args, Args+3, "", BB);
799}
800
801/// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
802/// mark the start of the specified subprogram.
803void DIFactory::InsertSubprogramStart(DISubprogram SP, BasicBlock *BB) {
804 // Lazily construct llvm.dbg.func.start.
805 if (!FuncStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000806 FuncStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_func_start);
Chris Lattnera45664f2008-11-10 02:56:27 +0000807
808 // Call llvm.dbg.func.start which also implicitly sets a stoppoint.
Devang Patele4b27562009-08-28 23:24:31 +0000809 CallInst::Create(FuncStartFn, SP.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000810}
811
812/// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
813/// mark the start of a region for the specified scoping descriptor.
814void DIFactory::InsertRegionStart(DIDescriptor D, BasicBlock *BB) {
815 // Lazily construct llvm.dbg.region.start function.
816 if (!RegionStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000817 RegionStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_start);
818
Chris Lattnera45664f2008-11-10 02:56:27 +0000819 // Call llvm.dbg.func.start.
Devang Patele4b27562009-08-28 23:24:31 +0000820 CallInst::Create(RegionStartFn, D.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000821}
822
Chris Lattnera45664f2008-11-10 02:56:27 +0000823/// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
824/// mark the end of a region for the specified scoping descriptor.
825void DIFactory::InsertRegionEnd(DIDescriptor D, BasicBlock *BB) {
826 // Lazily construct llvm.dbg.region.end function.
827 if (!RegionEndFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000828 RegionEndFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_end);
829
830 // Call llvm.dbg.region.end.
Devang Patele4b27562009-08-28 23:24:31 +0000831 CallInst::Create(RegionEndFn, D.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000832}
833
834/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000835void DIFactory::InsertDeclare(Value *Storage, DIVariable D, BasicBlock *BB) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000836 // Cast the storage to a {}* for the call to llvm.dbg.declare.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000837 Storage = new BitCastInst(Storage, EmptyStructPtr, "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000838
839 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000840 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
841
Devang Patele4b27562009-08-28 23:24:31 +0000842 Value *Args[] = { Storage, D.getNode() };
Chris Lattnera45664f2008-11-10 02:56:27 +0000843 CallInst::Create(DeclareFn, Args, Args+2, "", BB);
844}
Torok Edwin620f2802008-12-16 09:07:36 +0000845
Devang Patele4b27562009-08-28 23:24:31 +0000846
Devang Pateld2f79a12009-07-28 19:55:13 +0000847//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +0000848// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +0000849//===----------------------------------------------------------------------===//
850
Devang Patel98c65172009-07-30 18:25:15 +0000851/// processModule - Process entire module and collect debug info.
852void DebugInfoFinder::processModule(Module &M) {
Devang Patele4b27562009-08-28 23:24:31 +0000853
854
Devang Pateld2f79a12009-07-28 19:55:13 +0000855 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
856 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
857 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
858 ++BI) {
859 if (DbgStopPointInst *SPI = dyn_cast<DbgStopPointInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000860 processStopPoint(SPI);
Devang Pateld2f79a12009-07-28 19:55:13 +0000861 else if (DbgFuncStartInst *FSI = dyn_cast<DbgFuncStartInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000862 processFuncStart(FSI);
Devang Patele802f1c2009-07-30 17:30:23 +0000863 else if (DbgRegionStartInst *DRS = dyn_cast<DbgRegionStartInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000864 processRegionStart(DRS);
Devang Patele802f1c2009-07-30 17:30:23 +0000865 else if (DbgRegionEndInst *DRE = dyn_cast<DbgRegionEndInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000866 processRegionEnd(DRE);
Devang Patelb4d31302009-07-31 18:18:52 +0000867 else if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
868 processDeclare(DDI);
Devang Pateld2f79a12009-07-28 19:55:13 +0000869 }
Devang Patele4b27562009-08-28 23:24:31 +0000870
871 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
872 if (!NMD)
873 return;
874
875 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
876 DIGlobalVariable DIG(cast<MDNode>(NMD->getElement(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +0000877 if (addGlobalVariable(DIG)) {
878 addCompileUnit(DIG.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +0000879 processType(DIG.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +0000880 }
881 }
882}
883
Devang Patel98c65172009-07-30 18:25:15 +0000884/// processType - Process DIType.
885void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +0000886 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +0000887 return;
888
889 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +0000890 if (DT.isCompositeType()) {
Devang Patele4b27562009-08-28 23:24:31 +0000891 DICompositeType DCT(DT.getNode());
Devang Patel98c65172009-07-30 18:25:15 +0000892 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +0000893 DIArray DA = DCT.getTypeArray();
894 if (!DA.isNull())
895 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
896 DIDescriptor D = DA.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +0000897 DIType TypeE = DIType(D.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +0000898 if (!TypeE.isNull())
Devang Patel98c65172009-07-30 18:25:15 +0000899 processType(TypeE);
Devang Pateld2f79a12009-07-28 19:55:13 +0000900 else
Devang Patele4b27562009-08-28 23:24:31 +0000901 processSubprogram(DISubprogram(D.getNode()));
Devang Pateld2f79a12009-07-28 19:55:13 +0000902 }
Devang Patel6ceea332009-08-31 18:49:10 +0000903 } else if (DT.isDerivedType()) {
Devang Patele4b27562009-08-28 23:24:31 +0000904 DIDerivedType DDT(DT.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +0000905 if (!DDT.isNull())
Devang Patel98c65172009-07-30 18:25:15 +0000906 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +0000907 }
908}
909
Devang Patel98c65172009-07-30 18:25:15 +0000910/// processSubprogram - Process DISubprogram.
911void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Patele802f1c2009-07-30 17:30:23 +0000912 if (SP.isNull())
913 return;
Devang Pateld2f79a12009-07-28 19:55:13 +0000914 if (!addSubprogram(SP))
915 return;
916 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +0000917 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +0000918}
919
Devang Patel98c65172009-07-30 18:25:15 +0000920/// processStopPoint - Process DbgStopPointInst.
921void DebugInfoFinder::processStopPoint(DbgStopPointInst *SPI) {
Devang Patele4b27562009-08-28 23:24:31 +0000922 MDNode *Context = dyn_cast<MDNode>(SPI->getContext());
Devang Pateld2f79a12009-07-28 19:55:13 +0000923 addCompileUnit(DICompileUnit(Context));
924}
925
Devang Patel98c65172009-07-30 18:25:15 +0000926/// processFuncStart - Process DbgFuncStartInst.
927void DebugInfoFinder::processFuncStart(DbgFuncStartInst *FSI) {
Devang Patele4b27562009-08-28 23:24:31 +0000928 MDNode *SP = dyn_cast<MDNode>(FSI->getSubprogram());
Devang Patel98c65172009-07-30 18:25:15 +0000929 processSubprogram(DISubprogram(SP));
Devang Pateld2f79a12009-07-28 19:55:13 +0000930}
931
Devang Patel98c65172009-07-30 18:25:15 +0000932/// processRegionStart - Process DbgRegionStart.
933void DebugInfoFinder::processRegionStart(DbgRegionStartInst *DRS) {
Devang Patele4b27562009-08-28 23:24:31 +0000934 MDNode *SP = dyn_cast<MDNode>(DRS->getContext());
Devang Patel98c65172009-07-30 18:25:15 +0000935 processSubprogram(DISubprogram(SP));
Devang Patele802f1c2009-07-30 17:30:23 +0000936}
937
Devang Patel98c65172009-07-30 18:25:15 +0000938/// processRegionEnd - Process DbgRegionEnd.
939void DebugInfoFinder::processRegionEnd(DbgRegionEndInst *DRE) {
Devang Patele4b27562009-08-28 23:24:31 +0000940 MDNode *SP = dyn_cast<MDNode>(DRE->getContext());
Devang Patel98c65172009-07-30 18:25:15 +0000941 processSubprogram(DISubprogram(SP));
Devang Patele802f1c2009-07-30 17:30:23 +0000942}
943
Devang Patelb4d31302009-07-31 18:18:52 +0000944/// processDeclare - Process DbgDeclareInst.
945void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patele4b27562009-08-28 23:24:31 +0000946 DIVariable DV(cast<MDNode>(DDI->getVariable()));
Devang Patelb4d31302009-07-31 18:18:52 +0000947 if (DV.isNull())
948 return;
949
Devang Patele4b27562009-08-28 23:24:31 +0000950 if (!NodesSeen.insert(DV.getNode()))
Devang Patelb4d31302009-07-31 18:18:52 +0000951 return;
952
953 addCompileUnit(DV.getCompileUnit());
954 processType(DV.getType());
955}
956
Devang Patel72bcdb62009-08-10 22:09:58 +0000957/// addType - Add type into Tys.
958bool DebugInfoFinder::addType(DIType DT) {
959 if (DT.isNull())
960 return false;
961
Devang Patele4b27562009-08-28 23:24:31 +0000962 if (!NodesSeen.insert(DT.getNode()))
Devang Patel72bcdb62009-08-10 22:09:58 +0000963 return false;
964
Devang Patele4b27562009-08-28 23:24:31 +0000965 TYs.push_back(DT.getNode());
Devang Patel72bcdb62009-08-10 22:09:58 +0000966 return true;
967}
968
Devang Pateld2f79a12009-07-28 19:55:13 +0000969/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +0000970bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Pateld2f79a12009-07-28 19:55:13 +0000971 if (CU.isNull())
972 return false;
973
Devang Patele4b27562009-08-28 23:24:31 +0000974 if (!NodesSeen.insert(CU.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +0000975 return false;
976
Devang Patele4b27562009-08-28 23:24:31 +0000977 CUs.push_back(CU.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +0000978 return true;
979}
980
981/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +0000982bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Pateld2f79a12009-07-28 19:55:13 +0000983 if (DIG.isNull())
984 return false;
985
Devang Patele4b27562009-08-28 23:24:31 +0000986 if (!NodesSeen.insert(DIG.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +0000987 return false;
988
Devang Patele4b27562009-08-28 23:24:31 +0000989 GVs.push_back(DIG.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +0000990 return true;
991}
992
993// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +0000994bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +0000995 if (SP.isNull())
996 return false;
997
Devang Patele4b27562009-08-28 23:24:31 +0000998 if (!NodesSeen.insert(SP.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +0000999 return false;
1000
Devang Patele4b27562009-08-28 23:24:31 +00001001 SPs.push_back(SP.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001002 return true;
1003}
1004
Torok Edwin620f2802008-12-16 09:07:36 +00001005namespace llvm {
Bill Wendlingdc817b62009-05-14 18:26:15 +00001006 /// findStopPoint - Find the stoppoint coressponding to this instruction, that
1007 /// is the stoppoint that dominates this instruction.
1008 const DbgStopPointInst *findStopPoint(const Instruction *Inst) {
Torok Edwin620f2802008-12-16 09:07:36 +00001009 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst))
1010 return DSI;
1011
1012 const BasicBlock *BB = Inst->getParent();
1013 BasicBlock::const_iterator I = Inst, B;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001014 while (BB) {
Torok Edwin620f2802008-12-16 09:07:36 +00001015 B = BB->begin();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001016
Torok Edwin620f2802008-12-16 09:07:36 +00001017 // A BB consisting only of a terminator can't have a stoppoint.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001018 while (I != B) {
1019 --I;
1020 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1021 return DSI;
Torok Edwin620f2802008-12-16 09:07:36 +00001022 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001023
1024 // This BB didn't have a stoppoint: if there is only one predecessor, look
1025 // for a stoppoint there. We could use getIDom(), but that would require
1026 // dominator info.
Torok Edwin620f2802008-12-16 09:07:36 +00001027 BB = I->getParent()->getUniquePredecessor();
1028 if (BB)
1029 I = BB->getTerminator();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001030 }
1031
Torok Edwin620f2802008-12-16 09:07:36 +00001032 return 0;
1033 }
1034
Bill Wendlingdc817b62009-05-14 18:26:15 +00001035 /// findBBStopPoint - Find the stoppoint corresponding to first real
1036 /// (non-debug intrinsic) instruction in this Basic Block, and return the
1037 /// stoppoint for it.
1038 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB) {
1039 for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001040 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1041 return DSI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001042
1043 // Fallback to looking for stoppoint of unique predecessor. Useful if this
1044 // BB contains no stoppoints, but unique predecessor does.
Torok Edwin620f2802008-12-16 09:07:36 +00001045 BB = BB->getUniquePredecessor();
1046 if (BB)
1047 return findStopPoint(BB->getTerminator());
Bill Wendlingdc817b62009-05-14 18:26:15 +00001048
Torok Edwin620f2802008-12-16 09:07:36 +00001049 return 0;
1050 }
1051
Bill Wendlingdc817b62009-05-14 18:26:15 +00001052 Value *findDbgGlobalDeclare(GlobalVariable *V) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001053 const Module *M = V->getParent();
Devang Patele4b27562009-08-28 23:24:31 +00001054 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1055 if (!NMD)
1056 return 0;
Owen Anderson99035272009-07-07 17:12:53 +00001057
Devang Patele4b27562009-08-28 23:24:31 +00001058 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
1059 DIGlobalVariable DIG(cast_or_null<MDNode>(NMD->getElement(i)));
1060 if (DIG.isNull())
1061 continue;
1062 if (DIG.getGlobal() == V)
1063 return DIG.getNode();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001064 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001065 return 0;
1066 }
1067
Bill Wendlingdc817b62009-05-14 18:26:15 +00001068 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
Torok Edwin620f2802008-12-16 09:07:36 +00001069 /// It looks through pointer casts too.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001070 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts) {
Torok Edwin620f2802008-12-16 09:07:36 +00001071 if (stripCasts) {
1072 V = V->stripPointerCasts();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001073
Torok Edwin620f2802008-12-16 09:07:36 +00001074 // Look for the bitcast.
1075 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001076 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001077 if (isa<BitCastInst>(I))
1078 return findDbgDeclare(*I, false);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001079
Torok Edwin620f2802008-12-16 09:07:36 +00001080 return 0;
1081 }
1082
Bill Wendlingdc817b62009-05-14 18:26:15 +00001083 // Find llvm.dbg.declare among uses of the instruction.
Torok Edwin620f2802008-12-16 09:07:36 +00001084 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001085 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001086 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))
1087 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001088
Torok Edwin620f2802008-12-16 09:07:36 +00001089 return 0;
1090 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001091
Bill Wendlingdc817b62009-05-14 18:26:15 +00001092 bool getLocationInfo(const Value *V, std::string &DisplayName,
1093 std::string &Type, unsigned &LineNo, std::string &File,
1094 std::string &Dir) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001095 DICompileUnit Unit;
1096 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001097
Torok Edwinff7d0e92009-03-10 13:41:26 +00001098 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1099 Value *DIGV = findDbgGlobalDeclare(GV);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001100 if (!DIGV) return false;
Devang Patele4b27562009-08-28 23:24:31 +00001101 DIGlobalVariable Var(cast<MDNode>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001102
Bill Wendling0582ae92009-03-13 04:39:26 +00001103 Var.getDisplayName(DisplayName);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001104 LineNo = Var.getLineNumber();
1105 Unit = Var.getCompileUnit();
1106 TypeD = Var.getType();
1107 } else {
1108 const DbgDeclareInst *DDI = findDbgDeclare(V);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001109 if (!DDI) return false;
Devang Patele4b27562009-08-28 23:24:31 +00001110 DIVariable Var(cast<MDNode>(DDI->getVariable()));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001111
Bill Wendling0582ae92009-03-13 04:39:26 +00001112 Var.getName(DisplayName);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001113 LineNo = Var.getLineNumber();
1114 Unit = Var.getCompileUnit();
1115 TypeD = Var.getType();
1116 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001117
Bill Wendling0582ae92009-03-13 04:39:26 +00001118 TypeD.getName(Type);
1119 Unit.getFilename(File);
1120 Unit.getDirectory(Dir);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001121 return true;
1122 }
Devang Patel13e16b62009-06-26 01:49:18 +00001123
Devang Patel9e529c32009-07-02 01:15:24 +00001124 /// isValidDebugInfoIntrinsic - Return true if SPI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001125 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001126 bool isValidDebugInfoIntrinsic(DbgStopPointInst &SPI,
1127 CodeGenOpt::Level OptLev) {
1128 return DIDescriptor::ValidDebugInfo(SPI.getContext(), OptLev);
1129 }
1130
1131 /// isValidDebugInfoIntrinsic - Return true if FSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001132 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001133 bool isValidDebugInfoIntrinsic(DbgFuncStartInst &FSI,
1134 CodeGenOpt::Level OptLev) {
1135 return DIDescriptor::ValidDebugInfo(FSI.getSubprogram(), OptLev);
1136 }
1137
1138 /// isValidDebugInfoIntrinsic - Return true if RSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001139 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001140 bool isValidDebugInfoIntrinsic(DbgRegionStartInst &RSI,
1141 CodeGenOpt::Level OptLev) {
1142 return DIDescriptor::ValidDebugInfo(RSI.getContext(), OptLev);
1143 }
1144
1145 /// isValidDebugInfoIntrinsic - Return true if REI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001146 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001147 bool isValidDebugInfoIntrinsic(DbgRegionEndInst &REI,
1148 CodeGenOpt::Level OptLev) {
1149 return DIDescriptor::ValidDebugInfo(REI.getContext(), OptLev);
1150 }
1151
1152
1153 /// isValidDebugInfoIntrinsic - Return true if DI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001154 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001155 bool isValidDebugInfoIntrinsic(DbgDeclareInst &DI,
1156 CodeGenOpt::Level OptLev) {
1157 return DIDescriptor::ValidDebugInfo(DI.getVariable(), OptLev);
1158 }
1159
1160 /// ExtractDebugLocation - Extract debug location information
1161 /// from llvm.dbg.stoppoint intrinsic.
1162 DebugLoc ExtractDebugLocation(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +00001163 DebugLocTracker &DebugLocInfo) {
1164 DebugLoc DL;
1165 Value *Context = SPI.getContext();
Devang Patel9e529c32009-07-02 01:15:24 +00001166
1167 // If this location is already tracked then use it.
Devang Patele4b27562009-08-28 23:24:31 +00001168 DebugLocTuple Tuple(cast<MDNode>(Context), SPI.getLine(),
Devang Patel9e529c32009-07-02 01:15:24 +00001169 SPI.getColumn());
1170 DenseMap<DebugLocTuple, unsigned>::iterator II
1171 = DebugLocInfo.DebugIdMap.find(Tuple);
1172 if (II != DebugLocInfo.DebugIdMap.end())
1173 return DebugLoc::get(II->second);
1174
1175 // Add a new location entry.
1176 unsigned Id = DebugLocInfo.DebugLocations.size();
1177 DebugLocInfo.DebugLocations.push_back(Tuple);
1178 DebugLocInfo.DebugIdMap[Tuple] = Id;
1179
1180 return DebugLoc::get(Id);
1181 }
1182
1183 /// ExtractDebugLocation - Extract debug location information
1184 /// from llvm.dbg.func_start intrinsic.
1185 DebugLoc ExtractDebugLocation(DbgFuncStartInst &FSI,
Devang Patel9e529c32009-07-02 01:15:24 +00001186 DebugLocTracker &DebugLocInfo) {
1187 DebugLoc DL;
1188 Value *SP = FSI.getSubprogram();
Devang Patel9e529c32009-07-02 01:15:24 +00001189
Devang Patele4b27562009-08-28 23:24:31 +00001190 DISubprogram Subprogram(cast<MDNode>(SP));
Devang Patel9e529c32009-07-02 01:15:24 +00001191 unsigned Line = Subprogram.getLineNumber();
1192 DICompileUnit CU(Subprogram.getCompileUnit());
1193
1194 // If this location is already tracked then use it.
Devang Patele4b27562009-08-28 23:24:31 +00001195 DebugLocTuple Tuple(CU.getNode(), Line, /* Column */ 0);
Devang Patel9e529c32009-07-02 01:15:24 +00001196 DenseMap<DebugLocTuple, unsigned>::iterator II
1197 = DebugLocInfo.DebugIdMap.find(Tuple);
1198 if (II != DebugLocInfo.DebugIdMap.end())
1199 return DebugLoc::get(II->second);
1200
1201 // Add a new location entry.
1202 unsigned Id = DebugLocInfo.DebugLocations.size();
1203 DebugLocInfo.DebugLocations.push_back(Tuple);
1204 DebugLocInfo.DebugIdMap[Tuple] = Id;
1205
1206 return DebugLoc::get(Id);
1207 }
1208
1209 /// isInlinedFnStart - Return true if FSI is starting an inlined function.
1210 bool isInlinedFnStart(DbgFuncStartInst &FSI, const Function *CurrentFn) {
Devang Patele4b27562009-08-28 23:24:31 +00001211 DISubprogram Subprogram(cast<MDNode>(FSI.getSubprogram()));
Devang Patel9e529c32009-07-02 01:15:24 +00001212 if (Subprogram.describes(CurrentFn))
1213 return false;
1214
1215 return true;
1216 }
1217
1218 /// isInlinedFnEnd - Return true if REI is ending an inlined function.
1219 bool isInlinedFnEnd(DbgRegionEndInst &REI, const Function *CurrentFn) {
Devang Patele4b27562009-08-28 23:24:31 +00001220 DISubprogram Subprogram(cast<MDNode>(REI.getContext()));
Devang Patel9e529c32009-07-02 01:15:24 +00001221 if (Subprogram.isNull() || Subprogram.describes(CurrentFn))
1222 return false;
1223
1224 return true;
1225 }
Torok Edwin620f2802008-12-16 09:07:36 +00001226}