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