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
));
55 // Create a named metadata so that it is easier to find cu in a module.
56 NamedMDNode
*NMD
= M
.getOrInsertNamedMetadata("llvm.dbg.cu");
57 NMD
->addOperand(TheCU
);
60 /// createFile - Create a file descriptor to hold debugging information
62 DIFile
DIBuilder::createFile(StringRef Filename
, StringRef Directory
) {
63 assert(TheCU
&& "Unable to create DW_TAG_file_type without CompileUnit");
65 GetTagConstant(VMContext
, dwarf::DW_TAG_file_type
),
66 MDString::get(VMContext
, Filename
),
67 MDString::get(VMContext
, Directory
),
70 return DIFile(MDNode::get(VMContext
, Elts
));
73 /// createEnumerator - Create a single enumerator value.
74 DIEnumerator
DIBuilder::createEnumerator(StringRef Name
, uint64_t Val
) {
76 GetTagConstant(VMContext
, dwarf::DW_TAG_enumerator
),
77 MDString::get(VMContext
, Name
),
78 ConstantInt::get(Type::getInt64Ty(VMContext
), Val
)
80 return DIEnumerator(MDNode::get(VMContext
, Elts
));
83 /// createBasicType - Create debugging information entry for a basic
85 DIType
DIBuilder::createBasicType(StringRef Name
, uint64_t SizeInBits
,
88 // Basic types are encoded in DIBasicType format. Line number, filename,
89 // offset and flags are always empty here.
91 GetTagConstant(VMContext
, dwarf::DW_TAG_base_type
),
93 MDString::get(VMContext
, Name
),
95 ConstantInt::get(Type::getInt32Ty(VMContext
), 0), // Line
96 ConstantInt::get(Type::getInt64Ty(VMContext
), SizeInBits
),
97 ConstantInt::get(Type::getInt64Ty(VMContext
), AlignInBits
),
98 ConstantInt::get(Type::getInt64Ty(VMContext
), 0), // Offset
99 ConstantInt::get(Type::getInt32Ty(VMContext
), 0), // Flags;
100 ConstantInt::get(Type::getInt32Ty(VMContext
), Encoding
)
102 return DIType(MDNode::get(VMContext
, Elts
));
105 /// createQaulifiedType - Create debugging information entry for a qualified
106 /// type, e.g. 'const int'.
107 DIType
DIBuilder::createQualifiedType(unsigned Tag
, DIType FromTy
) {
108 // Qualified types are encoded in DIDerivedType format.
110 GetTagConstant(VMContext
, Tag
),
112 MDString::get(VMContext
, StringRef()), // Empty name.
114 ConstantInt::get(Type::getInt32Ty(VMContext
), 0), // Line
115 ConstantInt::get(Type::getInt64Ty(VMContext
), 0), // Size
116 ConstantInt::get(Type::getInt64Ty(VMContext
), 0), // Align
117 ConstantInt::get(Type::getInt64Ty(VMContext
), 0), // Offset
118 ConstantInt::get(Type::getInt32Ty(VMContext
), 0), // Flags
121 return DIType(MDNode::get(VMContext
, Elts
));
124 /// createPointerType - Create debugging information entry for a pointer.
125 DIType
DIBuilder::createPointerType(DIType PointeeTy
, uint64_t SizeInBits
,
126 uint64_t AlignInBits
, StringRef Name
) {
127 // Pointer types are encoded in DIDerivedType format.
129 GetTagConstant(VMContext
, dwarf::DW_TAG_pointer_type
),
131 MDString::get(VMContext
, Name
),
133 ConstantInt::get(Type::getInt32Ty(VMContext
), 0), // Line
134 ConstantInt::get(Type::getInt64Ty(VMContext
), SizeInBits
),
135 ConstantInt::get(Type::getInt64Ty(VMContext
), AlignInBits
),
136 ConstantInt::get(Type::getInt64Ty(VMContext
), 0), // Offset
137 ConstantInt::get(Type::getInt32Ty(VMContext
), 0), // Flags
140 return DIType(MDNode::get(VMContext
, Elts
));
143 /// createReferenceType - Create debugging information entry for a reference.
144 DIType
DIBuilder::createReferenceType(DIType RTy
) {
145 // References are encoded in DIDerivedType format.
147 GetTagConstant(VMContext
, dwarf::DW_TAG_reference_type
),
151 ConstantInt::get(Type::getInt32Ty(VMContext
), 0), // Line
152 ConstantInt::get(Type::getInt64Ty(VMContext
), 0), // Size
153 ConstantInt::get(Type::getInt64Ty(VMContext
), 0), // Align
154 ConstantInt::get(Type::getInt64Ty(VMContext
), 0), // Offset
155 ConstantInt::get(Type::getInt32Ty(VMContext
), 0), // Flags
158 return DIType(MDNode::get(VMContext
, Elts
));
161 /// createTypedef - Create debugging information entry for a typedef.
162 DIType
DIBuilder::createTypedef(DIType Ty
, StringRef Name
, DIFile File
,
163 unsigned LineNo
, DIDescriptor Context
) {
164 // typedefs are encoded in DIDerivedType format.
165 assert(Ty
.Verify() && "Invalid typedef type!");
167 GetTagConstant(VMContext
, dwarf::DW_TAG_typedef
),
169 MDString::get(VMContext
, Name
),
171 ConstantInt::get(Type::getInt32Ty(VMContext
), LineNo
),
172 ConstantInt::get(Type::getInt64Ty(VMContext
), 0), // Size
173 ConstantInt::get(Type::getInt64Ty(VMContext
), 0), // Align
174 ConstantInt::get(Type::getInt64Ty(VMContext
), 0), // Offset
175 ConstantInt::get(Type::getInt32Ty(VMContext
), 0), // Flags
178 return DIType(MDNode::get(VMContext
, Elts
));
181 /// createFriend - Create debugging information entry for a 'friend'.
182 DIType
DIBuilder::createFriend(DIType Ty
, DIType FriendTy
) {
183 // typedefs are encoded in DIDerivedType format.
184 assert(Ty
.Verify() && "Invalid type!");
185 assert(FriendTy
.Verify() && "Invalid friend type!");
187 GetTagConstant(VMContext
, dwarf::DW_TAG_friend
),
191 ConstantInt::get(Type::getInt32Ty(VMContext
), 0), // Line
192 ConstantInt::get(Type::getInt64Ty(VMContext
), 0), // Size
193 ConstantInt::get(Type::getInt64Ty(VMContext
), 0), // Align
194 ConstantInt::get(Type::getInt64Ty(VMContext
), 0), // Offset
195 ConstantInt::get(Type::getInt32Ty(VMContext
), 0), // Flags
198 return DIType(MDNode::get(VMContext
, Elts
));
201 /// createInheritance - Create debugging information entry to establish
202 /// inheritnace relationship between two types.
203 DIType
DIBuilder::createInheritance(DIType Ty
, DIType BaseTy
,
204 uint64_t BaseOffset
, unsigned Flags
) {
205 // TAG_inheritance is encoded in DIDerivedType format.
207 GetTagConstant(VMContext
, dwarf::DW_TAG_inheritance
),
211 ConstantInt::get(Type::getInt32Ty(VMContext
), 0), // Line
212 ConstantInt::get(Type::getInt64Ty(VMContext
), 0), // Size
213 ConstantInt::get(Type::getInt64Ty(VMContext
), 0), // Align
214 ConstantInt::get(Type::getInt64Ty(VMContext
), BaseOffset
),
215 ConstantInt::get(Type::getInt32Ty(VMContext
), Flags
),
218 return DIType(MDNode::get(VMContext
, Elts
));
221 /// createMemberType - Create debugging information entry for a member.
222 DIType
DIBuilder::createMemberType(DIDescriptor Scope
, StringRef Name
,
223 DIFile File
, unsigned LineNumber
,
224 uint64_t SizeInBits
, uint64_t AlignInBits
,
225 uint64_t OffsetInBits
, unsigned Flags
,
227 // TAG_member is encoded in DIDerivedType format.
229 GetTagConstant(VMContext
, dwarf::DW_TAG_member
),
231 MDString::get(VMContext
, Name
),
233 ConstantInt::get(Type::getInt32Ty(VMContext
), LineNumber
),
234 ConstantInt::get(Type::getInt64Ty(VMContext
), SizeInBits
),
235 ConstantInt::get(Type::getInt64Ty(VMContext
), AlignInBits
),
236 ConstantInt::get(Type::getInt64Ty(VMContext
), OffsetInBits
),
237 ConstantInt::get(Type::getInt32Ty(VMContext
), Flags
),
240 return DIType(MDNode::get(VMContext
, Elts
));
243 /// createObjCIVar - Create debugging information entry for Objective-C
244 /// instance variable.
245 DIType
DIBuilder::createObjCIVar(StringRef Name
,
246 DIFile File
, unsigned LineNumber
,
247 uint64_t SizeInBits
, uint64_t AlignInBits
,
248 uint64_t OffsetInBits
, unsigned Flags
,
249 DIType Ty
, StringRef PropertyName
,
250 StringRef GetterName
, StringRef SetterName
,
251 unsigned PropertyAttributes
) {
252 // TAG_member is encoded in DIDerivedType format.
254 GetTagConstant(VMContext
, dwarf::DW_TAG_member
),
255 File
, // Or TheCU ? Ty ?
256 MDString::get(VMContext
, Name
),
258 ConstantInt::get(Type::getInt32Ty(VMContext
), LineNumber
),
259 ConstantInt::get(Type::getInt64Ty(VMContext
), SizeInBits
),
260 ConstantInt::get(Type::getInt64Ty(VMContext
), AlignInBits
),
261 ConstantInt::get(Type::getInt64Ty(VMContext
), OffsetInBits
),
262 ConstantInt::get(Type::getInt32Ty(VMContext
), Flags
),
264 MDString::get(VMContext
, PropertyName
),
265 MDString::get(VMContext
, GetterName
),
266 MDString::get(VMContext
, SetterName
),
267 ConstantInt::get(Type::getInt32Ty(VMContext
), PropertyAttributes
)
269 return DIType(MDNode::get(VMContext
, Elts
));
272 /// createClassType - Create debugging information entry for a class.
273 DIType
DIBuilder::createClassType(DIDescriptor Context
, StringRef Name
,
274 DIFile File
, unsigned LineNumber
,
275 uint64_t SizeInBits
, uint64_t AlignInBits
,
276 uint64_t OffsetInBits
, unsigned Flags
,
277 DIType DerivedFrom
, DIArray Elements
,
278 MDNode
*VTableHoder
, MDNode
*TemplateParams
) {
279 // TAG_class_type is encoded in DICompositeType format.
281 GetTagConstant(VMContext
, dwarf::DW_TAG_class_type
),
283 MDString::get(VMContext
, Name
),
285 ConstantInt::get(Type::getInt32Ty(VMContext
), LineNumber
),
286 ConstantInt::get(Type::getInt64Ty(VMContext
), SizeInBits
),
287 ConstantInt::get(Type::getInt64Ty(VMContext
), AlignInBits
),
288 ConstantInt::get(Type::getInt32Ty(VMContext
), OffsetInBits
),
289 ConstantInt::get(Type::getInt32Ty(VMContext
), Flags
),
292 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
296 return DIType(MDNode::get(VMContext
, Elts
));
299 /// createTemplateTypeParameter - Create debugging information for template
301 DITemplateTypeParameter
302 DIBuilder::createTemplateTypeParameter(DIDescriptor Context
, StringRef Name
,
303 DIType Ty
, MDNode
*File
, unsigned LineNo
,
306 GetTagConstant(VMContext
, dwarf::DW_TAG_template_type_parameter
),
308 MDString::get(VMContext
, Name
),
311 ConstantInt::get(Type::getInt32Ty(VMContext
), LineNo
),
312 ConstantInt::get(Type::getInt32Ty(VMContext
), ColumnNo
)
314 return DITemplateTypeParameter(MDNode::get(VMContext
, Elts
));
317 /// createTemplateValueParameter - Create debugging information for template
319 DITemplateValueParameter
320 DIBuilder::createTemplateValueParameter(DIDescriptor Context
, StringRef Name
,
321 DIType Ty
, uint64_t Val
,
322 MDNode
*File
, unsigned LineNo
,
325 GetTagConstant(VMContext
, dwarf::DW_TAG_template_value_parameter
),
327 MDString::get(VMContext
, Name
),
329 ConstantInt::get(Type::getInt64Ty(VMContext
), Val
),
331 ConstantInt::get(Type::getInt32Ty(VMContext
), LineNo
),
332 ConstantInt::get(Type::getInt32Ty(VMContext
), ColumnNo
)
334 return DITemplateValueParameter(MDNode::get(VMContext
, Elts
));
337 /// createStructType - Create debugging information entry for a struct.
338 DIType
DIBuilder::createStructType(DIDescriptor Context
, StringRef Name
,
339 DIFile File
, unsigned LineNumber
,
340 uint64_t SizeInBits
, uint64_t AlignInBits
,
341 unsigned Flags
, DIArray Elements
,
342 unsigned RunTimeLang
) {
343 // TAG_structure_type is encoded in DICompositeType format.
345 GetTagConstant(VMContext
, dwarf::DW_TAG_structure_type
),
347 MDString::get(VMContext
, Name
),
349 ConstantInt::get(Type::getInt32Ty(VMContext
), LineNumber
),
350 ConstantInt::get(Type::getInt64Ty(VMContext
), SizeInBits
),
351 ConstantInt::get(Type::getInt64Ty(VMContext
), AlignInBits
),
352 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
353 ConstantInt::get(Type::getInt32Ty(VMContext
), Flags
),
354 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext
)),
356 ConstantInt::get(Type::getInt32Ty(VMContext
), RunTimeLang
),
357 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext
)),
359 return DIType(MDNode::get(VMContext
, Elts
));
362 /// createUnionType - Create debugging information entry for an union.
363 DIType
DIBuilder::createUnionType(DIDescriptor Scope
, StringRef Name
,
365 unsigned LineNumber
, uint64_t SizeInBits
,
366 uint64_t AlignInBits
, unsigned Flags
,
367 DIArray Elements
, unsigned RunTimeLang
) {
368 // TAG_union_type is encoded in DICompositeType format.
370 GetTagConstant(VMContext
, dwarf::DW_TAG_union_type
),
372 MDString::get(VMContext
, Name
),
374 ConstantInt::get(Type::getInt32Ty(VMContext
), LineNumber
),
375 ConstantInt::get(Type::getInt64Ty(VMContext
), SizeInBits
),
376 ConstantInt::get(Type::getInt64Ty(VMContext
), AlignInBits
),
377 ConstantInt::get(Type::getInt64Ty(VMContext
), 0),
378 ConstantInt::get(Type::getInt32Ty(VMContext
), Flags
),
379 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext
)),
381 ConstantInt::get(Type::getInt32Ty(VMContext
), RunTimeLang
),
382 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext
)),
384 return DIType(MDNode::get(VMContext
, Elts
));
387 /// createSubroutineType - Create subroutine type.
388 DIType
DIBuilder::createSubroutineType(DIFile File
, DIArray ParameterTypes
) {
389 // TAG_subroutine_type is encoded in DICompositeType format.
391 GetTagConstant(VMContext
, dwarf::DW_TAG_subroutine_type
),
393 MDString::get(VMContext
, ""),
395 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
396 ConstantInt::get(Type::getInt64Ty(VMContext
), 0),
397 ConstantInt::get(Type::getInt64Ty(VMContext
), 0),
398 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
399 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
400 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext
)),
402 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
403 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext
)),
405 return DIType(MDNode::get(VMContext
, Elts
));
408 /// createEnumerationType - Create debugging information entry for an
410 DIType
DIBuilder::createEnumerationType(DIDescriptor Scope
, StringRef Name
,
411 DIFile File
, unsigned LineNumber
,
413 uint64_t AlignInBits
, DIArray Elements
) {
414 // TAG_enumeration_type is encoded in DICompositeType format.
416 GetTagConstant(VMContext
, dwarf::DW_TAG_enumeration_type
),
418 MDString::get(VMContext
, Name
),
420 ConstantInt::get(Type::getInt32Ty(VMContext
), LineNumber
),
421 ConstantInt::get(Type::getInt64Ty(VMContext
), SizeInBits
),
422 ConstantInt::get(Type::getInt64Ty(VMContext
), AlignInBits
),
423 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
424 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
425 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext
)),
427 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
428 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext
)),
430 MDNode
*Node
= MDNode::get(VMContext
, Elts
);
431 NamedMDNode
*NMD
= M
.getOrInsertNamedMetadata("llvm.dbg.enum");
432 NMD
->addOperand(Node
);
436 /// createArrayType - Create debugging information entry for an array.
437 DIType
DIBuilder::createArrayType(uint64_t Size
, uint64_t AlignInBits
,
438 DIType Ty
, DIArray Subscripts
) {
439 // TAG_array_type is encoded in DICompositeType format.
441 GetTagConstant(VMContext
, dwarf::DW_TAG_array_type
),
443 MDString::get(VMContext
, ""),
445 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
446 ConstantInt::get(Type::getInt64Ty(VMContext
), Size
),
447 ConstantInt::get(Type::getInt64Ty(VMContext
), AlignInBits
),
448 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
449 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
452 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
453 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext
)),
455 return DIType(MDNode::get(VMContext
, Elts
));
458 /// createVectorType - Create debugging information entry for a vector.
459 DIType
DIBuilder::createVectorType(uint64_t Size
, uint64_t AlignInBits
,
460 DIType Ty
, DIArray Subscripts
) {
461 // TAG_vector_type is encoded in DICompositeType format.
463 GetTagConstant(VMContext
, dwarf::DW_TAG_vector_type
),
465 MDString::get(VMContext
, ""),
467 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
468 ConstantInt::get(Type::getInt64Ty(VMContext
), Size
),
469 ConstantInt::get(Type::getInt64Ty(VMContext
), AlignInBits
),
470 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
471 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
474 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
475 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext
)),
477 return DIType(MDNode::get(VMContext
, Elts
));
480 /// createArtificialType - Create a new DIType with "artificial" flag set.
481 DIType
DIBuilder::createArtificialType(DIType Ty
) {
482 if (Ty
.isArtificial())
485 SmallVector
<Value
*, 9> Elts
;
487 assert (N
&& "Unexpected input DIType!");
488 for (unsigned i
= 0, e
= N
->getNumOperands(); i
!= e
; ++i
) {
489 if (Value
*V
= N
->getOperand(i
))
492 Elts
.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext
)));
495 unsigned CurFlags
= Ty
.getFlags();
496 CurFlags
= CurFlags
| DIType::FlagArtificial
;
498 // Flags are stored at this slot.
499 Elts
[8] = ConstantInt::get(Type::getInt32Ty(VMContext
), CurFlags
);
501 return DIType(MDNode::get(VMContext
, Elts
));
504 /// retainType - Retain DIType in a module even if it is not referenced
505 /// through debug info anchors.
506 void DIBuilder::retainType(DIType T
) {
507 NamedMDNode
*NMD
= M
.getOrInsertNamedMetadata("llvm.dbg.ty");
511 /// createUnspecifiedParameter - Create unspeicified type descriptor
512 /// for the subroutine type.
513 DIDescriptor
DIBuilder::createUnspecifiedParameter() {
515 GetTagConstant(VMContext
, dwarf::DW_TAG_unspecified_parameters
)
517 return DIDescriptor(MDNode::get(VMContext
, Elts
));
520 /// createTemporaryType - Create a temporary forward-declared type.
521 DIType
DIBuilder::createTemporaryType() {
522 // Give the temporary MDNode a tag. It doesn't matter what tag we
523 // use here as long as DIType accepts it.
524 Value
*Elts
[] = { GetTagConstant(VMContext
, DW_TAG_base_type
) };
525 MDNode
*Node
= MDNode::getTemporary(VMContext
, Elts
);
529 /// createTemporaryType - Create a temporary forward-declared type.
530 DIType
DIBuilder::createTemporaryType(DIFile F
) {
531 // Give the temporary MDNode a tag. It doesn't matter what tag we
532 // use here as long as DIType accepts it.
534 GetTagConstant(VMContext
, DW_TAG_base_type
),
539 MDNode
*Node
= MDNode::getTemporary(VMContext
, Elts
);
543 /// getOrCreateArray - Get a DIArray, create one if required.
544 DIArray
DIBuilder::getOrCreateArray(ArrayRef
<Value
*> Elements
) {
545 if (Elements
.empty()) {
546 Value
*Null
= llvm::Constant::getNullValue(Type::getInt32Ty(VMContext
));
547 return DIArray(MDNode::get(VMContext
, Null
));
549 return DIArray(MDNode::get(VMContext
, Elements
));
552 /// getOrCreateSubrange - Create a descriptor for a value range. This
553 /// implicitly uniques the values returned.
554 DISubrange
DIBuilder::getOrCreateSubrange(int64_t Lo
, int64_t Hi
) {
556 GetTagConstant(VMContext
, dwarf::DW_TAG_subrange_type
),
557 ConstantInt::get(Type::getInt64Ty(VMContext
), Lo
),
558 ConstantInt::get(Type::getInt64Ty(VMContext
), Hi
)
561 return DISubrange(MDNode::get(VMContext
, Elts
));
564 /// createGlobalVariable - Create a new descriptor for the specified global.
565 DIGlobalVariable
DIBuilder::
566 createGlobalVariable(StringRef Name
, DIFile F
, unsigned LineNumber
,
567 DIType Ty
, bool isLocalToUnit
, llvm::Value
*Val
) {
569 GetTagConstant(VMContext
, dwarf::DW_TAG_variable
),
570 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext
)),
572 MDString::get(VMContext
, Name
),
573 MDString::get(VMContext
, Name
),
574 MDString::get(VMContext
, Name
),
576 ConstantInt::get(Type::getInt32Ty(VMContext
), LineNumber
),
578 ConstantInt::get(Type::getInt32Ty(VMContext
), isLocalToUnit
),
579 ConstantInt::get(Type::getInt32Ty(VMContext
), 1), /* isDefinition*/
582 MDNode
*Node
= MDNode::get(VMContext
, Elts
);
583 // Create a named metadata so that we do not lose this mdnode.
584 NamedMDNode
*NMD
= M
.getOrInsertNamedMetadata("llvm.dbg.gv");
585 NMD
->addOperand(Node
);
586 return DIGlobalVariable(Node
);
589 /// createStaticVariable - Create a new descriptor for the specified static
591 DIGlobalVariable
DIBuilder::
592 createStaticVariable(DIDescriptor Context
, StringRef Name
,
593 StringRef LinkageName
, DIFile F
, unsigned LineNumber
,
594 DIType Ty
, bool isLocalToUnit
, llvm::Value
*Val
) {
596 GetTagConstant(VMContext
, dwarf::DW_TAG_variable
),
597 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext
)),
599 MDString::get(VMContext
, Name
),
600 MDString::get(VMContext
, Name
),
601 MDString::get(VMContext
, LinkageName
),
603 ConstantInt::get(Type::getInt32Ty(VMContext
), LineNumber
),
605 ConstantInt::get(Type::getInt32Ty(VMContext
), isLocalToUnit
),
606 ConstantInt::get(Type::getInt32Ty(VMContext
), 1), /* isDefinition*/
609 MDNode
*Node
= MDNode::get(VMContext
, Elts
);
610 // Create a named metadata so that we do not lose this mdnode.
611 NamedMDNode
*NMD
= M
.getOrInsertNamedMetadata("llvm.dbg.gv");
612 NMD
->addOperand(Node
);
613 return DIGlobalVariable(Node
);
616 /// createVariable - Create a new descriptor for the specified variable.
617 DIVariable
DIBuilder::createLocalVariable(unsigned Tag
, DIDescriptor Scope
,
618 StringRef Name
, DIFile File
,
619 unsigned LineNo
, DIType Ty
,
620 bool AlwaysPreserve
, unsigned Flags
,
623 GetTagConstant(VMContext
, Tag
),
625 MDString::get(VMContext
, Name
),
627 ConstantInt::get(Type::getInt32Ty(VMContext
), (LineNo
| (ArgNo
<< 24))),
629 ConstantInt::get(Type::getInt32Ty(VMContext
), Flags
)
631 MDNode
*Node
= MDNode::get(VMContext
, Elts
);
632 if (AlwaysPreserve
) {
633 // The optimizer may remove local variable. If there is an interest
634 // to preserve variable info in such situation then stash it in a
636 DISubprogram
Fn(getDISubprogram(Scope
));
637 StringRef FName
= "fn";
638 if (Fn
.getFunction())
639 FName
= Fn
.getFunction()->getName();
641 if (FName
.startswith(StringRef(&One
, 1)))
642 FName
= FName
.substr(1);
643 NamedMDNode
*FnLocals
= getOrInsertFnSpecificMDNode(M
, FName
);
644 FnLocals
->addOperand(Node
);
646 return DIVariable(Node
);
649 /// createComplexVariable - Create a new descriptor for the specified variable
650 /// which has a complex address expression for its address.
651 DIVariable
DIBuilder::createComplexVariable(unsigned Tag
, DIDescriptor Scope
,
652 StringRef Name
, DIFile F
,
654 DIType Ty
, ArrayRef
<Value
*> Addr
,
656 SmallVector
<Value
*, 15> Elts
;
657 Elts
.push_back(GetTagConstant(VMContext
, Tag
));
658 Elts
.push_back(Scope
);
659 Elts
.push_back(MDString::get(VMContext
, Name
));
661 Elts
.push_back(ConstantInt::get(Type::getInt32Ty(VMContext
), (LineNo
| (ArgNo
<< 24))));
663 Elts
.push_back(llvm::Constant::getNullValue(Type::getInt32Ty(VMContext
)));
664 Elts
.append(Addr
.begin(), Addr
.end());
666 return DIVariable(MDNode::get(VMContext
, Elts
));
669 /// createFunction - Create a new descriptor for the specified function.
670 DISubprogram
DIBuilder::createFunction(DIDescriptor Context
,
672 StringRef LinkageName
,
673 DIFile File
, unsigned LineNo
,
675 bool isLocalToUnit
, bool isDefinition
,
676 unsigned Flags
, bool isOptimized
,
681 GetTagConstant(VMContext
, dwarf::DW_TAG_subprogram
),
682 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext
)),
684 MDString::get(VMContext
, Name
),
685 MDString::get(VMContext
, Name
),
686 MDString::get(VMContext
, LinkageName
),
688 ConstantInt::get(Type::getInt32Ty(VMContext
), LineNo
),
690 ConstantInt::get(Type::getInt1Ty(VMContext
), isLocalToUnit
),
691 ConstantInt::get(Type::getInt1Ty(VMContext
), isDefinition
),
692 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
693 ConstantInt::get(Type::getInt32Ty(VMContext
), 0),
694 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext
)),
695 ConstantInt::get(Type::getInt32Ty(VMContext
), Flags
),
696 ConstantInt::get(Type::getInt1Ty(VMContext
), isOptimized
),
701 MDNode
*Node
= MDNode::get(VMContext
, Elts
);
703 // Create a named metadata so that we do not lose this mdnode.
704 NamedMDNode
*NMD
= M
.getOrInsertNamedMetadata("llvm.dbg.sp");
705 NMD
->addOperand(Node
);
706 return DISubprogram(Node
);
709 /// createMethod - Create a new descriptor for the specified C++ method.
710 DISubprogram
DIBuilder::createMethod(DIDescriptor Context
,
712 StringRef LinkageName
,
714 unsigned LineNo
, DIType Ty
,
717 unsigned VK
, unsigned VIndex
,
718 MDNode
*VTableHolder
,
724 GetTagConstant(VMContext
, dwarf::DW_TAG_subprogram
),
725 llvm::Constant::getNullValue(Type::getInt32Ty(VMContext
)),
727 MDString::get(VMContext
, Name
),
728 MDString::get(VMContext
, Name
),
729 MDString::get(VMContext
, LinkageName
),
731 ConstantInt::get(Type::getInt32Ty(VMContext
), LineNo
),
733 ConstantInt::get(Type::getInt1Ty(VMContext
), isLocalToUnit
),
734 ConstantInt::get(Type::getInt1Ty(VMContext
), isDefinition
),
735 ConstantInt::get(Type::getInt32Ty(VMContext
), (unsigned)VK
),
736 ConstantInt::get(Type::getInt32Ty(VMContext
), VIndex
),
738 ConstantInt::get(Type::getInt32Ty(VMContext
), Flags
),
739 ConstantInt::get(Type::getInt1Ty(VMContext
), isOptimized
),
743 MDNode
*Node
= MDNode::get(VMContext
, Elts
);
745 // Create a named metadata so that we do not lose this mdnode.
746 NamedMDNode
*NMD
= M
.getOrInsertNamedMetadata("llvm.dbg.sp");
747 NMD
->addOperand(Node
);
748 return DISubprogram(Node
);
751 /// createNameSpace - This creates new descriptor for a namespace
752 /// with the specified parent scope.
753 DINameSpace
DIBuilder::createNameSpace(DIDescriptor Scope
, StringRef Name
,
754 DIFile File
, unsigned LineNo
) {
756 GetTagConstant(VMContext
, dwarf::DW_TAG_namespace
),
758 MDString::get(VMContext
, Name
),
760 ConstantInt::get(Type::getInt32Ty(VMContext
), LineNo
)
762 return DINameSpace(MDNode::get(VMContext
, Elts
));
765 DILexicalBlock
DIBuilder::createLexicalBlock(DIDescriptor Scope
, DIFile File
,
766 unsigned Line
, unsigned Col
) {
767 // Defeat MDNode uniqing for lexical blocks by using unique id.
768 static unsigned int unique_id
= 0;
770 GetTagConstant(VMContext
, dwarf::DW_TAG_lexical_block
),
772 ConstantInt::get(Type::getInt32Ty(VMContext
), Line
),
773 ConstantInt::get(Type::getInt32Ty(VMContext
), Col
),
775 ConstantInt::get(Type::getInt32Ty(VMContext
), unique_id
++)
777 return DILexicalBlock(MDNode::get(VMContext
, Elts
));
780 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
781 Instruction
*DIBuilder::insertDeclare(Value
*Storage
, DIVariable VarInfo
,
782 Instruction
*InsertBefore
) {
783 assert(Storage
&& "no storage passed to dbg.declare");
784 assert(VarInfo
.Verify() && "empty DIVariable passed to dbg.declare");
786 DeclareFn
= Intrinsic::getDeclaration(&M
, Intrinsic::dbg_declare
);
788 Value
*Args
[] = { MDNode::get(Storage
->getContext(), Storage
), VarInfo
};
789 return CallInst::Create(DeclareFn
, Args
, Args
+2, "", InsertBefore
);
792 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
793 Instruction
*DIBuilder::insertDeclare(Value
*Storage
, DIVariable VarInfo
,
794 BasicBlock
*InsertAtEnd
) {
795 assert(Storage
&& "no storage passed to dbg.declare");
796 assert(VarInfo
.Verify() && "invalid DIVariable passed to dbg.declare");
798 DeclareFn
= Intrinsic::getDeclaration(&M
, Intrinsic::dbg_declare
);
800 Value
*Args
[] = { MDNode::get(Storage
->getContext(), Storage
), VarInfo
};
802 // If this block already has a terminator then insert this intrinsic
803 // before the terminator.
804 if (TerminatorInst
*T
= InsertAtEnd
->getTerminator())
805 return CallInst::Create(DeclareFn
, Args
, Args
+2, "", T
);
807 return CallInst::Create(DeclareFn
, Args
, Args
+2, "", InsertAtEnd
);
810 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
811 Instruction
*DIBuilder::insertDbgValueIntrinsic(Value
*V
, uint64_t Offset
,
813 Instruction
*InsertBefore
) {
814 assert(V
&& "no value passed to dbg.value");
815 assert(VarInfo
.Verify() && "invalid DIVariable passed to dbg.value");
817 ValueFn
= Intrinsic::getDeclaration(&M
, Intrinsic::dbg_value
);
819 Value
*Args
[] = { MDNode::get(V
->getContext(), V
),
820 ConstantInt::get(Type::getInt64Ty(V
->getContext()), Offset
),
822 return CallInst::Create(ValueFn
, Args
, Args
+3, "", InsertBefore
);
825 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
826 Instruction
*DIBuilder::insertDbgValueIntrinsic(Value
*V
, uint64_t Offset
,
828 BasicBlock
*InsertAtEnd
) {
829 assert(V
&& "no value passed to dbg.value");
830 assert(VarInfo
.Verify() && "invalid DIVariable passed to dbg.value");
832 ValueFn
= Intrinsic::getDeclaration(&M
, Intrinsic::dbg_value
);
834 Value
*Args
[] = { MDNode::get(V
->getContext(), V
),
835 ConstantInt::get(Type::getInt64Ty(V
->getContext()), Offset
),
837 return CallInst::Create(ValueFn
, Args
, Args
+3, "", InsertAtEnd
);