blob: db5a0cbb21476f034f58d972ec9d287de6366f77 [file] [log] [blame]
Chris Lattnera45664f2008-11-10 02:56:27 +00001//===--- DebugInfo.cpp - Debug Information Helper Classes -----------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the helper classes used to build and interpret debug
11// information in LLVM IR form.
12//
13//===----------------------------------------------------------------------===//
14
15#include "llvm/Analysis/DebugInfo.h"
16#include "llvm/Constants.h"
17#include "llvm/DerivedTypes.h"
18#include "llvm/Intrinsics.h"
Torok Edwin620f2802008-12-16 09:07:36 +000019#include "llvm/IntrinsicInst.h"
Chris Lattnera45664f2008-11-10 02:56:27 +000020#include "llvm/Instructions.h"
Owen Anderson99035272009-07-07 17:12:53 +000021#include "llvm/LLVMContext.h"
Chris Lattnera45664f2008-11-10 02:56:27 +000022#include "llvm/Module.h"
23#include "llvm/Analysis/ValueTracking.h"
Devang Patele4b27562009-08-28 23:24:31 +000024#include "llvm/ADT/SmallPtrSet.h"
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000025#include "llvm/Support/Dwarf.h"
Devang Patel9e529c32009-07-02 01:15:24 +000026#include "llvm/Support/DebugLoc.h"
Chris Lattnera81d29b2009-08-23 07:33:14 +000027#include "llvm/Support/raw_ostream.h"
Chris Lattnera45664f2008-11-10 02:56:27 +000028using namespace llvm;
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000029using namespace llvm::dwarf;
Chris Lattnera45664f2008-11-10 02:56:27 +000030
31//===----------------------------------------------------------------------===//
32// DIDescriptor
33//===----------------------------------------------------------------------===//
34
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000035/// ValidDebugInfo - Return true if V represents valid debug info value.
Devang Patele4b27562009-08-28 23:24:31 +000036/// FIXME : Add DIDescriptor.isValid()
37bool DIDescriptor::ValidDebugInfo(MDNode *N, CodeGenOpt::Level OptLevel) {
38 if (!N)
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000039 return false;
40
Devang Patele4b27562009-08-28 23:24:31 +000041 DIDescriptor DI(N);
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000042
43 // Check current version. Allow Version6 for now.
44 unsigned Version = DI.getVersion();
45 if (Version != LLVMDebugVersion && Version != LLVMDebugVersion6)
46 return false;
47
48 unsigned Tag = DI.getTag();
49 switch (Tag) {
50 case DW_TAG_variable:
Devang Patele4b27562009-08-28 23:24:31 +000051 assert(DIVariable(N).Verify() && "Invalid DebugInfo value");
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000052 break;
53 case DW_TAG_compile_unit:
Devang Patele4b27562009-08-28 23:24:31 +000054 assert(DICompileUnit(N).Verify() && "Invalid DebugInfo value");
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000055 break;
56 case DW_TAG_subprogram:
Devang Patele4b27562009-08-28 23:24:31 +000057 assert(DISubprogram(N).Verify() && "Invalid DebugInfo value");
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000058 break;
59 case DW_TAG_lexical_block:
Bill Wendlingdc817b62009-05-14 18:26:15 +000060 // FIXME: This interfers with the quality of generated code during
61 // optimization.
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000062 if (OptLevel != CodeGenOpt::None)
63 return false;
Bill Wendlingdc817b62009-05-14 18:26:15 +000064 // FALLTHROUGH
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000065 default:
66 break;
67 }
68
69 return true;
70}
71
Devang Patele4b27562009-08-28 23:24:31 +000072DIDescriptor::DIDescriptor(MDNode *N, unsigned RequiredTag) {
73 DbgNode = N;
Daniel Dunbarf612ff62009-09-19 20:40:05 +000074
Bill Wendlingdc817b62009-05-14 18:26:15 +000075 // If this is non-null, check to see if the Tag matches. If not, set to null.
Devang Patele4b27562009-08-28 23:24:31 +000076 if (N && getTag() != RequiredTag) {
77 DbgNode = 0;
78 }
Chris Lattnera45664f2008-11-10 02:56:27 +000079}
80
Devang Patel5ccdd102009-09-29 18:40:58 +000081const char *
82DIDescriptor::getStringField(unsigned Elt) const {
Devang Patele4b27562009-08-28 23:24:31 +000083 if (DbgNode == 0)
Devang Patel5ccdd102009-09-29 18:40:58 +000084 return NULL;
Chris Lattnera45664f2008-11-10 02:56:27 +000085
Daniel Dunbarf612ff62009-09-19 20:40:05 +000086 if (Elt < DbgNode->getNumElements())
Devang Patel5ccdd102009-09-29 18:40:58 +000087 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getElement(Elt)))
88 return MDS->getString().data();
Daniel Dunbarf612ff62009-09-19 20:40:05 +000089
Devang Patel5ccdd102009-09-29 18:40:58 +000090 return NULL;
Chris Lattnera45664f2008-11-10 02:56:27 +000091}
92
93uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +000094 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +000095 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +000096
Devang Patele4b27562009-08-28 23:24:31 +000097 if (Elt < DbgNode->getNumElements())
98 if (ConstantInt *CI = dyn_cast<ConstantInt>(DbgNode->getElement(Elt)))
99 return CI->getZExtValue();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000100
Chris Lattnera45664f2008-11-10 02:56:27 +0000101 return 0;
102}
103
Chris Lattnera45664f2008-11-10 02:56:27 +0000104DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000105 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +0000106 return DIDescriptor();
Bill Wendlingdc817b62009-05-14 18:26:15 +0000107
Devang Patele4b27562009-08-28 23:24:31 +0000108 if (Elt < DbgNode->getNumElements() && DbgNode->getElement(Elt))
109 return DIDescriptor(dyn_cast<MDNode>(DbgNode->getElement(Elt)));
110
111 return DIDescriptor();
Chris Lattnera45664f2008-11-10 02:56:27 +0000112}
113
114GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000115 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +0000116 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000117
Devang Patele4b27562009-08-28 23:24:31 +0000118 if (Elt < DbgNode->getNumElements())
119 return dyn_cast<GlobalVariable>(DbgNode->getElement(Elt));
120 return 0;
Chris Lattnera45664f2008-11-10 02:56:27 +0000121}
122
Chris Lattnera45664f2008-11-10 02:56:27 +0000123//===----------------------------------------------------------------------===//
Devang Patel6ceea332009-08-31 18:49:10 +0000124// Predicates
Chris Lattnera45664f2008-11-10 02:56:27 +0000125//===----------------------------------------------------------------------===//
126
Devang Patel6ceea332009-08-31 18:49:10 +0000127/// isBasicType - Return true if the specified tag is legal for
128/// DIBasicType.
129bool DIDescriptor::isBasicType() const {
Devang Patel5a685092009-08-31 20:27:49 +0000130 assert (!isNull() && "Invalid descriptor!");
Devang Patel6ceea332009-08-31 18:49:10 +0000131 unsigned Tag = getTag();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000132
Devang Patel6ceea332009-08-31 18:49:10 +0000133 return Tag == dwarf::DW_TAG_base_type;
Torok Edwinb07fbd92008-12-13 08:25:29 +0000134}
Chris Lattnera45664f2008-11-10 02:56:27 +0000135
Devang Patel6ceea332009-08-31 18:49:10 +0000136/// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
137bool DIDescriptor::isDerivedType() const {
Devang Patel5a685092009-08-31 20:27:49 +0000138 assert (!isNull() && "Invalid descriptor!");
Devang Patel6ceea332009-08-31 18:49:10 +0000139 unsigned Tag = getTag();
140
Chris Lattnera45664f2008-11-10 02:56:27 +0000141 switch (Tag) {
142 case dwarf::DW_TAG_typedef:
143 case dwarf::DW_TAG_pointer_type:
144 case dwarf::DW_TAG_reference_type:
145 case dwarf::DW_TAG_const_type:
146 case dwarf::DW_TAG_volatile_type:
147 case dwarf::DW_TAG_restrict_type:
148 case dwarf::DW_TAG_member:
149 case dwarf::DW_TAG_inheritance:
150 return true;
151 default:
Devang Patele4b27562009-08-28 23:24:31 +0000152 // CompositeTypes are currently modelled as DerivedTypes.
Devang Patel6ceea332009-08-31 18:49:10 +0000153 return isCompositeType();
Chris Lattnera45664f2008-11-10 02:56:27 +0000154 }
155}
156
Chris Lattnera45664f2008-11-10 02:56:27 +0000157/// isCompositeType - Return true if the specified tag is legal for
158/// DICompositeType.
Devang Patel6ceea332009-08-31 18:49:10 +0000159bool DIDescriptor::isCompositeType() const {
Devang Patel5a685092009-08-31 20:27:49 +0000160 assert (!isNull() && "Invalid descriptor!");
Devang Patel6ceea332009-08-31 18:49:10 +0000161 unsigned Tag = getTag();
162
163 switch (Tag) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000164 case dwarf::DW_TAG_array_type:
165 case dwarf::DW_TAG_structure_type:
166 case dwarf::DW_TAG_union_type:
167 case dwarf::DW_TAG_enumeration_type:
168 case dwarf::DW_TAG_vector_type:
169 case dwarf::DW_TAG_subroutine_type:
Devang Patel25cb0d72009-03-25 03:52:06 +0000170 case dwarf::DW_TAG_class_type:
Chris Lattnera45664f2008-11-10 02:56:27 +0000171 return true;
172 default:
173 return false;
174 }
175}
176
Chris Lattnera45664f2008-11-10 02:56:27 +0000177/// isVariable - Return true if the specified tag is legal for DIVariable.
Devang Patel6ceea332009-08-31 18:49:10 +0000178bool DIDescriptor::isVariable() const {
Devang Patel5a685092009-08-31 20:27:49 +0000179 assert (!isNull() && "Invalid descriptor!");
Devang Patel6ceea332009-08-31 18:49:10 +0000180 unsigned Tag = getTag();
181
Chris Lattnera45664f2008-11-10 02:56:27 +0000182 switch (Tag) {
183 case dwarf::DW_TAG_auto_variable:
184 case dwarf::DW_TAG_arg_variable:
185 case dwarf::DW_TAG_return_variable:
186 return true;
187 default:
188 return false;
189 }
190}
191
Devang Patel6ceea332009-08-31 18:49:10 +0000192/// isSubprogram - Return true if the specified tag is legal for
193/// DISubprogram.
194bool DIDescriptor::isSubprogram() const {
Devang Patel5a685092009-08-31 20:27:49 +0000195 assert (!isNull() && "Invalid descriptor!");
Devang Patel6ceea332009-08-31 18:49:10 +0000196 unsigned Tag = getTag();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000197
Devang Patel6ceea332009-08-31 18:49:10 +0000198 return Tag == dwarf::DW_TAG_subprogram;
199}
200
201/// isGlobalVariable - Return true if the specified tag is legal for
202/// DIGlobalVariable.
203bool DIDescriptor::isGlobalVariable() const {
Devang Patel5a685092009-08-31 20:27:49 +0000204 assert (!isNull() && "Invalid descriptor!");
Devang Patel6ceea332009-08-31 18:49:10 +0000205 unsigned Tag = getTag();
206
207 return Tag == dwarf::DW_TAG_variable;
208}
209
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000210/// isScope - Return true if the specified tag is one of the scope
Devang Patel43d98b32009-08-31 20:44:45 +0000211/// related tag.
212bool DIDescriptor::isScope() const {
213 assert (!isNull() && "Invalid descriptor!");
214 unsigned Tag = getTag();
215
216 switch (Tag) {
217 case dwarf::DW_TAG_compile_unit:
218 case dwarf::DW_TAG_lexical_block:
219 case dwarf::DW_TAG_subprogram:
220 return true;
221 default:
222 break;
223 }
224 return false;
225}
Devang Patel6ceea332009-08-31 18:49:10 +0000226
Devang Patelc9f322d2009-08-31 21:34:44 +0000227/// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
228bool DIDescriptor::isCompileUnit() const {
229 assert (!isNull() && "Invalid descriptor!");
230 unsigned Tag = getTag();
231
232 return Tag == dwarf::DW_TAG_compile_unit;
233}
234
Devang Patel5e005d82009-08-31 22:00:15 +0000235/// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
236bool DIDescriptor::isLexicalBlock() const {
237 assert (!isNull() && "Invalid descriptor!");
238 unsigned Tag = getTag();
239
240 return Tag == dwarf::DW_TAG_lexical_block;
241}
242
Devang Patel6ceea332009-08-31 18:49:10 +0000243//===----------------------------------------------------------------------===//
244// Simple Descriptor Constructors and other Methods
245//===----------------------------------------------------------------------===//
246
247DIType::DIType(MDNode *N) : DIDescriptor(N) {
248 if (!N) return;
249 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
250 DbgNode = 0;
251 }
252}
253
Devang Patel68afdc32009-01-05 18:33:01 +0000254unsigned DIArray::getNumElements() const {
Devang Patele4b27562009-08-28 23:24:31 +0000255 assert (DbgNode && "Invalid DIArray");
256 return DbgNode->getNumElements();
Devang Patel68afdc32009-01-05 18:33:01 +0000257}
Chris Lattnera45664f2008-11-10 02:56:27 +0000258
Devang Patelc4999d72009-07-22 18:23:44 +0000259/// replaceAllUsesWith - Replace all uses of debug info referenced by
260/// this descriptor. After this completes, the current debug info value
261/// is erased.
262void DIDerivedType::replaceAllUsesWith(DIDescriptor &D) {
263 if (isNull())
264 return;
265
Devang Patel6930f4f2009-07-22 18:56:16 +0000266 assert (!D.isNull() && "Can not replace with null");
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000267
268 // Since we use a TrackingVH for the node, its easy for clients to manufacture
269 // legitimate situations where they want to replaceAllUsesWith() on something
270 // which, due to uniquing, has merged with the source. We shield clients from
271 // this detail by allowing a value to be replaced with replaceAllUsesWith()
272 // itself.
273 if (getNode() != D.getNode()) {
274 MDNode *Node = DbgNode;
275 Node->replaceAllUsesWith(D.getNode());
276 delete Node;
277 }
Devang Patelc4999d72009-07-22 18:23:44 +0000278}
279
Devang Patelb79b5352009-01-19 23:21:49 +0000280/// Verify - Verify that a compile unit is well formed.
281bool DICompileUnit::Verify() const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000282 if (isNull())
Devang Patelb79b5352009-01-19 23:21:49 +0000283 return false;
Devang Patel5ccdd102009-09-29 18:40:58 +0000284 const char *N = getFilename();
285 if (!N)
Bill Wendling0582ae92009-03-13 04:39:26 +0000286 return false;
Devang Patelb79b5352009-01-19 23:21:49 +0000287 // It is possible that directory and produce string is empty.
Bill Wendling0582ae92009-03-13 04:39:26 +0000288 return true;
Devang Patelb79b5352009-01-19 23:21:49 +0000289}
290
291/// Verify - Verify that a type descriptor is well formed.
292bool DIType::Verify() const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000293 if (isNull())
Devang Patelb79b5352009-01-19 23:21:49 +0000294 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000295 if (getContext().isNull())
Devang Patelb79b5352009-01-19 23:21:49 +0000296 return false;
297
298 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000299 if (!CU.isNull() && !CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000300 return false;
301 return true;
302}
303
304/// Verify - Verify that a composite type descriptor is well formed.
305bool DICompositeType::Verify() const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000306 if (isNull())
Devang Patelb79b5352009-01-19 23:21:49 +0000307 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000308 if (getContext().isNull())
Devang Patelb79b5352009-01-19 23:21:49 +0000309 return false;
310
311 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000312 if (!CU.isNull() && !CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000313 return false;
314 return true;
315}
316
317/// Verify - Verify that a subprogram descriptor is well formed.
318bool DISubprogram::Verify() const {
319 if (isNull())
320 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000321
Devang Patelb79b5352009-01-19 23:21:49 +0000322 if (getContext().isNull())
323 return false;
324
325 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000326 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000327 return false;
328
329 DICompositeType Ty = getType();
330 if (!Ty.isNull() && !Ty.Verify())
331 return false;
332 return true;
333}
334
335/// Verify - Verify that a global variable descriptor is well formed.
336bool DIGlobalVariable::Verify() const {
337 if (isNull())
338 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000339
Devang Patelb79b5352009-01-19 23:21:49 +0000340 if (getContext().isNull())
341 return false;
342
343 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000344 if (!CU.isNull() && !CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000345 return false;
346
347 DIType Ty = getType();
348 if (!Ty.Verify())
349 return false;
350
351 if (!getGlobal())
352 return false;
353
354 return true;
355}
356
357/// Verify - Verify that a variable descriptor is well formed.
358bool DIVariable::Verify() const {
359 if (isNull())
360 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000361
Devang Patelb79b5352009-01-19 23:21:49 +0000362 if (getContext().isNull())
363 return false;
364
365 DIType Ty = getType();
366 if (!Ty.Verify())
367 return false;
368
Devang Patelb79b5352009-01-19 23:21:49 +0000369 return true;
370}
371
Devang Patel36375ee2009-02-17 21:23:59 +0000372/// getOriginalTypeSize - If this type is derived from a base type then
373/// return base type size.
374uint64_t DIDerivedType::getOriginalTypeSize() const {
375 if (getTag() != dwarf::DW_TAG_member)
376 return getSizeInBits();
377 DIType BT = getTypeDerivedFrom();
378 if (BT.getTag() != dwarf::DW_TAG_base_type)
379 return getSizeInBits();
380 return BT.getSizeInBits();
381}
Devang Patelb79b5352009-01-19 23:21:49 +0000382
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000383/// describes - Return true if this subprogram provides debugging
384/// information for the function F.
385bool DISubprogram::describes(const Function *F) {
386 assert (F && "Invalid function");
Devang Patel5ccdd102009-09-29 18:40:58 +0000387 const char *Name = getLinkageName();
388 if (!Name)
389 Name = getName();
390 if (strcmp(F->getName().data(), Name) == 0)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000391 return true;
392 return false;
393}
394
Chris Lattnera45664f2008-11-10 02:56:27 +0000395//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000396// DIDescriptor: dump routines for all descriptors.
397//===----------------------------------------------------------------------===//
398
399
400/// dump - Print descriptor.
401void DIDescriptor::dump() const {
Devang Patele4b27562009-08-28 23:24:31 +0000402 errs() << "[" << dwarf::TagString(getTag()) << "] ";
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000403 errs().write_hex((intptr_t) &*DbgNode) << ']';
Devang Patel7136a652009-07-01 22:10:23 +0000404}
405
406/// dump - Print compile unit.
407void DICompileUnit::dump() const {
408 if (getLanguage())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000409 errs() << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000410
Devang Patel5ccdd102009-09-29 18:40:58 +0000411 errs() << " [" << getDirectory() << "/" << getFilename() << " ]";
Devang Patel7136a652009-07-01 22:10:23 +0000412}
413
414/// dump - Print type.
415void DIType::dump() const {
416 if (isNull()) return;
417
Devang Patel5ccdd102009-09-29 18:40:58 +0000418 if (const char *Res = getName())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000419 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000420
421 unsigned Tag = getTag();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000422 errs() << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000423
424 // TODO : Print context
425 getCompileUnit().dump();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000426 errs() << " ["
427 << getLineNumber() << ", "
Chris Lattnera81d29b2009-08-23 07:33:14 +0000428 << getSizeInBits() << ", "
429 << getAlignInBits() << ", "
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000430 << getOffsetInBits()
Chris Lattnera81d29b2009-08-23 07:33:14 +0000431 << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000432
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000433 if (isPrivate())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000434 errs() << " [private] ";
Devang Patel7136a652009-07-01 22:10:23 +0000435 else if (isProtected())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000436 errs() << " [protected] ";
Devang Patel7136a652009-07-01 22:10:23 +0000437
438 if (isForwardDecl())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000439 errs() << " [fwd] ";
Devang Patel7136a652009-07-01 22:10:23 +0000440
Devang Patel6ceea332009-08-31 18:49:10 +0000441 if (isBasicType())
Devang Patele4b27562009-08-28 23:24:31 +0000442 DIBasicType(DbgNode).dump();
Devang Patel6ceea332009-08-31 18:49:10 +0000443 else if (isDerivedType())
Devang Patele4b27562009-08-28 23:24:31 +0000444 DIDerivedType(DbgNode).dump();
Devang Patel6ceea332009-08-31 18:49:10 +0000445 else if (isCompositeType())
Devang Patele4b27562009-08-28 23:24:31 +0000446 DICompositeType(DbgNode).dump();
Devang Patel7136a652009-07-01 22:10:23 +0000447 else {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000448 errs() << "Invalid DIType\n";
Devang Patel7136a652009-07-01 22:10:23 +0000449 return;
450 }
451
Chris Lattnera81d29b2009-08-23 07:33:14 +0000452 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000453}
454
455/// dump - Print basic type.
456void DIBasicType::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000457 errs() << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000458}
459
460/// dump - Print derived type.
461void DIDerivedType::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000462 errs() << "\n\t Derived From: "; getTypeDerivedFrom().dump();
Devang Patel7136a652009-07-01 22:10:23 +0000463}
464
465/// dump - Print composite type.
466void DICompositeType::dump() const {
467 DIArray A = getTypeArray();
468 if (A.isNull())
469 return;
Chris Lattnera81d29b2009-08-23 07:33:14 +0000470 errs() << " [" << A.getNumElements() << " elements]";
Devang Patel7136a652009-07-01 22:10:23 +0000471}
472
473/// dump - Print global.
474void DIGlobal::dump() const {
Devang Patel5ccdd102009-09-29 18:40:58 +0000475 if (const char *Res = getName())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000476 errs() << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000477
478 unsigned Tag = getTag();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000479 errs() << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000480
481 // TODO : Print context
482 getCompileUnit().dump();
Chris Lattnera81d29b2009-08-23 07:33:14 +0000483 errs() << " [" << getLineNumber() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000484
485 if (isLocalToUnit())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000486 errs() << " [local] ";
Devang Patel7136a652009-07-01 22:10:23 +0000487
488 if (isDefinition())
Chris Lattnera81d29b2009-08-23 07:33:14 +0000489 errs() << " [def] ";
Devang Patel7136a652009-07-01 22:10:23 +0000490
Devang Patel6ceea332009-08-31 18:49:10 +0000491 if (isGlobalVariable())
Devang Patele4b27562009-08-28 23:24:31 +0000492 DIGlobalVariable(DbgNode).dump();
Devang Patel7136a652009-07-01 22:10:23 +0000493
Chris Lattnera81d29b2009-08-23 07:33:14 +0000494 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000495}
496
497/// dump - Print subprogram.
498void DISubprogram::dump() const {
Devang Patel5ccdd102009-09-29 18:40:58 +0000499 if (const char *Res = getName())
Devang Patel82dfc0c2009-08-31 22:47:13 +0000500 errs() << " [" << Res << "] ";
501
502 unsigned Tag = getTag();
503 errs() << " [" << dwarf::TagString(Tag) << "] ";
504
505 // TODO : Print context
506 getCompileUnit().dump();
507 errs() << " [" << getLineNumber() << "] ";
508
509 if (isLocalToUnit())
510 errs() << " [local] ";
511
512 if (isDefinition())
513 errs() << " [def] ";
514
515 errs() << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000516}
517
518/// dump - Print global variable.
519void DIGlobalVariable::dump() const {
Chris Lattnera81d29b2009-08-23 07:33:14 +0000520 errs() << " [";
521 getGlobal()->dump();
522 errs() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000523}
524
525/// dump - Print variable.
526void DIVariable::dump() const {
Devang Patel5ccdd102009-09-29 18:40:58 +0000527 if (const char *Res = getName())
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";
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000534
535 // FIXME: Dump complex addresses
Devang Patel7136a652009-07-01 22:10:23 +0000536}
537
538//===----------------------------------------------------------------------===//
Chris Lattnera45664f2008-11-10 02:56:27 +0000539// DIFactory: Basic Helpers
540//===----------------------------------------------------------------------===//
541
Bill Wendlingdc817b62009-05-14 18:26:15 +0000542DIFactory::DIFactory(Module &m)
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000543 : M(m), VMContext(M.getContext()), StopPointFn(0), FuncStartFn(0),
Owen Anderson99035272009-07-07 17:12:53 +0000544 RegionStartFn(0), RegionEndFn(0),
Bill Wendlingdc817b62009-05-14 18:26:15 +0000545 DeclareFn(0) {
Owen Andersond7f2a6c2009-08-05 23:16:16 +0000546 EmptyStructPtr = PointerType::getUnqual(StructType::get(VMContext));
Chris Lattner497a7a82008-11-10 04:10:34 +0000547}
548
Chris Lattnera45664f2008-11-10 02:56:27 +0000549Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000550 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000551 "Tag too large for debug encoding!");
Owen Anderson1d0be152009-08-13 21:58:54 +0000552 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000553}
554
Chris Lattnera45664f2008-11-10 02:56:27 +0000555//===----------------------------------------------------------------------===//
556// DIFactory: Primary Constructors
557//===----------------------------------------------------------------------===//
558
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000559/// GetOrCreateArray - Create an descriptor for an array of descriptors.
Chris Lattnera45664f2008-11-10 02:56:27 +0000560/// This implicitly uniques the arrays created.
561DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
Devang Patele4b27562009-08-28 23:24:31 +0000562 SmallVector<Value*, 16> Elts;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000563
Devang Patele4b27562009-08-28 23:24:31 +0000564 if (NumTys == 0)
565 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
566 else
567 for (unsigned i = 0; i != NumTys; ++i)
568 Elts.push_back(Tys[i].getNode());
Devang Patel82459882009-08-26 05:01:18 +0000569
Devang Patele4b27562009-08-28 23:24:31 +0000570 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
Chris Lattnera45664f2008-11-10 02:56:27 +0000571}
572
573/// GetOrCreateSubrange - Create a descriptor for a value range. This
574/// implicitly uniques the values returned.
575DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
Devang Patele4b27562009-08-28 23:24:31 +0000576 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000577 GetTagConstant(dwarf::DW_TAG_subrange_type),
Owen Anderson1d0be152009-08-13 21:58:54 +0000578 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
579 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
Chris Lattnera45664f2008-11-10 02:56:27 +0000580 };
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000581
Devang Patele4b27562009-08-28 23:24:31 +0000582 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000583}
584
585
586
587/// CreateCompileUnit - Create a new descriptor for the specified compile
588/// unit. Note that this does not unique compile units within the module.
589DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
Devang Patel5ccdd102009-09-29 18:40:58 +0000590 StringRef Filename,
591 StringRef Directory,
592 StringRef Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000593 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000594 bool isOptimized,
Devang Patel13319ce2009-02-17 22:43:44 +0000595 const char *Flags,
596 unsigned RunTimeVer) {
Devang Patele4b27562009-08-28 23:24:31 +0000597 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000598 GetTagConstant(dwarf::DW_TAG_compile_unit),
Devang Patele4b27562009-08-28 23:24:31 +0000599 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Owen Anderson1d0be152009-08-13 21:58:54 +0000600 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
Devang Patele4b27562009-08-28 23:24:31 +0000601 MDString::get(VMContext, Filename),
602 MDString::get(VMContext, Directory),
603 MDString::get(VMContext, Producer),
Owen Anderson1d0be152009-08-13 21:58:54 +0000604 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
605 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patele4b27562009-08-28 23:24:31 +0000606 MDString::get(VMContext, Flags),
Owen Anderson1d0be152009-08-13 21:58:54 +0000607 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000608 };
Devang Patele4b27562009-08-28 23:24:31 +0000609
610 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000611}
612
613/// CreateEnumerator - Create a single enumerator value.
Devang Patel5ccdd102009-09-29 18:40:58 +0000614DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
Devang Patele4b27562009-08-28 23:24:31 +0000615 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000616 GetTagConstant(dwarf::DW_TAG_enumerator),
Devang Patele4b27562009-08-28 23:24:31 +0000617 MDString::get(VMContext, Name),
Owen Anderson1d0be152009-08-13 21:58:54 +0000618 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000619 };
Devang Patele4b27562009-08-28 23:24:31 +0000620 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000621}
622
623
624/// CreateBasicType - Create a basic type like int, float, etc.
625DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Devang Patel5ccdd102009-09-29 18:40:58 +0000626 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000627 DICompileUnit CompileUnit,
628 unsigned LineNumber,
629 uint64_t SizeInBits,
630 uint64_t AlignInBits,
631 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000632 unsigned Encoding) {
Devang Patele4b27562009-08-28 23:24:31 +0000633 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000634 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patele4b27562009-08-28 23:24:31 +0000635 Context.getNode(),
636 MDString::get(VMContext, Name),
637 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000638 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
639 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
640 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
641 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
642 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
643 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000644 };
Devang Patele4b27562009-08-28 23:24:31 +0000645 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000646}
647
648/// CreateDerivedType - Create a derived type like const qualified type,
649/// pointer, typedef, etc.
650DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
651 DIDescriptor Context,
Devang Patel5ccdd102009-09-29 18:40:58 +0000652 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000653 DICompileUnit CompileUnit,
654 unsigned LineNumber,
655 uint64_t SizeInBits,
656 uint64_t AlignInBits,
657 uint64_t OffsetInBits,
658 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000659 DIType DerivedFrom) {
Devang Patele4b27562009-08-28 23:24:31 +0000660 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000661 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000662 Context.getNode(),
663 MDString::get(VMContext, Name),
664 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000665 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
666 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
667 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
668 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
669 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patele4b27562009-08-28 23:24:31 +0000670 DerivedFrom.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000671 };
Devang Patele4b27562009-08-28 23:24:31 +0000672 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000673}
674
675/// CreateCompositeType - Create a composite type like array, struct, etc.
676DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
677 DIDescriptor Context,
Devang Patel5ccdd102009-09-29 18:40:58 +0000678 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000679 DICompileUnit CompileUnit,
680 unsigned LineNumber,
681 uint64_t SizeInBits,
682 uint64_t AlignInBits,
683 uint64_t OffsetInBits,
684 unsigned Flags,
685 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000686 DIArray Elements,
687 unsigned RuntimeLang) {
Owen Andersone277fed2009-07-07 16:31:25 +0000688
Devang Patele4b27562009-08-28 23:24:31 +0000689 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000690 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000691 Context.getNode(),
692 MDString::get(VMContext, Name),
693 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000694 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
695 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
696 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
697 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
698 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patele4b27562009-08-28 23:24:31 +0000699 DerivedFrom.getNode(),
700 Elements.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000701 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
Chris Lattnera45664f2008-11-10 02:56:27 +0000702 };
Devang Patele4b27562009-08-28 23:24:31 +0000703 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
Chris Lattnera45664f2008-11-10 02:56:27 +0000704}
705
706
707/// CreateSubprogram - Create a new descriptor for the specified subprogram.
708/// See comments in DISubprogram for descriptions of these fields. This
709/// method does not unique the generated descriptors.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000710DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
Devang Patel5ccdd102009-09-29 18:40:58 +0000711 StringRef Name,
712 StringRef DisplayName,
713 StringRef LinkageName,
Chris Lattnera45664f2008-11-10 02:56:27 +0000714 DICompileUnit CompileUnit,
715 unsigned LineNo, DIType Type,
716 bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000717 bool isDefinition) {
Devang Patel854967e2008-12-17 22:39:29 +0000718
Devang Patele4b27562009-08-28 23:24:31 +0000719 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000720 GetTagConstant(dwarf::DW_TAG_subprogram),
Devang Patele4b27562009-08-28 23:24:31 +0000721 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
722 Context.getNode(),
723 MDString::get(VMContext, Name),
724 MDString::get(VMContext, DisplayName),
725 MDString::get(VMContext, LinkageName),
726 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000727 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000728 Type.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000729 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
730 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition)
Chris Lattnera45664f2008-11-10 02:56:27 +0000731 };
Devang Patele4b27562009-08-28 23:24:31 +0000732 return DISubprogram(MDNode::get(VMContext, &Elts[0], 11));
Chris Lattnera45664f2008-11-10 02:56:27 +0000733}
734
735/// CreateGlobalVariable - Create a new descriptor for the specified global.
736DIGlobalVariable
Devang Patel5ccdd102009-09-29 18:40:58 +0000737DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
738 StringRef DisplayName,
739 StringRef LinkageName,
Chris Lattnera45664f2008-11-10 02:56:27 +0000740 DICompileUnit CompileUnit,
741 unsigned LineNo, DIType Type,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +0000742 bool isDefinition, llvm::GlobalVariable *Val) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000743 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000744 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patele4b27562009-08-28 23:24:31 +0000745 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
746 Context.getNode(),
747 MDString::get(VMContext, Name),
748 MDString::get(VMContext, DisplayName),
749 MDString::get(VMContext, LinkageName),
750 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000751 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000752 Type.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000753 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
754 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
Devang Patele4b27562009-08-28 23:24:31 +0000755 Val
Chris Lattnera45664f2008-11-10 02:56:27 +0000756 };
Devang Patele4b27562009-08-28 23:24:31 +0000757
758 Value *const *Vs = &Elts[0];
759 MDNode *Node = MDNode::get(VMContext,Vs, 12);
760
761 // Create a named metadata so that we do not lose this mdnode.
762 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
763 NMD->addElement(Node);
764
765 return DIGlobalVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +0000766}
767
768
769/// CreateVariable - Create a new descriptor for the specified variable.
770DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
Devang Patel5ccdd102009-09-29 18:40:58 +0000771 StringRef Name,
Chris Lattnera45664f2008-11-10 02:56:27 +0000772 DICompileUnit CompileUnit, unsigned LineNo,
Devang Pateldd9db662009-01-30 18:20:31 +0000773 DIType Type) {
Devang Patele4b27562009-08-28 23:24:31 +0000774 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000775 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000776 Context.getNode(),
777 MDString::get(VMContext, Name),
778 CompileUnit.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000779 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patele4b27562009-08-28 23:24:31 +0000780 Type.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000781 };
Devang Patele4b27562009-08-28 23:24:31 +0000782 return DIVariable(MDNode::get(VMContext, &Elts[0], 6));
Chris Lattnera45664f2008-11-10 02:56:27 +0000783}
784
785
Mike Stump3e4c9bd2009-09-30 00:08:22 +0000786/// CreateComplexVariable - Create a new descriptor for the specified variable
787/// which has a complex address expression for its address.
788DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
789 const std::string &Name,
790 DICompileUnit CompileUnit,
791 unsigned LineNo,
792 DIType Type, SmallVector<Value *, 9> &addr) {
793 SmallVector<Value *, 9> Elts;
794 Elts.push_back(GetTagConstant(Tag));
795 Elts.push_back(Context.getNode());
796 Elts.push_back(MDString::get(VMContext, Name));
797 Elts.push_back(CompileUnit.getNode());
798 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
799 Elts.push_back(Type.getNode());
800 Elts.insert(Elts.end(), addr.begin(), addr.end());
801
802 return DIVariable(MDNode::get(VMContext, &Elts[0], 6+addr.size()));
803}
804
805
Chris Lattnera45664f2008-11-10 02:56:27 +0000806/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +0000807/// specified parent VMContext.
Devang Patel5e005d82009-08-31 22:00:15 +0000808DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context) {
Devang Patele4b27562009-08-28 23:24:31 +0000809 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000810 GetTagConstant(dwarf::DW_TAG_lexical_block),
Devang Patele4b27562009-08-28 23:24:31 +0000811 Context.getNode()
Chris Lattnera45664f2008-11-10 02:56:27 +0000812 };
Devang Patel5e005d82009-08-31 22:00:15 +0000813 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 2));
Chris Lattnera45664f2008-11-10 02:56:27 +0000814}
815
Devang Patelf98d8fe2009-09-01 01:14:15 +0000816/// CreateLocation - Creates a debug info location.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000817DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
Daniel Dunbara279bc32009-09-20 02:20:51 +0000818 DIScope S, DILocation OrigLoc) {
Devang Patelf98d8fe2009-09-01 01:14:15 +0000819 Value *Elts[] = {
820 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
821 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
822 S.getNode(),
823 OrigLoc.getNode(),
824 };
825 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
826}
827
Chris Lattnera45664f2008-11-10 02:56:27 +0000828
829//===----------------------------------------------------------------------===//
830// DIFactory: Routines for inserting code into a function
831//===----------------------------------------------------------------------===//
832
833/// InsertStopPoint - Create a new llvm.dbg.stoppoint intrinsic invocation,
834/// inserting it at the end of the specified basic block.
835void DIFactory::InsertStopPoint(DICompileUnit CU, unsigned LineNo,
836 unsigned ColNo, BasicBlock *BB) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000837
Chris Lattnera45664f2008-11-10 02:56:27 +0000838 // Lazily construct llvm.dbg.stoppoint function.
839 if (!StopPointFn)
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000840 StopPointFn = llvm::Intrinsic::getDeclaration(&M,
Chris Lattnera45664f2008-11-10 02:56:27 +0000841 llvm::Intrinsic::dbg_stoppoint);
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000842
Chris Lattnera45664f2008-11-10 02:56:27 +0000843 // Invoke llvm.dbg.stoppoint
844 Value *Args[] = {
Owen Anderson1d0be152009-08-13 21:58:54 +0000845 ConstantInt::get(llvm::Type::getInt32Ty(VMContext), LineNo),
846 ConstantInt::get(llvm::Type::getInt32Ty(VMContext), ColNo),
Devang Patele4b27562009-08-28 23:24:31 +0000847 CU.getNode()
Chris Lattnera45664f2008-11-10 02:56:27 +0000848 };
849 CallInst::Create(StopPointFn, Args, Args+3, "", BB);
850}
851
852/// InsertSubprogramStart - Create a new llvm.dbg.func.start intrinsic to
853/// mark the start of the specified subprogram.
854void DIFactory::InsertSubprogramStart(DISubprogram SP, BasicBlock *BB) {
855 // Lazily construct llvm.dbg.func.start.
856 if (!FuncStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000857 FuncStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_func_start);
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000858
Chris Lattnera45664f2008-11-10 02:56:27 +0000859 // Call llvm.dbg.func.start which also implicitly sets a stoppoint.
Devang Patele4b27562009-08-28 23:24:31 +0000860 CallInst::Create(FuncStartFn, SP.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000861}
862
863/// InsertRegionStart - Insert a new llvm.dbg.region.start intrinsic call to
864/// mark the start of a region for the specified scoping descriptor.
865void DIFactory::InsertRegionStart(DIDescriptor D, BasicBlock *BB) {
866 // Lazily construct llvm.dbg.region.start function.
867 if (!RegionStartFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000868 RegionStartFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_start);
869
Chris Lattnera45664f2008-11-10 02:56:27 +0000870 // Call llvm.dbg.func.start.
Devang Patele4b27562009-08-28 23:24:31 +0000871 CallInst::Create(RegionStartFn, D.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000872}
873
Chris Lattnera45664f2008-11-10 02:56:27 +0000874/// InsertRegionEnd - Insert a new llvm.dbg.region.end intrinsic call to
875/// mark the end of a region for the specified scoping descriptor.
876void DIFactory::InsertRegionEnd(DIDescriptor D, BasicBlock *BB) {
877 // Lazily construct llvm.dbg.region.end function.
878 if (!RegionEndFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000879 RegionEndFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_region_end);
880
881 // Call llvm.dbg.region.end.
Devang Patele4b27562009-08-28 23:24:31 +0000882 CallInst::Create(RegionEndFn, D.getNode(), "", BB);
Chris Lattnera45664f2008-11-10 02:56:27 +0000883}
884
885/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000886void DIFactory::InsertDeclare(Value *Storage, DIVariable D, BasicBlock *BB) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000887 // Cast the storage to a {}* for the call to llvm.dbg.declare.
Bill Wendlingdc817b62009-05-14 18:26:15 +0000888 Storage = new BitCastInst(Storage, EmptyStructPtr, "", BB);
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000889
Chris Lattnera45664f2008-11-10 02:56:27 +0000890 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +0000891 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
892
Devang Patele4b27562009-08-28 23:24:31 +0000893 Value *Args[] = { Storage, D.getNode() };
Chris Lattnera45664f2008-11-10 02:56:27 +0000894 CallInst::Create(DeclareFn, Args, Args+2, "", BB);
895}
Torok Edwin620f2802008-12-16 09:07:36 +0000896
Devang Patele4b27562009-08-28 23:24:31 +0000897
Devang Pateld2f79a12009-07-28 19:55:13 +0000898//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +0000899// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +0000900//===----------------------------------------------------------------------===//
901
Devang Patel98c65172009-07-30 18:25:15 +0000902/// processModule - Process entire module and collect debug info.
903void DebugInfoFinder::processModule(Module &M) {
Devang Patele4b27562009-08-28 23:24:31 +0000904
905
Devang Pateld2f79a12009-07-28 19:55:13 +0000906 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
907 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
908 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
909 ++BI) {
910 if (DbgStopPointInst *SPI = dyn_cast<DbgStopPointInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000911 processStopPoint(SPI);
Devang Pateld2f79a12009-07-28 19:55:13 +0000912 else if (DbgFuncStartInst *FSI = dyn_cast<DbgFuncStartInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000913 processFuncStart(FSI);
Devang Patele802f1c2009-07-30 17:30:23 +0000914 else if (DbgRegionStartInst *DRS = dyn_cast<DbgRegionStartInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000915 processRegionStart(DRS);
Devang Patele802f1c2009-07-30 17:30:23 +0000916 else if (DbgRegionEndInst *DRE = dyn_cast<DbgRegionEndInst>(BI))
Devang Patel98c65172009-07-30 18:25:15 +0000917 processRegionEnd(DRE);
Devang Patelb4d31302009-07-31 18:18:52 +0000918 else if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
919 processDeclare(DDI);
Devang Pateld2f79a12009-07-28 19:55:13 +0000920 }
Devang Patele4b27562009-08-28 23:24:31 +0000921
922 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
923 if (!NMD)
924 return;
925
926 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
927 DIGlobalVariable DIG(cast<MDNode>(NMD->getElement(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +0000928 if (addGlobalVariable(DIG)) {
929 addCompileUnit(DIG.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +0000930 processType(DIG.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +0000931 }
932 }
933}
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000934
Devang Patel98c65172009-07-30 18:25:15 +0000935/// processType - Process DIType.
936void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +0000937 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +0000938 return;
939
940 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +0000941 if (DT.isCompositeType()) {
Devang Patele4b27562009-08-28 23:24:31 +0000942 DICompositeType DCT(DT.getNode());
Devang Patel98c65172009-07-30 18:25:15 +0000943 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +0000944 DIArray DA = DCT.getTypeArray();
945 if (!DA.isNull())
946 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
947 DIDescriptor D = DA.getElement(i);
Devang Patele4b27562009-08-28 23:24:31 +0000948 DIType TypeE = DIType(D.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +0000949 if (!TypeE.isNull())
Devang Patel98c65172009-07-30 18:25:15 +0000950 processType(TypeE);
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000951 else
Devang Patele4b27562009-08-28 23:24:31 +0000952 processSubprogram(DISubprogram(D.getNode()));
Devang Pateld2f79a12009-07-28 19:55:13 +0000953 }
Devang Patel6ceea332009-08-31 18:49:10 +0000954 } else if (DT.isDerivedType()) {
Devang Patele4b27562009-08-28 23:24:31 +0000955 DIDerivedType DDT(DT.getNode());
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000956 if (!DDT.isNull())
Devang Patel98c65172009-07-30 18:25:15 +0000957 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +0000958 }
959}
960
Devang Patel98c65172009-07-30 18:25:15 +0000961/// processSubprogram - Process DISubprogram.
962void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Patele802f1c2009-07-30 17:30:23 +0000963 if (SP.isNull())
964 return;
Devang Pateld2f79a12009-07-28 19:55:13 +0000965 if (!addSubprogram(SP))
966 return;
967 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +0000968 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +0000969}
970
Devang Patel98c65172009-07-30 18:25:15 +0000971/// processStopPoint - Process DbgStopPointInst.
972void DebugInfoFinder::processStopPoint(DbgStopPointInst *SPI) {
Devang Patele4b27562009-08-28 23:24:31 +0000973 MDNode *Context = dyn_cast<MDNode>(SPI->getContext());
Devang Pateld2f79a12009-07-28 19:55:13 +0000974 addCompileUnit(DICompileUnit(Context));
975}
976
Devang Patel98c65172009-07-30 18:25:15 +0000977/// processFuncStart - Process DbgFuncStartInst.
978void DebugInfoFinder::processFuncStart(DbgFuncStartInst *FSI) {
Devang Patele4b27562009-08-28 23:24:31 +0000979 MDNode *SP = dyn_cast<MDNode>(FSI->getSubprogram());
Devang Patel98c65172009-07-30 18:25:15 +0000980 processSubprogram(DISubprogram(SP));
Devang Pateld2f79a12009-07-28 19:55:13 +0000981}
982
Devang Patel98c65172009-07-30 18:25:15 +0000983/// processRegionStart - Process DbgRegionStart.
984void DebugInfoFinder::processRegionStart(DbgRegionStartInst *DRS) {
Devang Patele4b27562009-08-28 23:24:31 +0000985 MDNode *SP = dyn_cast<MDNode>(DRS->getContext());
Devang Patel98c65172009-07-30 18:25:15 +0000986 processSubprogram(DISubprogram(SP));
Devang Patele802f1c2009-07-30 17:30:23 +0000987}
988
Devang Patel98c65172009-07-30 18:25:15 +0000989/// processRegionEnd - Process DbgRegionEnd.
990void DebugInfoFinder::processRegionEnd(DbgRegionEndInst *DRE) {
Devang Patele4b27562009-08-28 23:24:31 +0000991 MDNode *SP = dyn_cast<MDNode>(DRE->getContext());
Devang Patel98c65172009-07-30 18:25:15 +0000992 processSubprogram(DISubprogram(SP));
Devang Patele802f1c2009-07-30 17:30:23 +0000993}
994
Devang Patelb4d31302009-07-31 18:18:52 +0000995/// processDeclare - Process DbgDeclareInst.
996void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patele4b27562009-08-28 23:24:31 +0000997 DIVariable DV(cast<MDNode>(DDI->getVariable()));
Devang Patelb4d31302009-07-31 18:18:52 +0000998 if (DV.isNull())
999 return;
1000
Devang Patele4b27562009-08-28 23:24:31 +00001001 if (!NodesSeen.insert(DV.getNode()))
Devang Patelb4d31302009-07-31 18:18:52 +00001002 return;
1003
1004 addCompileUnit(DV.getCompileUnit());
1005 processType(DV.getType());
1006}
1007
Devang Patel72bcdb62009-08-10 22:09:58 +00001008/// addType - Add type into Tys.
1009bool DebugInfoFinder::addType(DIType DT) {
1010 if (DT.isNull())
1011 return false;
1012
Devang Patele4b27562009-08-28 23:24:31 +00001013 if (!NodesSeen.insert(DT.getNode()))
Devang Patel72bcdb62009-08-10 22:09:58 +00001014 return false;
1015
Devang Patele4b27562009-08-28 23:24:31 +00001016 TYs.push_back(DT.getNode());
Devang Patel72bcdb62009-08-10 22:09:58 +00001017 return true;
1018}
1019
Devang Pateld2f79a12009-07-28 19:55:13 +00001020/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +00001021bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001022 if (CU.isNull())
1023 return false;
1024
Devang Patele4b27562009-08-28 23:24:31 +00001025 if (!NodesSeen.insert(CU.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001026 return false;
1027
Devang Patele4b27562009-08-28 23:24:31 +00001028 CUs.push_back(CU.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001029 return true;
1030}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001031
Devang Pateld2f79a12009-07-28 19:55:13 +00001032/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +00001033bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001034 if (DIG.isNull())
1035 return false;
1036
Devang Patele4b27562009-08-28 23:24:31 +00001037 if (!NodesSeen.insert(DIG.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001038 return false;
1039
Devang Patele4b27562009-08-28 23:24:31 +00001040 GVs.push_back(DIG.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001041 return true;
1042}
1043
1044// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +00001045bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001046 if (SP.isNull())
1047 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001048
Devang Patele4b27562009-08-28 23:24:31 +00001049 if (!NodesSeen.insert(SP.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001050 return false;
1051
Devang Patele4b27562009-08-28 23:24:31 +00001052 SPs.push_back(SP.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001053 return true;
1054}
1055
Torok Edwin620f2802008-12-16 09:07:36 +00001056namespace llvm {
Bill Wendlingdc817b62009-05-14 18:26:15 +00001057 /// findStopPoint - Find the stoppoint coressponding to this instruction, that
1058 /// is the stoppoint that dominates this instruction.
1059 const DbgStopPointInst *findStopPoint(const Instruction *Inst) {
Torok Edwin620f2802008-12-16 09:07:36 +00001060 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(Inst))
1061 return DSI;
1062
1063 const BasicBlock *BB = Inst->getParent();
1064 BasicBlock::const_iterator I = Inst, B;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001065 while (BB) {
Torok Edwin620f2802008-12-16 09:07:36 +00001066 B = BB->begin();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001067
Torok Edwin620f2802008-12-16 09:07:36 +00001068 // A BB consisting only of a terminator can't have a stoppoint.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001069 while (I != B) {
1070 --I;
1071 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1072 return DSI;
Torok Edwin620f2802008-12-16 09:07:36 +00001073 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001074
1075 // This BB didn't have a stoppoint: if there is only one predecessor, look
1076 // for a stoppoint there. We could use getIDom(), but that would require
1077 // dominator info.
Torok Edwin620f2802008-12-16 09:07:36 +00001078 BB = I->getParent()->getUniquePredecessor();
1079 if (BB)
1080 I = BB->getTerminator();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001081 }
1082
Torok Edwin620f2802008-12-16 09:07:36 +00001083 return 0;
1084 }
1085
Bill Wendlingdc817b62009-05-14 18:26:15 +00001086 /// findBBStopPoint - Find the stoppoint corresponding to first real
1087 /// (non-debug intrinsic) instruction in this Basic Block, and return the
1088 /// stoppoint for it.
1089 const DbgStopPointInst *findBBStopPoint(const BasicBlock *BB) {
1090 for(BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001091 if (const DbgStopPointInst *DSI = dyn_cast<DbgStopPointInst>(I))
1092 return DSI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001093
1094 // Fallback to looking for stoppoint of unique predecessor. Useful if this
1095 // BB contains no stoppoints, but unique predecessor does.
Torok Edwin620f2802008-12-16 09:07:36 +00001096 BB = BB->getUniquePredecessor();
1097 if (BB)
1098 return findStopPoint(BB->getTerminator());
Bill Wendlingdc817b62009-05-14 18:26:15 +00001099
Torok Edwin620f2802008-12-16 09:07:36 +00001100 return 0;
1101 }
1102
Bill Wendlingdc817b62009-05-14 18:26:15 +00001103 Value *findDbgGlobalDeclare(GlobalVariable *V) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001104 const Module *M = V->getParent();
Devang Patele4b27562009-08-28 23:24:31 +00001105 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1106 if (!NMD)
1107 return 0;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001108
Devang Patele4b27562009-08-28 23:24:31 +00001109 for (unsigned i = 0, e = NMD->getNumElements(); i != e; ++i) {
1110 DIGlobalVariable DIG(cast_or_null<MDNode>(NMD->getElement(i)));
1111 if (DIG.isNull())
1112 continue;
1113 if (DIG.getGlobal() == V)
1114 return DIG.getNode();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001115 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001116 return 0;
1117 }
1118
Bill Wendlingdc817b62009-05-14 18:26:15 +00001119 /// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
Torok Edwin620f2802008-12-16 09:07:36 +00001120 /// It looks through pointer casts too.
Bill Wendlingdc817b62009-05-14 18:26:15 +00001121 const DbgDeclareInst *findDbgDeclare(const Value *V, bool stripCasts) {
Torok Edwin620f2802008-12-16 09:07:36 +00001122 if (stripCasts) {
1123 V = V->stripPointerCasts();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001124
Torok Edwin620f2802008-12-16 09:07:36 +00001125 // Look for the bitcast.
1126 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001127 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001128 if (isa<BitCastInst>(I))
1129 return findDbgDeclare(*I, false);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001130
Torok Edwin620f2802008-12-16 09:07:36 +00001131 return 0;
1132 }
1133
Bill Wendlingdc817b62009-05-14 18:26:15 +00001134 // Find llvm.dbg.declare among uses of the instruction.
Torok Edwin620f2802008-12-16 09:07:36 +00001135 for (Value::use_const_iterator I = V->use_begin(), E =V->use_end();
Bill Wendlingdc817b62009-05-14 18:26:15 +00001136 I != E; ++I)
Torok Edwin620f2802008-12-16 09:07:36 +00001137 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(I))
1138 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001139
Torok Edwin620f2802008-12-16 09:07:36 +00001140 return 0;
1141 }
Torok Edwinff7d0e92009-03-10 13:41:26 +00001142
Devang Patel5ccdd102009-09-29 18:40:58 +00001143bool getLocationInfo(const Value *V, std::string &DisplayName,
1144 std::string &Type, unsigned &LineNo, std::string &File,
Bill Wendlingdc817b62009-05-14 18:26:15 +00001145 std::string &Dir) {
Torok Edwinff7d0e92009-03-10 13:41:26 +00001146 DICompileUnit Unit;
1147 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001148
Torok Edwinff7d0e92009-03-10 13:41:26 +00001149 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1150 Value *DIGV = findDbgGlobalDeclare(GV);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001151 if (!DIGV) return false;
Devang Patele4b27562009-08-28 23:24:31 +00001152 DIGlobalVariable Var(cast<MDNode>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001153
Devang Patel5ccdd102009-09-29 18:40:58 +00001154 if (const char *D = Var.getDisplayName())
1155 DisplayName = D;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001156 LineNo = Var.getLineNumber();
1157 Unit = Var.getCompileUnit();
1158 TypeD = Var.getType();
1159 } else {
1160 const DbgDeclareInst *DDI = findDbgDeclare(V);
Bill Wendlingdc817b62009-05-14 18:26:15 +00001161 if (!DDI) return false;
Devang Patele4b27562009-08-28 23:24:31 +00001162 DIVariable Var(cast<MDNode>(DDI->getVariable()));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001163
Devang Patel5ccdd102009-09-29 18:40:58 +00001164 if (const char *D = Var.getName())
1165 DisplayName = D;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001166 LineNo = Var.getLineNumber();
1167 Unit = Var.getCompileUnit();
1168 TypeD = Var.getType();
1169 }
Bill Wendlingdc817b62009-05-14 18:26:15 +00001170
Devang Patel5ccdd102009-09-29 18:40:58 +00001171 if (const char *T = TypeD.getName())
1172 Type = T;
1173 if (const char *F = Unit.getFilename())
1174 File = F;
1175 if (const char *D = Unit.getDirectory())
1176 Dir = D;
Torok Edwinff7d0e92009-03-10 13:41:26 +00001177 return true;
1178 }
Devang Patel13e16b62009-06-26 01:49:18 +00001179
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001180 /// isValidDebugInfoIntrinsic - Return true if SPI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001181 /// info intrinsic.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001182 bool isValidDebugInfoIntrinsic(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +00001183 CodeGenOpt::Level OptLev) {
1184 return DIDescriptor::ValidDebugInfo(SPI.getContext(), OptLev);
1185 }
1186
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001187 /// isValidDebugInfoIntrinsic - Return true if FSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001188 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001189 bool isValidDebugInfoIntrinsic(DbgFuncStartInst &FSI,
1190 CodeGenOpt::Level OptLev) {
1191 return DIDescriptor::ValidDebugInfo(FSI.getSubprogram(), OptLev);
1192 }
1193
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001194 /// isValidDebugInfoIntrinsic - Return true if RSI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001195 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001196 bool isValidDebugInfoIntrinsic(DbgRegionStartInst &RSI,
1197 CodeGenOpt::Level OptLev) {
1198 return DIDescriptor::ValidDebugInfo(RSI.getContext(), OptLev);
1199 }
1200
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001201 /// isValidDebugInfoIntrinsic - Return true if REI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001202 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001203 bool isValidDebugInfoIntrinsic(DbgRegionEndInst &REI,
1204 CodeGenOpt::Level OptLev) {
1205 return DIDescriptor::ValidDebugInfo(REI.getContext(), OptLev);
1206 }
1207
1208
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001209 /// isValidDebugInfoIntrinsic - Return true if DI is a valid debug
Devang Pateldfc85362009-07-02 17:17:03 +00001210 /// info intrinsic.
Devang Patel9e529c32009-07-02 01:15:24 +00001211 bool isValidDebugInfoIntrinsic(DbgDeclareInst &DI,
1212 CodeGenOpt::Level OptLev) {
1213 return DIDescriptor::ValidDebugInfo(DI.getVariable(), OptLev);
1214 }
1215
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001216 /// ExtractDebugLocation - Extract debug location information
Devang Patel9e529c32009-07-02 01:15:24 +00001217 /// from llvm.dbg.stoppoint intrinsic.
1218 DebugLoc ExtractDebugLocation(DbgStopPointInst &SPI,
Devang Patel9e529c32009-07-02 01:15:24 +00001219 DebugLocTracker &DebugLocInfo) {
1220 DebugLoc DL;
1221 Value *Context = SPI.getContext();
Devang Patel9e529c32009-07-02 01:15:24 +00001222
1223 // If this location is already tracked then use it.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001224 DebugLocTuple Tuple(cast<MDNode>(Context), SPI.getLine(),
Devang Patel9e529c32009-07-02 01:15:24 +00001225 SPI.getColumn());
1226 DenseMap<DebugLocTuple, unsigned>::iterator II
1227 = DebugLocInfo.DebugIdMap.find(Tuple);
1228 if (II != DebugLocInfo.DebugIdMap.end())
1229 return DebugLoc::get(II->second);
1230
1231 // Add a new location entry.
1232 unsigned Id = DebugLocInfo.DebugLocations.size();
1233 DebugLocInfo.DebugLocations.push_back(Tuple);
1234 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001235
Devang Patel9e529c32009-07-02 01:15:24 +00001236 return DebugLoc::get(Id);
1237 }
1238
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001239 /// ExtractDebugLocation - Extract debug location information
Devang Patel1b75f442009-09-16 18:20:05 +00001240 /// from DILocation.
1241 DebugLoc ExtractDebugLocation(DILocation &Loc,
1242 DebugLocTracker &DebugLocInfo) {
1243 DebugLoc DL;
1244 MDNode *Context = Loc.getScope().getNode();
1245
1246 // If this location is already tracked then use it.
1247 DebugLocTuple Tuple(Context, Loc.getLineNumber(),
Daniel Dunbara279bc32009-09-20 02:20:51 +00001248 Loc.getColumnNumber());
Devang Patel1b75f442009-09-16 18:20:05 +00001249 DenseMap<DebugLocTuple, unsigned>::iterator II
1250 = DebugLocInfo.DebugIdMap.find(Tuple);
1251 if (II != DebugLocInfo.DebugIdMap.end())
1252 return DebugLoc::get(II->second);
1253
1254 // Add a new location entry.
1255 unsigned Id = DebugLocInfo.DebugLocations.size();
1256 DebugLocInfo.DebugLocations.push_back(Tuple);
1257 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001258
Devang Patel1b75f442009-09-16 18:20:05 +00001259 return DebugLoc::get(Id);
1260 }
1261
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001262 /// ExtractDebugLocation - Extract debug location information
Devang Patel9e529c32009-07-02 01:15:24 +00001263 /// from llvm.dbg.func_start intrinsic.
1264 DebugLoc ExtractDebugLocation(DbgFuncStartInst &FSI,
Devang Patel9e529c32009-07-02 01:15:24 +00001265 DebugLocTracker &DebugLocInfo) {
1266 DebugLoc DL;
1267 Value *SP = FSI.getSubprogram();
Devang Patel9e529c32009-07-02 01:15:24 +00001268
Devang Patele4b27562009-08-28 23:24:31 +00001269 DISubprogram Subprogram(cast<MDNode>(SP));
Devang Patel9e529c32009-07-02 01:15:24 +00001270 unsigned Line = Subprogram.getLineNumber();
1271 DICompileUnit CU(Subprogram.getCompileUnit());
1272
1273 // If this location is already tracked then use it.
Devang Patele4b27562009-08-28 23:24:31 +00001274 DebugLocTuple Tuple(CU.getNode(), Line, /* Column */ 0);
Devang Patel9e529c32009-07-02 01:15:24 +00001275 DenseMap<DebugLocTuple, unsigned>::iterator II
1276 = DebugLocInfo.DebugIdMap.find(Tuple);
1277 if (II != DebugLocInfo.DebugIdMap.end())
1278 return DebugLoc::get(II->second);
1279
1280 // Add a new location entry.
1281 unsigned Id = DebugLocInfo.DebugLocations.size();
1282 DebugLocInfo.DebugLocations.push_back(Tuple);
1283 DebugLocInfo.DebugIdMap[Tuple] = Id;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001284
Devang Patel9e529c32009-07-02 01:15:24 +00001285 return DebugLoc::get(Id);
1286 }
1287
1288 /// isInlinedFnStart - Return true if FSI is starting an inlined function.
1289 bool isInlinedFnStart(DbgFuncStartInst &FSI, const Function *CurrentFn) {
Devang Patele4b27562009-08-28 23:24:31 +00001290 DISubprogram Subprogram(cast<MDNode>(FSI.getSubprogram()));
Devang Patel9e529c32009-07-02 01:15:24 +00001291 if (Subprogram.describes(CurrentFn))
1292 return false;
1293
1294 return true;
1295 }
1296
1297 /// isInlinedFnEnd - Return true if REI is ending an inlined function.
1298 bool isInlinedFnEnd(DbgRegionEndInst &REI, const Function *CurrentFn) {
Devang Patele4b27562009-08-28 23:24:31 +00001299 DISubprogram Subprogram(cast<MDNode>(REI.getContext()));
Devang Patel9e529c32009-07-02 01:15:24 +00001300 if (Subprogram.isNull() || Subprogram.describes(CurrentFn))
1301 return false;
1302
1303 return true;
1304 }
Torok Edwin620f2802008-12-16 09:07:36 +00001305}