blob: 531f609fd18870532ead2e82375eec331fa2c8f6 [file] [log] [blame]
Chris Lattnera45664f2008-11-10 02:56:27 +00001//===--- DebugInfo.cpp - Debug Information Helper Classes -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the helper classes used to build and interpret debug
11// information in LLVM IR form.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Analysis/DebugInfo.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Intrinsics.h"
Torok Edwin620f2802008-12-16 09:07:36 +000019#include "llvm/IntrinsicInst.h"
Chris Lattnera45664f2008-11-10 02:56:27 +000020#include "llvm/Instructions.h"
Owen Anderson99035272009-07-07 17:12:53 +000021#include "llvm/LLVMContext.h"
Chris Lattnera45664f2008-11-10 02:56:27 +000022#include "llvm/Module.h"
23#include "llvm/Analysis/ValueTracking.h"
Devang Patele4b27562009-08-28 23:24:31 +000024#include "llvm/ADT/SmallPtrSet.h"
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000025#include "llvm/Support/Dwarf.h"
Devang Patel9e529c32009-07-02 01:15:24 +000026#include "llvm/Support/DebugLoc.h"
Chris Lattnera81d29b2009-08-23 07:33:14 +000027#include "llvm/Support/raw_ostream.h"
Chris Lattnera45664f2008-11-10 02:56:27 +000028using namespace llvm;
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000029using namespace llvm::dwarf;
Chris Lattnera45664f2008-11-10 02:56:27 +000030
31//===----------------------------------------------------------------------===//
32// DIDescriptor
33//===----------------------------------------------------------------------===//
34
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000035/// ValidDebugInfo - Return true if V represents valid debug info value.
Devang Patele4b27562009-08-28 23:24:31 +000036/// FIXME : Add DIDescriptor.isValid()
37bool DIDescriptor::ValidDebugInfo(MDNode *N, CodeGenOpt::Level OptLevel) {
38 if (!N)
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000039 return false;
40
Devang Patele4b27562009-08-28 23:24:31 +000041 DIDescriptor DI(N);
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000042
43 // Check current version. Allow Version6 for now.
44 unsigned Version = DI.getVersion();
45 if (Version != LLVMDebugVersion && Version != LLVMDebugVersion6)
46 return false;
47
48 unsigned Tag = DI.getTag();
49 switch (Tag) {
50 case DW_TAG_variable:
Devang Patele4b27562009-08-28 23:24:31 +000051 assert(DIVariable(N).Verify() && "Invalid DebugInfo value");
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000052 break;
53 case DW_TAG_compile_unit:
Devang Patele4b27562009-08-28 23:24:31 +000054 assert(DICompileUnit(N).Verify() && "Invalid DebugInfo value");
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000055 break;
56 case DW_TAG_subprogram:
Devang Patele4b27562009-08-28 23:24:31 +000057 assert(DISubprogram(N).Verify() && "Invalid DebugInfo value");
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000058 break;
59 case DW_TAG_lexical_block:
Bill Wendlingdc817b62009-05-14 18:26:15 +000060 // FIXME: This interfers with the quality of generated code during
61 // optimization.
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000062 if (OptLevel != CodeGenOpt::None)
63 return false;
Bill Wendlingdc817b62009-05-14 18:26:15 +000064 // FALLTHROUGH
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000065 default:
66 break;
67 }
68
69 return true;
70}
71
Devang Patele4b27562009-08-28 23:24:31 +000072DIDescriptor::DIDescriptor(MDNode *N, unsigned RequiredTag) {
73 DbgNode = N;
Daniel Dunbarf612ff62009-09-19 20:40:05 +000074
Bill Wendlingdc817b62009-05-14 18:26:15 +000075 // If this is non-null, check to see if the Tag matches. If not, set to null.
Devang Patele4b27562009-08-28 23:24:31 +000076 if (N && getTag() != RequiredTag) {
77 DbgNode = 0;
78 }
Chris Lattnera45664f2008-11-10 02:56:27 +000079}
80
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
Daniel Dunbarf612ff62009-09-19 20:40:05 +000087 if (Elt < DbgNode->getNumElements())
Devang Patele4b27562009-08-28 23:24:31 +000088 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getElement(Elt))) {
89 Result.assign(MDS->begin(), MDS->begin() + MDS->length());
90 return Result;
91 }
Daniel Dunbarf612ff62009-09-19 20:40:05 +000092
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 {
Daniel Dunbarf612ff62009-09-19 20:40:05 +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();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000103
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 {
Daniel Dunbarf612ff62009-09-19 20:40:05 +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 {
Daniel Dunbarf612ff62009-09-19 20:40:05 +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();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000135
Devang Patel6ceea332009-08-31 18:49:10 +0000136 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();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000200
Devang Patel6ceea332009-08-31 18:49:10 +0000201 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
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000213/// isScope - Return true if the specified tag is one of the scope
Devang Patel43d98b32009-08-31 20:44:45 +0000214/// 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");
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000270
271 // Since we use a TrackingVH for the node, its easy for clients to manufacture
272 // legitimate situations where they want to replaceAllUsesWith() on something
273 // which, due to uniquing, has merged with the source. We shield clients from
274 // this detail by allowing a value to be replaced with replaceAllUsesWith()
275 // itself.
276 if (getNode() != D.getNode()) {
277 MDNode *Node = DbgNode;
278 Node->replaceAllUsesWith(D.getNode());
279 delete Node;
280 }
Devang Patelc4999d72009-07-22 18:23:44 +0000281}
282
Devang Patelb79b5352009-01-19 23:21:49 +0000283/// Verify - Verify that a compile unit is well formed.
284bool DICompileUnit::Verify() const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000285 if (isNull())
Devang Patelb79b5352009-01-19 23:21:49 +0000286 return false;
Bill Wendling0582ae92009-03-13 04:39:26 +0000287 std::string Res;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000288 if (getFilename(Res).empty())
Bill Wendling0582ae92009-03-13 04:39:26 +0000289 return false;
Devang Patelb79b5352009-01-19 23:21:49 +0000290 // It is possible that directory and produce string is empty.
Bill Wendling0582ae92009-03-13 04:39:26 +0000291 return true;
Devang Patelb79b5352009-01-19 23:21:49 +0000292}
293
294/// Verify - Verify that a type descriptor is well formed.
295bool DIType::Verify() const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000296 if (isNull())
Devang Patelb79b5352009-01-19 23:21:49 +0000297 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000298 if (getContext().isNull())
Devang Patelb79b5352009-01-19 23:21:49 +0000299 return false;
300
301 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000302 if (!CU.isNull() && !CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000303 return false;
304 return true;
305}
306
307/// Verify - Verify that a composite type descriptor is well formed.
308bool DICompositeType::Verify() const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000309 if (isNull())
Devang Patelb79b5352009-01-19 23:21:49 +0000310 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000311 if (getContext().isNull())
Devang Patelb79b5352009-01-19 23:21:49 +0000312 return false;
313
314 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000315 if (!CU.isNull() && !CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000316 return false;
317 return true;
318}
319
320/// Verify - Verify that a subprogram descriptor is well formed.
321bool DISubprogram::Verify() const {
322 if (isNull())
323 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000324
Devang Patelb79b5352009-01-19 23:21:49 +0000325 if (getContext().isNull())
326 return false;
327
328 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000329 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000330 return false;
331
332 DICompositeType Ty = getType();
333 if (!Ty.isNull() && !Ty.Verify())
334 return false;
335 return true;
336}
337
338/// Verify - Verify that a global variable descriptor is well formed.
339bool DIGlobalVariable::Verify() const {
340 if (isNull())
341 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000342
Devang Patelb79b5352009-01-19 23:21:49 +0000343 if (getContext().isNull())
344 return false;
345
346 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000347 if (!CU.isNull() && !CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000348 return false;
349
350 DIType Ty = getType();
351 if (!Ty.Verify())
352 return false;
353
354 if (!getGlobal())
355 return false;
356
357 return true;
358}
359
360/// Verify - Verify that a variable descriptor is well formed.
361bool DIVariable::Verify() const {
362 if (isNull())
363 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000364
Devang Patelb79b5352009-01-19 23:21:49 +0000365 if (getContext().isNull())
366 return false;
367
368 DIType Ty = getType();
369 if (!Ty.Verify())
370 return false;
371
Devang Patelb79b5352009-01-19 23:21:49 +0000372 return true;
373}
374
Devang Patel36375ee2009-02-17 21:23:59 +0000375/// getOriginalTypeSize - If this type is derived from a base type then
376/// return base type size.
377uint64_t DIDerivedType::getOriginalTypeSize() const {
378 if (getTag() != dwarf::DW_TAG_member)
379 return getSizeInBits();
380 DIType BT = getTypeDerivedFrom();
381 if (BT.getTag() != dwarf::DW_TAG_base_type)
382 return getSizeInBits();
383 return BT.getSizeInBits();
384}
Devang Patelb79b5352009-01-19 23:21:49 +0000385
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000386/// describes - Return true if this subprogram provides debugging
387/// information for the function F.
388bool DISubprogram::describes(const Function *F) {
389 assert (F && "Invalid function");
390 std::string Name;
391 getLinkageName(Name);
392 if (Name.empty())
393 getName(Name);
Daniel Dunbar460f6562009-07-26 09:48:23 +0000394 if (F->getName() == Name)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000395 return true;
396 return false;
397}
398
Chris Lattnera45664f2008-11-10 02:56:27 +0000399//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000400// DIDescriptor: dump routines for all descriptors.
401//===----------------------------------------------------------------------===//
402
403
404/// dump - Print descriptor.
405void DIDescriptor::dump() const {
Devang Patele4b27562009-08-28 23:24:31 +0000406 errs() << "[" << dwarf::TagString(getTag()) << "] ";
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000407 errs().write_hex((intptr_t) &*DbgNode) << ']';
Devang Patel7136a652009-07-01 22:10:23 +0000408}
409
410/// dump - Print compile unit.
411void DICompileUnit::dump() const {
412 if (getLanguage())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000413 errs() << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000414
415 std::string Res1, Res2;
Chris Lattnera81d29b2009-08-23 07:33:14 +0000416 errs() << " [" << getDirectory(Res1) << "/" << getFilename(Res2) << " ]";
Devang Patel7136a652009-07-01 22:10:23 +0000417}
418
419/// dump - Print type.
420void DIType::dump() const {
421 if (isNull()) return;
422
423 std::string Res;
424 if (!getName(Res).empty())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000425 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000426
427 unsigned Tag = getTag();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000428 errs() << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000429
430 // TODO : Print context
431 getCompileUnit().dump();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000432 errs() << " ["
433 << getLineNumber() << ", "
Chris Lattnera81d29b2009-08-23 07:33:14 +0000434 << getSizeInBits() << ", "
435 << getAlignInBits() << ", "
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000436 << getOffsetInBits()
Chris Lattnera81d29b2009-08-23 07:33:14 +0000437 << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000438
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000439 if (isPrivate())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000440 errs() << " [private] ";
Devang Patel7136a652009-07-01 22:10:23 +0000441 else if (isProtected())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000442 errs() << " [protected] ";
Devang Patel7136a652009-07-01 22:10:23 +0000443
444 if (isForwardDecl())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000445 errs() << " [fwd] ";
Devang Patel7136a652009-07-01 22:10:23 +0000446
Devang Patel6ceea332009-08-31 18:49:10 +0000447 if (isBasicType())
Devang Patele4b27562009-08-28 23:24:31 +0000448 DIBasicType(DbgNode).dump();
Devang Patel6ceea332009-08-31 18:49:10 +0000449 else if (isDerivedType())
Devang Patele4b27562009-08-28 23:24:31 +0000450 DIDerivedType(DbgNode).dump();
Devang Patel6ceea332009-08-31 18:49:10 +0000451 else if (isCompositeType())
Devang Patele4b27562009-08-28 23:24:31 +0000452 DICompositeType(DbgNode).dump();
Devang Patel7136a652009-07-01 22:10:23 +0000453 else {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000454 errs() << "Invalid DIType\n";
Devang Patel7136a652009-07-01 22:10:23 +0000455 return;
456 }
457
Chris Lattnera81d29b2009-08-23 07:33:14 +0000458 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000459}
460
461/// dump - Print basic type.
462void DIBasicType::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000463 errs() << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000464}
465
466/// dump - Print derived type.
467void DIDerivedType::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000468 errs() << "\n\t Derived From: "; getTypeDerivedFrom().dump();
Devang Patel7136a652009-07-01 22:10:23 +0000469}
470
471/// dump - Print composite type.
472void DICompositeType::dump() const {
473 DIArray A = getTypeArray();
474 if (A.isNull())
475 return;
Chris Lattnera81d29b2009-08-23 07:33:14 +0000476 errs() << " [" << A.getNumElements() << " elements]";
Devang Patel7136a652009-07-01 22:10:23 +0000477}
478
479/// dump - Print global.
480void DIGlobal::dump() const {
481 std::string Res;
482 if (!getName(Res).empty())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000483 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000484
485 unsigned Tag = getTag();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000486 errs() << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000487
488 // TODO : Print context
489 getCompileUnit().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000490 errs() << " [" << getLineNumber() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000491
492 if (isLocalToUnit())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000493 errs() << " [local] ";
Devang Patel7136a652009-07-01 22:10:23 +0000494
495 if (isDefinition())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000496 errs() << " [def] ";
Devang Patel7136a652009-07-01 22:10:23 +0000497
Devang Patel6ceea332009-08-31 18:49:10 +0000498 if (isGlobalVariable())
Devang Patele4b27562009-08-28 23:24:31 +0000499 DIGlobalVariable(DbgNode).dump();
Devang Patel7136a652009-07-01 22:10:23 +0000500
Chris Lattnera81d29b2009-08-23 07:33:14 +0000501 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000502}
503
504/// dump - Print subprogram.
505void DISubprogram::dump() const {
Devang Patel82dfc0c2009-08-31 22:47:13 +0000506 std::string Res;
507 if (!getName(Res).empty())
508 errs() << " [" << Res << "] ";
509
510 unsigned Tag = getTag();
511 errs() << " [" << dwarf::TagString(Tag) << "] ";
512
513 // TODO : Print context
514 getCompileUnit().dump();
515 errs() << " [" << getLineNumber() << "] ";
516
517 if (isLocalToUnit())
518 errs() << " [local] ";
519
520 if (isDefinition())
521 errs() << " [def] ";
522
523 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000524}
525
526/// dump - Print global variable.
527void DIGlobalVariable::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000528 errs() << " [";
529 getGlobal()->dump();
530 errs() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000531}
532
533/// dump - Print variable.
534void DIVariable::dump() const {
535 std::string Res;
536 if (!getName(Res).empty())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000537 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000538
539 getCompileUnit().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000540 errs() << " [" << getLineNumber() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000541 getType().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000542 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000543}
544
545//===----------------------------------------------------------------------===//
Chris Lattnera45664f2008-11-10 02:56:27 +0000546// DIFactory: Basic Helpers
547//===----------------------------------------------------------------------===//
548
Bill Wendlingdc817b62009-05-14 18:26:15 +0000549DIFactory::DIFactory(Module &m)
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000550 : M(m), VMContext(M.getContext()), StopPointFn(0), FuncStartFn(0),
Owen Anderson99035272009-07-07 17:12:53 +0000551 RegionStartFn(0), RegionEndFn(0),
Bill Wendlingdc817b62009-05-14 18:26:15 +0000552 DeclareFn(0) {
Owen Andersond7f2a6c2009-08-05 23:16:16 +0000553 EmptyStructPtr = PointerType::getUnqual(StructType::get(VMContext));
Chris Lattner497a7a82008-11-10 04:10:34 +0000554}
555
Chris Lattnera45664f2008-11-10 02:56:27 +0000556Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000557 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000558 "Tag too large for debug encoding!");
Owen Anderson1d0be152009-08-13 21:58:54 +0000559 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000560}
561
Chris Lattnera45664f2008-11-10 02:56:27 +0000562//===----------------------------------------------------------------------===//
563// DIFactory: Primary Constructors
564//===----------------------------------------------------------------------===//
565
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000566/// GetOrCreateArray - Create an descriptor for an array of descriptors.
Chris Lattnera45664f2008-11-10 02:56:27 +0000567/// This implicitly uniques the arrays created.
568DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
Devang Patele4b27562009-08-28 23:24:31 +0000569 SmallVector<Value*, 16> Elts;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000570
Devang Patele4b27562009-08-28 23:24:31 +0000571 if (NumTys == 0)
572 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
573 else
574 for (unsigned i = 0; i != NumTys; ++i)
575 Elts.push_back(Tys[i].getNode());
Devang Patel82459882009-08-26 05:01:18 +0000576
Devang Patele4b27562009-08-28 23:24:31 +0000577 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
Chris Lattnera45664f2008-11-10 02:56:27 +0000578}
579
580/// GetOrCreateSubrange - Create a descriptor for a value range. This
581/// implicitly uniques the values returned.
582DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
Devang Patele4b27562009-08-28 23:24:31 +0000583 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000584 GetTagConstant(dwarf::DW_TAG_subrange_type),
Owen Anderson1d0be152009-08-13 21:58:54 +0000585 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
586 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
Chris Lattnera45664f2008-11-10 02:56:27 +0000587 };
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000588
Devang Patele4b27562009-08-28 23:24:31 +0000589 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000590}
591
592
593
594/// CreateCompileUnit - Create a new descriptor for the specified compile
595/// unit. Note that this does not unique compile units within the module.
596DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
597 const std::string &Filename,
598 const std::string &Directory,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000599 const std::string &Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000600 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000601 bool isOptimized,
Devang Patel13319ce2009-02-17 22:43:44 +0000602 const char *Flags,
603 unsigned RunTimeVer) {
Devang Patele4b27562009-08-28 23:24:31 +0000604 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000605 GetTagConstant(dwarf::DW_TAG_compile_unit),
Devang Patele4b27562009-08-28 23:24:31 +0000606 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Owen Anderson1d0be152009-08-13 21:58:54 +0000607 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
Devang Patele4b27562009-08-28 23:24:31 +0000608 MDString::get(VMContext, Filename),
609 MDString::get(VMContext, Directory),
610 MDString::get(VMContext, Producer),
Owen Anderson1d0be152009-08-13 21:58:54 +0000611 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
612 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patele4b27562009-08-28 23:24:31 +0000613 MDString::get(VMContext, Flags),
Owen Anderson1d0be152009-08-13 21:58:54 +0000614 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000615 };
Devang Patele4b27562009-08-28 23:24:31 +0000616
617 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000618}
619
620/// CreateEnumerator - Create a single enumerator value.
621DIEnumerator DIFactory::CreateEnumerator(const std::string &Name, uint64_t Val){
Devang Patele4b27562009-08-28 23:24:31 +0000622 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000623 GetTagConstant(dwarf::DW_TAG_enumerator),
Devang Patele4b27562009-08-28 23:24:31 +0000624 MDString::get(VMContext, Name),
Owen Anderson1d0be152009-08-13 21:58:54 +0000625 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000626 };
Devang Patele4b27562009-08-28 23:24:31 +0000627 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000628}
629
630
631/// CreateBasicType - Create a basic type like int, float, etc.
632DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Bill Wendling0582ae92009-03-13 04:39:26 +0000633 const std::string &Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000634 DICompileUnit CompileUnit,
635 unsigned LineNumber,
636 uint64_t SizeInBits,
637 uint64_t AlignInBits,
638 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000639 unsigned Encoding) {
Devang Patele4b27562009-08-28 23:24:31 +0000640 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000641 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patele4b27562009-08-28 23:24:31 +0000642 Context.getNode(),
643 MDString::get(VMContext, Name),
644 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000645 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
646 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
647 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
648 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
649 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
650 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000651 };
Devang Patele4b27562009-08-28 23:24:31 +0000652 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000653}
654
655/// CreateDerivedType - Create a derived type like const qualified type,
656/// pointer, typedef, etc.
657DIDerivedType DIFactory::CreateDerivedType(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,
Devang Pateldd9db662009-01-30 18:20:31 +0000666 DIType DerivedFrom) {
Devang Patele4b27562009-08-28 23:24:31 +0000667 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000668 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000669 Context.getNode(),
670 MDString::get(VMContext, Name),
671 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000672 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
673 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
674 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
675 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
676 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patele4b27562009-08-28 23:24:31 +0000677 DerivedFrom.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000678 };
Devang Patele4b27562009-08-28 23:24:31 +0000679 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000680}
681
682/// CreateCompositeType - Create a composite type like array, struct, etc.
683DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
684 DIDescriptor Context,
685 const std::string &Name,
686 DICompileUnit CompileUnit,
687 unsigned LineNumber,
688 uint64_t SizeInBits,
689 uint64_t AlignInBits,
690 uint64_t OffsetInBits,
691 unsigned Flags,
692 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000693 DIArray Elements,
694 unsigned RuntimeLang) {
Owen Andersone277fed2009-07-07 16:31:25 +0000695
Devang Patele4b27562009-08-28 23:24:31 +0000696 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000697 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000698 Context.getNode(),
699 MDString::get(VMContext, Name),
700 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000701 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
702 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
703 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
704 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
705 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patele4b27562009-08-28 23:24:31 +0000706 DerivedFrom.getNode(),
707 Elements.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000708 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
Chris Lattnera45664f2008-11-10 02:56:27 +0000709 };
Devang Patele4b27562009-08-28 23:24:31 +0000710 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
Chris Lattnera45664f2008-11-10 02:56:27 +0000711}
712
713
714/// CreateSubprogram - Create a new descriptor for the specified subprogram.
715/// See comments in DISubprogram for descriptions of these fields. This
716/// method does not unique the generated descriptors.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000717DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
Chris Lattnera45664f2008-11-10 02:56:27 +0000718 const std::string &Name,
719 const std::string &DisplayName,
720 const std::string &LinkageName,
721 DICompileUnit CompileUnit,
722 unsigned LineNo, DIType Type,
723 bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000724 bool isDefinition) {
Devang Patel854967e2008-12-17 22:39:29 +0000725
Devang Patele4b27562009-08-28 23:24:31 +0000726 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000727 GetTagConstant(dwarf::DW_TAG_subprogram),
Devang Patele4b27562009-08-28 23:24:31 +0000728 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
729 Context.getNode(),
730 MDString::get(VMContext, Name),
731 MDString::get(VMContext, DisplayName),
732 MDString::get(VMContext, LinkageName),
733 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000734 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000735 Type.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000736 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
737 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition)
Chris Lattnera45664f2008-11-10 02:56:27 +0000738 };
Devang Patele4b27562009-08-28 23:24:31 +0000739 return DISubprogram(MDNode::get(VMContext, &Elts[0], 11));
Chris Lattnera45664f2008-11-10 02:56:27 +0000740}
741
742/// CreateGlobalVariable - Create a new descriptor for the specified global.
743DIGlobalVariable
744DIFactory::CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
745 const std::string &DisplayName,
746 const std::string &LinkageName,
747 DICompileUnit CompileUnit,
748 unsigned LineNo, DIType Type,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000749 bool isDefinition, llvm::GlobalVariable *Val) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000750 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000751 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patele4b27562009-08-28 23:24:31 +0000752 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
753 Context.getNode(),
754 MDString::get(VMContext, Name),
755 MDString::get(VMContext, DisplayName),
756 MDString::get(VMContext, LinkageName),
757 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000758 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000759 Type.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000760 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
761 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
Devang Patele4b27562009-08-28 23:24:31 +0000762 Val
Chris Lattnera45664f2008-11-10 02:56:27 +0000763 };
Devang Patele4b27562009-08-28 23:24:31 +0000764
765 Value *const *Vs = &Elts[0];
766 MDNode *Node = MDNode::get(VMContext,Vs, 12);
767
768 // Create a named metadata so that we do not lose this mdnode.
769 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
770 NMD->addElement(Node);
771
772 return DIGlobalVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +0000773}
774
775
776/// CreateVariable - Create a new descriptor for the specified variable.
777DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
778 const std::string &Name,
779 DICompileUnit CompileUnit, unsigned LineNo,
Devang Pateldd9db662009-01-30 18:20:31 +0000780 DIType Type) {
Devang Patele4b27562009-08-28 23:24:31 +0000781 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000782 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000783 Context.getNode(),
784 MDString::get(VMContext, Name),
785 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000786 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000787 Type.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000788 };
Devang Patele4b27562009-08-28 23:24:31 +0000789 return DIVariable(MDNode::get(VMContext, &Elts[0], 6));
Chris Lattnera45664f2008-11-10 02:56:27 +0000790}
791
792
793/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +0000794/// specified parent VMContext.
Devang Patel5e005d82009-08-31 22:00:15 +0000795DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context) {
Devang Patele4b27562009-08-28 23:24:31 +0000796 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000797 GetTagConstant(dwarf::DW_TAG_lexical_block),
Devang Patele4b27562009-08-28 23:24:31 +0000798 Context.getNode()
Chris Lattnera45664f2008-11-10 02:56:27 +0000799 };
Devang Patel5e005d82009-08-31 22:00:15 +0000800 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 2));
Chris Lattnera45664f2008-11-10 02:56:27 +0000801}
802
Devang Patelf98d8fe2009-09-01 01:14:15 +0000803/// CreateLocation - Creates a debug info location.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000804DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
Daniel Dunbara279bc32009-09-20 02:20:51 +0000805 DIScope S, DILocation OrigLoc) {
Devang Patelf98d8fe2009-09-01 01:14:15 +0000806 Value *Elts[] = {
807 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
808 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
809 S.getNode(),
810 OrigLoc.getNode(),
811 };
812 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
813}
814
Chris Lattnera45664f2008-11-10 02:56:27 +0000815
816//===----------------------------------------------------------------------===//
817// DIFactory: Routines for inserting code into a function
818//===----------------------------------------------------------------------===//
819
820/// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
821/// inserting it at the end of the specified basic block.
822void DIFactory::InsertStopPoint(DICompileUnit CU, unsigned LineNo,
823 unsigned ColNo, BasicBlock *BB) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000824
Chris Lattnera45664f2008-11-10 02:56:27 +0000825 // Lazily construct llvm.dbg.stoppoint function.
826 if (!StopPointFn)
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000827 StopPointFn = llvm::Intrinsic::getDeclaration(&M,
Chris Lattnera45664f2008-11-10 02:56:27 +0000828 llvm::Intrinsic::dbg_stoppoint);
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000829
Chris Lattnera45664f2008-11-10 02:56:27 +0000830 // Invoke llvm.dbg.stoppoint
831 Value *Args[] = {
Owen Anderson1d0be152009-08-13 21:58:54 +0000832 ConstantInt::get(llvm::Type::getInt32Ty(VMContext), LineNo),
833 ConstantInt::get(llvm::Type::getInt32Ty(VMContext), ColNo),
Devang Patele4b27562009-08-28 23:24:31 +0000834 CU.getNode()
Chris Lattnera45664f2008-11-10 02:56:27 +0000835 };
836 CallInst::Create(StopPointFn, Args, Args+3, "", BB);
837}
838
839/// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
840/// mark the start of the specified subprogram.
841void DIFactory::InsertSubprogramStart(DISubprogram SP, BasicBlock *BB) {
842 // Lazily construct llvm.dbg.func.start.
843 if (!FuncStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000844 FuncStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_func_start);
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000845
Chris Lattnera45664f2008-11-10 02:56:27 +0000846 // Call llvm.dbg.func.start which also implicitly sets a stoppoint.
Devang Patele4b27562009-08-28 23:24:31 +0000847 CallInst::Create(FuncStartFn, SP.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000848}
849
850/// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
851/// mark the start of a region for the specified scoping descriptor.
852void DIFactory::InsertRegionStart(DIDescriptor D, BasicBlock *BB) {
853 // Lazily construct llvm.dbg.region.start function.
854 if (!RegionStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000855 RegionStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_start);
856
Chris Lattnera45664f2008-11-10 02:56:27 +0000857 // Call llvm.dbg.func.start.
Devang Patele4b27562009-08-28 23:24:31 +0000858 CallInst::Create(RegionStartFn, D.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000859}
860
Chris Lattnera45664f2008-11-10 02:56:27 +0000861/// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
862/// mark the end of a region for the specified scoping descriptor.
863void DIFactory::InsertRegionEnd(DIDescriptor D, BasicBlock *BB) {
864 // Lazily construct llvm.dbg.region.end function.
865 if (!RegionEndFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000866 RegionEndFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_end);
867
868 // Call llvm.dbg.region.end.
Devang Patele4b27562009-08-28 23:24:31 +0000869 CallInst::Create(RegionEndFn, D.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000870}
871
872/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000873void DIFactory::InsertDeclare(Value *Storage, DIVariable D, BasicBlock *BB) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000874 // Cast the storage to a {}* for the call to llvm.dbg.declare.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000875 Storage = new BitCastInst(Storage, EmptyStructPtr, "", BB);
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000876
Chris Lattnera45664f2008-11-10 02:56:27 +0000877 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000878 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
879
Devang Patele4b27562009-08-28 23:24:31 +0000880 Value *Args[] = { Storage, D.getNode() };
Chris Lattnera45664f2008-11-10 02:56:27 +0000881 CallInst::Create(DeclareFn, Args, Args+2, "", BB);
882}
Torok Edwin620f2802008-12-16 09:07:36 +0000883
Devang Patele4b27562009-08-28 23:24:31 +0000884
Devang Pateld2f79a12009-07-28 19:55:13 +0000885//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +0000886// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +0000887//===----------------------------------------------------------------------===//
888
Devang Patel98c65172009-07-30 18:25:15 +0000889/// processModule - Process entire module and collect debug info.
890void DebugInfoFinder::processModule(Module &M) {
Devang Patele4b27562009-08-28 23:24:31 +0000891
892
Devang Pateld2f79a12009-07-28 19:55:13 +0000893 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
894 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
895 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
896 ++BI) {
897 if (DbgStopPointInst *SPI = dyn_cast<DbgStopPointInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000898 processStopPoint(SPI);
Devang Pateld2f79a12009-07-28 19:55:13 +0000899 else if (DbgFuncStartInst *FSI = dyn_cast<DbgFuncStartInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000900 processFuncStart(FSI);
Devang Patele802f1c2009-07-30 17:30:23 +0000901 else if (DbgRegionStartInst *DRS = dyn_cast<DbgRegionStartInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000902 processRegionStart(DRS);
Devang Patele802f1c2009-07-30 17:30:23 +0000903 else if (DbgRegionEndInst *DRE = dyn_cast<DbgRegionEndInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000904 processRegionEnd(DRE);
Devang Patelb4d31302009-07-31 18:18:52 +0000905 else if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
906 processDeclare(DDI);
Devang Pateld2f79a12009-07-28 19:55:13 +0000907 }
Devang Patele4b27562009-08-28 23:24:31 +0000908
909 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
910 if (!NMD)
911 return;
912
913 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
914 DIGlobalVariable DIG(cast<MDNode>(NMD->getElement(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +0000915 if (addGlobalVariable(DIG)) {
916 addCompileUnit(DIG.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +0000917 processType(DIG.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +0000918 }
919 }
920}
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000921
Devang Patel98c65172009-07-30 18:25:15 +0000922/// processType - Process DIType.
923void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +0000924 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +0000925 return;
926
927 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +0000928 if (DT.isCompositeType()) {
Devang Patele4b27562009-08-28 23:24:31 +0000929 DICompositeType DCT(DT.getNode());
Devang Patel98c65172009-07-30 18:25:15 +0000930 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +0000931 DIArray DA = DCT.getTypeArray();
932 if (!DA.isNull())
933 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
934 DIDescriptor D = DA.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +0000935 DIType TypeE = DIType(D.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +0000936 if (!TypeE.isNull())
Devang Patel98c65172009-07-30 18:25:15 +0000937 processType(TypeE);
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000938 else
Devang Patele4b27562009-08-28 23:24:31 +0000939 processSubprogram(DISubprogram(D.getNode()));
Devang Pateld2f79a12009-07-28 19:55:13 +0000940 }
Devang Patel6ceea332009-08-31 18:49:10 +0000941 } else if (DT.isDerivedType()) {
Devang Patele4b27562009-08-28 23:24:31 +0000942 DIDerivedType DDT(DT.getNode());
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000943 if (!DDT.isNull())
Devang Patel98c65172009-07-30 18:25:15 +0000944 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +0000945 }
946}
947
Devang Patel98c65172009-07-30 18:25:15 +0000948/// processSubprogram - Process DISubprogram.
949void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Patele802f1c2009-07-30 17:30:23 +0000950 if (SP.isNull())
951 return;
Devang Pateld2f79a12009-07-28 19:55:13 +0000952 if (!addSubprogram(SP))
953 return;
954 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +0000955 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +0000956}
957
Devang Patel98c65172009-07-30 18:25:15 +0000958/// processStopPoint - Process DbgStopPointInst.
959void DebugInfoFinder::processStopPoint(DbgStopPointInst *SPI) {
Devang Patele4b27562009-08-28 23:24:31 +0000960 MDNode *Context = dyn_cast<MDNode>(SPI->getContext());
Devang Pateld2f79a12009-07-28 19:55:13 +0000961 addCompileUnit(DICompileUnit(Context));
962}
963
Devang Patel98c65172009-07-30 18:25:15 +0000964/// processFuncStart - Process DbgFuncStartInst.
965void DebugInfoFinder::processFuncStart(DbgFuncStartInst *FSI) {
Devang Patele4b27562009-08-28 23:24:31 +0000966 MDNode *SP = dyn_cast<MDNode>(FSI->getSubprogram());
Devang Patel98c65172009-07-30 18:25:15 +0000967 processSubprogram(DISubprogram(SP));
Devang Pateld2f79a12009-07-28 19:55:13 +0000968}
969
Devang Patel98c65172009-07-30 18:25:15 +0000970/// processRegionStart - Process DbgRegionStart.
971void DebugInfoFinder::processRegionStart(DbgRegionStartInst *DRS) {
Devang Patele4b27562009-08-28 23:24:31 +0000972 MDNode *SP = dyn_cast<MDNode>(DRS->getContext());
Devang Patel98c65172009-07-30 18:25:15 +0000973 processSubprogram(DISubprogram(SP));
Devang Patele802f1c2009-07-30 17:30:23 +0000974}
975
Devang Patel98c65172009-07-30 18:25:15 +0000976/// processRegionEnd - Process DbgRegionEnd.
977void DebugInfoFinder::processRegionEnd(DbgRegionEndInst *DRE) {
Devang Patele4b27562009-08-28 23:24:31 +0000978 MDNode *SP = dyn_cast<MDNode>(DRE->getContext());
Devang Patel98c65172009-07-30 18:25:15 +0000979 processSubprogram(DISubprogram(SP));
Devang Patele802f1c2009-07-30 17:30:23 +0000980}
981
Devang Patelb4d31302009-07-31 18:18:52 +0000982/// processDeclare - Process DbgDeclareInst.
983void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patele4b27562009-08-28 23:24:31 +0000984 DIVariable DV(cast<MDNode>(DDI->getVariable()));
Devang Patelb4d31302009-07-31 18:18:52 +0000985 if (DV.isNull())
986 return;
987
Devang Patele4b27562009-08-28 23:24:31 +0000988 if (!NodesSeen.insert(DV.getNode()))
Devang Patelb4d31302009-07-31 18:18:52 +0000989 return;
990
991 addCompileUnit(DV.getCompileUnit());
992 processType(DV.getType());
993}
994
Devang Patel72bcdb62009-08-10 22:09:58 +0000995/// addType - Add type into Tys.
996bool DebugInfoFinder::addType(DIType DT) {
997 if (DT.isNull())
998 return false;
999
Devang Patele4b27562009-08-28 23:24:31 +00001000 if (!NodesSeen.insert(DT.getNode()))
Devang Patel72bcdb62009-08-10 22:09:58 +00001001 return false;
1002
Devang Patele4b27562009-08-28 23:24:31 +00001003 TYs.push_back(DT.getNode());
Devang Patel72bcdb62009-08-10 22:09:58 +00001004 return true;
1005}
1006
Devang Pateld2f79a12009-07-28 19:55:13 +00001007/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +00001008bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001009 if (CU.isNull())
1010 return false;
1011
Devang Patele4b27562009-08-28 23:24:31 +00001012 if (!NodesSeen.insert(CU.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001013 return false;
1014
Devang Patele4b27562009-08-28 23:24:31 +00001015 CUs.push_back(CU.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001016 return true;
1017}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001018
Devang Pateld2f79a12009-07-28 19:55:13 +00001019/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +00001020bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001021 if (DIG.isNull())
1022 return false;
1023
Devang Patele4b27562009-08-28 23:24:31 +00001024 if (!NodesSeen.insert(DIG.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001025 return false;
1026
Devang Patele4b27562009-08-28 23:24:31 +00001027 GVs.push_back(DIG.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001028 return true;
1029}
1030
1031// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +00001032bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001033 if (SP.isNull())
1034 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001035
Devang Patele4b27562009-08-28 23:24:31 +00001036 if (!NodesSeen.insert(SP.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001037 return false;
1038
Devang Patele4b27562009-08-28 23:24:31 +00001039 SPs.push_back(SP.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001040 return true;
1041}
1042
Torok Edwin620f2802008-12-16 09:07:36 +00001043namespace llvm {
Bill Wendlingdc817b62009-05-14 18:26:15 +00001044 /// findStopPoint - Find the stoppoint coressponding to this instruction, that
1045 /// is the stoppoint that dominates this instruction.
1046 const DbgStopPointInst *findStopPoint(const Instruction *Inst) {
Torok Edwin620f2802008-12-16 09:07:36 +00001047 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst))
1048 return DSI;
1049
1050 const BasicBlock *BB = Inst->getParent();
1051 BasicBlock::const_iterator I = Inst, B;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001052 while (BB) {
Torok Edwin620f2802008-12-16 09:07:36 +00001053 B = BB->begin();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001054
Torok Edwin620f2802008-12-16 09:07:36 +00001055 // A BB consisting only of a terminator can't have a stoppoint.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001056 while (I != B) {
1057 --I;
1058 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1059 return DSI;
Torok Edwin620f2802008-12-16 09:07:36 +00001060 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001061
1062 // This BB didn't have a stoppoint: if there is only one predecessor, look
1063 // for a stoppoint there. We could use getIDom(), but that would require
1064 // dominator info.
Torok Edwin620f2802008-12-16 09:07:36 +00001065 BB = I->getParent()->getUniquePredecessor();
1066 if (BB)
1067 I = BB->getTerminator();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001068 }
1069
Torok Edwin620f2802008-12-16 09:07:36 +00001070 return 0;
1071 }
1072
Bill Wendlingdc817b62009-05-14 18:26:15 +00001073 /// findBBStopPoint - Find the stoppoint corresponding to first real
1074 /// (non-debug intrinsic) instruction in this Basic Block, and return the
1075 /// stoppoint for it.
1076 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB) {
1077 for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001078 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1079 return DSI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001080
1081 // Fallback to looking for stoppoint of unique predecessor. Useful if this
1082 // BB contains no stoppoints, but unique predecessor does.
Torok Edwin620f2802008-12-16 09:07:36 +00001083 BB = BB->getUniquePredecessor();
1084 if (BB)
1085 return findStopPoint(BB->getTerminator());
Bill Wendlingdc817b62009-05-14 18:26:15 +00001086
Torok Edwin620f2802008-12-16 09:07:36 +00001087 return 0;
1088 }
1089
Bill Wendlingdc817b62009-05-14 18:26:15 +00001090 Value *findDbgGlobalDeclare(GlobalVariable *V) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001091 const Module *M = V->getParent();
Devang Patele4b27562009-08-28 23:24:31 +00001092 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1093 if (!NMD)
1094 return 0;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001095
Devang Patele4b27562009-08-28 23:24:31 +00001096 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
1097 DIGlobalVariable DIG(cast_or_null<MDNode>(NMD->getElement(i)));
1098 if (DIG.isNull())
1099 continue;
1100 if (DIG.getGlobal() == V)
1101 return DIG.getNode();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001102 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001103 return 0;
1104 }
1105
Bill Wendlingdc817b62009-05-14 18:26:15 +00001106 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
Torok Edwin620f2802008-12-16 09:07:36 +00001107 /// It looks through pointer casts too.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001108 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts) {
Torok Edwin620f2802008-12-16 09:07:36 +00001109 if (stripCasts) {
1110 V = V->stripPointerCasts();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001111
Torok Edwin620f2802008-12-16 09:07:36 +00001112 // Look for the bitcast.
1113 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 (isa<BitCastInst>(I))
1116 return findDbgDeclare(*I, false);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001117
Torok Edwin620f2802008-12-16 09:07:36 +00001118 return 0;
1119 }
1120
Bill Wendlingdc817b62009-05-14 18:26:15 +00001121 // Find llvm.dbg.declare among uses of the instruction.
Torok Edwin620f2802008-12-16 09:07:36 +00001122 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001123 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001124 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))
1125 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001126
Torok Edwin620f2802008-12-16 09:07:36 +00001127 return 0;
1128 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001129
Bill Wendlingdc817b62009-05-14 18:26:15 +00001130 bool getLocationInfo(const Value *V, std::string &DisplayName,
1131 std::string &Type, unsigned &LineNo, std::string &File,
1132 std::string &Dir) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001133 DICompileUnit Unit;
1134 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001135
Torok Edwinff7d0e92009-03-10 13:41:26 +00001136 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1137 Value *DIGV = findDbgGlobalDeclare(GV);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001138 if (!DIGV) return false;
Devang Patele4b27562009-08-28 23:24:31 +00001139 DIGlobalVariable Var(cast<MDNode>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001140
Bill Wendling0582ae92009-03-13 04:39:26 +00001141 Var.getDisplayName(DisplayName);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001142 LineNo = Var.getLineNumber();
1143 Unit = Var.getCompileUnit();
1144 TypeD = Var.getType();
1145 } else {
1146 const DbgDeclareInst *DDI = findDbgDeclare(V);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001147 if (!DDI) return false;
Devang Patele4b27562009-08-28 23:24:31 +00001148 DIVariable Var(cast<MDNode>(DDI->getVariable()));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001149
Bill Wendling0582ae92009-03-13 04:39:26 +00001150 Var.getName(DisplayName);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001151 LineNo = Var.getLineNumber();
1152 Unit = Var.getCompileUnit();
1153 TypeD = Var.getType();
1154 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001155
Bill Wendling0582ae92009-03-13 04:39:26 +00001156 TypeD.getName(Type);
1157 Unit.getFilename(File);
1158 Unit.getDirectory(Dir);
Torok Edwinff7d0e92009-03-10 13:41:26 +00001159 return true;
1160 }
Devang Patel13e16b62009-06-26 01:49:18 +00001161
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001162 /// isValidDebugInfoIntrinsic - Return true if SPI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001163 /// info intrinsic.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001164 bool isValidDebugInfoIntrinsic(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +00001165 CodeGenOpt::Level OptLev) {
1166 return DIDescriptor::ValidDebugInfo(SPI.getContext(), OptLev);
1167 }
1168
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001169 /// isValidDebugInfoIntrinsic - Return true if FSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001170 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001171 bool isValidDebugInfoIntrinsic(DbgFuncStartInst &FSI,
1172 CodeGenOpt::Level OptLev) {
1173 return DIDescriptor::ValidDebugInfo(FSI.getSubprogram(), OptLev);
1174 }
1175
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001176 /// isValidDebugInfoIntrinsic - Return true if RSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001177 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001178 bool isValidDebugInfoIntrinsic(DbgRegionStartInst &RSI,
1179 CodeGenOpt::Level OptLev) {
1180 return DIDescriptor::ValidDebugInfo(RSI.getContext(), OptLev);
1181 }
1182
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001183 /// isValidDebugInfoIntrinsic - Return true if REI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001184 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001185 bool isValidDebugInfoIntrinsic(DbgRegionEndInst &REI,
1186 CodeGenOpt::Level OptLev) {
1187 return DIDescriptor::ValidDebugInfo(REI.getContext(), OptLev);
1188 }
1189
1190
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001191 /// isValidDebugInfoIntrinsic - Return true if DI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001192 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001193 bool isValidDebugInfoIntrinsic(DbgDeclareInst &DI,
1194 CodeGenOpt::Level OptLev) {
1195 return DIDescriptor::ValidDebugInfo(DI.getVariable(), OptLev);
1196 }
1197
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001198 /// ExtractDebugLocation - Extract debug location information
Devang Patel9e529c32009-07-02 01:15:24 +00001199 /// from llvm.dbg.stoppoint intrinsic.
1200 DebugLoc ExtractDebugLocation(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +00001201 DebugLocTracker &DebugLocInfo) {
1202 DebugLoc DL;
1203 Value *Context = SPI.getContext();
Devang Patel9e529c32009-07-02 01:15:24 +00001204
1205 // If this location is already tracked then use it.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001206 DebugLocTuple Tuple(cast<MDNode>(Context), SPI.getLine(),
Devang Patel9e529c32009-07-02 01:15:24 +00001207 SPI.getColumn());
1208 DenseMap<DebugLocTuple, unsigned>::iterator II
1209 = DebugLocInfo.DebugIdMap.find(Tuple);
1210 if (II != DebugLocInfo.DebugIdMap.end())
1211 return DebugLoc::get(II->second);
1212
1213 // Add a new location entry.
1214 unsigned Id = DebugLocInfo.DebugLocations.size();
1215 DebugLocInfo.DebugLocations.push_back(Tuple);
1216 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001217
Devang Patel9e529c32009-07-02 01:15:24 +00001218 return DebugLoc::get(Id);
1219 }
1220
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001221 /// ExtractDebugLocation - Extract debug location information
Devang Patel1b75f442009-09-16 18:20:05 +00001222 /// from DILocation.
1223 DebugLoc ExtractDebugLocation(DILocation &Loc,
1224 DebugLocTracker &DebugLocInfo) {
1225 DebugLoc DL;
1226 MDNode *Context = Loc.getScope().getNode();
1227
1228 // If this location is already tracked then use it.
1229 DebugLocTuple Tuple(Context, Loc.getLineNumber(),
Daniel Dunbara279bc32009-09-20 02:20:51 +00001230 Loc.getColumnNumber());
Devang Patel1b75f442009-09-16 18:20:05 +00001231 DenseMap<DebugLocTuple, unsigned>::iterator II
1232 = DebugLocInfo.DebugIdMap.find(Tuple);
1233 if (II != DebugLocInfo.DebugIdMap.end())
1234 return DebugLoc::get(II->second);
1235
1236 // Add a new location entry.
1237 unsigned Id = DebugLocInfo.DebugLocations.size();
1238 DebugLocInfo.DebugLocations.push_back(Tuple);
1239 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001240
Devang Patel1b75f442009-09-16 18:20:05 +00001241 return DebugLoc::get(Id);
1242 }
1243
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001244 /// ExtractDebugLocation - Extract debug location information
Devang Patel9e529c32009-07-02 01:15:24 +00001245 /// from llvm.dbg.func_start intrinsic.
1246 DebugLoc ExtractDebugLocation(DbgFuncStartInst &FSI,
Devang Patel9e529c32009-07-02 01:15:24 +00001247 DebugLocTracker &DebugLocInfo) {
1248 DebugLoc DL;
1249 Value *SP = FSI.getSubprogram();
Devang Patel9e529c32009-07-02 01:15:24 +00001250
Devang Patele4b27562009-08-28 23:24:31 +00001251 DISubprogram Subprogram(cast<MDNode>(SP));
Devang Patel9e529c32009-07-02 01:15:24 +00001252 unsigned Line = Subprogram.getLineNumber();
1253 DICompileUnit CU(Subprogram.getCompileUnit());
1254
1255 // If this location is already tracked then use it.
Devang Patele4b27562009-08-28 23:24:31 +00001256 DebugLocTuple Tuple(CU.getNode(), Line, /* Column */ 0);
Devang Patel9e529c32009-07-02 01:15:24 +00001257 DenseMap<DebugLocTuple, unsigned>::iterator II
1258 = DebugLocInfo.DebugIdMap.find(Tuple);
1259 if (II != DebugLocInfo.DebugIdMap.end())
1260 return DebugLoc::get(II->second);
1261
1262 // Add a new location entry.
1263 unsigned Id = DebugLocInfo.DebugLocations.size();
1264 DebugLocInfo.DebugLocations.push_back(Tuple);
1265 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001266
Devang Patel9e529c32009-07-02 01:15:24 +00001267 return DebugLoc::get(Id);
1268 }
1269
1270 /// isInlinedFnStart - Return true if FSI is starting an inlined function.
1271 bool isInlinedFnStart(DbgFuncStartInst &FSI, const Function *CurrentFn) {
Devang Patele4b27562009-08-28 23:24:31 +00001272 DISubprogram Subprogram(cast<MDNode>(FSI.getSubprogram()));
Devang Patel9e529c32009-07-02 01:15:24 +00001273 if (Subprogram.describes(CurrentFn))
1274 return false;
1275
1276 return true;
1277 }
1278
1279 /// isInlinedFnEnd - Return true if REI is ending an inlined function.
1280 bool isInlinedFnEnd(DbgRegionEndInst &REI, const Function *CurrentFn) {
Devang Patele4b27562009-08-28 23:24:31 +00001281 DISubprogram Subprogram(cast<MDNode>(REI.getContext()));
Devang Patel9e529c32009-07-02 01:15:24 +00001282 if (Subprogram.isNull() || Subprogram.describes(CurrentFn))
1283 return false;
1284
1285 return true;
1286 }
Torok Edwin620f2802008-12-16 09:07:36 +00001287}