blob: f3289340ec9b8cb28e4787d884faf769bf8ab2d8 [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"
Chris Lattner784b8502009-12-29 09:15:46 +000016#include "llvm/Target/TargetMachine.h" // FIXME: LAYERING VIOLATION!
Chris Lattnera45664f2008-11-10 02:56:27 +000017#include "llvm/Constants.h"
18#include "llvm/DerivedTypes.h"
19#include "llvm/Intrinsics.h"
Torok Edwin620f2802008-12-16 09:07:36 +000020#include "llvm/IntrinsicInst.h"
Chris Lattnera45664f2008-11-10 02:56:27 +000021#include "llvm/Instructions.h"
22#include "llvm/Module.h"
23#include "llvm/Analysis/ValueTracking.h"
Devang Patele4b27562009-08-28 23:24:31 +000024#include "llvm/ADT/SmallPtrSet.h"
David Greene0eb5b662009-12-23 19:45:49 +000025#include "llvm/Support/Debug.h"
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000026#include "llvm/Support/Dwarf.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()
Chris Lattner784b8502009-12-29 09:15:46 +000037bool DIDescriptor::ValidDebugInfo(MDNode *N, unsigned OptLevel) {
Devang Patele4b27562009-08-28 23:24:31 +000038 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
Devang Patel4b945502010-03-09 00:44:10 +000043 // Check current version. Allow Version7 for now.
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000044 unsigned Version = DI.getVersion();
Devang Patel4b945502010-03-09 00:44:10 +000045 if (Version != LLVMDebugVersion && Version != LLVMDebugVersion7)
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000046 return false;
47
Chris Lattner099b7792009-12-29 09:22:47 +000048 switch (DI.getTag()) {
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000049 case DW_TAG_variable:
Devang Patele4b27562009-08-28 23:24:31 +000050 assert(DIVariable(N).Verify() && "Invalid DebugInfo value");
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000051 break;
52 case DW_TAG_compile_unit:
Devang Patele4b27562009-08-28 23:24:31 +000053 assert(DICompileUnit(N).Verify() && "Invalid DebugInfo value");
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000054 break;
55 case DW_TAG_subprogram:
Devang Patele4b27562009-08-28 23:24:31 +000056 assert(DISubprogram(N).Verify() && "Invalid DebugInfo value");
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000057 break;
58 case DW_TAG_lexical_block:
Bill Wendlingdc817b62009-05-14 18:26:15 +000059 // FIXME: This interfers with the quality of generated code during
60 // optimization.
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000061 if (OptLevel != CodeGenOpt::None)
62 return false;
Bill Wendlingdc817b62009-05-14 18:26:15 +000063 // FALLTHROUGH
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +000064 default:
65 break;
66 }
67
68 return true;
69}
70
Devang Patel65dbc902009-11-25 17:36:49 +000071StringRef
Devang Patel5ccdd102009-09-29 18:40:58 +000072DIDescriptor::getStringField(unsigned Elt) const {
Devang Patele4b27562009-08-28 23:24:31 +000073 if (DbgNode == 0)
Devang Patel65dbc902009-11-25 17:36:49 +000074 return StringRef();
Chris Lattnera45664f2008-11-10 02:56:27 +000075
Chris Lattner5d0cacd2009-12-31 01:22:29 +000076 if (Elt < DbgNode->getNumOperands())
77 if (MDString *MDS = dyn_cast_or_null<MDString>(DbgNode->getOperand(Elt)))
Devang Patel65dbc902009-11-25 17:36:49 +000078 return MDS->getString();
Daniel Dunbarf612ff62009-09-19 20:40:05 +000079
Devang Patel65dbc902009-11-25 17:36:49 +000080 return StringRef();
Chris Lattnera45664f2008-11-10 02:56:27 +000081}
82
83uint64_t DIDescriptor::getUInt64Field(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +000084 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +000085 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +000086
Chris Lattner5d0cacd2009-12-31 01:22:29 +000087 if (Elt < DbgNode->getNumOperands())
88 if (ConstantInt *CI = dyn_cast<ConstantInt>(DbgNode->getOperand(Elt)))
Devang Patele4b27562009-08-28 23:24:31 +000089 return CI->getZExtValue();
Daniel Dunbarf612ff62009-09-19 20:40:05 +000090
Chris Lattnera45664f2008-11-10 02:56:27 +000091 return 0;
92}
93
Chris Lattnera45664f2008-11-10 02:56:27 +000094DIDescriptor DIDescriptor::getDescriptorField(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +000095 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +000096 return DIDescriptor();
Bill Wendlingdc817b62009-05-14 18:26:15 +000097
Chris Lattner7a2f3e02010-03-31 05:53:47 +000098 if (Elt < DbgNode->getNumOperands())
99 return DIDescriptor(dyn_cast_or_null<MDNode>(DbgNode->getOperand(Elt)));
Devang Patele4b27562009-08-28 23:24:31 +0000100 return DIDescriptor();
Chris Lattnera45664f2008-11-10 02:56:27 +0000101}
102
103GlobalVariable *DIDescriptor::getGlobalVariableField(unsigned Elt) const {
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000104 if (DbgNode == 0)
Chris Lattnera45664f2008-11-10 02:56:27 +0000105 return 0;
Bill Wendlingdc817b62009-05-14 18:26:15 +0000106
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000107 if (Elt < DbgNode->getNumOperands())
108 return dyn_cast_or_null<GlobalVariable>(DbgNode->getOperand(Elt));
Devang Patele4b27562009-08-28 23:24:31 +0000109 return 0;
Chris Lattnera45664f2008-11-10 02:56:27 +0000110}
111
Chris Lattnerf0908a32009-12-31 03:02:08 +0000112unsigned DIVariable::getNumAddrElements() const {
113 return DbgNode->getNumOperands()-6;
114}
115
116
Chris Lattnera45664f2008-11-10 02:56:27 +0000117//===----------------------------------------------------------------------===//
Devang Patel6ceea332009-08-31 18:49:10 +0000118// Predicates
Chris Lattnera45664f2008-11-10 02:56:27 +0000119//===----------------------------------------------------------------------===//
120
Devang Patel6ceea332009-08-31 18:49:10 +0000121/// isBasicType - Return true if the specified tag is legal for
122/// DIBasicType.
123bool DIDescriptor::isBasicType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000124 return DbgNode && getTag() == dwarf::DW_TAG_base_type;
Torok Edwinb07fbd92008-12-13 08:25:29 +0000125}
Chris Lattnera45664f2008-11-10 02:56:27 +0000126
Devang Patel6ceea332009-08-31 18:49:10 +0000127/// isDerivedType - Return true if the specified tag is legal for DIDerivedType.
128bool DIDescriptor::isDerivedType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000129 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000130 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000131 case dwarf::DW_TAG_typedef:
132 case dwarf::DW_TAG_pointer_type:
133 case dwarf::DW_TAG_reference_type:
134 case dwarf::DW_TAG_const_type:
135 case dwarf::DW_TAG_volatile_type:
136 case dwarf::DW_TAG_restrict_type:
137 case dwarf::DW_TAG_member:
138 case dwarf::DW_TAG_inheritance:
139 return true;
140 default:
Devang Patele4b27562009-08-28 23:24:31 +0000141 // CompositeTypes are currently modelled as DerivedTypes.
Devang Patel6ceea332009-08-31 18:49:10 +0000142 return isCompositeType();
Chris Lattnera45664f2008-11-10 02:56:27 +0000143 }
144}
145
Chris Lattnera45664f2008-11-10 02:56:27 +0000146/// isCompositeType - Return true if the specified tag is legal for
147/// DICompositeType.
Devang Patel6ceea332009-08-31 18:49:10 +0000148bool DIDescriptor::isCompositeType() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000149 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000150 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000151 case dwarf::DW_TAG_array_type:
152 case dwarf::DW_TAG_structure_type:
153 case dwarf::DW_TAG_union_type:
154 case dwarf::DW_TAG_enumeration_type:
155 case dwarf::DW_TAG_vector_type:
156 case dwarf::DW_TAG_subroutine_type:
Devang Patel25cb0d72009-03-25 03:52:06 +0000157 case dwarf::DW_TAG_class_type:
Chris Lattnera45664f2008-11-10 02:56:27 +0000158 return true;
159 default:
160 return false;
161 }
162}
163
Chris Lattnera45664f2008-11-10 02:56:27 +0000164/// isVariable - Return true if the specified tag is legal for DIVariable.
Devang Patel6ceea332009-08-31 18:49:10 +0000165bool DIDescriptor::isVariable() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000166 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000167 switch (getTag()) {
Chris Lattnera45664f2008-11-10 02:56:27 +0000168 case dwarf::DW_TAG_auto_variable:
169 case dwarf::DW_TAG_arg_variable:
170 case dwarf::DW_TAG_return_variable:
171 return true;
172 default:
173 return false;
174 }
175}
176
Devang Patelecbeb1a2009-09-30 22:34:41 +0000177/// isType - Return true if the specified tag is legal for DIType.
178bool DIDescriptor::isType() const {
179 return isBasicType() || isCompositeType() || isDerivedType();
180}
181
Devang Patel6ceea332009-08-31 18:49:10 +0000182/// isSubprogram - Return true if the specified tag is legal for
183/// DISubprogram.
184bool DIDescriptor::isSubprogram() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000185 return DbgNode && getTag() == dwarf::DW_TAG_subprogram;
Devang Patel6ceea332009-08-31 18:49:10 +0000186}
187
188/// isGlobalVariable - Return true if the specified tag is legal for
189/// DIGlobalVariable.
190bool DIDescriptor::isGlobalVariable() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000191 return DbgNode && getTag() == dwarf::DW_TAG_variable;
Devang Patel6ceea332009-08-31 18:49:10 +0000192}
193
Devang Patelecbeb1a2009-09-30 22:34:41 +0000194/// isGlobal - Return true if the specified tag is legal for DIGlobal.
195bool DIDescriptor::isGlobal() const {
196 return isGlobalVariable();
197}
198
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000199/// isScope - Return true if the specified tag is one of the scope
Devang Patel43d98b32009-08-31 20:44:45 +0000200/// related tag.
201bool DIDescriptor::isScope() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000202 if (!DbgNode) return false;
Chris Lattner099b7792009-12-29 09:22:47 +0000203 switch (getTag()) {
204 case dwarf::DW_TAG_compile_unit:
205 case dwarf::DW_TAG_lexical_block:
206 case dwarf::DW_TAG_subprogram:
207 case dwarf::DW_TAG_namespace:
208 return true;
209 default:
210 break;
Devang Patel43d98b32009-08-31 20:44:45 +0000211 }
212 return false;
213}
Devang Patel6ceea332009-08-31 18:49:10 +0000214
Devang Patelc9f322d2009-08-31 21:34:44 +0000215/// isCompileUnit - Return true if the specified tag is DW_TAG_compile_unit.
216bool DIDescriptor::isCompileUnit() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000217 return DbgNode && getTag() == dwarf::DW_TAG_compile_unit;
Devang Patelc9f322d2009-08-31 21:34:44 +0000218}
219
Devang Patel7aa81892010-03-08 22:27:22 +0000220/// isFile - Return true if the specified tag is DW_TAG_file_type.
221bool DIDescriptor::isFile() const {
222 return DbgNode && getTag() == dwarf::DW_TAG_file_type;
223}
224
Devang Patel6404e4e2009-12-15 19:16:48 +0000225/// isNameSpace - Return true if the specified tag is DW_TAG_namespace.
226bool DIDescriptor::isNameSpace() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000227 return DbgNode && getTag() == dwarf::DW_TAG_namespace;
Devang Patel6404e4e2009-12-15 19:16:48 +0000228}
229
Devang Patel5e005d82009-08-31 22:00:15 +0000230/// isLexicalBlock - Return true if the specified tag is DW_TAG_lexical_block.
231bool DIDescriptor::isLexicalBlock() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000232 return DbgNode && getTag() == dwarf::DW_TAG_lexical_block;
Devang Patel5e005d82009-08-31 22:00:15 +0000233}
234
Devang Patelecbeb1a2009-09-30 22:34:41 +0000235/// isSubrange - Return true if the specified tag is DW_TAG_subrange_type.
236bool DIDescriptor::isSubrange() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000237 return DbgNode && getTag() == dwarf::DW_TAG_subrange_type;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000238}
239
240/// isEnumerator - Return true if the specified tag is DW_TAG_enumerator.
241bool DIDescriptor::isEnumerator() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000242 return DbgNode && getTag() == dwarf::DW_TAG_enumerator;
Devang Patelecbeb1a2009-09-30 22:34:41 +0000243}
244
Devang Patel6ceea332009-08-31 18:49:10 +0000245//===----------------------------------------------------------------------===//
246// Simple Descriptor Constructors and other Methods
247//===----------------------------------------------------------------------===//
248
Devang Patel77bf2952010-03-08 22:02:50 +0000249DIType::DIType(MDNode *N) : DIScope(N) {
Devang Patel6ceea332009-08-31 18:49:10 +0000250 if (!N) return;
251 if (!isBasicType() && !isDerivedType() && !isCompositeType()) {
252 DbgNode = 0;
253 }
254}
255
Devang Patel68afdc32009-01-05 18:33:01 +0000256unsigned DIArray::getNumElements() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000257 if (!DbgNode)
258 return 0;
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000259 return DbgNode->getNumOperands();
Devang Patel68afdc32009-01-05 18:33:01 +0000260}
Chris Lattnera45664f2008-11-10 02:56:27 +0000261
Devang Patelc4999d72009-07-22 18:23:44 +0000262/// replaceAllUsesWith - Replace all uses of debug info referenced by
263/// this descriptor. After this completes, the current debug info value
264/// is erased.
265void DIDerivedType::replaceAllUsesWith(DIDescriptor &D) {
Devang Patel3c91b052010-03-08 20:52:55 +0000266 if (!DbgNode)
Devang Patelc4999d72009-07-22 18:23:44 +0000267 return;
268
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000269 // Since we use a TrackingVH for the node, its easy for clients to manufacture
270 // legitimate situations where they want to replaceAllUsesWith() on something
271 // which, due to uniquing, has merged with the source. We shield clients from
272 // this detail by allowing a value to be replaced with replaceAllUsesWith()
273 // itself.
274 if (getNode() != D.getNode()) {
275 MDNode *Node = DbgNode;
276 Node->replaceAllUsesWith(D.getNode());
Chris Lattnerb76359e2009-12-31 01:05:46 +0000277 Node->destroy();
Daniel Dunbar48a097b2009-09-22 02:03:18 +0000278 }
Devang Patelc4999d72009-07-22 18:23:44 +0000279}
280
Devang Patelb79b5352009-01-19 23:21:49 +0000281/// Verify - Verify that a compile unit is well formed.
282bool DICompileUnit::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000283 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000284 return false;
Devang Patel65dbc902009-11-25 17:36:49 +0000285 StringRef N = getFilename();
286 if (N.empty())
Bill Wendling0582ae92009-03-13 04:39:26 +0000287 return false;
Devang Patelb79b5352009-01-19 23:21:49 +0000288 // It is possible that directory and produce string is empty.
Bill Wendling0582ae92009-03-13 04:39:26 +0000289 return true;
Devang Patelb79b5352009-01-19 23:21:49 +0000290}
291
292/// Verify - Verify that a type descriptor is well formed.
293bool DIType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000294 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000295 return false;
Devang Patel3c91b052010-03-08 20:52:55 +0000296 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000297 return false;
298
299 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000300 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000301 return false;
302 return true;
303}
304
305/// Verify - Verify that a composite type descriptor is well formed.
306bool DICompositeType::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000307 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000308 return false;
Devang Patel3c91b052010-03-08 20:52:55 +0000309 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000310 return false;
311
312 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000313 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000314 return false;
315 return true;
316}
317
318/// Verify - Verify that a subprogram descriptor is well formed.
319bool DISubprogram::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000320 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000321 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000322
Devang Patel3c91b052010-03-08 20:52:55 +0000323 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000324 return false;
325
326 DICompileUnit CU = getCompileUnit();
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000327 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000328 return false;
329
330 DICompositeType Ty = getType();
Devang Patel3c91b052010-03-08 20:52:55 +0000331 if (!Ty.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000332 return false;
333 return true;
334}
335
336/// Verify - Verify that a global variable descriptor is well formed.
337bool DIGlobalVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000338 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000339 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000340
Devang Patel65dbc902009-11-25 17:36:49 +0000341 if (getDisplayName().empty())
Devang Patel84c73e92009-11-06 17:58:12 +0000342 return false;
343
Devang Patel3c91b052010-03-08 20:52:55 +0000344 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000345 return false;
346
347 DICompileUnit CU = getCompileUnit();
Devang Patel3c91b052010-03-08 20:52:55 +0000348 if (!CU.Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000349 return false;
350
351 DIType Ty = getType();
352 if (!Ty.Verify())
353 return false;
354
355 if (!getGlobal())
356 return false;
357
358 return true;
359}
360
361/// Verify - Verify that a variable descriptor is well formed.
362bool DIVariable::Verify() const {
Devang Patel3c91b052010-03-08 20:52:55 +0000363 if (!DbgNode)
Devang Patelb79b5352009-01-19 23:21:49 +0000364 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000365
Devang Patel3c91b052010-03-08 20:52:55 +0000366 if (!getContext().Verify())
Devang Patelb79b5352009-01-19 23:21:49 +0000367 return false;
368
369 DIType Ty = getType();
370 if (!Ty.Verify())
371 return false;
372
Devang Patelb79b5352009-01-19 23:21:49 +0000373 return true;
374}
375
Devang Patel3c91b052010-03-08 20:52:55 +0000376/// Verify - Verify that a location descriptor is well formed.
377bool DILocation::Verify() const {
378 if (!DbgNode)
379 return false;
380
381 return DbgNode->getNumOperands() == 4;
382}
383
Devang Patel36375ee2009-02-17 21:23:59 +0000384/// getOriginalTypeSize - If this type is derived from a base type then
385/// return base type size.
386uint64_t DIDerivedType::getOriginalTypeSize() const {
Devang Patel61ecbd12009-11-04 23:48:00 +0000387 unsigned Tag = getTag();
388 if (Tag == dwarf::DW_TAG_member || Tag == dwarf::DW_TAG_typedef ||
389 Tag == dwarf::DW_TAG_const_type || Tag == dwarf::DW_TAG_volatile_type ||
390 Tag == dwarf::DW_TAG_restrict_type) {
391 DIType BaseType = getTypeDerivedFrom();
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000392 // If this type is not derived from any type then take conservative
393 // approach.
Devang Patel3c91b052010-03-08 20:52:55 +0000394 if (!BaseType.isValid())
Devang Patel5ebfa2d2009-11-06 18:24:05 +0000395 return getSizeInBits();
Devang Patel61ecbd12009-11-04 23:48:00 +0000396 if (BaseType.isDerivedType())
397 return DIDerivedType(BaseType.getNode()).getOriginalTypeSize();
398 else
399 return BaseType.getSizeInBits();
400 }
401
402 return getSizeInBits();
Devang Patel36375ee2009-02-17 21:23:59 +0000403}
Devang Patelb79b5352009-01-19 23:21:49 +0000404
Devang Patel719f6a92010-04-29 20:40:36 +0000405/// isInlinedFnArgument - Return trule if this variable provides debugging
406/// information for an inlined function arguments.
407bool DIVariable::isInlinedFnArgument(const Function *CurFn) {
408 assert(CurFn && "Invalid function");
409 if (!getContext().isSubprogram())
410 return false;
411 // This variable is not inlined function argument if its scope
412 // does not describe current function.
413 return !(DISubprogram(getContext().getNode()).describes(CurFn));
414}
415
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000416/// describes - Return true if this subprogram provides debugging
417/// information for the function F.
418bool DISubprogram::describes(const Function *F) {
Chris Lattner099b7792009-12-29 09:22:47 +0000419 assert(F && "Invalid function");
Devang Patel65dbc902009-11-25 17:36:49 +0000420 StringRef Name = getLinkageName();
421 if (Name.empty())
Devang Patel5ccdd102009-09-29 18:40:58 +0000422 Name = getName();
Devang Patel65dbc902009-11-25 17:36:49 +0000423 if (F->getName() == Name)
Devang Patelaf5b6bb2009-04-15 00:06:07 +0000424 return true;
425 return false;
426}
427
Devang Patelccff8122010-04-30 19:38:23 +0000428unsigned DISubprogram::isOptimized() const {
429 assert (DbgNode && "Invalid subprogram descriptor!");
430 if (DbgNode->getNumOperands() == 16)
431 return getUnsignedField(15);
432 return 0;
433}
434
Devang Patel65dbc902009-11-25 17:36:49 +0000435StringRef DIScope::getFilename() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000436 if (!DbgNode)
437 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000438 if (isLexicalBlock())
439 return DILexicalBlock(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000440 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000441 return DISubprogram(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000442 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000443 return DICompileUnit(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000444 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000445 return DINameSpace(DbgNode).getFilename();
Devang Patel77bf2952010-03-08 22:02:50 +0000446 if (isType())
447 return DIType(DbgNode).getFilename();
Devang Patel7aa81892010-03-08 22:27:22 +0000448 if (isFile())
449 return DIFile(DbgNode).getFilename();
Chris Lattner099b7792009-12-29 09:22:47 +0000450 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000451 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000452}
453
Devang Patel65dbc902009-11-25 17:36:49 +0000454StringRef DIScope::getDirectory() const {
Devang Patel77bf2952010-03-08 22:02:50 +0000455 if (!DbgNode)
456 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000457 if (isLexicalBlock())
458 return DILexicalBlock(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000459 if (isSubprogram())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000460 return DISubprogram(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000461 if (isCompileUnit())
Devang Patelecbeb1a2009-09-30 22:34:41 +0000462 return DICompileUnit(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000463 if (isNameSpace())
Devang Patel6404e4e2009-12-15 19:16:48 +0000464 return DINameSpace(DbgNode).getDirectory();
Devang Patel77bf2952010-03-08 22:02:50 +0000465 if (isType())
466 return DIType(DbgNode).getDirectory();
Devang Patel7aa81892010-03-08 22:27:22 +0000467 if (isFile())
468 return DIFile(DbgNode).getDirectory();
Chris Lattner099b7792009-12-29 09:22:47 +0000469 assert(0 && "Invalid DIScope!");
Devang Patel65dbc902009-11-25 17:36:49 +0000470 return StringRef();
Devang Patelecbeb1a2009-09-30 22:34:41 +0000471}
472
Chris Lattnera45664f2008-11-10 02:56:27 +0000473//===----------------------------------------------------------------------===//
Devang Patel7136a652009-07-01 22:10:23 +0000474// DIDescriptor: dump routines for all descriptors.
475//===----------------------------------------------------------------------===//
476
477
Dan Gohman50404362010-05-07 15:30:29 +0000478/// print - Print descriptor.
479void DIDescriptor::print(raw_ostream &OS) const {
480 OS << "[" << dwarf::TagString(getTag()) << "] ";
481 OS.write_hex((intptr_t) &*DbgNode) << ']';
Devang Patel7136a652009-07-01 22:10:23 +0000482}
483
Dan Gohman50404362010-05-07 15:30:29 +0000484/// print - Print compile unit.
485void DICompileUnit::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000486 if (getLanguage())
Dan Gohman50404362010-05-07 15:30:29 +0000487 OS << " [" << dwarf::LanguageString(getLanguage()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000488
Dan Gohman50404362010-05-07 15:30:29 +0000489 OS << " [" << getDirectory() << "/" << getFilename() << " ]";
Devang Patel7136a652009-07-01 22:10:23 +0000490}
491
Dan Gohman50404362010-05-07 15:30:29 +0000492/// print - Print type.
493void DIType::print(raw_ostream &OS) const {
Devang Patel3c91b052010-03-08 20:52:55 +0000494 if (!DbgNode) return;
Devang Patel7136a652009-07-01 22:10:23 +0000495
Devang Patel65dbc902009-11-25 17:36:49 +0000496 StringRef Res = getName();
497 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000498 OS << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000499
500 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000501 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000502
503 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000504 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000505 OS << " ["
Dan Gohman9a7063e2010-05-07 16:39:27 +0000506 << "line " << getLineNumber() << ", "
507 << getSizeInBits() << " bits, "
508 << getAlignInBits() << " bit alignment, "
509 << getOffsetInBits() << " bit offset"
Chris Lattnera81d29b2009-08-23 07:33:14 +0000510 << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000511
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000512 if (isPrivate())
Dan Gohman50404362010-05-07 15:30:29 +0000513 OS << " [private] ";
Devang Patel7136a652009-07-01 22:10:23 +0000514 else if (isProtected())
Dan Gohman50404362010-05-07 15:30:29 +0000515 OS << " [protected] ";
Devang Patel7136a652009-07-01 22:10:23 +0000516
517 if (isForwardDecl())
Dan Gohman50404362010-05-07 15:30:29 +0000518 OS << " [fwd] ";
Devang Patel7136a652009-07-01 22:10:23 +0000519
Devang Patel6ceea332009-08-31 18:49:10 +0000520 if (isBasicType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000521 DIBasicType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000522 else if (isDerivedType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000523 DIDerivedType(DbgNode).print(OS);
Devang Patel6ceea332009-08-31 18:49:10 +0000524 else if (isCompositeType())
Dan Gohmanc014d092010-05-07 16:17:22 +0000525 DICompositeType(DbgNode).print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000526 else {
Dan Gohman50404362010-05-07 15:30:29 +0000527 OS << "Invalid DIType\n";
Devang Patel7136a652009-07-01 22:10:23 +0000528 return;
529 }
530
Dan Gohman50404362010-05-07 15:30:29 +0000531 OS << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000532}
533
Dan Gohman50404362010-05-07 15:30:29 +0000534/// print - Print basic type.
535void DIBasicType::print(raw_ostream &OS) const {
536 OS << " [" << dwarf::AttributeEncodingString(getEncoding()) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000537}
538
Dan Gohman50404362010-05-07 15:30:29 +0000539/// print - Print derived type.
540void DIDerivedType::print(raw_ostream &OS) const {
Dan Gohmanc014d092010-05-07 16:17:22 +0000541 OS << "\n\t Derived From: "; getTypeDerivedFrom().print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000542}
543
Dan Gohman50404362010-05-07 15:30:29 +0000544/// print - Print composite type.
545void DICompositeType::print(raw_ostream &OS) const {
Devang Patel7136a652009-07-01 22:10:23 +0000546 DIArray A = getTypeArray();
Dan Gohman50404362010-05-07 15:30:29 +0000547 OS << " [" << A.getNumElements() << " elements]";
Devang Patel7136a652009-07-01 22:10:23 +0000548}
549
Dan Gohman50404362010-05-07 15:30:29 +0000550/// print - Print global.
551void DIGlobal::print(raw_ostream &OS) const {
Devang Patel65dbc902009-11-25 17:36:49 +0000552 StringRef Res = getName();
553 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000554 OS << " [" << Res << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000555
556 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000557 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000558
559 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000560 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000561 OS << " [" << getLineNumber() << "] ";
Devang Patel7136a652009-07-01 22:10:23 +0000562
563 if (isLocalToUnit())
Dan Gohman50404362010-05-07 15:30:29 +0000564 OS << " [local] ";
Devang Patel7136a652009-07-01 22:10:23 +0000565
566 if (isDefinition())
Dan Gohman50404362010-05-07 15:30:29 +0000567 OS << " [def] ";
Devang Patel7136a652009-07-01 22:10:23 +0000568
Devang Patel6ceea332009-08-31 18:49:10 +0000569 if (isGlobalVariable())
Dan Gohmanc014d092010-05-07 16:17:22 +0000570 DIGlobalVariable(DbgNode).print(OS);
Devang Patel7136a652009-07-01 22:10:23 +0000571
Dan Gohman50404362010-05-07 15:30:29 +0000572 OS << "\n";
Devang Patel7136a652009-07-01 22:10:23 +0000573}
574
Dan Gohman50404362010-05-07 15:30:29 +0000575/// print - Print subprogram.
576void DISubprogram::print(raw_ostream &OS) const {
Devang Patel65dbc902009-11-25 17:36:49 +0000577 StringRef Res = getName();
578 if (!Res.empty())
Dan Gohman50404362010-05-07 15:30:29 +0000579 OS << " [" << Res << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000580
581 unsigned Tag = getTag();
Dan Gohman50404362010-05-07 15:30:29 +0000582 OS << " [" << dwarf::TagString(Tag) << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000583
584 // TODO : Print context
Dan Gohmanc014d092010-05-07 16:17:22 +0000585 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000586 OS << " [" << getLineNumber() << "] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000587
588 if (isLocalToUnit())
Dan Gohman50404362010-05-07 15:30:29 +0000589 OS << " [local] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000590
591 if (isDefinition())
Dan Gohman50404362010-05-07 15:30:29 +0000592 OS << " [def] ";
Devang Patel82dfc0c2009-08-31 22:47:13 +0000593
Dan Gohman50404362010-05-07 15:30:29 +0000594 OS << "\n";
595}
596
597/// print - Print global variable.
598void DIGlobalVariable::print(raw_ostream &OS) const {
599 OS << " [";
Dan Gohmanc014d092010-05-07 16:17:22 +0000600 getGlobal()->print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000601 OS << "] ";
602}
603
604/// print - Print variable.
605void DIVariable::print(raw_ostream &OS) const {
606 StringRef Res = getName();
607 if (!Res.empty())
608 OS << " [" << Res << "] ";
609
Dan Gohmanc014d092010-05-07 16:17:22 +0000610 getCompileUnit().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000611 OS << " [" << getLineNumber() << "] ";
Dan Gohmanc014d092010-05-07 16:17:22 +0000612 getType().print(OS);
Dan Gohman50404362010-05-07 15:30:29 +0000613 OS << "\n";
614
615 // FIXME: Dump complex addresses
616}
617
618/// dump - Print descriptor to dbgs() with a newline.
619void DIDescriptor::dump() const {
620 print(dbgs()); dbgs() << '\n';
621}
622
623/// dump - Print compile unit to dbgs() with a newline.
624void DICompileUnit::dump() const {
625 print(dbgs()); dbgs() << '\n';
626}
627
628/// dump - Print type to dbgs() with a newline.
629void DIType::dump() const {
630 print(dbgs()); dbgs() << '\n';
631}
632
633/// dump - Print basic type to dbgs() with a newline.
634void DIBasicType::dump() const {
635 print(dbgs()); dbgs() << '\n';
636}
637
638/// dump - Print derived type to dbgs() with a newline.
639void DIDerivedType::dump() const {
640 print(dbgs()); dbgs() << '\n';
641}
642
643/// dump - Print composite type to dbgs() with a newline.
644void DICompositeType::dump() const {
645 print(dbgs()); dbgs() << '\n';
646}
647
648/// dump - Print global to dbgs() with a newline.
649void DIGlobal::dump() const {
650 print(dbgs()); dbgs() << '\n';
651}
652
653/// dump - Print subprogram to dbgs() with a newline.
654void DISubprogram::dump() const {
655 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000656}
657
658/// dump - Print global variable.
659void DIGlobalVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000660 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000661}
662
663/// dump - Print variable.
664void DIVariable::dump() const {
Dan Gohman50404362010-05-07 15:30:29 +0000665 print(dbgs()); dbgs() << '\n';
Devang Patel7136a652009-07-01 22:10:23 +0000666}
667
668//===----------------------------------------------------------------------===//
Chris Lattnera45664f2008-11-10 02:56:27 +0000669// DIFactory: Basic Helpers
670//===----------------------------------------------------------------------===//
671
Bill Wendlingdc817b62009-05-14 18:26:15 +0000672DIFactory::DIFactory(Module &m)
Victor Hernandez06203122010-01-23 00:03:28 +0000673 : M(m), VMContext(M.getContext()), DeclareFn(0), ValueFn(0) {}
Chris Lattner497a7a82008-11-10 04:10:34 +0000674
Chris Lattnera45664f2008-11-10 02:56:27 +0000675Constant *DIFactory::GetTagConstant(unsigned TAG) {
Devang Patel6906ba52009-01-20 19:22:03 +0000676 assert((TAG & LLVMDebugVersionMask) == 0 &&
Chris Lattnera45664f2008-11-10 02:56:27 +0000677 "Tag too large for debug encoding!");
Owen Anderson1d0be152009-08-13 21:58:54 +0000678 return ConstantInt::get(Type::getInt32Ty(VMContext), TAG | LLVMDebugVersion);
Chris Lattnera45664f2008-11-10 02:56:27 +0000679}
680
Chris Lattnera45664f2008-11-10 02:56:27 +0000681//===----------------------------------------------------------------------===//
682// DIFactory: Primary Constructors
683//===----------------------------------------------------------------------===//
684
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000685/// GetOrCreateArray - Create an descriptor for an array of descriptors.
Chris Lattnera45664f2008-11-10 02:56:27 +0000686/// This implicitly uniques the arrays created.
687DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) {
Devang Patele4b27562009-08-28 23:24:31 +0000688 SmallVector<Value*, 16> Elts;
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000689
Devang Patele4b27562009-08-28 23:24:31 +0000690 if (NumTys == 0)
691 Elts.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)));
692 else
693 for (unsigned i = 0; i != NumTys; ++i)
694 Elts.push_back(Tys[i].getNode());
Devang Patel82459882009-08-26 05:01:18 +0000695
Devang Patele4b27562009-08-28 23:24:31 +0000696 return DIArray(MDNode::get(VMContext,Elts.data(), Elts.size()));
Chris Lattnera45664f2008-11-10 02:56:27 +0000697}
698
699/// GetOrCreateSubrange - Create a descriptor for a value range. This
700/// implicitly uniques the values returned.
701DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
Devang Patele4b27562009-08-28 23:24:31 +0000702 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000703 GetTagConstant(dwarf::DW_TAG_subrange_type),
Owen Anderson1d0be152009-08-13 21:58:54 +0000704 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
705 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
Chris Lattnera45664f2008-11-10 02:56:27 +0000706 };
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000707
Devang Patele4b27562009-08-28 23:24:31 +0000708 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000709}
710
711
712
713/// CreateCompileUnit - Create a new descriptor for the specified compile
714/// unit. Note that this does not unique compile units within the module.
715DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
Devang Patel65dbc902009-11-25 17:36:49 +0000716 StringRef Filename,
717 StringRef Directory,
718 StringRef Producer,
Devang Pateldd9db662009-01-30 18:20:31 +0000719 bool isMain,
Devang Patel3b64c6b2009-01-23 22:33:47 +0000720 bool isOptimized,
Devang Patel65dbc902009-11-25 17:36:49 +0000721 StringRef Flags,
Devang Patel13319ce2009-02-17 22:43:44 +0000722 unsigned RunTimeVer) {
Devang Patele4b27562009-08-28 23:24:31 +0000723 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000724 GetTagConstant(dwarf::DW_TAG_compile_unit),
Devang Patele4b27562009-08-28 23:24:31 +0000725 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Owen Anderson1d0be152009-08-13 21:58:54 +0000726 ConstantInt::get(Type::getInt32Ty(VMContext), LangID),
Devang Patele4b27562009-08-28 23:24:31 +0000727 MDString::get(VMContext, Filename),
728 MDString::get(VMContext, Directory),
729 MDString::get(VMContext, Producer),
Owen Anderson1d0be152009-08-13 21:58:54 +0000730 ConstantInt::get(Type::getInt1Ty(VMContext), isMain),
731 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
Devang Patele4b27562009-08-28 23:24:31 +0000732 MDString::get(VMContext, Flags),
Owen Anderson1d0be152009-08-13 21:58:54 +0000733 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
Chris Lattnera45664f2008-11-10 02:56:27 +0000734 };
Devang Patele4b27562009-08-28 23:24:31 +0000735
736 return DICompileUnit(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000737}
738
Devang Patel7aa81892010-03-08 22:27:22 +0000739/// CreateFile - Create a new descriptor for the specified file.
740DIFile DIFactory::CreateFile(StringRef Filename,
741 StringRef Directory,
742 DICompileUnit CU) {
743 Value *Elts[] = {
744 GetTagConstant(dwarf::DW_TAG_file_type),
745 MDString::get(VMContext, Filename),
746 MDString::get(VMContext, Directory),
747 CU.getNode()
748 };
749
750 return DIFile(MDNode::get(VMContext, &Elts[0], 4));
751}
752
Chris Lattnera45664f2008-11-10 02:56:27 +0000753/// CreateEnumerator - Create a single enumerator value.
Devang Patel65dbc902009-11-25 17:36:49 +0000754DIEnumerator DIFactory::CreateEnumerator(StringRef Name, uint64_t Val){
Devang Patele4b27562009-08-28 23:24:31 +0000755 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000756 GetTagConstant(dwarf::DW_TAG_enumerator),
Devang Patele4b27562009-08-28 23:24:31 +0000757 MDString::get(VMContext, Name),
Owen Anderson1d0be152009-08-13 21:58:54 +0000758 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
Chris Lattnera45664f2008-11-10 02:56:27 +0000759 };
Devang Patele4b27562009-08-28 23:24:31 +0000760 return DIEnumerator(MDNode::get(VMContext, &Elts[0], 3));
Chris Lattnera45664f2008-11-10 02:56:27 +0000761}
762
763
764/// CreateBasicType - Create a basic type like int, float, etc.
765DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000766 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000767 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000768 unsigned LineNumber,
769 uint64_t SizeInBits,
770 uint64_t AlignInBits,
771 uint64_t OffsetInBits, unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000772 unsigned Encoding) {
Devang Patele4b27562009-08-28 23:24:31 +0000773 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000774 GetTagConstant(dwarf::DW_TAG_base_type),
Devang Patele4b27562009-08-28 23:24:31 +0000775 Context.getNode(),
776 MDString::get(VMContext, Name),
Devang Patel4b945502010-03-09 00:44:10 +0000777 F.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000778 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
779 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
780 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
781 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
782 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
783 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
Chris Lattnera45664f2008-11-10 02:56:27 +0000784 };
Devang Patele4b27562009-08-28 23:24:31 +0000785 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000786}
787
Devang Patelac16d442009-10-26 16:54:35 +0000788
789/// CreateBasicType - Create a basic type like int, float, etc.
790DIBasicType DIFactory::CreateBasicTypeEx(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000791 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000792 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000793 unsigned LineNumber,
794 Constant *SizeInBits,
795 Constant *AlignInBits,
796 Constant *OffsetInBits, unsigned Flags,
797 unsigned Encoding) {
798 Value *Elts[] = {
799 GetTagConstant(dwarf::DW_TAG_base_type),
800 Context.getNode(),
801 MDString::get(VMContext, Name),
Devang Patel4b945502010-03-09 00:44:10 +0000802 F.getNode(),
Devang Patelac16d442009-10-26 16:54:35 +0000803 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
804 SizeInBits,
805 AlignInBits,
806 OffsetInBits,
807 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
808 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
809 };
810 return DIBasicType(MDNode::get(VMContext, &Elts[0], 10));
811}
812
Devang Patelb4645642010-02-06 01:02:37 +0000813/// CreateArtificialType - Create a new DIType with "artificial" flag set.
814DIType DIFactory::CreateArtificialType(DIType Ty) {
815 if (Ty.isArtificial())
816 return Ty;
817
818 SmallVector<Value *, 9> Elts;
819 MDNode *N = Ty.getNode();
820 assert (N && "Unexpected input DIType!");
821 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
822 if (Value *V = N->getOperand(i))
823 Elts.push_back(V);
824 else
825 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
826 }
827
828 unsigned CurFlags = Ty.getFlags();
829 CurFlags = CurFlags | DIType::FlagArtificial;
830
831 // Flags are stored at this slot.
832 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
833
834 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
835}
Devang Patelac16d442009-10-26 16:54:35 +0000836
Chris Lattnera45664f2008-11-10 02:56:27 +0000837/// CreateDerivedType - Create a derived type like const qualified type,
838/// pointer, typedef, etc.
839DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
840 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000841 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000842 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000843 unsigned LineNumber,
844 uint64_t SizeInBits,
845 uint64_t AlignInBits,
846 uint64_t OffsetInBits,
847 unsigned Flags,
Devang Pateldd9db662009-01-30 18:20:31 +0000848 DIType DerivedFrom) {
Devang Patele4b27562009-08-28 23:24:31 +0000849 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000850 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000851 Context.getNode(),
852 MDString::get(VMContext, Name),
Devang Patel4b945502010-03-09 00:44:10 +0000853 F.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000854 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
855 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
856 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
857 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
858 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patele4b27562009-08-28 23:24:31 +0000859 DerivedFrom.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +0000860 };
Devang Patele4b27562009-08-28 23:24:31 +0000861 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
Chris Lattnera45664f2008-11-10 02:56:27 +0000862}
863
Devang Patelac16d442009-10-26 16:54:35 +0000864
865/// CreateDerivedType - Create a derived type like const qualified type,
866/// pointer, typedef, etc.
867DIDerivedType DIFactory::CreateDerivedTypeEx(unsigned Tag,
868 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000869 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000870 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000871 unsigned LineNumber,
872 Constant *SizeInBits,
873 Constant *AlignInBits,
874 Constant *OffsetInBits,
875 unsigned Flags,
876 DIType DerivedFrom) {
877 Value *Elts[] = {
878 GetTagConstant(Tag),
879 Context.getNode(),
880 MDString::get(VMContext, Name),
Devang Patel4b945502010-03-09 00:44:10 +0000881 F.getNode(),
Devang Patelac16d442009-10-26 16:54:35 +0000882 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
883 SizeInBits,
884 AlignInBits,
885 OffsetInBits,
886 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
887 DerivedFrom.getNode(),
888 };
889 return DIDerivedType(MDNode::get(VMContext, &Elts[0], 10));
890}
891
892
Chris Lattnera45664f2008-11-10 02:56:27 +0000893/// CreateCompositeType - Create a composite type like array, struct, etc.
894DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
895 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000896 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000897 DIFile F,
Chris Lattnera45664f2008-11-10 02:56:27 +0000898 unsigned LineNumber,
899 uint64_t SizeInBits,
900 uint64_t AlignInBits,
901 uint64_t OffsetInBits,
902 unsigned Flags,
903 DIType DerivedFrom,
Devang Patel13319ce2009-02-17 22:43:44 +0000904 DIArray Elements,
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000905 unsigned RuntimeLang,
906 MDNode *ContainingType) {
Owen Andersone277fed2009-07-07 16:31:25 +0000907
Devang Patele4b27562009-08-28 23:24:31 +0000908 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000909 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +0000910 Context.getNode(),
911 MDString::get(VMContext, Name),
Devang Patel4b945502010-03-09 00:44:10 +0000912 F.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000913 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
914 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
915 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
916 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
917 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
Devang Patele4b27562009-08-28 23:24:31 +0000918 DerivedFrom.getNode(),
919 Elements.getNode(),
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000920 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
921 ContainingType
Chris Lattnera45664f2008-11-10 02:56:27 +0000922 };
Devang Patel0fd7f9d2010-01-26 21:14:59 +0000923 return DICompositeType(MDNode::get(VMContext, &Elts[0], 13));
Chris Lattnera45664f2008-11-10 02:56:27 +0000924}
925
926
Devang Patelac16d442009-10-26 16:54:35 +0000927/// CreateCompositeType - Create a composite type like array, struct, etc.
928DICompositeType DIFactory::CreateCompositeTypeEx(unsigned Tag,
929 DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000930 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +0000931 DIFile F,
Devang Patelac16d442009-10-26 16:54:35 +0000932 unsigned LineNumber,
933 Constant *SizeInBits,
934 Constant *AlignInBits,
935 Constant *OffsetInBits,
936 unsigned Flags,
937 DIType DerivedFrom,
938 DIArray Elements,
939 unsigned RuntimeLang) {
940
941 Value *Elts[] = {
942 GetTagConstant(Tag),
943 Context.getNode(),
944 MDString::get(VMContext, Name),
Devang Patel4b945502010-03-09 00:44:10 +0000945 F.getNode(),
Devang Patelac16d442009-10-26 16:54:35 +0000946 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
947 SizeInBits,
948 AlignInBits,
949 OffsetInBits,
950 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
951 DerivedFrom.getNode(),
952 Elements.getNode(),
953 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
954 };
955 return DICompositeType(MDNode::get(VMContext, &Elts[0], 12));
956}
957
958
Chris Lattnera45664f2008-11-10 02:56:27 +0000959/// CreateSubprogram - Create a new descriptor for the specified subprogram.
960/// See comments in DISubprogram for descriptions of these fields. This
961/// method does not unique the generated descriptors.
Daniel Dunbarf612ff62009-09-19 20:40:05 +0000962DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +0000963 StringRef Name,
964 StringRef DisplayName,
965 StringRef LinkageName,
Devang Patel4b945502010-03-09 00:44:10 +0000966 DIFile F,
Devang Patel2e369932010-01-23 00:26:28 +0000967 unsigned LineNo, DIType Ty,
Chris Lattnera45664f2008-11-10 02:56:27 +0000968 bool isLocalToUnit,
Devang Patel5d11eb02009-12-03 19:11:07 +0000969 bool isDefinition,
970 unsigned VK, unsigned VIndex,
Devang Patel4e0d19d2010-02-03 19:57:19 +0000971 DIType ContainingType,
Devang Patelccff8122010-04-30 19:38:23 +0000972 bool isArtificial,
973 bool isOptimized) {
Devang Patel854967e2008-12-17 22:39:29 +0000974
Devang Patele4b27562009-08-28 23:24:31 +0000975 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +0000976 GetTagConstant(dwarf::DW_TAG_subprogram),
Devang Patele4b27562009-08-28 23:24:31 +0000977 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
978 Context.getNode(),
979 MDString::get(VMContext, Name),
980 MDString::get(VMContext, DisplayName),
981 MDString::get(VMContext, LinkageName),
Devang Patel4b945502010-03-09 00:44:10 +0000982 F.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000983 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2e369932010-01-23 00:26:28 +0000984 Ty.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +0000985 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
Devang Patel5d11eb02009-12-03 19:11:07 +0000986 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
987 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
988 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
Devang Patel4e0d19d2010-02-03 19:57:19 +0000989 ContainingType.getNode(),
Devang Patelccff8122010-04-30 19:38:23 +0000990 ConstantInt::get(Type::getInt1Ty(VMContext), isArtificial),
991 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized)
Chris Lattnera45664f2008-11-10 02:56:27 +0000992 };
Devang Patelccff8122010-04-30 19:38:23 +0000993 return DISubprogram(MDNode::get(VMContext, &Elts[0], 16));
Chris Lattnera45664f2008-11-10 02:56:27 +0000994}
995
Devang Patele3a18de2009-12-01 23:09:02 +0000996/// CreateSubprogramDefinition - Create new subprogram descriptor for the
997/// given declaration.
998DISubprogram DIFactory::CreateSubprogramDefinition(DISubprogram &SPDeclaration) {
999 if (SPDeclaration.isDefinition())
1000 return DISubprogram(SPDeclaration.getNode());
1001
1002 MDNode *DeclNode = SPDeclaration.getNode();
1003 Value *Elts[] = {
1004 GetTagConstant(dwarf::DW_TAG_subprogram),
1005 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001006 DeclNode->getOperand(2), // Context
1007 DeclNode->getOperand(3), // Name
1008 DeclNode->getOperand(4), // DisplayName
1009 DeclNode->getOperand(5), // LinkageName
1010 DeclNode->getOperand(6), // CompileUnit
1011 DeclNode->getOperand(7), // LineNo
1012 DeclNode->getOperand(8), // Type
1013 DeclNode->getOperand(9), // isLocalToUnit
Devang Patel5d11eb02009-12-03 19:11:07 +00001014 ConstantInt::get(Type::getInt1Ty(VMContext), true),
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001015 DeclNode->getOperand(11), // Virtuality
1016 DeclNode->getOperand(12), // VIndex
Devang Patel4e0d19d2010-02-03 19:57:19 +00001017 DeclNode->getOperand(13), // Containting Type
Devang Patelccff8122010-04-30 19:38:23 +00001018 DeclNode->getOperand(14), // isArtificial
1019 DeclNode->getOperand(15) // isOptimized
Devang Patele3a18de2009-12-01 23:09:02 +00001020 };
Devang Patelccff8122010-04-30 19:38:23 +00001021 return DISubprogram(MDNode::get(VMContext, &Elts[0], 16));
Devang Patele3a18de2009-12-01 23:09:02 +00001022}
1023
Chris Lattnera45664f2008-11-10 02:56:27 +00001024/// CreateGlobalVariable - Create a new descriptor for the specified global.
1025DIGlobalVariable
Devang Patel65dbc902009-11-25 17:36:49 +00001026DIFactory::CreateGlobalVariable(DIDescriptor Context, StringRef Name,
1027 StringRef DisplayName,
1028 StringRef LinkageName,
Devang Patel4b945502010-03-09 00:44:10 +00001029 DIFile F,
Devang Patel2e369932010-01-23 00:26:28 +00001030 unsigned LineNo, DIType Ty,bool isLocalToUnit,
Devang Pateldd9db662009-01-30 18:20:31 +00001031 bool isDefinition, llvm::GlobalVariable *Val) {
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001032 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001033 GetTagConstant(dwarf::DW_TAG_variable),
Devang Patele4b27562009-08-28 23:24:31 +00001034 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
1035 Context.getNode(),
1036 MDString::get(VMContext, Name),
1037 MDString::get(VMContext, DisplayName),
1038 MDString::get(VMContext, LinkageName),
Devang Patel4b945502010-03-09 00:44:10 +00001039 F.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +00001040 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2e369932010-01-23 00:26:28 +00001041 Ty.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +00001042 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1043 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
Devang Patele4b27562009-08-28 23:24:31 +00001044 Val
Chris Lattnera45664f2008-11-10 02:56:27 +00001045 };
Devang Patele4b27562009-08-28 23:24:31 +00001046
1047 Value *const *Vs = &Elts[0];
1048 MDNode *Node = MDNode::get(VMContext,Vs, 12);
1049
1050 // Create a named metadata so that we do not lose this mdnode.
1051 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001052 NMD->addOperand(Node);
Devang Patele4b27562009-08-28 23:24:31 +00001053
1054 return DIGlobalVariable(Node);
Chris Lattnera45664f2008-11-10 02:56:27 +00001055}
1056
1057
1058/// CreateVariable - Create a new descriptor for the specified variable.
1059DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
Devang Patel65dbc902009-11-25 17:36:49 +00001060 StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +00001061 DIFile F,
1062 unsigned LineNo,
Devang Patel2e369932010-01-23 00:26:28 +00001063 DIType Ty) {
Devang Patele4b27562009-08-28 23:24:31 +00001064 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001065 GetTagConstant(Tag),
Devang Patele4b27562009-08-28 23:24:31 +00001066 Context.getNode(),
1067 MDString::get(VMContext, Name),
Devang Patel4b945502010-03-09 00:44:10 +00001068 F.getNode(),
Owen Anderson1d0be152009-08-13 21:58:54 +00001069 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
Devang Patel2e369932010-01-23 00:26:28 +00001070 Ty.getNode(),
Chris Lattnera45664f2008-11-10 02:56:27 +00001071 };
Devang Patele4b27562009-08-28 23:24:31 +00001072 return DIVariable(MDNode::get(VMContext, &Elts[0], 6));
Chris Lattnera45664f2008-11-10 02:56:27 +00001073}
1074
1075
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001076/// CreateComplexVariable - Create a new descriptor for the specified variable
1077/// which has a complex address expression for its address.
1078DIVariable DIFactory::CreateComplexVariable(unsigned Tag, DIDescriptor Context,
1079 const std::string &Name,
Devang Patel4b945502010-03-09 00:44:10 +00001080 DIFile F,
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001081 unsigned LineNo,
Devang Patel2e369932010-01-23 00:26:28 +00001082 DIType Ty,
1083 SmallVector<Value *, 9> &addr) {
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001084 SmallVector<Value *, 9> Elts;
1085 Elts.push_back(GetTagConstant(Tag));
1086 Elts.push_back(Context.getNode());
1087 Elts.push_back(MDString::get(VMContext, Name));
Devang Patel4b945502010-03-09 00:44:10 +00001088 Elts.push_back(F.getNode());
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001089 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
Devang Patel2e369932010-01-23 00:26:28 +00001090 Elts.push_back(Ty.getNode());
Mike Stump3e4c9bd2009-09-30 00:08:22 +00001091 Elts.insert(Elts.end(), addr.begin(), addr.end());
1092
1093 return DIVariable(MDNode::get(VMContext, &Elts[0], 6+addr.size()));
1094}
1095
1096
Chris Lattnera45664f2008-11-10 02:56:27 +00001097/// CreateBlock - This creates a descriptor for a lexical block with the
Owen Anderson99035272009-07-07 17:12:53 +00001098/// specified parent VMContext.
Devang Patel3d821aa2010-02-16 21:39:34 +00001099DILexicalBlock DIFactory::CreateLexicalBlock(DIDescriptor Context,
1100 unsigned LineNo, unsigned Col) {
Devang Patele4b27562009-08-28 23:24:31 +00001101 Value *Elts[] = {
Chris Lattnera45664f2008-11-10 02:56:27 +00001102 GetTagConstant(dwarf::DW_TAG_lexical_block),
Devang Patel3d821aa2010-02-16 21:39:34 +00001103 Context.getNode(),
1104 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1105 ConstantInt::get(Type::getInt32Ty(VMContext), Col)
Chris Lattnera45664f2008-11-10 02:56:27 +00001106 };
Devang Patel3d821aa2010-02-16 21:39:34 +00001107 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], 4));
Chris Lattnera45664f2008-11-10 02:56:27 +00001108}
1109
Devang Patel6404e4e2009-12-15 19:16:48 +00001110/// CreateNameSpace - This creates new descriptor for a namespace
1111/// with the specified parent context.
1112DINameSpace DIFactory::CreateNameSpace(DIDescriptor Context, StringRef Name,
Devang Patel4b945502010-03-09 00:44:10 +00001113 DIFile F,
Devang Patel6404e4e2009-12-15 19:16:48 +00001114 unsigned LineNo) {
1115 Value *Elts[] = {
1116 GetTagConstant(dwarf::DW_TAG_namespace),
1117 Context.getNode(),
1118 MDString::get(VMContext, Name),
Devang Patel4b945502010-03-09 00:44:10 +00001119 F.getNode(),
Devang Patel6404e4e2009-12-15 19:16:48 +00001120 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1121 };
1122 return DINameSpace(MDNode::get(VMContext, &Elts[0], 5));
1123}
1124
Devang Patelf98d8fe2009-09-01 01:14:15 +00001125/// CreateLocation - Creates a debug info location.
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001126DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
Daniel Dunbara279bc32009-09-20 02:20:51 +00001127 DIScope S, DILocation OrigLoc) {
Devang Patelf98d8fe2009-09-01 01:14:15 +00001128 Value *Elts[] = {
1129 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1130 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1131 S.getNode(),
1132 OrigLoc.getNode(),
1133 };
1134 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1135}
1136
Devang Patele54a5e82009-11-23 19:11:20 +00001137/// CreateLocation - Creates a debug info location.
1138DILocation DIFactory::CreateLocation(unsigned LineNo, unsigned ColumnNo,
1139 DIScope S, MDNode *OrigLoc) {
1140 Value *Elts[] = {
1141 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1142 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo),
1143 S.getNode(),
1144 OrigLoc
1145 };
1146 return DILocation(MDNode::get(VMContext, &Elts[0], 4));
1147}
Chris Lattnera45664f2008-11-10 02:56:27 +00001148
1149//===----------------------------------------------------------------------===//
1150// DIFactory: Routines for inserting code into a function
1151//===----------------------------------------------------------------------===//
1152
Chris Lattnera45664f2008-11-10 02:56:27 +00001153/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001154Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001155 Instruction *InsertBefore) {
Victor Hernandez4cf292a2010-01-26 02:07:38 +00001156 assert(Storage && "no storage passed to dbg.declare");
1157 assert(D.getNode() && "empty DIVariable passed to dbg.declare");
Chris Lattnera45664f2008-11-10 02:56:27 +00001158 if (!DeclareFn)
Bill Wendlingdc817b62009-05-14 18:26:15 +00001159 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1160
Victor Hernandez756462b2010-01-18 20:42:09 +00001161 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1162 D.getNode() };
Devang Patel6daf99b2009-11-10 22:05:35 +00001163 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
Mike Stumpe4250392009-10-01 22:08:58 +00001164}
1165
1166/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
Devang Patel6daf99b2009-11-10 22:05:35 +00001167Instruction *DIFactory::InsertDeclare(Value *Storage, DIVariable D,
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001168 BasicBlock *InsertAtEnd) {
Victor Hernandez4cf292a2010-01-26 02:07:38 +00001169 assert(Storage && "no storage passed to dbg.declare");
1170 assert(D.getNode() && "empty DIVariable passed to dbg.declare");
Mike Stumpe4250392009-10-01 22:08:58 +00001171 if (!DeclareFn)
1172 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1173
Victor Hernandez756462b2010-01-18 20:42:09 +00001174 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1),
1175 D.getNode() };
Devang Patel27a53de2010-01-29 18:30:57 +00001176
1177 // If this block already has a terminator then insert this intrinsic
1178 // before the terminator.
1179 if (TerminatorInst *T = InsertAtEnd->getTerminator())
1180 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
1181 else
1182 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);}
Torok Edwin620f2802008-12-16 09:07:36 +00001183
Victor Hernandezc59b3352009-12-07 21:54:43 +00001184/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001185Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
Victor Hernandezc59b3352009-12-07 21:54:43 +00001186 DIVariable D,
1187 Instruction *InsertBefore) {
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001188 assert(V && "no value passed to dbg.value");
Victor Hernandez4cf292a2010-01-26 02:07:38 +00001189 assert(D.getNode() && "empty DIVariable passed to dbg.value");
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001190 if (!ValueFn)
1191 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1192
Victor Hernandezc8b7cd02010-01-20 05:44:11 +00001193 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001194 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001195 D.getNode() };
1196 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
1197}
1198
Victor Hernandezc59b3352009-12-07 21:54:43 +00001199/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001200Instruction *DIFactory::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
Victor Hernandezc59b3352009-12-07 21:54:43 +00001201 DIVariable D,
1202 BasicBlock *InsertAtEnd) {
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001203 assert(V && "no value passed to dbg.value");
Victor Hernandez4cf292a2010-01-26 02:07:38 +00001204 assert(D.getNode() && "empty DIVariable passed to dbg.value");
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001205 if (!ValueFn)
1206 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1207
Victor Hernandezc8b7cd02010-01-20 05:44:11 +00001208 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
Victor Hernandez5b7e48b2010-01-11 07:45:19 +00001209 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
Victor Hernandez2f9dac72009-12-07 19:36:34 +00001210 D.getNode() };
1211 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
1212}
Devang Patele4b27562009-08-28 23:24:31 +00001213
Devang Pateld2f79a12009-07-28 19:55:13 +00001214//===----------------------------------------------------------------------===//
Devang Patel98c65172009-07-30 18:25:15 +00001215// DebugInfoFinder implementations.
Devang Pateld2f79a12009-07-28 19:55:13 +00001216//===----------------------------------------------------------------------===//
1217
Devang Patel98c65172009-07-30 18:25:15 +00001218/// processModule - Process entire module and collect debug info.
1219void DebugInfoFinder::processModule(Module &M) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001220 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
1221 for (Function::iterator FI = (*I).begin(), FE = (*I).end(); FI != FE; ++FI)
1222 for (BasicBlock::iterator BI = (*FI).begin(), BE = (*FI).end(); BI != BE;
1223 ++BI) {
Devang Patel01c5ff62010-05-04 01:05:02 +00001224 if (DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
Devang Patelb4d31302009-07-31 18:18:52 +00001225 processDeclare(DDI);
Chris Lattner28a9bf62010-04-02 20:44:29 +00001226
1227 DebugLoc Loc = BI->getDebugLoc();
1228 if (Loc.isUnknown())
1229 continue;
1230
1231 LLVMContext &Ctx = BI->getContext();
1232 DIDescriptor Scope(Loc.getScope(Ctx));
1233
1234 if (Scope.isCompileUnit())
1235 addCompileUnit(DICompileUnit(Scope.getNode()));
1236 else if (Scope.isSubprogram())
1237 processSubprogram(DISubprogram(Scope.getNode()));
1238 else if (Scope.isLexicalBlock())
1239 processLexicalBlock(DILexicalBlock(Scope.getNode()));
1240
1241 if (MDNode *IA = Loc.getInlinedAt(Ctx))
1242 processLocation(DILocation(IA));
Devang Pateld2f79a12009-07-28 19:55:13 +00001243 }
Devang Patele4b27562009-08-28 23:24:31 +00001244
1245 NamedMDNode *NMD = M.getNamedMetadata("llvm.dbg.gv");
1246 if (!NMD)
1247 return;
1248
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001249 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
1250 DIGlobalVariable DIG(cast<MDNode>(NMD->getOperand(i)));
Devang Pateld2f79a12009-07-28 19:55:13 +00001251 if (addGlobalVariable(DIG)) {
1252 addCompileUnit(DIG.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001253 processType(DIG.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001254 }
1255 }
1256}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001257
Devang Patel6daf99b2009-11-10 22:05:35 +00001258/// processLocation - Process DILocation.
1259void DebugInfoFinder::processLocation(DILocation Loc) {
Devang Patel3c91b052010-03-08 20:52:55 +00001260 if (!Loc.Verify()) return;
1261 DIDescriptor S(Loc.getScope().getNode());
Devang Patel6daf99b2009-11-10 22:05:35 +00001262 if (S.isCompileUnit())
1263 addCompileUnit(DICompileUnit(S.getNode()));
1264 else if (S.isSubprogram())
1265 processSubprogram(DISubprogram(S.getNode()));
1266 else if (S.isLexicalBlock())
1267 processLexicalBlock(DILexicalBlock(S.getNode()));
1268 processLocation(Loc.getOrigLocation());
1269}
1270
Devang Patel98c65172009-07-30 18:25:15 +00001271/// processType - Process DIType.
1272void DebugInfoFinder::processType(DIType DT) {
Devang Patel72bcdb62009-08-10 22:09:58 +00001273 if (!addType(DT))
Devang Pateld2f79a12009-07-28 19:55:13 +00001274 return;
1275
1276 addCompileUnit(DT.getCompileUnit());
Devang Patel6ceea332009-08-31 18:49:10 +00001277 if (DT.isCompositeType()) {
Devang Patele4b27562009-08-28 23:24:31 +00001278 DICompositeType DCT(DT.getNode());
Devang Patel98c65172009-07-30 18:25:15 +00001279 processType(DCT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001280 DIArray DA = DCT.getTypeArray();
Devang Patel3c91b052010-03-08 20:52:55 +00001281 for (unsigned i = 0, e = DA.getNumElements(); i != e; ++i) {
1282 DIDescriptor D = DA.getElement(i);
1283 if (D.isType())
1284 processType(DIType(D.getNode()));
1285 else if (D.isSubprogram())
1286 processSubprogram(DISubprogram(D.getNode()));
1287 }
Devang Patel6ceea332009-08-31 18:49:10 +00001288 } else if (DT.isDerivedType()) {
Devang Patele4b27562009-08-28 23:24:31 +00001289 DIDerivedType DDT(DT.getNode());
Devang Patel3c91b052010-03-08 20:52:55 +00001290 processType(DDT.getTypeDerivedFrom());
Devang Pateld2f79a12009-07-28 19:55:13 +00001291 }
1292}
1293
Devang Patelbeab41b2009-10-07 22:04:08 +00001294/// processLexicalBlock
1295void DebugInfoFinder::processLexicalBlock(DILexicalBlock LB) {
Devang Patelbeab41b2009-10-07 22:04:08 +00001296 DIScope Context = LB.getContext();
1297 if (Context.isLexicalBlock())
1298 return processLexicalBlock(DILexicalBlock(Context.getNode()));
1299 else
1300 return processSubprogram(DISubprogram(Context.getNode()));
1301}
1302
Devang Patel98c65172009-07-30 18:25:15 +00001303/// processSubprogram - Process DISubprogram.
1304void DebugInfoFinder::processSubprogram(DISubprogram SP) {
Devang Pateld2f79a12009-07-28 19:55:13 +00001305 if (!addSubprogram(SP))
1306 return;
1307 addCompileUnit(SP.getCompileUnit());
Devang Patel98c65172009-07-30 18:25:15 +00001308 processType(SP.getType());
Devang Pateld2f79a12009-07-28 19:55:13 +00001309}
1310
Devang Patelb4d31302009-07-31 18:18:52 +00001311/// processDeclare - Process DbgDeclareInst.
1312void DebugInfoFinder::processDeclare(DbgDeclareInst *DDI) {
Devang Patel3c91b052010-03-08 20:52:55 +00001313 MDNode *N = dyn_cast<MDNode>(DDI->getVariable());
1314 if (!N) return;
1315
1316 DIDescriptor DV(N);
1317 if (!DV.isVariable())
Devang Patelb4d31302009-07-31 18:18:52 +00001318 return;
1319
Devang Patele4b27562009-08-28 23:24:31 +00001320 if (!NodesSeen.insert(DV.getNode()))
Devang Patelb4d31302009-07-31 18:18:52 +00001321 return;
1322
Devang Patel3c91b052010-03-08 20:52:55 +00001323 addCompileUnit(DIVariable(N).getCompileUnit());
1324 processType(DIVariable(N).getType());
Devang Patelb4d31302009-07-31 18:18:52 +00001325}
1326
Devang Patel72bcdb62009-08-10 22:09:58 +00001327/// addType - Add type into Tys.
1328bool DebugInfoFinder::addType(DIType DT) {
Devang Patel3c91b052010-03-08 20:52:55 +00001329 if (!DT.isValid())
Devang Patel72bcdb62009-08-10 22:09:58 +00001330 return false;
1331
Devang Patele4b27562009-08-28 23:24:31 +00001332 if (!NodesSeen.insert(DT.getNode()))
Devang Patel72bcdb62009-08-10 22:09:58 +00001333 return false;
1334
Devang Patele4b27562009-08-28 23:24:31 +00001335 TYs.push_back(DT.getNode());
Devang Patel72bcdb62009-08-10 22:09:58 +00001336 return true;
1337}
1338
Devang Pateld2f79a12009-07-28 19:55:13 +00001339/// addCompileUnit - Add compile unit into CUs.
Devang Patel98c65172009-07-30 18:25:15 +00001340bool DebugInfoFinder::addCompileUnit(DICompileUnit CU) {
Devang Patel3c91b052010-03-08 20:52:55 +00001341 if (!CU.Verify())
Devang Pateld2f79a12009-07-28 19:55:13 +00001342 return false;
1343
Devang Patele4b27562009-08-28 23:24:31 +00001344 if (!NodesSeen.insert(CU.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001345 return false;
1346
Devang Patele4b27562009-08-28 23:24:31 +00001347 CUs.push_back(CU.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001348 return true;
1349}
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001350
Devang Pateld2f79a12009-07-28 19:55:13 +00001351/// addGlobalVariable - Add global variable into GVs.
Devang Patel98c65172009-07-30 18:25:15 +00001352bool DebugInfoFinder::addGlobalVariable(DIGlobalVariable DIG) {
Devang Patel3c91b052010-03-08 20:52:55 +00001353 if (!DIDescriptor(DIG.getNode()).isGlobalVariable())
Devang Pateld2f79a12009-07-28 19:55:13 +00001354 return false;
1355
Devang Patele4b27562009-08-28 23:24:31 +00001356 if (!NodesSeen.insert(DIG.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001357 return false;
1358
Devang Patele4b27562009-08-28 23:24:31 +00001359 GVs.push_back(DIG.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001360 return true;
1361}
1362
1363// addSubprogram - Add subprgoram into SPs.
Devang Patel98c65172009-07-30 18:25:15 +00001364bool DebugInfoFinder::addSubprogram(DISubprogram SP) {
Devang Patel3c91b052010-03-08 20:52:55 +00001365 if (!DIDescriptor(SP.getNode()).isSubprogram())
Devang Pateld2f79a12009-07-28 19:55:13 +00001366 return false;
Daniel Dunbarf612ff62009-09-19 20:40:05 +00001367
Devang Patele4b27562009-08-28 23:24:31 +00001368 if (!NodesSeen.insert(SP.getNode()))
Devang Pateld2f79a12009-07-28 19:55:13 +00001369 return false;
1370
Devang Patele4b27562009-08-28 23:24:31 +00001371 SPs.push_back(SP.getNode());
Devang Pateld2f79a12009-07-28 19:55:13 +00001372 return true;
1373}
1374
Victor Hernandez756462b2010-01-18 20:42:09 +00001375/// Find the debug info descriptor corresponding to this global variable.
1376static Value *findDbgGlobalDeclare(GlobalVariable *V) {
Chris Lattner099b7792009-12-29 09:22:47 +00001377 const Module *M = V->getParent();
1378 NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.gv");
1379 if (!NMD)
Torok Edwinff7d0e92009-03-10 13:41:26 +00001380 return 0;
Chris Lattner099b7792009-12-29 09:22:47 +00001381
Chris Lattner5d0cacd2009-12-31 01:22:29 +00001382 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
Devang Patel3c91b052010-03-08 20:52:55 +00001383 DIDescriptor DIG(cast_or_null<MDNode>(NMD->getOperand(i)));
1384 if (!DIG.isGlobalVariable())
Chris Lattner099b7792009-12-29 09:22:47 +00001385 continue;
Devang Patel3c91b052010-03-08 20:52:55 +00001386 if (DIGlobalVariable(DIG.getNode()).getGlobal() == V)
Chris Lattner099b7792009-12-29 09:22:47 +00001387 return DIG.getNode();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001388 }
Chris Lattner099b7792009-12-29 09:22:47 +00001389 return 0;
1390}
Torok Edwinff7d0e92009-03-10 13:41:26 +00001391
Chris Lattner099b7792009-12-29 09:22:47 +00001392/// Finds the llvm.dbg.declare intrinsic corresponding to this value if any.
1393/// It looks through pointer casts too.
Victor Hernandez756462b2010-01-18 20:42:09 +00001394static const DbgDeclareInst *findDbgDeclare(const Value *V) {
Victor Hernandez3a328652010-01-15 19:04:09 +00001395 V = V->stripPointerCasts();
1396
1397 if (!isa<Instruction>(V) && !isa<Argument>(V))
Torok Edwin620f2802008-12-16 09:07:36 +00001398 return 0;
Victor Hernandez3a328652010-01-15 19:04:09 +00001399
1400 const Function *F = NULL;
1401 if (const Instruction *I = dyn_cast<Instruction>(V))
1402 F = I->getParent()->getParent();
1403 else if (const Argument *A = dyn_cast<Argument>(V))
1404 F = A->getParent();
1405
1406 for (Function::const_iterator FI = F->begin(), FE = F->end(); FI != FE; ++FI)
1407 for (BasicBlock::const_iterator BI = (*FI).begin(), BE = (*FI).end();
1408 BI != BE; ++BI)
1409 if (const DbgDeclareInst *DDI = dyn_cast<DbgDeclareInst>(BI))
1410 if (DDI->getAddress() == V)
1411 return DDI;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001412
Chris Lattner099b7792009-12-29 09:22:47 +00001413 return 0;
1414}
Bill Wendlingdc817b62009-05-14 18:26:15 +00001415
Chris Lattner099b7792009-12-29 09:22:47 +00001416bool llvm::getLocationInfo(const Value *V, std::string &DisplayName,
1417 std::string &Type, unsigned &LineNo,
1418 std::string &File, std::string &Dir) {
1419 DICompileUnit Unit;
1420 DIType TypeD;
Bill Wendlingdc817b62009-05-14 18:26:15 +00001421
Chris Lattner099b7792009-12-29 09:22:47 +00001422 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(const_cast<Value*>(V))) {
1423 Value *DIGV = findDbgGlobalDeclare(GV);
1424 if (!DIGV) return false;
1425 DIGlobalVariable Var(cast<MDNode>(DIGV));
Bill Wendlingdc817b62009-05-14 18:26:15 +00001426
Chris Lattner099b7792009-12-29 09:22:47 +00001427 StringRef D = Var.getDisplayName();
Devang Patel65dbc902009-11-25 17:36:49 +00001428 if (!D.empty())
Chris Lattner099b7792009-12-29 09:22:47 +00001429 DisplayName = D;
1430 LineNo = Var.getLineNumber();
1431 Unit = Var.getCompileUnit();
1432 TypeD = Var.getType();
1433 } else {
1434 const DbgDeclareInst *DDI = findDbgDeclare(V);
1435 if (!DDI) return false;
1436 DIVariable Var(cast<MDNode>(DDI->getVariable()));
1437
1438 StringRef D = Var.getName();
1439 if (!D.empty())
1440 DisplayName = D;
1441 LineNo = Var.getLineNumber();
1442 Unit = Var.getCompileUnit();
1443 TypeD = Var.getType();
Torok Edwinff7d0e92009-03-10 13:41:26 +00001444 }
Devang Patel13e16b62009-06-26 01:49:18 +00001445
Chris Lattner099b7792009-12-29 09:22:47 +00001446 StringRef T = TypeD.getName();
1447 if (!T.empty())
1448 Type = T;
1449 StringRef F = Unit.getFilename();
1450 if (!F.empty())
1451 File = F;
1452 StringRef D = Unit.getDirectory();
1453 if (!D.empty())
1454 Dir = D;
1455 return true;
1456}
Devang Patel9e529c32009-07-02 01:15:24 +00001457
Chris Lattner099b7792009-12-29 09:22:47 +00001458/// getDISubprogram - Find subprogram that is enclosing this scope.
1459DISubprogram llvm::getDISubprogram(MDNode *Scope) {
1460 DIDescriptor D(Scope);
Chris Lattner099b7792009-12-29 09:22:47 +00001461 if (D.isSubprogram())
1462 return DISubprogram(Scope);
1463
1464 if (D.isLexicalBlock())
1465 return getDISubprogram(DILexicalBlock(Scope).getContext().getNode());
1466
1467 return DISubprogram();
1468}
Devang Patel193f7202009-11-24 01:14:22 +00001469
Chris Lattner099b7792009-12-29 09:22:47 +00001470/// getDICompositeType - Find underlying composite type.
1471DICompositeType llvm::getDICompositeType(DIType T) {
Chris Lattner099b7792009-12-29 09:22:47 +00001472 if (T.isCompositeType())
1473 return DICompositeType(T.getNode());
1474
1475 if (T.isDerivedType())
1476 return getDICompositeType(DIDerivedType(T.getNode()).getTypeDerivedFrom());
1477
1478 return DICompositeType();
Torok Edwin620f2802008-12-16 09:07:36 +00001479}