1 //===--- DIBuilder.cpp - Debug Information Builder ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the DIBuilder.
12 //===----------------------------------------------------------------------===//
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"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/Support/Dwarf.h"
23 using namespace llvm::dwarf
;
25 static 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
);
31 DIBuilder::DIBuilder(Module
&m
)
32 : M(m
), VMContext(M
.getContext()), TheCU(0), DeclareFn(0), ValueFn(0) {}
34 /// createCompileUnit - A CompileUnit provides an anchor for all debugging
35 /// information generated during this instance of compilation.
36 void DIBuilder::createCompileUnit(unsigned Lang
, StringRef Filename
,
37 StringRef Directory
, StringRef Producer
,
38 bool isOptimized
, StringRef Flags
,
39 unsigned RunTimeVer
) {
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
)
53 TheCU
= DICompileUnit(MDNode::get(VMContext
, &Elts
[0], array_lengthof(Elts
)));
56 /// createFile - Create a file descriptor to hold debugging information
58 DIFile
DIBuilder::createFile(StringRef Filename
, StringRef Directory
) {
59 assert(TheCU
&& "Unable to create DW_TAG_file_type without CompileUnit");
61 GetTagConstant(VMContext
, dwarf::DW_TAG_file_type
),
62 MDString::get(VMContext
, Filename
),
63 MDString::get(VMContext
, Directory
),
66 return DIFile(MDNode::get(VMContext
, &Elts
[0], array_lengthof(Elts
)));
69 /// createEnumerator - Create a single enumerator value.
70 DIEnumerator
DIBuilder::createEnumerator(StringRef Name
, uint64_t Val
) {
72 GetTagConstant(VMContext
, dwarf::DW_TAG_enumerator
),
73 MDString::get(VMContext
, Name
),
74 ConstantInt::get(Type::getInt64Ty(VMContext
), Val
)
76 return DIEnumerator(MDNode::get(VMContext
, &Elts
[0], array_lengthof(Elts
)));
79 /// createBasicType - Create debugging information entry for a basic
81 DIType
DIBuilder::createBasicType(StringRef Name
, uint64_t SizeInBits
,
84 // Basic types are encoded in DIBasicType format. Line number, filename,
85 // offset and flags are always empty here.
87 GetTagConstant(VMContext
, dwarf::DW_TAG_base_type
),
89 MDString::get(VMContext
, Name
),
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
)
98 return DIType(MDNode::get(VMContext
, &Elts
[0], array_lengthof(Elts
)));
101 /// createQaulifiedType - Create debugging information entry for a qualified
102 /// type, e.g. 'const int'.
103 DIType
DIBuilder::createQualifiedType(unsigned Tag
, DIType FromTy
) {
104 // Qualified types are encoded in DIDerivedType format.
106 GetTagConstant(VMContext
, Tag
),
108 MDString::get(VMContext
, StringRef()), // Empty name.
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
117 return DIType(MDNode::get(VMContext
, &Elts
[0], array_lengthof(Elts
)));
120 /// createPointerType - Create debugging information entry for a pointer.
121 DIType
DIBuilder::createPointerType(DIType PointeeTy
, uint64_t SizeInBits
,
122 uint64_t AlignInBits
, StringRef Name
) {
123 // Pointer types are encoded in DIDerivedType format.
125 GetTagConstant(VMContext
, dwarf::DW_TAG_pointer_type
),
127 MDString::get(VMContext
, Name
),
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
136 return DIType(MDNode::get(VMContext
, &Elts
[0], array_lengthof(Elts
)));
139 /// createReferenceType - Create debugging information entry for a reference.
140 DIType
DIBuilder::createReferenceType(DIType RTy
) {
141 // References are encoded in DIDerivedType format.
143 GetTagConstant(VMContext
, dwarf::DW_TAG_reference_type
),
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
154 return DIType(MDNode::get(VMContext
, &Elts
[0], array_lengthof(Elts
)));
157 /// createTypedef - Create debugging information entry for a typedef.
158 DIType
DIBuilder::createTypedef(DIType Ty
, StringRef Name
, DIFile File
,
160 // typedefs are encoded in DIDerivedType format.
161 assert(Ty
.Verify() && "Invalid typedef type!");
163 GetTagConstant(VMContext
, dwarf::DW_TAG_typedef
),
165 MDString::get(VMContext
, Name
),
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
174 return DIType(MDNode::get(VMContext
, &Elts
[0], array_lengthof(Elts
)));
177 /// createFriend - Create debugging information entry for a 'friend'.
178 DIType
DIBuilder::createFriend(DIType Ty
, DIType FriendTy
) {
179 // typedefs are encoded in DIDerivedType format.
180 assert(Ty
.Verify() && "Invalid type!");
181 assert(FriendTy
.Verify() && "Invalid friend type!");
183 GetTagConstant(VMContext
, dwarf::DW_TAG_friend
),
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
194 return DIType(MDNode::get(VMContext
, &Elts
[0], array_lengthof(Elts
)));
197 /// createInheritance - Create debugging information entry to establish
198 /// inheritnace relationship between two types.
199 DIType
DIBuilder::createInheritance(DIType Ty
, DIType BaseTy
,
200 uint64_t BaseOffset
, unsigned Flags
) {
201 // TAG_inheritance is encoded in DIDerivedType format.
203 GetTagConstant(VMContext
, dwarf::DW_TAG_inheritance
),
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
),
214 return DIType(MDNode::get(VMContext
, &Elts
[0], array_lengthof(Elts
)));
217 /// createMemberType - Create debugging information entry for a member.
218 DIType
DIBuilder::createMemberType(StringRef Name
,
219 DIFile File
, unsigned LineNumber
,
220 uint64_t SizeInBits
, uint64_t AlignInBits
,
221 uint64_t OffsetInBits
, unsigned Flags
,
223 // TAG_member is encoded in DIDerivedType format.
225 GetTagConstant(VMContext
, dwarf::DW_TAG_member
),
226 File
, // Or TheCU ? Ty ?
227 MDString::get(VMContext
, Name
),
229 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
),
236 return DIType(MDNode::get(VMContext
, &Elts
[0], array_lengthof(Elts
)));
239 /// createClassType - Create debugging information entry for a class.
240 DIType
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
, MDNode
*TemplateParams
) {
246 // TAG_class_type is encoded in DICompositeType format.
248 GetTagConstant(VMContext
, dwarf::DW_TAG_class_type
),
250 MDString::get(VMContext
, Name
),
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
),
259 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
263 return DIType(MDNode::get(VMContext
, &Elts
[0], array_lengthof(Elts
)));
266 /// createTemplateTypeParameter - Create debugging information for template
268 DITemplateTypeParameter
269 DIBuilder::createTemplateTypeParameter(DIDescriptor Context
, StringRef Name
,
270 DIType Ty
, MDNode
*File
, unsigned LineNo
,
273 GetTagConstant(VMContext
, dwarf::DW_TAG_template_type_parameter
),
275 MDString::get(VMContext
, Name
),
278 ConstantInt::get(Type::getInt32Ty(VMContext
), LineNo
),
279 ConstantInt::get(Type::getInt32Ty(VMContext
), ColumnNo
)
281 return DITemplateTypeParameter(MDNode::get(VMContext
, &Elts
[0],
282 array_lengthof(Elts
)));
285 /// createTemplateValueParameter - Create debugging information for template
287 DITemplateValueParameter
288 DIBuilder::createTemplateValueParameter(DIDescriptor Context
, StringRef Name
,
289 DIType Ty
, uint64_t Val
,
290 MDNode
*File
, unsigned LineNo
,
293 GetTagConstant(VMContext
, dwarf::DW_TAG_template_value_parameter
),
295 MDString::get(VMContext
, Name
),
297 ConstantInt::get(Type::getInt64Ty(VMContext
), Val
),
299 ConstantInt::get(Type::getInt32Ty(VMContext
), LineNo
),
300 ConstantInt::get(Type::getInt32Ty(VMContext
), ColumnNo
)
302 return DITemplateValueParameter(MDNode::get(VMContext
, &Elts
[0],
303 array_lengthof(Elts
)));
306 /// createStructType - Create debugging information entry for a struct.
307 DIType
DIBuilder::createStructType(DIDescriptor Context
, StringRef Name
,
308 DIFile File
, unsigned LineNumber
,
309 uint64_t SizeInBits
, uint64_t AlignInBits
,
310 unsigned Flags
, DIArray Elements
,
311 unsigned RunTimeLang
) {
312 // TAG_structure_type is encoded in DICompositeType format.
314 GetTagConstant(VMContext
, dwarf::DW_TAG_structure_type
),
316 MDString::get(VMContext
, Name
),
318 ConstantInt::get(Type::getInt32Ty(VMContext
), LineNumber
),
319 ConstantInt::get(Type::getInt64Ty(VMContext
), SizeInBits
),
320 ConstantInt::get(Type::getInt64Ty(VMContext
), AlignInBits
),
321 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
322 ConstantInt::get(Type::getInt32Ty(VMContext
), Flags
),
323 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext
)),
325 ConstantInt::get(Type::getInt32Ty(VMContext
), RunTimeLang
),
326 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext
)),
328 return DIType(MDNode::get(VMContext
, &Elts
[0], array_lengthof(Elts
)));
331 /// createUnionType - Create debugging information entry for an union.
332 DIType
DIBuilder::createUnionType(DIDescriptor Scope
, StringRef Name
,
334 unsigned LineNumber
, uint64_t SizeInBits
,
335 uint64_t AlignInBits
, unsigned Flags
,
336 DIArray Elements
, unsigned RunTimeLang
) {
337 // TAG_union_type is encoded in DICompositeType format.
339 GetTagConstant(VMContext
, dwarf::DW_TAG_union_type
),
341 MDString::get(VMContext
, Name
),
343 ConstantInt::get(Type::getInt32Ty(VMContext
), LineNumber
),
344 ConstantInt::get(Type::getInt64Ty(VMContext
), SizeInBits
),
345 ConstantInt::get(Type::getInt64Ty(VMContext
), AlignInBits
),
346 ConstantInt::get(Type::getInt64Ty(VMContext
), 0),
347 ConstantInt::get(Type::getInt32Ty(VMContext
), Flags
),
348 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext
)),
350 ConstantInt::get(Type::getInt32Ty(VMContext
), RunTimeLang
),
351 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext
)),
353 return DIType(MDNode::get(VMContext
, &Elts
[0], array_lengthof(Elts
)));
356 /// createSubroutineType - Create subroutine type.
357 DIType
DIBuilder::createSubroutineType(DIFile File
, DIArray ParameterTypes
) {
358 // TAG_subroutine_type is encoded in DICompositeType format.
360 GetTagConstant(VMContext
, dwarf::DW_TAG_subroutine_type
),
362 MDString::get(VMContext
, ""),
364 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
365 ConstantInt::get(Type::getInt64Ty(VMContext
), 0),
366 ConstantInt::get(Type::getInt64Ty(VMContext
), 0),
367 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
368 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
369 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext
)),
371 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
372 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext
)),
374 return DIType(MDNode::get(VMContext
, &Elts
[0], array_lengthof(Elts
)));
377 /// createEnumerationType - Create debugging information entry for an
379 DIType
DIBuilder::createEnumerationType(DIDescriptor Scope
, StringRef Name
,
380 DIFile File
, unsigned LineNumber
,
382 uint64_t AlignInBits
, DIArray Elements
) {
383 // TAG_enumeration_type is encoded in DICompositeType format.
385 GetTagConstant(VMContext
, dwarf::DW_TAG_enumeration_type
),
387 MDString::get(VMContext
, Name
),
389 ConstantInt::get(Type::getInt32Ty(VMContext
), LineNumber
),
390 ConstantInt::get(Type::getInt64Ty(VMContext
), SizeInBits
),
391 ConstantInt::get(Type::getInt64Ty(VMContext
), AlignInBits
),
392 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
393 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
394 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext
)),
396 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
397 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext
)),
399 MDNode
*Node
= MDNode::get(VMContext
, &Elts
[0], array_lengthof(Elts
));
400 NamedMDNode
*NMD
= M
.getOrInsertNamedMetadata("llvm.dbg.enum");
401 NMD
->addOperand(Node
);
405 /// createArrayType - Create debugging information entry for an array.
406 DIType
DIBuilder::createArrayType(uint64_t Size
, uint64_t AlignInBits
,
407 DIType Ty
, DIArray Subscripts
) {
408 // TAG_array_type is encoded in DICompositeType format.
410 GetTagConstant(VMContext
, dwarf::DW_TAG_array_type
),
412 MDString::get(VMContext
, ""),
414 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
415 ConstantInt::get(Type::getInt64Ty(VMContext
), Size
),
416 ConstantInt::get(Type::getInt64Ty(VMContext
), AlignInBits
),
417 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
418 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
421 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
422 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext
)),
424 return DIType(MDNode::get(VMContext
, &Elts
[0], array_lengthof(Elts
)));
427 /// createVectorType - Create debugging information entry for a vector.
428 DIType
DIBuilder::createVectorType(uint64_t Size
, uint64_t AlignInBits
,
429 DIType Ty
, DIArray Subscripts
) {
430 // TAG_vector_type is encoded in DICompositeType format.
432 GetTagConstant(VMContext
, dwarf::DW_TAG_vector_type
),
434 MDString::get(VMContext
, ""),
436 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
437 ConstantInt::get(Type::getInt64Ty(VMContext
), Size
),
438 ConstantInt::get(Type::getInt64Ty(VMContext
), AlignInBits
),
439 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
440 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
443 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
444 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext
)),
446 return DIType(MDNode::get(VMContext
, &Elts
[0], array_lengthof(Elts
)));
449 /// createArtificialType - Create a new DIType with "artificial" flag set.
450 DIType
DIBuilder::createArtificialType(DIType Ty
) {
451 if (Ty
.isArtificial())
454 SmallVector
<Value
*, 9> Elts
;
456 assert (N
&& "Unexpected input DIType!");
457 for (unsigned i
= 0, e
= N
->getNumOperands(); i
!= e
; ++i
) {
458 if (Value
*V
= N
->getOperand(i
))
461 Elts
.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext
)));
464 unsigned CurFlags
= Ty
.getFlags();
465 CurFlags
= CurFlags
| DIType::FlagArtificial
;
467 // Flags are stored at this slot.
468 Elts
[8] = ConstantInt::get(Type::getInt32Ty(VMContext
), CurFlags
);
470 return DIType(MDNode::get(VMContext
, Elts
.data(), Elts
.size()));
473 /// retainType - Retain DIType in a module even if it is not referenced
474 /// through debug info anchors.
475 void DIBuilder::retainType(DIType T
) {
476 NamedMDNode
*NMD
= M
.getOrInsertNamedMetadata("llvm.dbg.ty");
480 /// createUnspecifiedParameter - Create unspeicified type descriptor
481 /// for the subroutine type.
482 DIDescriptor
DIBuilder::createUnspecifiedParameter() {
484 GetTagConstant(VMContext
, dwarf::DW_TAG_unspecified_parameters
)
486 return DIDescriptor(MDNode::get(VMContext
, &Elts
[0], 1));
489 /// createTemporaryType - Create a temporary forward-declared type.
490 DIType
DIBuilder::createTemporaryType() {
491 // Give the temporary MDNode a tag. It doesn't matter what tag we
492 // use here as long as DIType accepts it.
493 Value
*Elts
[] = { GetTagConstant(VMContext
, DW_TAG_base_type
) };
494 MDNode
*Node
= MDNode::getTemporary(VMContext
, Elts
, array_lengthof(Elts
));
498 /// createTemporaryType - Create a temporary forward-declared type.
499 DIType
DIBuilder::createTemporaryType(DIFile F
) {
500 // Give the temporary MDNode a tag. It doesn't matter what tag we
501 // use here as long as DIType accepts it.
503 GetTagConstant(VMContext
, DW_TAG_base_type
),
508 MDNode
*Node
= MDNode::getTemporary(VMContext
, Elts
, array_lengthof(Elts
));
512 /// getOrCreateArray - Get a DIArray, create one if required.
513 DIArray
DIBuilder::getOrCreateArray(Value
*const *Elements
, unsigned NumElements
) {
514 if (NumElements
== 0) {
515 Value
*Null
= llvm::Constant::getNullValue(Type::getInt32Ty(VMContext
));
516 return DIArray(MDNode::get(VMContext
, &Null
, 1));
518 return DIArray(MDNode::get(VMContext
, Elements
, NumElements
));
521 /// getOrCreateSubrange - Create a descriptor for a value range. This
522 /// implicitly uniques the values returned.
523 DISubrange
DIBuilder::getOrCreateSubrange(int64_t Lo
, int64_t Hi
) {
525 GetTagConstant(VMContext
, dwarf::DW_TAG_subrange_type
),
526 ConstantInt::get(Type::getInt64Ty(VMContext
), Lo
),
527 ConstantInt::get(Type::getInt64Ty(VMContext
), Hi
)
530 return DISubrange(MDNode::get(VMContext
, &Elts
[0], 3));
533 /// createGlobalVariable - Create a new descriptor for the specified global.
534 DIGlobalVariable
DIBuilder::
535 createGlobalVariable(StringRef Name
, DIFile F
, unsigned LineNumber
,
536 DIType Ty
, bool isLocalToUnit
, llvm::Value
*Val
) {
538 GetTagConstant(VMContext
, dwarf::DW_TAG_variable
),
539 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext
)),
541 MDString::get(VMContext
, Name
),
542 MDString::get(VMContext
, Name
),
543 MDString::get(VMContext
, Name
),
545 ConstantInt::get(Type::getInt32Ty(VMContext
), LineNumber
),
547 ConstantInt::get(Type::getInt32Ty(VMContext
), isLocalToUnit
),
548 ConstantInt::get(Type::getInt32Ty(VMContext
), 1), /* isDefinition*/
551 MDNode
*Node
= MDNode::get(VMContext
, &Elts
[0], array_lengthof(Elts
));
552 // Create a named metadata so that we do not lose this mdnode.
553 NamedMDNode
*NMD
= M
.getOrInsertNamedMetadata("llvm.dbg.gv");
554 NMD
->addOperand(Node
);
555 return DIGlobalVariable(Node
);
558 /// createStaticVariable - Create a new descriptor for the specified static
560 DIGlobalVariable
DIBuilder::
561 createStaticVariable(DIDescriptor Context
, StringRef Name
,
562 StringRef LinkageName
, DIFile F
, unsigned LineNumber
,
563 DIType Ty
, bool isLocalToUnit
, llvm::Value
*Val
) {
565 GetTagConstant(VMContext
, dwarf::DW_TAG_variable
),
566 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext
)),
568 MDString::get(VMContext
, Name
),
569 MDString::get(VMContext
, Name
),
570 MDString::get(VMContext
, LinkageName
),
572 ConstantInt::get(Type::getInt32Ty(VMContext
), LineNumber
),
574 ConstantInt::get(Type::getInt32Ty(VMContext
), isLocalToUnit
),
575 ConstantInt::get(Type::getInt32Ty(VMContext
), 1), /* isDefinition*/
578 MDNode
*Node
= MDNode::get(VMContext
, &Elts
[0], array_lengthof(Elts
));
579 // Create a named metadata so that we do not lose this mdnode.
580 NamedMDNode
*NMD
= M
.getOrInsertNamedMetadata("llvm.dbg.gv");
581 NMD
->addOperand(Node
);
582 return DIGlobalVariable(Node
);
585 /// createVariable - Create a new descriptor for the specified variable.
586 DIVariable
DIBuilder::createLocalVariable(unsigned Tag
, DIDescriptor Scope
,
587 StringRef Name
, DIFile File
,
588 unsigned LineNo
, DIType Ty
,
589 bool AlwaysPreserve
, unsigned Flags
,
592 GetTagConstant(VMContext
, Tag
),
594 MDString::get(VMContext
, Name
),
596 ConstantInt::get(Type::getInt32Ty(VMContext
), (LineNo
| (ArgNo
<< 24))),
598 ConstantInt::get(Type::getInt32Ty(VMContext
), Flags
)
600 MDNode
*Node
= MDNode::get(VMContext
, &Elts
[0], array_lengthof(Elts
));
601 if (AlwaysPreserve
) {
602 // The optimizer may remove local variable. If there is an interest
603 // to preserve variable info in such situation then stash it in a
605 DISubprogram
Fn(getDISubprogram(Scope
));
606 StringRef FName
= "fn";
607 if (Fn
.getFunction())
608 FName
= Fn
.getFunction()->getName();
610 if (FName
.startswith(StringRef(&One
, 1)))
611 FName
= FName
.substr(1);
612 NamedMDNode
*FnLocals
= getOrInsertFnSpecificMDNode(M
, FName
);
613 FnLocals
->addOperand(Node
);
615 return DIVariable(Node
);
618 /// createComplexVariable - Create a new descriptor for the specified variable
619 /// which has a complex address expression for its address.
620 DIVariable
DIBuilder::createComplexVariable(unsigned Tag
, DIDescriptor Scope
,
621 StringRef Name
, DIFile F
,
623 DIType Ty
, Value
*const *Addr
,
624 unsigned NumAddr
, unsigned ArgNo
) {
625 SmallVector
<Value
*, 15> Elts
;
626 Elts
.push_back(GetTagConstant(VMContext
, Tag
));
627 Elts
.push_back(Scope
);
628 Elts
.push_back(MDString::get(VMContext
, Name
));
630 Elts
.push_back(ConstantInt::get(Type::getInt32Ty(VMContext
), (LineNo
| (ArgNo
<< 24))));
632 Elts
.append(Addr
, Addr
+NumAddr
);
634 return DIVariable(MDNode::get(VMContext
, Elts
.data(), Elts
.size()));
637 /// createFunction - Create a new descriptor for the specified function.
638 DISubprogram
DIBuilder::createFunction(DIDescriptor Context
,
640 StringRef LinkageName
,
641 DIFile File
, unsigned LineNo
,
643 bool isLocalToUnit
, bool isDefinition
,
644 unsigned Flags
, bool isOptimized
,
648 GetTagConstant(VMContext
, dwarf::DW_TAG_subprogram
),
649 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext
)),
651 MDString::get(VMContext
, Name
),
652 MDString::get(VMContext
, Name
),
653 MDString::get(VMContext
, LinkageName
),
655 ConstantInt::get(Type::getInt32Ty(VMContext
), LineNo
),
657 ConstantInt::get(Type::getInt1Ty(VMContext
), isLocalToUnit
),
658 ConstantInt::get(Type::getInt1Ty(VMContext
), isDefinition
),
659 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
660 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
661 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext
)),
662 ConstantInt::get(Type::getInt32Ty(VMContext
), Flags
),
663 ConstantInt::get(Type::getInt1Ty(VMContext
), isOptimized
),
667 MDNode
*Node
= MDNode::get(VMContext
, &Elts
[0], array_lengthof(Elts
));
669 // Create a named metadata so that we do not lose this mdnode.
670 NamedMDNode
*NMD
= M
.getOrInsertNamedMetadata("llvm.dbg.sp");
671 NMD
->addOperand(Node
);
672 return DISubprogram(Node
);
675 /// createMethod - Create a new descriptor for the specified C++ method.
676 DISubprogram
DIBuilder::createMethod(DIDescriptor Context
,
678 StringRef LinkageName
,
680 unsigned LineNo
, DIType Ty
,
683 unsigned VK
, unsigned VIndex
,
684 MDNode
*VTableHolder
,
690 GetTagConstant(VMContext
, dwarf::DW_TAG_subprogram
),
691 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext
)),
693 MDString::get(VMContext
, Name
),
694 MDString::get(VMContext
, Name
),
695 MDString::get(VMContext
, LinkageName
),
697 ConstantInt::get(Type::getInt32Ty(VMContext
), LineNo
),
699 ConstantInt::get(Type::getInt1Ty(VMContext
), isLocalToUnit
),
700 ConstantInt::get(Type::getInt1Ty(VMContext
), isDefinition
),
701 ConstantInt::get(Type::getInt32Ty(VMContext
), (unsigned)VK
),
702 ConstantInt::get(Type::getInt32Ty(VMContext
), VIndex
),
704 ConstantInt::get(Type::getInt32Ty(VMContext
), Flags
),
705 ConstantInt::get(Type::getInt1Ty(VMContext
), isOptimized
),
709 MDNode
*Node
= MDNode::get(VMContext
, &Elts
[0], array_lengthof(Elts
));
711 // Create a named metadata so that we do not lose this mdnode.
712 NamedMDNode
*NMD
= M
.getOrInsertNamedMetadata("llvm.dbg.sp");
713 NMD
->addOperand(Node
);
714 return DISubprogram(Node
);
717 /// createNameSpace - This creates new descriptor for a namespace
718 /// with the specified parent scope.
719 DINameSpace
DIBuilder::createNameSpace(DIDescriptor Scope
, StringRef Name
,
720 DIFile File
, unsigned LineNo
) {
722 GetTagConstant(VMContext
, dwarf::DW_TAG_namespace
),
724 MDString::get(VMContext
, Name
),
726 ConstantInt::get(Type::getInt32Ty(VMContext
), LineNo
)
728 return DINameSpace(MDNode::get(VMContext
, &Elts
[0], array_lengthof(Elts
)));
731 DILexicalBlock
DIBuilder::createLexicalBlock(DIDescriptor Scope
, DIFile File
,
732 unsigned Line
, unsigned Col
) {
733 // Defeat MDNode uniqing for lexical blocks by using unique id.
734 static unsigned int unique_id
= 0;
736 GetTagConstant(VMContext
, dwarf::DW_TAG_lexical_block
),
738 ConstantInt::get(Type::getInt32Ty(VMContext
), Line
),
739 ConstantInt::get(Type::getInt32Ty(VMContext
), Col
),
741 ConstantInt::get(Type::getInt32Ty(VMContext
), unique_id
++)
743 return DILexicalBlock(MDNode::get(VMContext
, &Elts
[0], array_lengthof(Elts
)));
746 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
747 Instruction
*DIBuilder::insertDeclare(Value
*Storage
, DIVariable VarInfo
,
748 Instruction
*InsertBefore
) {
749 assert(Storage
&& "no storage passed to dbg.declare");
750 assert(VarInfo
.Verify() && "empty DIVariable passed to dbg.declare");
752 DeclareFn
= Intrinsic::getDeclaration(&M
, Intrinsic::dbg_declare
);
754 Value
*Args
[] = { MDNode::get(Storage
->getContext(), &Storage
, 1), VarInfo
};
755 return CallInst::Create(DeclareFn
, Args
, Args
+2, "", InsertBefore
);
758 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
759 Instruction
*DIBuilder::insertDeclare(Value
*Storage
, DIVariable VarInfo
,
760 BasicBlock
*InsertAtEnd
) {
761 assert(Storage
&& "no storage passed to dbg.declare");
762 assert(VarInfo
.Verify() && "invalid DIVariable passed to dbg.declare");
764 DeclareFn
= Intrinsic::getDeclaration(&M
, Intrinsic::dbg_declare
);
766 Value
*Args
[] = { MDNode::get(Storage
->getContext(), &Storage
, 1), VarInfo
};
768 // If this block already has a terminator then insert this intrinsic
769 // before the terminator.
770 if (TerminatorInst
*T
= InsertAtEnd
->getTerminator())
771 return CallInst::Create(DeclareFn
, Args
, Args
+2, "", T
);
773 return CallInst::Create(DeclareFn
, Args
, Args
+2, "", InsertAtEnd
);
776 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
777 Instruction
*DIBuilder::insertDbgValueIntrinsic(Value
*V
, uint64_t Offset
,
779 Instruction
*InsertBefore
) {
780 assert(V
&& "no value passed to dbg.value");
781 assert(VarInfo
.Verify() && "invalid DIVariable passed to dbg.value");
783 ValueFn
= Intrinsic::getDeclaration(&M
, Intrinsic::dbg_value
);
785 Value
*Args
[] = { MDNode::get(V
->getContext(), &V
, 1),
786 ConstantInt::get(Type::getInt64Ty(V
->getContext()), Offset
),
788 return CallInst::Create(ValueFn
, Args
, Args
+3, "", InsertBefore
);
791 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
792 Instruction
*DIBuilder::insertDbgValueIntrinsic(Value
*V
, uint64_t Offset
,
794 BasicBlock
*InsertAtEnd
) {
795 assert(V
&& "no value passed to dbg.value");
796 assert(VarInfo
.Verify() && "invalid DIVariable passed to dbg.value");
798 ValueFn
= Intrinsic::getDeclaration(&M
, Intrinsic::dbg_value
);
800 Value
*Args
[] = { MDNode::get(V
->getContext(), &V
, 1),
801 ConstantInt::get(Type::getInt64Ty(V
->getContext()), Offset
),
803 return CallInst::Create(ValueFn
, Args
, Args
+3, "", InsertAtEnd
);