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/DIBuilder.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/DebugInfo.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/IntrinsicInst.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/Dwarf.h"
24 using namespace llvm::dwarf;
26 static Constant *GetTagConstant(LLVMContext &VMContext, unsigned Tag) {
27 assert((Tag & LLVMDebugVersionMask) == 0 &&
28 "Tag too large for debug encoding!");
29 return ConstantInt::get(Type::getInt32Ty(VMContext), Tag | LLVMDebugVersion);
32 DIBuilder::DIBuilder(Module &m)
33 : M(m), VMContext(M.getContext()), TheCU(0), TempEnumTypes(0),
34 TempRetainTypes(0), TempSubprograms(0), TempGVs(0), DeclareFn(0),
38 /// finalize - Construct any deferred debug info descriptors.
39 void DIBuilder::finalize() {
40 DIArray Enums = getOrCreateArray(AllEnumTypes);
41 DIType(TempEnumTypes).replaceAllUsesWith(Enums);
43 DIArray RetainTypes = getOrCreateArray(AllRetainTypes);
44 DIType(TempRetainTypes).replaceAllUsesWith(RetainTypes);
46 DIArray SPs = getOrCreateArray(AllSubprograms);
47 DIType(TempSubprograms).replaceAllUsesWith(SPs);
48 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
49 DISubprogram SP(SPs.getElement(i));
50 SmallVector<Value *, 4> Variables;
51 if (NamedMDNode *NMD = getFnSpecificMDNode(M, SP)) {
52 for (unsigned ii = 0, ee = NMD->getNumOperands(); ii != ee; ++ii)
53 Variables.push_back(NMD->getOperand(ii));
54 NMD->eraseFromParent();
56 if (MDNode *Temp = SP.getVariablesNodes()) {
57 DIArray AV = getOrCreateArray(Variables);
58 DIType(Temp).replaceAllUsesWith(AV);
62 DIArray GVs = getOrCreateArray(AllGVs);
63 DIType(TempGVs).replaceAllUsesWith(GVs);
66 /// getNonCompileUnitScope - If N is compile unit return NULL otherwise return
68 static MDNode *getNonCompileUnitScope(MDNode *N) {
69 if (DIDescriptor(N).isCompileUnit())
74 /// createCompileUnit - A CompileUnit provides an anchor for all debugging
75 /// information generated during this instance of compilation.
76 void DIBuilder::createCompileUnit(unsigned Lang, StringRef Filename,
77 StringRef Directory, StringRef Producer,
78 bool isOptimized, StringRef Flags,
79 unsigned RunTimeVer, StringRef SplitName) {
80 assert(((Lang <= dwarf::DW_LANG_Python && Lang >= dwarf::DW_LANG_C89) ||
81 (Lang <= dwarf::DW_LANG_hi_user && Lang >= dwarf::DW_LANG_lo_user)) &&
82 "Invalid Language tag");
83 assert(!Filename.empty() &&
84 "Unable to create compile unit without filename");
85 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
86 TempEnumTypes = MDNode::getTemporary(VMContext, TElts);
88 TempRetainTypes = MDNode::getTemporary(VMContext, TElts);
90 TempSubprograms = MDNode::getTemporary(VMContext, TElts);
92 TempGVs = MDNode::getTemporary(VMContext, TElts);
95 GetTagConstant(VMContext, dwarf::DW_TAG_compile_unit),
96 Constant::getNullValue(Type::getInt32Ty(VMContext)),
97 ConstantInt::get(Type::getInt32Ty(VMContext), Lang),
98 createFile(Filename, Directory),
99 MDString::get(VMContext, Producer),
100 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
101 MDString::get(VMContext, Flags),
102 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer),
107 MDString::get(VMContext, SplitName)
109 TheCU = DICompileUnit(MDNode::get(VMContext, Elts));
111 // Create a named metadata so that it is easier to find cu in a module.
112 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
113 NMD->addOperand(TheCU);
116 /// createFile - Create a file descriptor to hold debugging information
118 DIFile DIBuilder::createFile(StringRef Filename, StringRef Directory) {
119 assert(!Filename.empty() && "Unable to create file without name");
121 MDString::get(VMContext, Filename),
122 MDString::get(VMContext, Directory),
125 GetTagConstant(VMContext, dwarf::DW_TAG_file_type),
126 MDNode::get(VMContext, Pair)
128 return DIFile(MDNode::get(VMContext, Elts));
131 /// createEnumerator - Create a single enumerator value.
132 DIEnumerator DIBuilder::createEnumerator(StringRef Name, uint64_t Val) {
133 assert(!Name.empty() && "Unable to create enumerator without name");
135 GetTagConstant(VMContext, dwarf::DW_TAG_enumerator),
136 MDString::get(VMContext, Name),
137 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
139 return DIEnumerator(MDNode::get(VMContext, Elts));
142 /// createNullPtrType - Create C++0x nullptr type.
143 DIType DIBuilder::createNullPtrType(StringRef Name) {
144 assert(!Name.empty() && "Unable to create type without name");
145 // nullptr is encoded in DIBasicType format. Line number, filename,
146 // ,size, alignment, offset and flags are always empty here.
148 GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_type),
150 MDString::get(VMContext, Name),
152 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
153 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
154 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
155 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
156 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
157 ConstantInt::get(Type::getInt32Ty(VMContext), 0) // Encoding
159 return DIType(MDNode::get(VMContext, Elts));
162 /// createBasicType - Create debugging information entry for a basic
163 /// type, e.g 'char'.
165 DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
166 uint64_t AlignInBits, unsigned Encoding) {
167 assert(!Name.empty() && "Unable to create type without name");
168 // Basic types are encoded in DIBasicType format. Line number, filename,
169 // offset and flags are always empty here.
171 GetTagConstant(VMContext, dwarf::DW_TAG_base_type),
173 MDString::get(VMContext, Name),
175 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
176 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
177 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
178 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
179 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
180 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
182 return DIBasicType(MDNode::get(VMContext, Elts));
185 /// createQualifiedType - Create debugging information entry for a qualified
186 /// type, e.g. 'const int'.
187 DIDerivedType DIBuilder::createQualifiedType(unsigned Tag, DIType FromTy) {
188 // Qualified types are encoded in DIDerivedType format.
190 GetTagConstant(VMContext, Tag),
192 MDString::get(VMContext, StringRef()), // Empty name.
194 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
195 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
196 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
197 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
198 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
201 return DIDerivedType(MDNode::get(VMContext, Elts));
204 /// createPointerType - Create debugging information entry for a pointer.
206 DIBuilder::createPointerType(DIType PointeeTy, uint64_t SizeInBits,
207 uint64_t AlignInBits, StringRef Name) {
208 // Pointer types are encoded in DIDerivedType format.
210 GetTagConstant(VMContext, dwarf::DW_TAG_pointer_type),
212 MDString::get(VMContext, Name),
214 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
215 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
216 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
217 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
218 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
221 return DIDerivedType(MDNode::get(VMContext, Elts));
224 DIDerivedType DIBuilder::createMemberPointerType(DIType PointeeTy, DIType Base) {
225 // Pointer types are encoded in DIDerivedType format.
227 GetTagConstant(VMContext, dwarf::DW_TAG_ptr_to_member_type),
231 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
232 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
233 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
234 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
235 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
239 return DIDerivedType(MDNode::get(VMContext, Elts));
242 /// createReferenceType - Create debugging information entry for a reference
244 DIDerivedType DIBuilder::createReferenceType(unsigned Tag, DIType RTy) {
245 assert(RTy.Verify() && "Unable to create reference type");
246 // References are encoded in DIDerivedType format.
248 GetTagConstant(VMContext, Tag),
252 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
253 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
254 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
255 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
256 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
259 return DIDerivedType(MDNode::get(VMContext, Elts));
262 /// createTypedef - Create debugging information entry for a typedef.
263 DIDerivedType DIBuilder::createTypedef(DIType Ty, StringRef Name, DIFile File,
264 unsigned LineNo, DIDescriptor Context) {
265 // typedefs are encoded in DIDerivedType format.
266 assert(Ty.Verify() && "Invalid typedef type!");
268 GetTagConstant(VMContext, dwarf::DW_TAG_typedef),
269 getNonCompileUnitScope(Context),
270 MDString::get(VMContext, Name),
272 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
273 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
274 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
275 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
276 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
279 return DIDerivedType(MDNode::get(VMContext, Elts));
282 /// createFriend - Create debugging information entry for a 'friend'.
283 DIType DIBuilder::createFriend(DIType Ty, DIType FriendTy) {
284 // typedefs are encoded in DIDerivedType format.
285 assert(Ty.Verify() && "Invalid type!");
286 assert(FriendTy.Verify() && "Invalid friend type!");
288 GetTagConstant(VMContext, dwarf::DW_TAG_friend),
292 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
293 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
294 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
295 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
296 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
299 return DIType(MDNode::get(VMContext, Elts));
302 /// createInheritance - Create debugging information entry to establish
303 /// inheritance relationship between two types.
304 DIDerivedType DIBuilder::createInheritance(
305 DIType Ty, DIType BaseTy, uint64_t BaseOffset, unsigned Flags) {
306 assert(Ty.Verify() && "Unable to create inheritance");
307 // TAG_inheritance is encoded in DIDerivedType format.
309 GetTagConstant(VMContext, dwarf::DW_TAG_inheritance),
313 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
314 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
315 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
316 ConstantInt::get(Type::getInt64Ty(VMContext), BaseOffset),
317 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
320 return DIDerivedType(MDNode::get(VMContext, Elts));
323 /// createMemberType - Create debugging information entry for a member.
324 DIDerivedType DIBuilder::createMemberType(
325 DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
326 uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
327 unsigned Flags, DIType Ty) {
328 // TAG_member is encoded in DIDerivedType format.
330 GetTagConstant(VMContext, dwarf::DW_TAG_member),
331 getNonCompileUnitScope(Scope),
332 MDString::get(VMContext, Name),
334 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
335 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
336 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
337 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
338 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
341 return DIDerivedType(MDNode::get(VMContext, Elts));
344 /// createStaticMemberType - Create debugging information entry for a
345 /// C++ static data member.
346 DIType DIBuilder::createStaticMemberType(DIDescriptor Scope, StringRef Name,
347 DIFile File, unsigned LineNumber,
348 DIType Ty, unsigned Flags,
350 // TAG_member is encoded in DIDerivedType format.
351 Flags |= DIDescriptor::FlagStaticMember;
353 GetTagConstant(VMContext, dwarf::DW_TAG_member),
354 getNonCompileUnitScope(Scope),
355 MDString::get(VMContext, Name),
357 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
358 ConstantInt::get(Type::getInt64Ty(VMContext), 0/*SizeInBits*/),
359 ConstantInt::get(Type::getInt64Ty(VMContext), 0/*AlignInBits*/),
360 ConstantInt::get(Type::getInt64Ty(VMContext), 0/*OffsetInBits*/),
361 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
365 return DIType(MDNode::get(VMContext, Elts));
368 /// createObjCIVar - Create debugging information entry for Objective-C
369 /// instance variable.
370 DIType DIBuilder::createObjCIVar(StringRef Name,
371 DIFile File, unsigned LineNumber,
372 uint64_t SizeInBits, uint64_t AlignInBits,
373 uint64_t OffsetInBits, unsigned Flags,
374 DIType Ty, StringRef PropertyName,
375 StringRef GetterName, StringRef SetterName,
376 unsigned PropertyAttributes) {
377 // TAG_member is encoded in DIDerivedType format.
379 GetTagConstant(VMContext, dwarf::DW_TAG_member),
380 getNonCompileUnitScope(File),
381 MDString::get(VMContext, Name),
383 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
384 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
385 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
386 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
387 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
389 MDString::get(VMContext, PropertyName),
390 MDString::get(VMContext, GetterName),
391 MDString::get(VMContext, SetterName),
392 ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes)
394 return DIType(MDNode::get(VMContext, Elts));
397 /// createObjCIVar - Create debugging information entry for Objective-C
398 /// instance variable.
399 DIType DIBuilder::createObjCIVar(StringRef Name,
400 DIFile File, unsigned LineNumber,
401 uint64_t SizeInBits, uint64_t AlignInBits,
402 uint64_t OffsetInBits, unsigned Flags,
403 DIType Ty, MDNode *PropertyNode) {
404 // TAG_member is encoded in DIDerivedType format.
406 GetTagConstant(VMContext, dwarf::DW_TAG_member),
407 getNonCompileUnitScope(File),
408 MDString::get(VMContext, Name),
410 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
411 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
412 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
413 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
414 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
418 return DIType(MDNode::get(VMContext, Elts));
421 /// createObjCProperty - Create debugging information entry for Objective-C
423 DIObjCProperty DIBuilder::createObjCProperty(StringRef Name,
424 DIFile File, unsigned LineNumber,
425 StringRef GetterName,
426 StringRef SetterName,
427 unsigned PropertyAttributes,
430 GetTagConstant(VMContext, dwarf::DW_TAG_APPLE_property),
431 MDString::get(VMContext, Name),
433 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
434 MDString::get(VMContext, GetterName),
435 MDString::get(VMContext, SetterName),
436 ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes),
439 return DIObjCProperty(MDNode::get(VMContext, Elts));
442 /// createTemplateTypeParameter - Create debugging information for template
444 DITemplateTypeParameter
445 DIBuilder::createTemplateTypeParameter(DIDescriptor Context, StringRef Name,
446 DIType Ty, MDNode *File, unsigned LineNo,
449 GetTagConstant(VMContext, dwarf::DW_TAG_template_type_parameter),
450 getNonCompileUnitScope(Context),
451 MDString::get(VMContext, Name),
454 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
455 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
457 return DITemplateTypeParameter(MDNode::get(VMContext, Elts));
460 /// createTemplateValueParameter - Create debugging information for template
462 DITemplateValueParameter
463 DIBuilder::createTemplateValueParameter(DIDescriptor Context, StringRef Name,
464 DIType Ty, uint64_t Val,
465 MDNode *File, unsigned LineNo,
468 GetTagConstant(VMContext, dwarf::DW_TAG_template_value_parameter),
469 getNonCompileUnitScope(Context),
470 MDString::get(VMContext, Name),
472 ConstantInt::get(Type::getInt64Ty(VMContext), Val),
474 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
475 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
477 return DITemplateValueParameter(MDNode::get(VMContext, Elts));
480 /// createClassType - Create debugging information entry for a class.
481 DIType DIBuilder::createClassType(DIDescriptor Context, StringRef Name,
482 DIFile File, unsigned LineNumber,
483 uint64_t SizeInBits, uint64_t AlignInBits,
484 uint64_t OffsetInBits, unsigned Flags,
485 DIType DerivedFrom, DIArray Elements,
486 MDNode *VTableHolder,
487 MDNode *TemplateParams) {
488 assert((!Context || Context.Verify()) &&
489 "createClassType should be called with a valid Context");
490 // TAG_class_type is encoded in DICompositeType format.
492 GetTagConstant(VMContext, dwarf::DW_TAG_class_type),
493 getNonCompileUnitScope(Context),
494 MDString::get(VMContext, Name),
496 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
497 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
498 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
499 ConstantInt::get(Type::getInt32Ty(VMContext), OffsetInBits),
500 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
503 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
507 DIType R(MDNode::get(VMContext, Elts));
508 assert(R.Verify() && "createClassType should return a verifiable DIType");
512 /// createStructType - Create debugging information entry for a struct.
513 DICompositeType DIBuilder::createStructType(DIDescriptor Context,
514 StringRef Name, DIFile File,
517 uint64_t AlignInBits,
518 unsigned Flags, DIType DerivedFrom,
520 unsigned RunTimeLang,
521 MDNode *VTableHolder) {
522 // TAG_structure_type is encoded in DICompositeType format.
524 GetTagConstant(VMContext, dwarf::DW_TAG_structure_type),
525 getNonCompileUnitScope(Context),
526 MDString::get(VMContext, Name),
528 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
529 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
530 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
531 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
532 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
535 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
539 DICompositeType R(MDNode::get(VMContext, Elts));
540 assert(R.Verify() && "createStructType should return a verifiable DIType");
544 /// createUnionType - Create debugging information entry for an union.
545 DICompositeType DIBuilder::createUnionType(
546 DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
547 uint64_t SizeInBits, uint64_t AlignInBits, unsigned Flags, DIArray Elements,
548 unsigned RunTimeLang) {
549 // TAG_union_type is encoded in DICompositeType format.
551 GetTagConstant(VMContext, dwarf::DW_TAG_union_type),
552 getNonCompileUnitScope(Scope),
553 MDString::get(VMContext, Name),
555 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
556 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
557 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
558 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
559 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
562 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
563 Constant::getNullValue(Type::getInt32Ty(VMContext))
565 return DICompositeType(MDNode::get(VMContext, Elts));
568 /// createSubroutineType - Create subroutine type.
570 DIBuilder::createSubroutineType(DIFile File, DIArray ParameterTypes) {
571 // TAG_subroutine_type is encoded in DICompositeType format.
573 GetTagConstant(VMContext, dwarf::DW_TAG_subroutine_type),
574 Constant::getNullValue(Type::getInt32Ty(VMContext)),
575 MDString::get(VMContext, ""),
576 Constant::getNullValue(Type::getInt32Ty(VMContext)),
577 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
578 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
579 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
580 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
581 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
584 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
585 Constant::getNullValue(Type::getInt32Ty(VMContext))
587 return DICompositeType(MDNode::get(VMContext, Elts));
590 /// createEnumerationType - Create debugging information entry for an
592 DICompositeType DIBuilder::createEnumerationType(
593 DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
594 uint64_t SizeInBits, uint64_t AlignInBits, DIArray Elements,
596 // TAG_enumeration_type is encoded in DICompositeType format.
598 GetTagConstant(VMContext, dwarf::DW_TAG_enumeration_type),
599 getNonCompileUnitScope(Scope),
600 MDString::get(VMContext, Name),
602 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
603 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
604 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
605 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
606 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
609 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
610 Constant::getNullValue(Type::getInt32Ty(VMContext))
612 MDNode *Node = MDNode::get(VMContext, Elts);
613 AllEnumTypes.push_back(Node);
614 return DICompositeType(Node);
617 /// createArrayType - Create debugging information entry for an array.
618 DICompositeType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
619 DIType Ty, DIArray Subscripts) {
620 // TAG_array_type is encoded in DICompositeType format.
622 GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
624 MDString::get(VMContext, ""),
626 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
627 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
628 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
629 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
630 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
633 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
634 Constant::getNullValue(Type::getInt32Ty(VMContext))
636 return DICompositeType(MDNode::get(VMContext, Elts));
639 /// createVectorType - Create debugging information entry for a vector.
640 DIType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits,
641 DIType Ty, DIArray Subscripts) {
643 // A vector is an array type with the FlagVector flag applied.
645 GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
647 MDString::get(VMContext, ""),
649 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
650 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
651 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
652 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
653 ConstantInt::get(Type::getInt32Ty(VMContext), DIType::FlagVector),
656 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
657 Constant::getNullValue(Type::getInt32Ty(VMContext))
659 return DIType(MDNode::get(VMContext, Elts));
662 /// createArtificialType - Create a new DIType with "artificial" flag set.
663 DIType DIBuilder::createArtificialType(DIType Ty) {
664 if (Ty.isArtificial())
667 SmallVector<Value *, 9> Elts;
669 assert (N && "Unexpected input DIType!");
670 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
671 if (Value *V = N->getOperand(i))
674 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
677 unsigned CurFlags = Ty.getFlags();
678 CurFlags = CurFlags | DIType::FlagArtificial;
680 // Flags are stored at this slot.
681 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
683 return DIType(MDNode::get(VMContext, Elts));
686 /// createObjectPointerType - Create a new type with both the object pointer
687 /// and artificial flags set.
688 DIType DIBuilder::createObjectPointerType(DIType Ty) {
689 if (Ty.isObjectPointer())
692 SmallVector<Value *, 9> Elts;
694 assert (N && "Unexpected input DIType!");
695 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
696 if (Value *V = N->getOperand(i))
699 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
702 unsigned CurFlags = Ty.getFlags();
703 CurFlags = CurFlags | (DIType::FlagObjectPointer | DIType::FlagArtificial);
705 // Flags are stored at this slot.
706 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
708 return DIType(MDNode::get(VMContext, Elts));
711 /// retainType - Retain DIType in a module even if it is not referenced
712 /// through debug info anchors.
713 void DIBuilder::retainType(DIType T) {
714 AllRetainTypes.push_back(T);
717 /// createUnspecifiedParameter - Create unspeicified type descriptor
718 /// for the subroutine type.
719 DIDescriptor DIBuilder::createUnspecifiedParameter() {
721 GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_parameters)
723 return DIDescriptor(MDNode::get(VMContext, Elts));
726 /// createTemporaryType - Create a temporary forward-declared type.
727 DIType DIBuilder::createTemporaryType() {
728 // Give the temporary MDNode a tag. It doesn't matter what tag we
729 // use here as long as DIType accepts it.
730 Value *Elts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
731 MDNode *Node = MDNode::getTemporary(VMContext, Elts);
735 /// createTemporaryType - Create a temporary forward-declared type.
736 DIType DIBuilder::createTemporaryType(DIFile F) {
737 // Give the temporary MDNode a tag. It doesn't matter what tag we
738 // use here as long as DIType accepts it.
740 GetTagConstant(VMContext, DW_TAG_base_type),
745 MDNode *Node = MDNode::getTemporary(VMContext, Elts);
749 /// createForwardDecl - Create a temporary forward-declared type that
750 /// can be RAUW'd if the full type is seen.
751 DIType DIBuilder::createForwardDecl(unsigned Tag, StringRef Name,
752 DIDescriptor Scope, DIFile F,
753 unsigned Line, unsigned RuntimeLang,
755 uint64_t AlignInBits) {
756 // Create a temporary MDNode.
758 GetTagConstant(VMContext, Tag),
759 getNonCompileUnitScope(Scope),
760 MDString::get(VMContext, Name),
762 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
763 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
764 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
765 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
766 ConstantInt::get(Type::getInt32Ty(VMContext),
767 DIDescriptor::FlagFwdDecl),
770 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
772 MDNode *Node = MDNode::getTemporary(VMContext, Elts);
773 assert(DIType(Node).Verify() &&
774 "createForwardDecl result should be verifiable");
778 /// getOrCreateArray - Get a DIArray, create one if required.
779 DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) {
780 if (Elements.empty()) {
781 Value *Null = Constant::getNullValue(Type::getInt32Ty(VMContext));
782 return DIArray(MDNode::get(VMContext, Null));
784 return DIArray(MDNode::get(VMContext, Elements));
787 /// getOrCreateSubrange - Create a descriptor for a value range. This
788 /// implicitly uniques the values returned.
789 DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
791 GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type),
792 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
793 ConstantInt::get(Type::getInt64Ty(VMContext), Count)
796 return DISubrange(MDNode::get(VMContext, Elts));
799 /// createGlobalVariable - Create a new descriptor for the specified global.
800 DIGlobalVariable DIBuilder::
801 createGlobalVariable(StringRef Name, DIFile F, unsigned LineNumber,
802 DIType Ty, bool isLocalToUnit, Value *Val) {
804 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
805 Constant::getNullValue(Type::getInt32Ty(VMContext)),
807 MDString::get(VMContext, Name),
808 MDString::get(VMContext, Name),
809 MDString::get(VMContext, Name),
811 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
813 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
814 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
818 MDNode *Node = MDNode::get(VMContext, Elts);
819 AllGVs.push_back(Node);
820 return DIGlobalVariable(Node);
823 /// createStaticVariable - Create a new descriptor for the specified static
825 DIGlobalVariable DIBuilder::
826 createStaticVariable(DIDescriptor Context, StringRef Name,
827 StringRef LinkageName, DIFile F, unsigned LineNumber,
828 DIType Ty, bool isLocalToUnit, Value *Val, MDNode *Decl) {
830 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
831 Constant::getNullValue(Type::getInt32Ty(VMContext)),
832 getNonCompileUnitScope(Context),
833 MDString::get(VMContext, Name),
834 MDString::get(VMContext, Name),
835 MDString::get(VMContext, LinkageName),
837 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
839 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
840 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
844 MDNode *Node = MDNode::get(VMContext, Elts);
845 AllGVs.push_back(Node);
846 return DIGlobalVariable(Node);
849 /// createVariable - Create a new descriptor for the specified variable.
850 DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
851 StringRef Name, DIFile File,
852 unsigned LineNo, DIType Ty,
853 bool AlwaysPreserve, unsigned Flags,
855 DIDescriptor Context(getNonCompileUnitScope(Scope));
856 assert((!Context || Context.Verify()) &&
857 "createLocalVariable should be called with a valid Context");
858 assert(Ty.Verify() &&
859 "createLocalVariable should be called with a valid type");
861 GetTagConstant(VMContext, Tag),
862 getNonCompileUnitScope(Scope),
863 MDString::get(VMContext, Name),
865 ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))),
867 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
868 Constant::getNullValue(Type::getInt32Ty(VMContext))
870 MDNode *Node = MDNode::get(VMContext, Elts);
871 if (AlwaysPreserve) {
872 // The optimizer may remove local variable. If there is an interest
873 // to preserve variable info in such situation then stash it in a
875 DISubprogram Fn(getDISubprogram(Scope));
876 NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, Fn);
877 FnLocals->addOperand(Node);
879 assert(DIVariable(Node).Verify() &&
880 "createLocalVariable should return a verifiable DIVariable");
881 return DIVariable(Node);
884 /// createComplexVariable - Create a new descriptor for the specified variable
885 /// which has a complex address expression for its address.
886 DIVariable DIBuilder::createComplexVariable(unsigned Tag, DIDescriptor Scope,
887 StringRef Name, DIFile F,
889 DIType Ty, ArrayRef<Value *> Addr,
891 SmallVector<Value *, 15> Elts;
892 Elts.push_back(GetTagConstant(VMContext, Tag));
893 Elts.push_back(getNonCompileUnitScope(Scope)),
894 Elts.push_back(MDString::get(VMContext, Name));
896 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext),
897 (LineNo | (ArgNo << 24))));
899 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
900 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
901 Elts.append(Addr.begin(), Addr.end());
903 return DIVariable(MDNode::get(VMContext, Elts));
906 /// createFunction - Create a new descriptor for the specified function.
907 DISubprogram DIBuilder::createFunction(DIDescriptor Context,
909 StringRef LinkageName,
910 DIFile File, unsigned LineNo,
912 bool isLocalToUnit, bool isDefinition,
914 unsigned Flags, bool isOptimized,
918 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
920 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
921 Constant::getNullValue(Type::getInt32Ty(VMContext)),
922 getNonCompileUnitScope(Context),
923 MDString::get(VMContext, Name),
924 MDString::get(VMContext, Name),
925 MDString::get(VMContext, LinkageName),
927 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
929 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
930 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
931 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
932 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
934 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
935 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
939 MDNode::getTemporary(VMContext, TElts),
940 ConstantInt::get(Type::getInt32Ty(VMContext), ScopeLine)
942 MDNode *Node = MDNode::get(VMContext, Elts);
944 // Create a named metadata so that we do not lose this mdnode.
946 AllSubprograms.push_back(Node);
947 return DISubprogram(Node);
950 /// createMethod - Create a new descriptor for the specified C++ method.
951 DISubprogram DIBuilder::createMethod(DIDescriptor Context,
953 StringRef LinkageName,
955 unsigned LineNo, DIType Ty,
958 unsigned VK, unsigned VIndex,
959 MDNode *VTableHolder,
964 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
966 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
967 Constant::getNullValue(Type::getInt32Ty(VMContext)),
968 getNonCompileUnitScope(Context),
969 MDString::get(VMContext, Name),
970 MDString::get(VMContext, Name),
971 MDString::get(VMContext, LinkageName),
973 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
975 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
976 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
977 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
978 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
980 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
981 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
984 Constant::getNullValue(Type::getInt32Ty(VMContext)),
985 MDNode::getTemporary(VMContext, TElts),
986 // FIXME: Do we want to use different scope/lines?
987 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
989 MDNode *Node = MDNode::get(VMContext, Elts);
991 AllSubprograms.push_back(Node);
992 return DISubprogram(Node);
995 /// createNameSpace - This creates new descriptor for a namespace
996 /// with the specified parent scope.
997 DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
998 DIFile File, unsigned LineNo) {
1000 GetTagConstant(VMContext, dwarf::DW_TAG_namespace),
1001 getNonCompileUnitScope(Scope),
1002 MDString::get(VMContext, Name),
1004 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1006 DINameSpace R(MDNode::get(VMContext, Elts));
1007 assert(R.Verify() &&
1008 "createNameSpace should return a verifiable DINameSpace");
1012 /// createLexicalBlockFile - This creates a new MDNode that encapsulates
1013 /// an existing scope with a new filename.
1014 DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
1017 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1021 DILexicalBlockFile R(MDNode::get(VMContext, Elts));
1024 "createLexicalBlockFile should return a verifiable DILexicalBlockFile");
1028 DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
1029 unsigned Line, unsigned Col) {
1030 // Defeat MDNode uniqing for lexical blocks by using unique id.
1031 static unsigned int unique_id = 0;
1033 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1034 getNonCompileUnitScope(Scope),
1035 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
1036 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1038 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
1040 DILexicalBlock R(MDNode::get(VMContext, Elts));
1041 assert(R.Verify() &&
1042 "createLexicalBlock should return a verifiable DILexicalBlock");
1046 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1047 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1048 Instruction *InsertBefore) {
1049 assert(Storage && "no storage passed to dbg.declare");
1050 assert(VarInfo.Verify() && "empty DIVariable passed to dbg.declare");
1052 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1054 Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1055 return CallInst::Create(DeclareFn, Args, "", InsertBefore);
1058 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1059 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1060 BasicBlock *InsertAtEnd) {
1061 assert(Storage && "no storage passed to dbg.declare");
1062 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.declare");
1064 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1066 Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1068 // If this block already has a terminator then insert this intrinsic
1069 // before the terminator.
1070 if (TerminatorInst *T = InsertAtEnd->getTerminator())
1071 return CallInst::Create(DeclareFn, Args, "", T);
1073 return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
1076 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1077 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1079 Instruction *InsertBefore) {
1080 assert(V && "no value passed to dbg.value");
1081 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
1083 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1085 Value *Args[] = { MDNode::get(V->getContext(), V),
1086 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1088 return CallInst::Create(ValueFn, Args, "", InsertBefore);
1091 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1092 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1094 BasicBlock *InsertAtEnd) {
1095 assert(V && "no value passed to dbg.value");
1096 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
1098 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1100 Value *Args[] = { MDNode::get(V->getContext(), V),
1101 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1103 return CallInst::Create(ValueFn, Args, "", InsertAtEnd);