blob: 658b3774af84186aa5cf97b7bfa1467f214958fd [file] [log] [blame]
Devang Patel35fcd652010-11-04 15:01:38 +00001//===--- DIBuilder.cpp - Debug Information Builder ------------------------===//
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 DIBuilder.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Analysis/DIBuilder.h"
15#include "llvm/Analysis/DebugInfo.h"
16#include "llvm/Constants.h"
17#include "llvm/IntrinsicInst.h"
18#include "llvm/Module.h"
Benjamin Kramer42c9b252010-11-04 18:45:27 +000019#include "llvm/ADT/STLExtras.h"
Devang Patel35fcd652010-11-04 15:01:38 +000020#include "llvm/Support/Dwarf.h"
21
22using namespace llvm;
23using namespace llvm::dwarf;
24
25static Constant *GetTagConstant(LLVMContext &VMContext, unsigned Tag) {
26 assert((Tag & LLVMDebugVersionMask) == 0 &&
27 "Tag too large for debug encoding!");
28 return ConstantInt::get(Type::getInt32Ty(VMContext), Tag | LLVMDebugVersion);
29}
Devang Patel48f17ba2010-12-07 23:58:00 +000030
Devang Patel35fcd652010-11-04 15:01:38 +000031DIBuilder::DIBuilder(Module &m)
Devang Patelfe58f952010-12-07 23:25:47 +000032 : M(m), VMContext(M.getContext()), TheCU(0), DeclareFn(0), ValueFn(0) {}
Devang Patel35fcd652010-11-04 15:01:38 +000033
34/// CreateCompileUnit - A CompileUnit provides an anchor for all debugging
35/// information generated during this instance of compilation.
36void DIBuilder::CreateCompileUnit(unsigned Lang, StringRef Filename,
37 StringRef Directory, StringRef Producer,
38 bool isOptimized, StringRef Flags,
39 unsigned RunTimeVer) {
Benjamin Kramer42c9b252010-11-04 18:45:27 +000040 Value *Elts[] = {
41 GetTagConstant(VMContext, dwarf::DW_TAG_compile_unit),
42 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
43 ConstantInt::get(Type::getInt32Ty(VMContext), Lang),
44 MDString::get(VMContext, Filename),
45 MDString::get(VMContext, Directory),
46 MDString::get(VMContext, Producer),
47 // Deprecate isMain field.
48 ConstantInt::get(Type::getInt1Ty(VMContext), true), // isMain
49 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
50 MDString::get(VMContext, Flags),
51 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer)
52 };
53 TheCU = DICompileUnit(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
Devang Patel35fcd652010-11-04 15:01:38 +000054}
55
56/// CreateFile - Create a file descriptor to hold debugging information
57/// for a file.
58DIFile DIBuilder::CreateFile(StringRef Filename, StringRef Directory) {
Benjamin Kramer42c9b252010-11-04 18:45:27 +000059 assert(TheCU && "Unable to create DW_TAG_file_type without CompileUnit");
60 Value *Elts[] = {
61 GetTagConstant(VMContext, dwarf::DW_TAG_file_type),
62 MDString::get(VMContext, Filename),
63 MDString::get(VMContext, Directory),
64 TheCU
65 };
66 return DIFile(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
Devang Patel35fcd652010-11-04 15:01:38 +000067}
68
69/// CreateEnumerator - Create a single enumerator value.
70DIEnumerator DIBuilder::CreateEnumerator(StringRef Name, uint64_t Val) {
Benjamin Kramer42c9b252010-11-04 18:45:27 +000071 Value *Elts[] = {
72 GetTagConstant(VMContext, dwarf::DW_TAG_enumerator),
73 MDString::get(VMContext, Name),
74 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
75 };
76 return DIEnumerator(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
Devang Patel35fcd652010-11-04 15:01:38 +000077}
78
79/// CreateBasicType - Create debugging information entry for a basic
80/// type, e.g 'char'.
81DIType DIBuilder::CreateBasicType(StringRef Name, uint64_t SizeInBits,
82 uint64_t AlignInBits,
83 unsigned Encoding) {
84 // Basic types are encoded in DIBasicType format. Line number, filename,
85 // offset and flags are always empty here.
Benjamin Kramer42c9b252010-11-04 18:45:27 +000086 Value *Elts[] = {
87 GetTagConstant(VMContext, dwarf::DW_TAG_base_type),
88 TheCU,
89 MDString::get(VMContext, Name),
90 NULL, // Filename
91 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
92 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
93 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
94 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
95 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
96 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
97 };
98 return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
Devang Patel35fcd652010-11-04 15:01:38 +000099}
100
101/// CreateQaulifiedType - Create debugging information entry for a qualified
102/// type, e.g. 'const int'.
103DIType DIBuilder::CreateQualifiedType(unsigned Tag, DIType FromTy) {
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000104 // Qualified types are encoded in DIDerivedType format.
105 Value *Elts[] = {
106 GetTagConstant(VMContext, Tag),
107 TheCU,
108 MDString::get(VMContext, StringRef()), // Empty name.
109 NULL, // Filename
110 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
111 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
112 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
113 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
114 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
115 FromTy
116 };
117 return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
Devang Patel35fcd652010-11-04 15:01:38 +0000118}
119
120/// CreatePointerType - Create debugging information entry for a pointer.
121DIType DIBuilder::CreatePointerType(DIType PointeeTy, uint64_t SizeInBits,
122 uint64_t AlignInBits, StringRef Name) {
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000123 // Pointer types are encoded in DIDerivedType format.
124 Value *Elts[] = {
125 GetTagConstant(VMContext, dwarf::DW_TAG_pointer_type),
126 TheCU,
127 MDString::get(VMContext, Name),
128 NULL, // Filename
129 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
130 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
131 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
132 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
133 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
134 PointeeTy
135 };
136 return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
Devang Patel35fcd652010-11-04 15:01:38 +0000137}
138
139/// CreateReferenceType - Create debugging information entry for a reference.
140DIType DIBuilder::CreateReferenceType(DIType RTy) {
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000141 // References are encoded in DIDerivedType format.
142 Value *Elts[] = {
143 GetTagConstant(VMContext, dwarf::DW_TAG_reference_type),
144 TheCU,
145 NULL, // Name
146 NULL, // Filename
147 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
148 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
149 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
150 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
151 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
152 RTy
153 };
154 return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
Devang Patel35fcd652010-11-04 15:01:38 +0000155}
156
157/// CreateTypedef - Create debugging information entry for a typedef.
158DIType DIBuilder::CreateTypedef(DIType Ty, StringRef Name, DIFile File,
159 unsigned LineNo) {
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000160 // typedefs are encoded in DIDerivedType format.
161 assert(Ty.Verify() && "Invalid typedef type!");
162 Value *Elts[] = {
163 GetTagConstant(VMContext, dwarf::DW_TAG_typedef),
164 Ty.getContext(),
165 MDString::get(VMContext, Name),
166 File,
167 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
168 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
169 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
170 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
171 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
172 Ty
173 };
174 return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
Devang Patel35fcd652010-11-04 15:01:38 +0000175}
176
177/// CreateFriend - Create debugging information entry for a 'friend'.
178DIType DIBuilder::CreateFriend(DIType Ty, DIType FriendTy) {
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000179 // typedefs are encoded in DIDerivedType format.
180 assert(Ty.Verify() && "Invalid type!");
181 assert(FriendTy.Verify() && "Invalid friend type!");
182 Value *Elts[] = {
183 GetTagConstant(VMContext, dwarf::DW_TAG_friend),
184 Ty,
185 NULL, // Name
186 Ty.getFile(),
187 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
188 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
189 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
190 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
191 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
192 FriendTy
193 };
194 return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
Devang Patel35fcd652010-11-04 15:01:38 +0000195}
196
197/// CreateInheritance - Create debugging information entry to establish
198/// inheritnace relationship between two types.
199DIType DIBuilder::CreateInheritance(DIType Ty, DIType BaseTy,
200 uint64_t BaseOffset, unsigned Flags) {
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000201 // TAG_inheritance is encoded in DIDerivedType format.
202 Value *Elts[] = {
203 GetTagConstant(VMContext, dwarf::DW_TAG_inheritance),
204 Ty,
205 NULL, // Name
206 NULL, // File
207 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
208 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
209 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
210 ConstantInt::get(Type::getInt64Ty(VMContext), BaseOffset),
211 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
212 BaseTy
213 };
214 return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
Devang Patel35fcd652010-11-04 15:01:38 +0000215}
216
217/// CreateMemberType - Create debugging information entry for a member.
Devang Patelfe58f952010-12-07 23:25:47 +0000218DIType DIBuilder::CreateMemberType(StringRef Name,
219 DIFile File, unsigned LineNumber,
Devang Patel35fcd652010-11-04 15:01:38 +0000220 uint64_t SizeInBits, uint64_t AlignInBits,
221 uint64_t OffsetInBits, unsigned Flags,
222 DIType Ty) {
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000223 // TAG_member is encoded in DIDerivedType format.
224 Value *Elts[] = {
225 GetTagConstant(VMContext, dwarf::DW_TAG_member),
Devang Patelfe58f952010-12-07 23:25:47 +0000226 File, // Or TheCU ? Ty ?
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000227 MDString::get(VMContext, Name),
Devang Patelfe58f952010-12-07 23:25:47 +0000228 File,
Benjamin Kramer42c9b252010-11-04 18:45:27 +0000229 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
230 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
231 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
232 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
233 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
234 Ty
235 };
236 return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
Devang Patel35fcd652010-11-04 15:01:38 +0000237}
238
Devang Patel0a2551d2010-12-08 20:18:20 +0000239/// CreateClassType - Create debugging information entry for a class.
240DIType DIBuilder::CreateClassType(DIDescriptor Context, StringRef Name,
241 DIFile File, unsigned LineNumber,
242 uint64_t SizeInBits, uint64_t AlignInBits,
243 uint64_t OffsetInBits, unsigned Flags,
244 DIType DerivedFrom, DIArray Elements,
245 MDNode *VTableHoder) {
246 // TAG_class_type is encoded in DICompositeType format.
247 Value *Elts[] = {
248 GetTagConstant(VMContext, dwarf::DW_TAG_class_type),
249 Context,
250 MDString::get(VMContext, Name),
251 File,
252 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
253 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
254 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
255 ConstantInt::get(Type::getInt32Ty(VMContext), OffsetInBits),
256 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
257 DerivedFrom,
258 Elements,
259 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
260 VTableHoder
261 };
262 return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
263}
264
Devang Patelfe58f952010-12-07 23:25:47 +0000265/// CreateStructType - Create debugging information entry for a struct.
Devang Patel43c249c2010-12-08 01:50:15 +0000266DIType DIBuilder::CreateStructType(DIDescriptor Context, StringRef Name,
267 DIFile File, unsigned LineNumber,
268 uint64_t SizeInBits, uint64_t AlignInBits,
269 unsigned Flags, DIArray Elements,
270 unsigned RunTimeLang) {
Devang Patelfe58f952010-12-07 23:25:47 +0000271 // TAG_structure_type is encoded in DICompositeType format.
272 Value *Elts[] = {
273 GetTagConstant(VMContext, dwarf::DW_TAG_structure_type),
274 Context,
275 MDString::get(VMContext, Name),
Devang Patel43c249c2010-12-08 01:50:15 +0000276 File,
Devang Patelfe58f952010-12-07 23:25:47 +0000277 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
278 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
279 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
Devang Patel43c249c2010-12-08 01:50:15 +0000280 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
Devang Patelfe58f952010-12-07 23:25:47 +0000281 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
282 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
283 Elements,
284 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
285 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
286 };
287 return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
288}
289
Devang Patel43c249c2010-12-08 01:50:15 +0000290/// CreateUnionType - Create debugging information entry for an union.
291DIType DIBuilder::CreateUnionType(DIDescriptor Scope, StringRef Name,
292 DIFile File,
293 unsigned LineNumber, uint64_t SizeInBits,
294 uint64_t AlignInBits, unsigned Flags,
295 DIArray Elements, unsigned RunTimeLang) {
296 // TAG_union_type is encoded in DICompositeType format.
297 Value *Elts[] = {
298 GetTagConstant(VMContext, dwarf::DW_TAG_union_type),
299 Scope,
300 MDString::get(VMContext, Name),
301 File,
302 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
303 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
304 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
305 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
306 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
307 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
308 Elements,
309 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
310 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
311 };
312 return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
313}
314
315/// CreateSubroutineType - Create subroutine type.
316DIType DIBuilder::CreateSubroutineType(DIFile File, DIArray ParameterTypes) {
317 // TAG_subroutine_type is encoded in DICompositeType format.
318 Value *Elts[] = {
319 GetTagConstant(VMContext, dwarf::DW_TAG_subroutine_type),
320 File,
321 MDString::get(VMContext, ""),
322 File,
323 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
324 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
325 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
326 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
327 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
328 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
329 ParameterTypes,
330 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
331 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
332 };
333 return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
334}
335
336/// CreateEnumerationType - Create debugging information entry for an
337/// enumeration.
338DIType DIBuilder::CreateEnumerationType(DIDescriptor Scope, StringRef Name,
339 DIFile File, unsigned LineNumber,
340 uint64_t SizeInBits,
341 uint64_t AlignInBits, DIArray Elements) {
342 // TAG_enumeration_type is encoded in DICompositeType format.
343 Value *Elts[] = {
344 GetTagConstant(VMContext, dwarf::DW_TAG_enumeration_type),
345 Scope,
346 MDString::get(VMContext, Name),
347 File,
348 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
349 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
350 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
351 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
352 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
353 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
354 Elements,
355 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
356 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
357 };
358 MDNode *Node = MDNode::get(VMContext, &Elts[0], array_lengthof(Elts));
359 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.enum");
360 NMD->addOperand(Node);
361 return DIType(Node);
362}
363
364/// CreateArrayType - Create debugging information entry for an array.
365DIType DIBuilder::CreateArrayType(uint64_t Size, uint64_t AlignInBits,
366 DIType Ty, DIArray Subscripts) {
367 // TAG_array_type is encoded in DICompositeType format.
368 Value *Elts[] = {
369 GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
370 TheCU,
371 MDString::get(VMContext, ""),
372 TheCU,
373 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
374 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
375 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
376 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
377 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
378 Ty,
379 Subscripts,
380 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
381 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
382 };
383 return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
384}
385
386/// CreateVectorType - Create debugging information entry for a vector.
387DIType DIBuilder::CreateVectorType(uint64_t Size, uint64_t AlignInBits,
388 DIType Ty, DIArray Subscripts) {
389 // TAG_vector_type is encoded in DICompositeType format.
390 Value *Elts[] = {
391 GetTagConstant(VMContext, dwarf::DW_TAG_vector_type),
392 TheCU,
393 MDString::get(VMContext, ""),
394 TheCU,
395 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
396 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
397 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
398 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
399 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
400 Ty,
401 Subscripts,
402 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
403 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
404 };
405 return DIType(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
406}
Devang Patelfe58f952010-12-07 23:25:47 +0000407
Devang Patel35fcd652010-11-04 15:01:38 +0000408/// CreateArtificialType - Create a new DIType with "artificial" flag set.
409DIType DIBuilder::CreateArtificialType(DIType Ty) {
410 if (Ty.isArtificial())
411 return Ty;
412
413 SmallVector<Value *, 9> Elts;
414 MDNode *N = Ty;
415 assert (N && "Unexpected input DIType!");
416 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
417 if (Value *V = N->getOperand(i))
418 Elts.push_back(V);
419 else
420 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
421 }
422
423 unsigned CurFlags = Ty.getFlags();
424 CurFlags = CurFlags | DIType::FlagArtificial;
425
426 // Flags are stored at this slot.
427 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
428
429 return DIType(MDNode::get(VMContext, Elts.data(), Elts.size()));
430}
Devang Patelfe58f952010-12-07 23:25:47 +0000431
Devang Patel43c249c2010-12-08 01:50:15 +0000432/// RetainType - Retain DIType in a module even if it is not referenced
433/// through debug info anchors.
434void DIBuilder::RetainType(DIType T) {
435 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.ty");
436 NMD->addOperand(T);
437}
438
439/// CreateUnspecifiedParameter - Create unspeicified type descriptor
440/// for the subroutine type.
441DIDescriptor DIBuilder::CreateUnspecifiedParameter() {
442 Value *Elts[] = {
443 GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_parameters)
444 };
445 return DIDescriptor(MDNode::get(VMContext, &Elts[0], 1));
446}
447
Devang Patelfe58f952010-12-07 23:25:47 +0000448/// CreateTemporaryType - Create a temporary forward-declared type.
449DIType DIBuilder::CreateTemporaryType() {
450 // Give the temporary MDNode a tag. It doesn't matter what tag we
451 // use here as long as DIType accepts it.
452 Value *Elts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
453 MDNode *Node = MDNode::getTemporary(VMContext, Elts, array_lengthof(Elts));
454 return DIType(Node);
455}
456
457/// CreateTemporaryType - Create a temporary forward-declared type.
458DIType DIBuilder::CreateTemporaryType(DIFile F) {
459 // Give the temporary MDNode a tag. It doesn't matter what tag we
460 // use here as long as DIType accepts it.
461 Value *Elts[] = {
462 GetTagConstant(VMContext, DW_TAG_base_type),
463 F.getCompileUnit(),
464 NULL,
465 F
466 };
467 MDNode *Node = MDNode::getTemporary(VMContext, Elts, array_lengthof(Elts));
468 return DIType(Node);
469}
470
471/// GetOrCreateArray - Get a DIArray, create one if required.
472DIArray DIBuilder::GetOrCreateArray(Value *const *Elements, unsigned NumElements) {
473 if (NumElements == 0) {
474 Value *Null = llvm::Constant::getNullValue(Type::getInt32Ty(VMContext));
475 return DIArray(MDNode::get(VMContext, &Null, 1));
476 }
477 return DIArray(MDNode::get(VMContext, Elements, NumElements));
478}
479
Devang Patel43c249c2010-12-08 01:50:15 +0000480/// GetOrCreateSubrange - Create a descriptor for a value range. This
481/// implicitly uniques the values returned.
482DISubrange DIBuilder::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
483 Value *Elts[] = {
484 GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type),
485 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
486 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
487 };
488
489 return DISubrange(MDNode::get(VMContext, &Elts[0], 3));
490}
491
Devang Patelfe58f952010-12-07 23:25:47 +0000492/// CreateGlobalVariable - Create a new descriptor for the specified global.
493DIGlobalVariable DIBuilder::
Devang Patel94541ef2010-12-08 00:06:22 +0000494CreateGlobalVariable(StringRef Name, DIFile F, unsigned LineNumber,
Devang Patelfe58f952010-12-07 23:25:47 +0000495 DIType Ty, bool isLocalToUnit, llvm::Value *Val) {
496 Value *Elts[] = {
497 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
498 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
499 TheCU,
500 MDString::get(VMContext, Name),
501 MDString::get(VMContext, Name),
Devang Patel94541ef2010-12-08 00:06:22 +0000502 MDString::get(VMContext, Name),
Devang Patelfe58f952010-12-07 23:25:47 +0000503 F,
504 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
505 Ty,
506 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
507 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
508 Val
509 };
510 MDNode *Node = MDNode::get(VMContext, &Elts[0], array_lengthof(Elts));
511 // Create a named metadata so that we do not lose this mdnode.
512 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
513 NMD->addOperand(Node);
514 return DIGlobalVariable(Node);
515}
516
517/// CreateStaticVariable - Create a new descriptor for the specified static
518/// variable.
519DIGlobalVariable DIBuilder::
520CreateStaticVariable(DIDescriptor Context, StringRef Name,
521 StringRef LinkageName, DIFile F, unsigned LineNumber,
522 DIType Ty, bool isLocalToUnit, llvm::Value *Val) {
523 Value *Elts[] = {
524 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
525 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext)),
526 Context,
527 MDString::get(VMContext, Name),
528 MDString::get(VMContext, Name),
529 MDString::get(VMContext, LinkageName),
530 F,
531 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
532 Ty,
533 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
534 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
535 Val
536 };
537 MDNode *Node = MDNode::get(VMContext, &Elts[0], array_lengthof(Elts));
538 // Create a named metadata so that we do not lose this mdnode.
539 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.gv");
540 NMD->addOperand(Node);
541 return DIGlobalVariable(Node);
542}
543
Devang Patel48f17ba2010-12-07 23:58:00 +0000544/// CreateVariable - Create a new descriptor for the specified variable.
545DIVariable DIBuilder::CreateLocalVariable(unsigned Tag, DIDescriptor Scope,
546 StringRef Name, DIFile File,
547 unsigned LineNo, DIType Ty,
548 bool AlwaysPreserve, unsigned Flags) {
549 Value *Elts[] = {
550 GetTagConstant(VMContext, Tag),
551 Scope,
552 MDString::get(VMContext, Name),
553 File,
554 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
555 Ty,
556 ConstantInt::get(Type::getInt32Ty(VMContext), Flags)
557 };
558 MDNode *Node = MDNode::get(VMContext, &Elts[0], array_lengthof(Elts));
559 if (AlwaysPreserve) {
560 // The optimizer may remove local variable. If there is an interest
561 // to preserve variable info in such situation then stash it in a
562 // named mdnode.
563 DISubprogram Fn(getDISubprogram(Scope));
564 StringRef FName = "fn";
565 if (Fn.getFunction())
566 FName = Fn.getFunction()->getName();
567 char One = '\1';
568 if (FName.startswith(StringRef(&One, 1)))
569 FName = FName.substr(1);
570 NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, FName);
571 FnLocals->addOperand(Node);
572 }
573 return DIVariable(Node);
574}
575
Devang Patelfe58f952010-12-07 23:25:47 +0000576/// CreateComplexVariable - Create a new descriptor for the specified variable
577/// which has a complex address expression for its address.
578DIVariable DIBuilder::CreateComplexVariable(unsigned Tag, DIDescriptor Scope,
579 StringRef Name, DIFile F,
580 unsigned LineNo,
581 DIType Ty, Value *const *Addr,
582 unsigned NumAddr) {
583 SmallVector<Value *, 15> Elts;
584 Elts.push_back(GetTagConstant(VMContext, Tag));
585 Elts.push_back(Scope);
586 Elts.push_back(MDString::get(VMContext, Name));
587 Elts.push_back(F);
588 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext), LineNo));
589 Elts.push_back(Ty);
590 Elts.append(Addr, Addr+NumAddr);
591
592 return DIVariable(MDNode::get(VMContext, Elts.data(), Elts.size()));
593}
594
Devang Patelfe58f952010-12-07 23:25:47 +0000595/// CreateNameSpace - This creates new descriptor for a namespace
596/// with the specified parent scope.
597DINameSpace DIBuilder::CreateNameSpace(DIDescriptor Scope, StringRef Name,
598 DIFile File, unsigned LineNo) {
599 Value *Elts[] = {
600 GetTagConstant(VMContext, dwarf::DW_TAG_namespace),
601 Scope,
602 MDString::get(VMContext, Name),
603 File,
604 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
605 };
606 return DINameSpace(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
607}
608
Devang Patel43c249c2010-12-08 01:50:15 +0000609DILexicalBlock DIBuilder::CreateLexicalBlock(DIDescriptor Scope, DIFile File,
610 unsigned Line, unsigned Col) {
611 // Defeat MDNode uniqing for lexical blocks by using unique id.
612 static unsigned int unique_id = 0;
613 Value *Elts[] = {
614 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
615 Scope,
616 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
617 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
618 File,
619 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
620 };
621 return DILexicalBlock(MDNode::get(VMContext, &Elts[0], array_lengthof(Elts)));
622}
623
Devang Patelfe58f952010-12-07 23:25:47 +0000624/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
625Instruction *DIBuilder::InsertDeclare(Value *Storage, DIVariable VarInfo,
626 Instruction *InsertBefore) {
627 assert(Storage && "no storage passed to dbg.declare");
628 assert(VarInfo.Verify() && "empty DIVariable passed to dbg.declare");
629 if (!DeclareFn)
630 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
631
632 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1), VarInfo };
633 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertBefore);
634}
635
636/// InsertDeclare - Insert a new llvm.dbg.declare intrinsic call.
637Instruction *DIBuilder::InsertDeclare(Value *Storage, DIVariable VarInfo,
638 BasicBlock *InsertAtEnd) {
639 assert(Storage && "no storage passed to dbg.declare");
640 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.declare");
641 if (!DeclareFn)
642 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
643
644 Value *Args[] = { MDNode::get(Storage->getContext(), &Storage, 1), VarInfo };
645
646 // If this block already has a terminator then insert this intrinsic
647 // before the terminator.
648 if (TerminatorInst *T = InsertAtEnd->getTerminator())
649 return CallInst::Create(DeclareFn, Args, Args+2, "", T);
650 else
651 return CallInst::Create(DeclareFn, Args, Args+2, "", InsertAtEnd);
652}
653
654/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
655Instruction *DIBuilder::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
656 DIVariable VarInfo,
657 Instruction *InsertBefore) {
658 assert(V && "no value passed to dbg.value");
659 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
660 if (!ValueFn)
661 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
662
663 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
664 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
665 VarInfo };
666 return CallInst::Create(ValueFn, Args, Args+3, "", InsertBefore);
667}
668
669/// InsertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
670Instruction *DIBuilder::InsertDbgValueIntrinsic(Value *V, uint64_t Offset,
671 DIVariable VarInfo,
672 BasicBlock *InsertAtEnd) {
673 assert(V && "no value passed to dbg.value");
674 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
675 if (!ValueFn)
676 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
677
678 Value *Args[] = { MDNode::get(V->getContext(), &V, 1),
679 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
680 VarInfo };
681 return CallInst::Create(ValueFn, Args, Args+3, "", InsertAtEnd);
682}
683