blob: d09704f303927f535e12c137b57056a24cc330bc [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 {
Devang Patel82dfc0c2009-08-31 22:47:13 +0000497 std::string Res;
498 if (!getName(Res).empty())
499 errs() << " [" << Res << "] ";
500
501 unsigned Tag = getTag();
502 errs() << " [" << dwarf::TagString(Tag) << "] ";
503
504 // TODO : Print context
505 getCompileUnit().dump();
506 errs() << " [" << getLineNumber() << "] ";
507
508 if (isLocalToUnit())
509 errs() << " [local] ";
510
511 if (isDefinition())
512 errs() << " [def] ";
513
514 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000515}
516
517/// dump - Print global variable.
518void DIGlobalVariable::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000519 errs() << " [";
520 getGlobal()->dump();
521 errs() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000522}
523
524/// dump - Print variable.
525void DIVariable::dump() const {
526 std::string Res;
527 if (!getName(Res).empty())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000528 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000529
530 getCompileUnit().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000531 errs() << " [" << getLineNumber() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000532 getType().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000533 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000534}
535
536//===----------------------------------------------------------------------===//
Chris Lattnera45664f2008-11-10 02:56:27 +0000537// DIFactory: Basic Helpers
538//===----------------------------------------------------------------------===//
539
Bill Wendlingdc817b62009-05-14 18:26:15 +0000540DIFactory::DIFactory(Module &m)
Owen Anderson99035272009-07-07 17:12:53 +0000541 : M(m), VMContext(M.getContext()), StopPointFn(0), FuncStartFn(0),
542 RegionStartFn(0), RegionEndFn(0),
Bill Wendlingdc817b62009-05-14 18:26:15 +0000543 DeclareFn(0) {
Owen Andersond7f2a6c2009-08-05 23:16:16 +0000544 EmptyStructPtr = PointerType::getUnqual(StructType::get(VMContext));
Chris Lattner497a7a82008-11-10 04:10:34 +0000545}
546
Chris Lattnera45664f2008-11-10 02:56:27 +0000547Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000548 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000549 "Tag too large for debug encoding!");
Owen Anderson1d0be152009-08-13 21:58:54 +0000550 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000551}
552
Chris Lattnera45664f2008-11-10 02:56:27 +0000553//===----------------------------------------------------------------------===//
554// DIFactory: Primary Constructors
555//===----------------------------------------------------------------------===//
556
Chris Lattnera45664f2008-11-10 02:56:27 +0000557/// GetOrCreateArray - Create an descriptor for an array of descriptors.
558/// This implicitly uniques the arrays created.
559DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
Devang Patele4b27562009-08-28 23:24:31 +0000560 SmallVector<Value*, 16> Elts;
Chris Lattnera45664f2008-11-10 02:56:27 +0000561
Devang Patele4b27562009-08-28 23:24:31 +0000562 if (NumTys == 0)
563 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
564 else
565 for (unsigned i = 0; i != NumTys; ++i)
566 Elts.push_back(Tys[i].getNode());
Devang Patel82459882009-08-26 05:01:18 +0000567
Devang Patele4b27562009-08-28 23:24:31 +0000568 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
Chris Lattnera45664f2008-11-10 02:56:27 +0000569}
570
571/// GetOrCreateSubrange - Create a descriptor for a value range. This
572/// implicitly uniques the values returned.
573DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
Devang Patele4b27562009-08-28 23:24:31 +0000574 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000575 GetTagConstant(dwarf::DW_TAG_subrange_type),
Owen Anderson1d0be152009-08-13 21:58:54 +0000576 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
577 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
Chris Lattnera45664f2008-11-10 02:56:27 +0000578 };
579
Devang Patele4b27562009-08-28 23:24:31 +0000580 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000581}
582
583
584
585/// CreateCompileUnit - Create a new descriptor for the specified compile
586/// unit. Note that this does not unique compile units within the module.
587DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
588 const std::string &Filename,
589 const std::string &Directory,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000590 const std::string &Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000591 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000592 bool isOptimized,
Devang Patel13319ce2009-02-17 22:43:44 +0000593 const char *Flags,
594 unsigned RunTimeVer) {
Devang Patele4b27562009-08-28 23:24:31 +0000595 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000596 GetTagConstant(dwarf::DW_TAG_compile_unit),
Devang Patele4b27562009-08-28 23:24:31 +0000597 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Owen Anderson1d0be152009-08-13 21:58:54 +0000598 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
Devang Patele4b27562009-08-28 23:24:31 +0000599 MDString::get(VMContext, Filename),
600 MDString::get(VMContext, Directory),
601 MDString::get(VMContext, Producer),
Owen Anderson1d0be152009-08-13 21:58:54 +0000602 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
603 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patele4b27562009-08-28 23:24:31 +0000604 MDString::get(VMContext, Flags),
Owen Anderson1d0be152009-08-13 21:58:54 +0000605 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000606 };
Devang Patele4b27562009-08-28 23:24:31 +0000607
608 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000609}
610
611/// CreateEnumerator - Create a single enumerator value.
612DIEnumerator DIFactory::CreateEnumerator(const std::string &Name, uint64_t Val){
Devang Patele4b27562009-08-28 23:24:31 +0000613 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000614 GetTagConstant(dwarf::DW_TAG_enumerator),
Devang Patele4b27562009-08-28 23:24:31 +0000615 MDString::get(VMContext, Name),
Owen Anderson1d0be152009-08-13 21:58:54 +0000616 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000617 };
Devang Patele4b27562009-08-28 23:24:31 +0000618 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000619}
620
621
622/// CreateBasicType - Create a basic type like int, float, etc.
623DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Bill Wendling0582ae92009-03-13 04:39:26 +0000624 const std::string &Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000625 DICompileUnit CompileUnit,
626 unsigned LineNumber,
627 uint64_t SizeInBits,
628 uint64_t AlignInBits,
629 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000630 unsigned Encoding) {
Devang Patele4b27562009-08-28 23:24:31 +0000631 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000632 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patele4b27562009-08-28 23:24:31 +0000633 Context.getNode(),
634 MDString::get(VMContext, Name),
635 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000636 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
637 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
638 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
639 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
640 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
641 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000642 };
Devang Patele4b27562009-08-28 23:24:31 +0000643 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000644}
645
646/// CreateDerivedType - Create a derived type like const qualified type,
647/// pointer, typedef, etc.
648DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
649 DIDescriptor Context,
650 const std::string &Name,
651 DICompileUnit CompileUnit,
652 unsigned LineNumber,
653 uint64_t SizeInBits,
654 uint64_t AlignInBits,
655 uint64_t OffsetInBits,
656 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000657 DIType DerivedFrom) {
Devang Patele4b27562009-08-28 23:24:31 +0000658 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000659 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000660 Context.getNode(),
661 MDString::get(VMContext, Name),
662 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000663 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
664 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
665 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
666 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
667 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patele4b27562009-08-28 23:24:31 +0000668 DerivedFrom.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000669 };
Devang Patele4b27562009-08-28 23:24:31 +0000670 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000671}
672
673/// CreateCompositeType - Create a composite type like array, struct, etc.
674DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
675 DIDescriptor Context,
676 const std::string &Name,
677 DICompileUnit CompileUnit,
678 unsigned LineNumber,
679 uint64_t SizeInBits,
680 uint64_t AlignInBits,
681 uint64_t OffsetInBits,
682 unsigned Flags,
683 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000684 DIArray Elements,
685 unsigned RuntimeLang) {
Owen Andersone277fed2009-07-07 16:31:25 +0000686
Devang Patele4b27562009-08-28 23:24:31 +0000687 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000688 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000689 Context.getNode(),
690 MDString::get(VMContext, Name),
691 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000692 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
693 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
694 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
695 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
696 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patele4b27562009-08-28 23:24:31 +0000697 DerivedFrom.getNode(),
698 Elements.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000699 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
Chris Lattnera45664f2008-11-10 02:56:27 +0000700 };
Devang Patele4b27562009-08-28 23:24:31 +0000701 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
Chris Lattnera45664f2008-11-10 02:56:27 +0000702}
703
704
705/// CreateSubprogram - Create a new descriptor for the specified subprogram.
706/// See comments in DISubprogram for descriptions of these fields. This
707/// method does not unique the generated descriptors.
708DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
709 const std::string &Name,
710 const std::string &DisplayName,
711 const std::string &LinkageName,
712 DICompileUnit CompileUnit,
713 unsigned LineNo, DIType Type,
714 bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000715 bool isDefinition) {
Devang Patel854967e2008-12-17 22:39:29 +0000716
Devang Patele4b27562009-08-28 23:24:31 +0000717 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000718 GetTagConstant(dwarf::DW_TAG_subprogram),
Devang Patele4b27562009-08-28 23:24:31 +0000719 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
720 Context.getNode(),
721 MDString::get(VMContext, Name),
722 MDString::get(VMContext, DisplayName),
723 MDString::get(VMContext, LinkageName),
724 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000725 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000726 Type.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000727 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
728 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition)
Chris Lattnera45664f2008-11-10 02:56:27 +0000729 };
Devang Patele4b27562009-08-28 23:24:31 +0000730 return DISubprogram(MDNode::get(VMContext, &Elts[0], 11));
Chris Lattnera45664f2008-11-10 02:56:27 +0000731}
732
733/// CreateGlobalVariable - Create a new descriptor for the specified global.
734DIGlobalVariable
735DIFactory::CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
736 const std::string &DisplayName,
737 const std::string &LinkageName,
738 DICompileUnit CompileUnit,
739 unsigned LineNo, DIType Type,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000740 bool isDefinition, llvm::GlobalVariable *Val) {
Devang Patele4b27562009-08-28 23:24:31 +0000741 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000742 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patele4b27562009-08-28 23:24:31 +0000743 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
744 Context.getNode(),
745 MDString::get(VMContext, Name),
746 MDString::get(VMContext, DisplayName),
747 MDString::get(VMContext, LinkageName),
748 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000749 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000750 Type.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000751 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
752 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
Devang Patele4b27562009-08-28 23:24:31 +0000753 Val
Chris Lattnera45664f2008-11-10 02:56:27 +0000754 };
Devang Patele4b27562009-08-28 23:24:31 +0000755
756 Value *const *Vs = &Elts[0];
757 MDNode *Node = MDNode::get(VMContext,Vs, 12);
758
759 // Create a named metadata so that we do not lose this mdnode.
760 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
761 NMD->addElement(Node);
762
763 return DIGlobalVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +0000764}
765
766
767/// CreateVariable - Create a new descriptor for the specified variable.
768DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
769 const std::string &Name,
770 DICompileUnit CompileUnit, unsigned LineNo,
Devang Pateldd9db662009-01-30 18:20:31 +0000771 DIType Type) {
Devang Patele4b27562009-08-28 23:24:31 +0000772 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000773 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000774 Context.getNode(),
775 MDString::get(VMContext, Name),
776 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000777 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000778 Type.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000779 };
Devang Patele4b27562009-08-28 23:24:31 +0000780 return DIVariable(MDNode::get(VMContext, &Elts[0], 6));
Chris Lattnera45664f2008-11-10 02:56:27 +0000781}
782
783
784/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +0000785/// specified parent VMContext.
Devang Patel5e005d82009-08-31 22:00:15 +0000786DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context) {
Devang Patele4b27562009-08-28 23:24:31 +0000787 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000788 GetTagConstant(dwarf::DW_TAG_lexical_block),
Devang Patele4b27562009-08-28 23:24:31 +0000789 Context.getNode()
Chris Lattnera45664f2008-11-10 02:56:27 +0000790 };
Devang Patel5e005d82009-08-31 22:00:15 +0000791 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 2));
Chris Lattnera45664f2008-11-10 02:56:27 +0000792}
793
Devang Patelf98d8fe2009-09-01 01:14:15 +0000794/// CreateLocation - Creates a debug info location.
795DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
796 DIScope S, DILocation OrigLoc) {
797 Value *Elts[] = {
798 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
799 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
800 S.getNode(),
801 OrigLoc.getNode(),
802 };
803 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
804}
805
Chris Lattnera45664f2008-11-10 02:56:27 +0000806
807//===----------------------------------------------------------------------===//
808// DIFactory: Routines for inserting code into a function
809//===----------------------------------------------------------------------===//
810
811/// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
812/// inserting it at the end of the specified basic block.
813void DIFactory::InsertStopPoint(DICompileUnit CU, unsigned LineNo,
814 unsigned ColNo, BasicBlock *BB) {
815
816 // Lazily construct llvm.dbg.stoppoint function.
817 if (!StopPointFn)
818 StopPointFn = llvm::Intrinsic::getDeclaration(&M,
819 llvm::Intrinsic::dbg_stoppoint);
820
821 // Invoke llvm.dbg.stoppoint
822 Value *Args[] = {
Owen Anderson1d0be152009-08-13 21:58:54 +0000823 ConstantInt::get(llvm::Type::getInt32Ty(VMContext), LineNo),
824 ConstantInt::get(llvm::Type::getInt32Ty(VMContext), ColNo),
Devang Patele4b27562009-08-28 23:24:31 +0000825 CU.getNode()
Chris Lattnera45664f2008-11-10 02:56:27 +0000826 };
827 CallInst::Create(StopPointFn, Args, Args+3, "", BB);
828}
829
830/// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
831/// mark the start of the specified subprogram.
832void DIFactory::InsertSubprogramStart(DISubprogram SP, BasicBlock *BB) {
833 // Lazily construct llvm.dbg.func.start.
834 if (!FuncStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000835 FuncStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_func_start);
Chris Lattnera45664f2008-11-10 02:56:27 +0000836
837 // Call llvm.dbg.func.start which also implicitly sets a stoppoint.
Devang Patele4b27562009-08-28 23:24:31 +0000838 CallInst::Create(FuncStartFn, SP.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000839}
840
841/// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
842/// mark the start of a region for the specified scoping descriptor.
843void DIFactory::InsertRegionStart(DIDescriptor D, BasicBlock *BB) {
844 // Lazily construct llvm.dbg.region.start function.
845 if (!RegionStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000846 RegionStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_start);
847
Chris Lattnera45664f2008-11-10 02:56:27 +0000848 // Call llvm.dbg.func.start.
Devang Patele4b27562009-08-28 23:24:31 +0000849 CallInst::Create(RegionStartFn, D.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000850}
851
Chris Lattnera45664f2008-11-10 02:56:27 +0000852/// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
853/// mark the end of a region for the specified scoping descriptor.
854void DIFactory::InsertRegionEnd(DIDescriptor D, BasicBlock *BB) {
855 // Lazily construct llvm.dbg.region.end function.
856 if (!RegionEndFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000857 RegionEndFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_end);
858
859 // Call llvm.dbg.region.end.
Devang Patele4b27562009-08-28 23:24:31 +0000860 CallInst::Create(RegionEndFn, D.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000861}
862
863/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000864void DIFactory::InsertDeclare(Value *Storage, DIVariable D, BasicBlock *BB) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000865 // Cast the storage to a {}* for the call to llvm.dbg.declare.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000866 Storage = new BitCastInst(Storage, EmptyStructPtr, "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000867
868 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000869 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
870
Devang Patele4b27562009-08-28 23:24:31 +0000871 Value *Args[] = { Storage, D.getNode() };
Chris Lattnera45664f2008-11-10 02:56:27 +0000872 CallInst::Create(DeclareFn, Args, Args+2, "", BB);
873}
Torok Edwin620f2802008-12-16 09:07:36 +0000874
Devang Patele4b27562009-08-28 23:24:31 +0000875
Devang Pateld2f79a12009-07-28 19:55:13 +0000876//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +0000877// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +0000878//===----------------------------------------------------------------------===//
879
Devang Patel98c65172009-07-30 18:25:15 +0000880/// processModule - Process entire module and collect debug info.
881void DebugInfoFinder::processModule(Module &M) {
Devang Patele4b27562009-08-28 23:24:31 +0000882
883
Devang Pateld2f79a12009-07-28 19:55:13 +0000884 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
885 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
886 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
887 ++BI) {
888 if (DbgStopPointInst *SPI = dyn_cast<DbgStopPointInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000889 processStopPoint(SPI);
Devang Pateld2f79a12009-07-28 19:55:13 +0000890 else if (DbgFuncStartInst *FSI = dyn_cast<DbgFuncStartInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000891 processFuncStart(FSI);
Devang Patele802f1c2009-07-30 17:30:23 +0000892 else if (DbgRegionStartInst *DRS = dyn_cast<DbgRegionStartInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000893 processRegionStart(DRS);
Devang Patele802f1c2009-07-30 17:30:23 +0000894 else if (DbgRegionEndInst *DRE = dyn_cast<DbgRegionEndInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000895 processRegionEnd(DRE);
Devang Patelb4d31302009-07-31 18:18:52 +0000896 else if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
897 processDeclare(DDI);
Devang Pateld2f79a12009-07-28 19:55:13 +0000898 }
Devang Patele4b27562009-08-28 23:24:31 +0000899
900 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
901 if (!NMD)
902 return;
903
904 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
905 DIGlobalVariable DIG(cast<MDNode>(NMD->getElement(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +0000906 if (addGlobalVariable(DIG)) {
907 addCompileUnit(DIG.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +0000908 processType(DIG.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +0000909 }
910 }
911}
912
Devang Patel98c65172009-07-30 18:25:15 +0000913/// processType - Process DIType.
914void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +0000915 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +0000916 return;
917
918 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +0000919 if (DT.isCompositeType()) {
Devang Patele4b27562009-08-28 23:24:31 +0000920 DICompositeType DCT(DT.getNode());
Devang Patel98c65172009-07-30 18:25:15 +0000921 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +0000922 DIArray DA = DCT.getTypeArray();
923 if (!DA.isNull())
924 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
925 DIDescriptor D = DA.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +0000926 DIType TypeE = DIType(D.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +0000927 if (!TypeE.isNull())
Devang Patel98c65172009-07-30 18:25:15 +0000928 processType(TypeE);
Devang Pateld2f79a12009-07-28 19:55:13 +0000929 else
Devang Patele4b27562009-08-28 23:24:31 +0000930 processSubprogram(DISubprogram(D.getNode()));
Devang Pateld2f79a12009-07-28 19:55:13 +0000931 }
Devang Patel6ceea332009-08-31 18:49:10 +0000932 } else if (DT.isDerivedType()) {
Devang Patele4b27562009-08-28 23:24:31 +0000933 DIDerivedType DDT(DT.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +0000934 if (!DDT.isNull())
Devang Patel98c65172009-07-30 18:25:15 +0000935 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +0000936 }
937}
938
Devang Patel98c65172009-07-30 18:25:15 +0000939/// processSubprogram - Process DISubprogram.
940void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Patele802f1c2009-07-30 17:30:23 +0000941 if (SP.isNull())
942 return;
Devang Pateld2f79a12009-07-28 19:55:13 +0000943 if (!addSubprogram(SP))
944 return;
945 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +0000946 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +0000947}
948
Devang Patel98c65172009-07-30 18:25:15 +0000949/// processStopPoint - Process DbgStopPointInst.
950void DebugInfoFinder::processStopPoint(DbgStopPointInst *SPI) {
Devang Patele4b27562009-08-28 23:24:31 +0000951 MDNode *Context = dyn_cast<MDNode>(SPI->getContext());
Devang Pateld2f79a12009-07-28 19:55:13 +0000952 addCompileUnit(DICompileUnit(Context));
953}
954
Devang Patel98c65172009-07-30 18:25:15 +0000955/// processFuncStart - Process DbgFuncStartInst.
956void DebugInfoFinder::processFuncStart(DbgFuncStartInst *FSI) {
Devang Patele4b27562009-08-28 23:24:31 +0000957 MDNode *SP = dyn_cast<MDNode>(FSI->getSubprogram());
Devang Patel98c65172009-07-30 18:25:15 +0000958 processSubprogram(DISubprogram(SP));
Devang Pateld2f79a12009-07-28 19:55:13 +0000959}
960
Devang Patel98c65172009-07-30 18:25:15 +0000961/// processRegionStart - Process DbgRegionStart.
962void DebugInfoFinder::processRegionStart(DbgRegionStartInst *DRS) {
Devang Patele4b27562009-08-28 23:24:31 +0000963 MDNode *SP = dyn_cast<MDNode>(DRS->getContext());
Devang Patel98c65172009-07-30 18:25:15 +0000964 processSubprogram(DISubprogram(SP));
Devang Patele802f1c2009-07-30 17:30:23 +0000965}
966
Devang Patel98c65172009-07-30 18:25:15 +0000967/// processRegionEnd - Process DbgRegionEnd.
968void DebugInfoFinder::processRegionEnd(DbgRegionEndInst *DRE) {
Devang Patele4b27562009-08-28 23:24:31 +0000969 MDNode *SP = dyn_cast<MDNode>(DRE->getContext());
Devang Patel98c65172009-07-30 18:25:15 +0000970 processSubprogram(DISubprogram(SP));
Devang Patele802f1c2009-07-30 17:30:23 +0000971}
972
Devang Patelb4d31302009-07-31 18:18:52 +0000973/// processDeclare - Process DbgDeclareInst.
974void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patele4b27562009-08-28 23:24:31 +0000975 DIVariable DV(cast<MDNode>(DDI->getVariable()));
Devang Patelb4d31302009-07-31 18:18:52 +0000976 if (DV.isNull())
977 return;
978
Devang Patele4b27562009-08-28 23:24:31 +0000979 if (!NodesSeen.insert(DV.getNode()))
Devang Patelb4d31302009-07-31 18:18:52 +0000980 return;
981
982 addCompileUnit(DV.getCompileUnit());
983 processType(DV.getType());
984}
985
Devang Patel72bcdb62009-08-10 22:09:58 +0000986/// addType - Add type into Tys.
987bool DebugInfoFinder::addType(DIType DT) {
988 if (DT.isNull())
989 return false;
990
Devang Patele4b27562009-08-28 23:24:31 +0000991 if (!NodesSeen.insert(DT.getNode()))
Devang Patel72bcdb62009-08-10 22:09:58 +0000992 return false;
993
Devang Patele4b27562009-08-28 23:24:31 +0000994 TYs.push_back(DT.getNode());
Devang Patel72bcdb62009-08-10 22:09:58 +0000995 return true;
996}
997
Devang Pateld2f79a12009-07-28 19:55:13 +0000998/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +0000999bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001000 if (CU.isNull())
1001 return false;
1002
Devang Patele4b27562009-08-28 23:24:31 +00001003 if (!NodesSeen.insert(CU.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001004 return false;
1005
Devang Patele4b27562009-08-28 23:24:31 +00001006 CUs.push_back(CU.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001007 return true;
1008}
1009
1010/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +00001011bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001012 if (DIG.isNull())
1013 return false;
1014
Devang Patele4b27562009-08-28 23:24:31 +00001015 if (!NodesSeen.insert(DIG.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001016 return false;
1017
Devang Patele4b27562009-08-28 23:24:31 +00001018 GVs.push_back(DIG.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001019 return true;
1020}
1021
1022// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +00001023bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001024 if (SP.isNull())
1025 return false;
1026
Devang Patele4b27562009-08-28 23:24:31 +00001027 if (!NodesSeen.insert(SP.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001028 return false;
1029
Devang Patele4b27562009-08-28 23:24:31 +00001030 SPs.push_back(SP.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001031 return true;
1032}
1033
Torok Edwin620f2802008-12-16 09:07:36 +00001034namespace llvm {
Bill Wendlingdc817b62009-05-14 18:26:15 +00001035 /// findStopPoint - Find the stoppoint coressponding to this instruction, that
1036 /// is the stoppoint that dominates this instruction.
1037 const DbgStopPointInst *findStopPoint(const Instruction *Inst) {
Torok Edwin620f2802008-12-16 09:07:36 +00001038 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst))
1039 return DSI;
1040
1041 const BasicBlock *BB = Inst->getParent();
1042 BasicBlock::const_iterator I = Inst, B;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001043 while (BB) {
Torok Edwin620f2802008-12-16 09:07:36 +00001044 B = BB->begin();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001045
Torok Edwin620f2802008-12-16 09:07:36 +00001046 // A BB consisting only of a terminator can't have a stoppoint.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001047 while (I != B) {
1048 --I;
1049 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1050 return DSI;
Torok Edwin620f2802008-12-16 09:07:36 +00001051 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001052
1053 // This BB didn't have a stoppoint: if there is only one predecessor, look
1054 // for a stoppoint there. We could use getIDom(), but that would require
1055 // dominator info.
Torok Edwin620f2802008-12-16 09:07:36 +00001056 BB = I->getParent()->getUniquePredecessor();
1057 if (BB)
1058 I = BB->getTerminator();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001059 }
1060
Torok Edwin620f2802008-12-16 09:07:36 +00001061 return 0;
1062 }
1063
Bill Wendlingdc817b62009-05-14 18:26:15 +00001064 /// findBBStopPoint - Find the stoppoint corresponding to first real
1065 /// (non-debug intrinsic) instruction in this Basic Block, and return the
1066 /// stoppoint for it.
1067 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB) {
1068 for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001069 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1070 return DSI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001071
1072 // Fallback to looking for stoppoint of unique predecessor. Useful if this
1073 // BB contains no stoppoints, but unique predecessor does.
Torok Edwin620f2802008-12-16 09:07:36 +00001074 BB = BB->getUniquePredecessor();
1075 if (BB)
1076 return findStopPoint(BB->getTerminator());
Bill Wendlingdc817b62009-05-14 18:26:15 +00001077
Torok Edwin620f2802008-12-16 09:07:36 +00001078 return 0;
1079 }
1080
Bill Wendlingdc817b62009-05-14 18:26:15 +00001081 Value *findDbgGlobalDeclare(GlobalVariable *V) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001082 const Module *M = V->getParent();
Devang Patele4b27562009-08-28 23:24:31 +00001083 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1084 if (!NMD)
1085 return 0;
Owen Anderson99035272009-07-07 17:12:53 +00001086
Devang Patele4b27562009-08-28 23:24:31 +00001087 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
1088 DIGlobalVariable DIG(cast_or_null<MDNode>(NMD->getElement(i)));
1089 if (DIG.isNull())
1090 continue;
1091 if (DIG.getGlobal() == V)
1092 return DIG.getNode();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001093 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001094 return 0;
1095 }
1096
Bill Wendlingdc817b62009-05-14 18:26:15 +00001097 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
Torok Edwin620f2802008-12-16 09:07:36 +00001098 /// It looks through pointer casts too.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001099 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts) {
Torok Edwin620f2802008-12-16 09:07:36 +00001100 if (stripCasts) {
1101 V = V->stripPointerCasts();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001102
Torok Edwin620f2802008-12-16 09:07:36 +00001103 // Look for the bitcast.
1104 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001105 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001106 if (isa<BitCastInst>(I))
1107 return findDbgDeclare(*I, false);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001108
Torok Edwin620f2802008-12-16 09:07:36 +00001109 return 0;
1110 }
1111
Bill Wendlingdc817b62009-05-14 18:26:15 +00001112 // Find llvm.dbg.declare among uses of the instruction.
Torok Edwin620f2802008-12-16 09:07:36 +00001113 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001114 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001115 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))
1116 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001117
Torok Edwin620f2802008-12-16 09:07:36 +00001118 return 0;
1119 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001120
Bill Wendlingdc817b62009-05-14 18:26:15 +00001121 bool getLocationInfo(const Value *V, std::string &DisplayName,
1122 std::string &Type, unsigned &LineNo, std::string &File,
1123 std::string &Dir) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001124 DICompileUnit Unit;
1125 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001126
Torok Edwinff7d0e92009-03-10 13:41:26 +00001127 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1128 Value *DIGV = findDbgGlobalDeclare(GV);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001129 if (!DIGV) return false;
Devang Patele4b27562009-08-28 23:24:31 +00001130 DIGlobalVariable Var(cast<MDNode>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001131
Bill Wendling0582ae92009-03-13 04:39:26 +00001132 Var.getDisplayName(DisplayName);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001133 LineNo = Var.getLineNumber();
1134 Unit = Var.getCompileUnit();
1135 TypeD = Var.getType();
1136 } else {
1137 const DbgDeclareInst *DDI = findDbgDeclare(V);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001138 if (!DDI) return false;
Devang Patele4b27562009-08-28 23:24:31 +00001139 DIVariable Var(cast<MDNode>(DDI->getVariable()));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001140
Bill Wendling0582ae92009-03-13 04:39:26 +00001141 Var.getName(DisplayName);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001142 LineNo = Var.getLineNumber();
1143 Unit = Var.getCompileUnit();
1144 TypeD = Var.getType();
1145 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001146
Bill Wendling0582ae92009-03-13 04:39:26 +00001147 TypeD.getName(Type);
1148 Unit.getFilename(File);
1149 Unit.getDirectory(Dir);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001150 return true;
1151 }
Devang Patel13e16b62009-06-26 01:49:18 +00001152
Devang Patel9e529c32009-07-02 01:15:24 +00001153 /// isValidDebugInfoIntrinsic - Return true if SPI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001154 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001155 bool isValidDebugInfoIntrinsic(DbgStopPointInst &SPI,
1156 CodeGenOpt::Level OptLev) {
1157 return DIDescriptor::ValidDebugInfo(SPI.getContext(), OptLev);
1158 }
1159
1160 /// isValidDebugInfoIntrinsic - Return true if FSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001161 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001162 bool isValidDebugInfoIntrinsic(DbgFuncStartInst &FSI,
1163 CodeGenOpt::Level OptLev) {
1164 return DIDescriptor::ValidDebugInfo(FSI.getSubprogram(), OptLev);
1165 }
1166
1167 /// isValidDebugInfoIntrinsic - Return true if RSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001168 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001169 bool isValidDebugInfoIntrinsic(DbgRegionStartInst &RSI,
1170 CodeGenOpt::Level OptLev) {
1171 return DIDescriptor::ValidDebugInfo(RSI.getContext(), OptLev);
1172 }
1173
1174 /// isValidDebugInfoIntrinsic - Return true if REI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001175 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001176 bool isValidDebugInfoIntrinsic(DbgRegionEndInst &REI,
1177 CodeGenOpt::Level OptLev) {
1178 return DIDescriptor::ValidDebugInfo(REI.getContext(), OptLev);
1179 }
1180
1181
1182 /// isValidDebugInfoIntrinsic - Return true if DI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001183 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001184 bool isValidDebugInfoIntrinsic(DbgDeclareInst &DI,
1185 CodeGenOpt::Level OptLev) {
1186 return DIDescriptor::ValidDebugInfo(DI.getVariable(), OptLev);
1187 }
1188
1189 /// ExtractDebugLocation - Extract debug location information
1190 /// from llvm.dbg.stoppoint intrinsic.
1191 DebugLoc ExtractDebugLocation(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +00001192 DebugLocTracker &DebugLocInfo) {
1193 DebugLoc DL;
1194 Value *Context = SPI.getContext();
Devang Patel9e529c32009-07-02 01:15:24 +00001195
1196 // If this location is already tracked then use it.
Devang Patele4b27562009-08-28 23:24:31 +00001197 DebugLocTuple Tuple(cast<MDNode>(Context), SPI.getLine(),
Devang Patel9e529c32009-07-02 01:15:24 +00001198 SPI.getColumn());
1199 DenseMap<DebugLocTuple, unsigned>::iterator II
1200 = DebugLocInfo.DebugIdMap.find(Tuple);
1201 if (II != DebugLocInfo.DebugIdMap.end())
1202 return DebugLoc::get(II->second);
1203
1204 // Add a new location entry.
1205 unsigned Id = DebugLocInfo.DebugLocations.size();
1206 DebugLocInfo.DebugLocations.push_back(Tuple);
1207 DebugLocInfo.DebugIdMap[Tuple] = Id;
1208
1209 return DebugLoc::get(Id);
1210 }
1211
1212 /// ExtractDebugLocation - Extract debug location information
1213 /// from llvm.dbg.func_start intrinsic.
1214 DebugLoc ExtractDebugLocation(DbgFuncStartInst &FSI,
Devang Patel9e529c32009-07-02 01:15:24 +00001215 DebugLocTracker &DebugLocInfo) {
1216 DebugLoc DL;
1217 Value *SP = FSI.getSubprogram();
Devang Patel9e529c32009-07-02 01:15:24 +00001218
Devang Patele4b27562009-08-28 23:24:31 +00001219 DISubprogram Subprogram(cast<MDNode>(SP));
Devang Patel9e529c32009-07-02 01:15:24 +00001220 unsigned Line = Subprogram.getLineNumber();
1221 DICompileUnit CU(Subprogram.getCompileUnit());
1222
1223 // If this location is already tracked then use it.
Devang Patele4b27562009-08-28 23:24:31 +00001224 DebugLocTuple Tuple(CU.getNode(), Line, /* Column */ 0);
Devang Patel9e529c32009-07-02 01:15:24 +00001225 DenseMap<DebugLocTuple, unsigned>::iterator II
1226 = DebugLocInfo.DebugIdMap.find(Tuple);
1227 if (II != DebugLocInfo.DebugIdMap.end())
1228 return DebugLoc::get(II->second);
1229
1230 // Add a new location entry.
1231 unsigned Id = DebugLocInfo.DebugLocations.size();
1232 DebugLocInfo.DebugLocations.push_back(Tuple);
1233 DebugLocInfo.DebugIdMap[Tuple] = Id;
1234
1235 return DebugLoc::get(Id);
1236 }
1237
1238 /// isInlinedFnStart - Return true if FSI is starting an inlined function.
1239 bool isInlinedFnStart(DbgFuncStartInst &FSI, const Function *CurrentFn) {
Devang Patele4b27562009-08-28 23:24:31 +00001240 DISubprogram Subprogram(cast<MDNode>(FSI.getSubprogram()));
Devang Patel9e529c32009-07-02 01:15:24 +00001241 if (Subprogram.describes(CurrentFn))
1242 return false;
1243
1244 return true;
1245 }
1246
1247 /// isInlinedFnEnd - Return true if REI is ending an inlined function.
1248 bool isInlinedFnEnd(DbgRegionEndInst &REI, const Function *CurrentFn) {
Devang Patele4b27562009-08-28 23:24:31 +00001249 DISubprogram Subprogram(cast<MDNode>(REI.getContext()));
Devang Patel9e529c32009-07-02 01:15:24 +00001250 if (Subprogram.isNull() || Subprogram.describes(CurrentFn))
1251 return false;
1252
1253 return true;
1254 }
Torok Edwin620f2802008-12-16 09:07:36 +00001255}