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);
65 DIArray IMs = getOrCreateArray(AllImportedModules);
66 DIType(TempImportedModules).replaceAllUsesWith(IMs);
69 /// getNonCompileUnitScope - If N is compile unit return NULL otherwise return
71 static MDNode *getNonCompileUnitScope(MDNode *N) {
72 if (DIDescriptor(N).isCompileUnit())
77 static MDNode *createFilePathPair(LLVMContext &VMContext, StringRef Filename,
78 StringRef Directory) {
79 assert(!Filename.empty() && "Unable to create file without name");
81 MDString::get(VMContext, Filename),
82 MDString::get(VMContext, Directory),
84 return MDNode::get(VMContext, Pair);
87 /// createCompileUnit - A CompileUnit provides an anchor for all debugging
88 /// information generated during this instance of compilation.
89 void DIBuilder::createCompileUnit(unsigned Lang, StringRef Filename,
90 StringRef Directory, StringRef Producer,
91 bool isOptimized, StringRef Flags,
92 unsigned RunTimeVer, StringRef SplitName) {
93 assert(((Lang <= dwarf::DW_LANG_Python && Lang >= dwarf::DW_LANG_C89) ||
94 (Lang <= dwarf::DW_LANG_hi_user && Lang >= dwarf::DW_LANG_lo_user)) &&
95 "Invalid Language tag");
96 assert(!Filename.empty() &&
97 "Unable to create compile unit without filename");
98 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
99 TempEnumTypes = MDNode::getTemporary(VMContext, TElts);
101 TempRetainTypes = MDNode::getTemporary(VMContext, TElts);
103 TempSubprograms = MDNode::getTemporary(VMContext, TElts);
105 TempGVs = MDNode::getTemporary(VMContext, TElts);
107 TempImportedModules = MDNode::getTemporary(VMContext, TElts);
110 GetTagConstant(VMContext, dwarf::DW_TAG_compile_unit),
111 createFilePathPair(VMContext, Filename, Directory),
112 ConstantInt::get(Type::getInt32Ty(VMContext), Lang),
113 MDString::get(VMContext, Producer),
114 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
115 MDString::get(VMContext, Flags),
116 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer),
122 MDString::get(VMContext, SplitName)
124 TheCU = DICompileUnit(MDNode::get(VMContext, Elts));
126 // Create a named metadata so that it is easier to find cu in a module.
127 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
128 NMD->addOperand(TheCU);
131 static DIImportedEntity
132 createImportedModule(LLVMContext &C, DIScope Context, DIDescriptor NS,
133 unsigned Line, StringRef Name,
134 SmallVectorImpl<Value *> &AllImportedModules) {
138 GetTagConstant(C, dwarf::DW_TAG_imported_module),
141 ConstantInt::get(Type::getInt32Ty(C), Line),
143 R = MDNode::get(C, Elts);
146 GetTagConstant(C, dwarf::DW_TAG_imported_module),
149 ConstantInt::get(Type::getInt32Ty(C), Line),
150 MDString::get(C, Name)
152 R = MDNode::get(C, Elts);
154 DIImportedEntity M(R);
155 assert(M.Verify() && "Imported module should be valid");
156 AllImportedModules.push_back(M);
160 DIImportedEntity DIBuilder::createImportedModule(DIScope Context,
161 DINameSpace NS, unsigned Line,
163 return ::createImportedModule(VMContext, Context, NS, Line, Name,
167 DIImportedEntity DIBuilder::createImportedModule(DIScope Context,
171 return ::createImportedModule(VMContext, Context, NS, Line, Name,
175 DIImportedEntity DIBuilder::createImportedDeclaration(DIScope Context,
179 GetTagConstant(VMContext, dwarf::DW_TAG_imported_declaration),
182 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
184 DIImportedEntity M(MDNode::get(VMContext, Elts));
185 assert(M.Verify() && "Imported module should be valid");
186 AllImportedModules.push_back(M);
190 /// createFile - Create a file descriptor to hold debugging information
192 DIFile DIBuilder::createFile(StringRef Filename, StringRef Directory) {
194 GetTagConstant(VMContext, dwarf::DW_TAG_file_type),
195 createFilePathPair(VMContext, Filename, Directory)
197 return DIFile(MDNode::get(VMContext, Elts));
200 /// createEnumerator - Create a single enumerator value.
201 DIEnumerator DIBuilder::createEnumerator(StringRef Name, uint64_t Val) {
202 assert(!Name.empty() && "Unable to create enumerator without name");
204 GetTagConstant(VMContext, dwarf::DW_TAG_enumerator),
205 MDString::get(VMContext, Name),
206 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
208 return DIEnumerator(MDNode::get(VMContext, Elts));
211 /// createNullPtrType - Create C++0x nullptr type.
212 DIType DIBuilder::createNullPtrType(StringRef Name) {
213 assert(!Name.empty() && "Unable to create type without name");
214 // nullptr is encoded in DIBasicType format. Line number, filename,
215 // ,size, alignment, offset and flags are always empty here.
217 GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_type),
220 MDString::get(VMContext, Name),
221 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
222 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
223 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
224 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
225 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
226 ConstantInt::get(Type::getInt32Ty(VMContext), 0) // Encoding
228 return DIType(MDNode::get(VMContext, Elts));
231 /// createBasicType - Create debugging information entry for a basic
232 /// type, e.g 'char'.
234 DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
235 uint64_t AlignInBits, unsigned Encoding) {
236 assert(!Name.empty() && "Unable to create type without name");
237 // Basic types are encoded in DIBasicType format. Line number, filename,
238 // offset and flags are always empty here.
240 GetTagConstant(VMContext, dwarf::DW_TAG_base_type),
241 NULL, // File/directory name
243 MDString::get(VMContext, Name),
244 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
245 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
246 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
247 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
248 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
249 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
251 return DIBasicType(MDNode::get(VMContext, Elts));
254 /// createQualifiedType - Create debugging information entry for a qualified
255 /// type, e.g. 'const int'.
256 DIDerivedType DIBuilder::createQualifiedType(unsigned Tag, DIType FromTy) {
257 // Qualified types are encoded in DIDerivedType format.
259 GetTagConstant(VMContext, Tag),
262 MDString::get(VMContext, StringRef()), // Empty name.
263 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
264 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
265 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
266 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
267 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
270 return DIDerivedType(MDNode::get(VMContext, Elts));
273 /// createPointerType - Create debugging information entry for a pointer.
275 DIBuilder::createPointerType(DIType PointeeTy, uint64_t SizeInBits,
276 uint64_t AlignInBits, StringRef Name) {
277 // Pointer types are encoded in DIDerivedType format.
279 GetTagConstant(VMContext, dwarf::DW_TAG_pointer_type),
282 MDString::get(VMContext, Name),
283 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
284 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
285 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
286 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
287 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
290 return DIDerivedType(MDNode::get(VMContext, Elts));
293 DIDerivedType DIBuilder::createMemberPointerType(DIType PointeeTy,
295 // Pointer types are encoded in DIDerivedType format.
297 GetTagConstant(VMContext, dwarf::DW_TAG_ptr_to_member_type),
301 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
302 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
303 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
304 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
305 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
309 return DIDerivedType(MDNode::get(VMContext, Elts));
312 /// createReferenceType - Create debugging information entry for a reference
314 DIDerivedType DIBuilder::createReferenceType(unsigned Tag, DIType RTy) {
315 assert(RTy.Verify() && "Unable to create reference type");
316 // References are encoded in DIDerivedType format.
318 GetTagConstant(VMContext, Tag),
322 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
323 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
324 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
325 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
326 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
329 return DIDerivedType(MDNode::get(VMContext, Elts));
332 /// createTypedef - Create debugging information entry for a typedef.
333 DIDerivedType DIBuilder::createTypedef(DIType Ty, StringRef Name, DIFile File,
334 unsigned LineNo, DIDescriptor Context) {
335 // typedefs are encoded in DIDerivedType format.
336 assert(Ty.Verify() && "Invalid typedef type!");
338 GetTagConstant(VMContext, dwarf::DW_TAG_typedef),
340 getNonCompileUnitScope(Context),
341 MDString::get(VMContext, Name),
342 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
343 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
344 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
345 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
346 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
349 return DIDerivedType(MDNode::get(VMContext, Elts));
352 /// createFriend - Create debugging information entry for a 'friend'.
353 DIType DIBuilder::createFriend(DIType Ty, DIType FriendTy) {
354 // typedefs are encoded in DIDerivedType format.
355 assert(Ty.Verify() && "Invalid type!");
356 assert(FriendTy.Verify() && "Invalid friend type!");
358 GetTagConstant(VMContext, dwarf::DW_TAG_friend),
362 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
363 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
364 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
365 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
366 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
369 return DIType(MDNode::get(VMContext, Elts));
372 /// createInheritance - Create debugging information entry to establish
373 /// inheritance relationship between two types.
374 DIDerivedType DIBuilder::createInheritance(
375 DIType Ty, DIType BaseTy, uint64_t BaseOffset, unsigned Flags) {
376 assert(Ty.Verify() && "Unable to create inheritance");
377 // TAG_inheritance is encoded in DIDerivedType format.
379 GetTagConstant(VMContext, dwarf::DW_TAG_inheritance),
383 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
384 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
385 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
386 ConstantInt::get(Type::getInt64Ty(VMContext), BaseOffset),
387 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
390 return DIDerivedType(MDNode::get(VMContext, Elts));
393 /// createMemberType - Create debugging information entry for a member.
394 DIDerivedType DIBuilder::createMemberType(
395 DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
396 uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
397 unsigned Flags, DIType Ty) {
398 // TAG_member is encoded in DIDerivedType format.
400 GetTagConstant(VMContext, dwarf::DW_TAG_member),
402 getNonCompileUnitScope(Scope),
403 MDString::get(VMContext, Name),
404 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
405 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
406 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
407 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
408 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
411 return DIDerivedType(MDNode::get(VMContext, Elts));
414 /// createStaticMemberType - Create debugging information entry for a
415 /// C++ static data member.
416 DIType DIBuilder::createStaticMemberType(DIDescriptor Scope, StringRef Name,
417 DIFile File, unsigned LineNumber,
418 DIType Ty, unsigned Flags,
420 // TAG_member is encoded in DIDerivedType format.
421 Flags |= DIDescriptor::FlagStaticMember;
423 GetTagConstant(VMContext, dwarf::DW_TAG_member),
425 getNonCompileUnitScope(Scope),
426 MDString::get(VMContext, Name),
427 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
428 ConstantInt::get(Type::getInt64Ty(VMContext), 0/*SizeInBits*/),
429 ConstantInt::get(Type::getInt64Ty(VMContext), 0/*AlignInBits*/),
430 ConstantInt::get(Type::getInt64Ty(VMContext), 0/*OffsetInBits*/),
431 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
435 return DIType(MDNode::get(VMContext, Elts));
438 /// createObjCIVar - Create debugging information entry for Objective-C
439 /// instance variable.
440 DIType DIBuilder::createObjCIVar(StringRef Name,
441 DIFile File, unsigned LineNumber,
442 uint64_t SizeInBits, uint64_t AlignInBits,
443 uint64_t OffsetInBits, unsigned Flags,
444 DIType Ty, StringRef PropertyName,
445 StringRef GetterName, StringRef SetterName,
446 unsigned PropertyAttributes) {
447 // TAG_member is encoded in DIDerivedType format.
449 GetTagConstant(VMContext, dwarf::DW_TAG_member),
451 getNonCompileUnitScope(File),
452 MDString::get(VMContext, Name),
453 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
454 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
455 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
456 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
457 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
459 MDString::get(VMContext, PropertyName),
460 MDString::get(VMContext, GetterName),
461 MDString::get(VMContext, SetterName),
462 ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes)
464 return DIType(MDNode::get(VMContext, Elts));
467 /// createObjCIVar - Create debugging information entry for Objective-C
468 /// instance variable.
469 DIType DIBuilder::createObjCIVar(StringRef Name,
470 DIFile File, unsigned LineNumber,
471 uint64_t SizeInBits, uint64_t AlignInBits,
472 uint64_t OffsetInBits, unsigned Flags,
473 DIType Ty, MDNode *PropertyNode) {
474 // TAG_member is encoded in DIDerivedType format.
476 GetTagConstant(VMContext, dwarf::DW_TAG_member),
478 getNonCompileUnitScope(File),
479 MDString::get(VMContext, Name),
480 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
481 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
482 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
483 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
484 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
488 return DIType(MDNode::get(VMContext, Elts));
491 /// createObjCProperty - Create debugging information entry for Objective-C
493 DIObjCProperty DIBuilder::createObjCProperty(StringRef Name,
494 DIFile File, unsigned LineNumber,
495 StringRef GetterName,
496 StringRef SetterName,
497 unsigned PropertyAttributes,
500 GetTagConstant(VMContext, dwarf::DW_TAG_APPLE_property),
501 MDString::get(VMContext, Name),
503 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
504 MDString::get(VMContext, GetterName),
505 MDString::get(VMContext, SetterName),
506 ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes),
509 return DIObjCProperty(MDNode::get(VMContext, Elts));
512 /// createTemplateTypeParameter - Create debugging information for template
514 DITemplateTypeParameter
515 DIBuilder::createTemplateTypeParameter(DIDescriptor Context, StringRef Name,
516 DIType Ty, MDNode *File, unsigned LineNo,
519 GetTagConstant(VMContext, dwarf::DW_TAG_template_type_parameter),
520 getNonCompileUnitScope(Context),
521 MDString::get(VMContext, Name),
524 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
525 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
527 return DITemplateTypeParameter(MDNode::get(VMContext, Elts));
530 /// createTemplateValueParameter - Create debugging information for template
532 DITemplateValueParameter
533 DIBuilder::createTemplateValueParameter(DIDescriptor Context, StringRef Name,
534 DIType Ty, Value *Val,
535 MDNode *File, unsigned LineNo,
538 GetTagConstant(VMContext, dwarf::DW_TAG_template_value_parameter),
539 getNonCompileUnitScope(Context),
540 MDString::get(VMContext, Name),
544 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
545 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
547 return DITemplateValueParameter(MDNode::get(VMContext, Elts));
550 /// createClassType - Create debugging information entry for a class.
551 DICompositeType DIBuilder::createClassType(DIDescriptor Context, StringRef Name,
552 DIFile File, unsigned LineNumber,
554 uint64_t AlignInBits,
555 uint64_t OffsetInBits,
556 unsigned Flags, DIType DerivedFrom,
558 MDNode *VTableHolder,
559 MDNode *TemplateParams) {
560 assert((!Context || Context.Verify()) &&
561 "createClassType should be called with a valid Context");
562 // TAG_class_type is encoded in DICompositeType format.
564 GetTagConstant(VMContext, dwarf::DW_TAG_class_type),
566 getNonCompileUnitScope(Context),
567 MDString::get(VMContext, Name),
568 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
569 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
570 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
571 ConstantInt::get(Type::getInt32Ty(VMContext), OffsetInBits),
572 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
575 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
579 DICompositeType R(MDNode::get(VMContext, Elts));
580 assert(R.Verify() && "createClassType should return a verifiable DIType");
584 /// createStructType - Create debugging information entry for a struct.
585 DICompositeType DIBuilder::createStructType(DIDescriptor Context,
586 StringRef Name, DIFile File,
589 uint64_t AlignInBits,
590 unsigned Flags, DIType DerivedFrom,
592 unsigned RunTimeLang,
593 MDNode *VTableHolder) {
594 // TAG_structure_type is encoded in DICompositeType format.
596 GetTagConstant(VMContext, dwarf::DW_TAG_structure_type),
598 getNonCompileUnitScope(Context),
599 MDString::get(VMContext, Name),
600 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
601 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
602 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
603 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
604 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
607 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
611 DICompositeType R(MDNode::get(VMContext, Elts));
612 assert(R.Verify() && "createStructType should return a verifiable DIType");
616 /// createUnionType - Create debugging information entry for an union.
617 DICompositeType DIBuilder::createUnionType(DIDescriptor Scope, StringRef Name,
618 DIFile File, unsigned LineNumber,
620 uint64_t AlignInBits, unsigned Flags,
622 unsigned RunTimeLang) {
623 // TAG_union_type is encoded in DICompositeType format.
625 GetTagConstant(VMContext, dwarf::DW_TAG_union_type),
627 getNonCompileUnitScope(Scope),
628 MDString::get(VMContext, Name),
629 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
630 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
631 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
632 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
633 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
636 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
637 Constant::getNullValue(Type::getInt32Ty(VMContext)),
640 return DICompositeType(MDNode::get(VMContext, Elts));
643 /// createSubroutineType - Create subroutine type.
645 DIBuilder::createSubroutineType(DIFile File, DIArray ParameterTypes) {
646 // TAG_subroutine_type is encoded in DICompositeType format.
648 GetTagConstant(VMContext, dwarf::DW_TAG_subroutine_type),
649 Constant::getNullValue(Type::getInt32Ty(VMContext)),
650 Constant::getNullValue(Type::getInt32Ty(VMContext)),
651 MDString::get(VMContext, ""),
652 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
653 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
654 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
655 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
656 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
659 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
660 Constant::getNullValue(Type::getInt32Ty(VMContext))
662 return DICompositeType(MDNode::get(VMContext, Elts));
665 /// createEnumerationType - Create debugging information entry for an
667 DICompositeType DIBuilder::createEnumerationType(
668 DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
669 uint64_t SizeInBits, uint64_t AlignInBits, DIArray Elements,
670 DIType UnderlyingType) {
671 // TAG_enumeration_type is encoded in DICompositeType format.
673 GetTagConstant(VMContext, dwarf::DW_TAG_enumeration_type),
675 getNonCompileUnitScope(Scope),
676 MDString::get(VMContext, Name),
677 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
678 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
679 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
680 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
681 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
684 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
685 Constant::getNullValue(Type::getInt32Ty(VMContext))
687 MDNode *Node = MDNode::get(VMContext, Elts);
688 AllEnumTypes.push_back(Node);
689 return DICompositeType(Node);
692 /// createArrayType - Create debugging information entry for an array.
693 DICompositeType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
694 DIType Ty, DIArray Subscripts) {
695 // TAG_array_type is encoded in DICompositeType format.
697 GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
698 NULL, // Filename/Directory,
700 MDString::get(VMContext, ""),
701 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
702 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
703 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
704 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
705 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
708 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
709 Constant::getNullValue(Type::getInt32Ty(VMContext))
711 return DICompositeType(MDNode::get(VMContext, Elts));
714 /// createVectorType - Create debugging information entry for a vector.
715 DICompositeType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits,
716 DIType Ty, DIArray Subscripts) {
718 // A vector is an array type with the FlagVector flag applied.
720 GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
721 NULL, // Filename/Directory,
723 MDString::get(VMContext, ""),
724 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
725 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
726 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
727 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
728 ConstantInt::get(Type::getInt32Ty(VMContext), DIType::FlagVector),
731 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
732 Constant::getNullValue(Type::getInt32Ty(VMContext))
734 return DICompositeType(MDNode::get(VMContext, Elts));
737 /// createArtificialType - Create a new DIType with "artificial" flag set.
738 DIType DIBuilder::createArtificialType(DIType Ty) {
739 if (Ty.isArtificial())
742 SmallVector<Value *, 9> Elts;
744 assert (N && "Unexpected input DIType!");
745 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
746 if (Value *V = N->getOperand(i))
749 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
752 unsigned CurFlags = Ty.getFlags();
753 CurFlags = CurFlags | DIType::FlagArtificial;
755 // Flags are stored at this slot.
756 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
758 return DIType(MDNode::get(VMContext, Elts));
761 /// createObjectPointerType - Create a new type with both the object pointer
762 /// and artificial flags set.
763 DIType DIBuilder::createObjectPointerType(DIType Ty) {
764 if (Ty.isObjectPointer())
767 SmallVector<Value *, 9> Elts;
769 assert (N && "Unexpected input DIType!");
770 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
771 if (Value *V = N->getOperand(i))
774 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
777 unsigned CurFlags = Ty.getFlags();
778 CurFlags = CurFlags | (DIType::FlagObjectPointer | DIType::FlagArtificial);
780 // Flags are stored at this slot.
781 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
783 return DIType(MDNode::get(VMContext, Elts));
786 /// retainType - Retain DIType in a module even if it is not referenced
787 /// through debug info anchors.
788 void DIBuilder::retainType(DIType T) {
789 AllRetainTypes.push_back(T);
792 /// createUnspecifiedParameter - Create unspeicified type descriptor
793 /// for the subroutine type.
794 DIDescriptor DIBuilder::createUnspecifiedParameter() {
796 GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_parameters)
798 return DIDescriptor(MDNode::get(VMContext, Elts));
801 /// createForwardDecl - Create a temporary forward-declared type that
802 /// can be RAUW'd if the full type is seen.
803 DIType DIBuilder::createForwardDecl(unsigned Tag, StringRef Name,
804 DIDescriptor Scope, DIFile F,
805 unsigned Line, unsigned RuntimeLang,
807 uint64_t AlignInBits) {
808 // Create a temporary MDNode.
810 GetTagConstant(VMContext, Tag),
812 getNonCompileUnitScope(Scope),
813 MDString::get(VMContext, Name),
814 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
815 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
816 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
817 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
818 ConstantInt::get(Type::getInt32Ty(VMContext),
819 DIDescriptor::FlagFwdDecl),
822 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
824 MDNode *Node = MDNode::getTemporary(VMContext, Elts);
825 assert(DIType(Node).Verify() &&
826 "createForwardDecl result should be verifiable");
830 /// getOrCreateArray - Get a DIArray, create one if required.
831 DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) {
832 if (Elements.empty()) {
833 Value *Null = Constant::getNullValue(Type::getInt32Ty(VMContext));
834 return DIArray(MDNode::get(VMContext, Null));
836 return DIArray(MDNode::get(VMContext, Elements));
839 /// getOrCreateSubrange - Create a descriptor for a value range. This
840 /// implicitly uniques the values returned.
841 DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
843 GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type),
844 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
845 ConstantInt::get(Type::getInt64Ty(VMContext), Count)
848 return DISubrange(MDNode::get(VMContext, Elts));
851 /// \brief Create a new descriptor for the specified global.
852 DIGlobalVariable DIBuilder::
853 createGlobalVariable(StringRef Name, StringRef LinkageName, DIFile F,
854 unsigned LineNumber, DIType Ty, bool isLocalToUnit,
857 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
858 Constant::getNullValue(Type::getInt32Ty(VMContext)),
860 MDString::get(VMContext, Name),
861 MDString::get(VMContext, Name),
862 MDString::get(VMContext, LinkageName),
864 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
866 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
867 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
871 MDNode *Node = MDNode::get(VMContext, Elts);
872 AllGVs.push_back(Node);
873 return DIGlobalVariable(Node);
876 /// \brief Create a new descriptor for the specified global.
877 DIGlobalVariable DIBuilder::
878 createGlobalVariable(StringRef Name, DIFile F, unsigned LineNumber,
879 DIType Ty, bool isLocalToUnit, Value *Val) {
880 return createGlobalVariable(Name, Name, F, LineNumber, Ty, isLocalToUnit,
884 /// createStaticVariable - Create a new descriptor for the specified static
886 DIGlobalVariable DIBuilder::
887 createStaticVariable(DIDescriptor Context, StringRef Name,
888 StringRef LinkageName, DIFile F, unsigned LineNumber,
889 DIType Ty, bool isLocalToUnit, Value *Val, MDNode *Decl) {
891 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
892 Constant::getNullValue(Type::getInt32Ty(VMContext)),
893 getNonCompileUnitScope(Context),
894 MDString::get(VMContext, Name),
895 MDString::get(VMContext, Name),
896 MDString::get(VMContext, LinkageName),
898 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
900 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
901 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
905 MDNode *Node = MDNode::get(VMContext, Elts);
906 AllGVs.push_back(Node);
907 return DIGlobalVariable(Node);
910 /// createVariable - Create a new descriptor for the specified variable.
911 DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
912 StringRef Name, DIFile File,
913 unsigned LineNo, DIType Ty,
914 bool AlwaysPreserve, unsigned Flags,
916 DIDescriptor Context(getNonCompileUnitScope(Scope));
917 assert((!Context || Context.Verify()) &&
918 "createLocalVariable should be called with a valid Context");
919 assert(Ty.Verify() &&
920 "createLocalVariable should be called with a valid type");
922 GetTagConstant(VMContext, Tag),
923 getNonCompileUnitScope(Scope),
924 MDString::get(VMContext, Name),
926 ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))),
928 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
929 Constant::getNullValue(Type::getInt32Ty(VMContext))
931 MDNode *Node = MDNode::get(VMContext, Elts);
932 if (AlwaysPreserve) {
933 // The optimizer may remove local variable. If there is an interest
934 // to preserve variable info in such situation then stash it in a
936 DISubprogram Fn(getDISubprogram(Scope));
937 NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, Fn);
938 FnLocals->addOperand(Node);
940 assert(DIVariable(Node).Verify() &&
941 "createLocalVariable should return a verifiable DIVariable");
942 return DIVariable(Node);
945 /// createComplexVariable - Create a new descriptor for the specified variable
946 /// which has a complex address expression for its address.
947 DIVariable DIBuilder::createComplexVariable(unsigned Tag, DIDescriptor Scope,
948 StringRef Name, DIFile F,
950 DIType Ty, ArrayRef<Value *> Addr,
952 SmallVector<Value *, 15> Elts;
953 Elts.push_back(GetTagConstant(VMContext, Tag));
954 Elts.push_back(getNonCompileUnitScope(Scope)),
955 Elts.push_back(MDString::get(VMContext, Name));
957 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext),
958 (LineNo | (ArgNo << 24))));
960 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
961 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
962 Elts.append(Addr.begin(), Addr.end());
964 return DIVariable(MDNode::get(VMContext, Elts));
967 /// createFunction - Create a new descriptor for the specified function.
968 DISubprogram DIBuilder::createFunction(DIDescriptor Context,
970 StringRef LinkageName,
971 DIFile File, unsigned LineNo,
973 bool isLocalToUnit, bool isDefinition,
975 unsigned Flags, bool isOptimized,
979 assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
980 "function types should be subroutines");
981 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
983 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
985 getNonCompileUnitScope(Context),
986 MDString::get(VMContext, Name),
987 MDString::get(VMContext, Name),
988 MDString::get(VMContext, LinkageName),
989 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
991 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
992 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
993 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
994 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
996 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
997 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1001 MDNode::getTemporary(VMContext, TElts),
1002 ConstantInt::get(Type::getInt32Ty(VMContext), ScopeLine)
1004 MDNode *Node = MDNode::get(VMContext, Elts);
1006 // Create a named metadata so that we do not lose this mdnode.
1008 AllSubprograms.push_back(Node);
1009 DISubprogram S(Node);
1010 assert(S.Verify() && "createFunction should return a valid DISubprogram");
1014 /// createMethod - Create a new descriptor for the specified C++ method.
1015 DISubprogram DIBuilder::createMethod(DIDescriptor Context,
1017 StringRef LinkageName,
1019 unsigned LineNo, DICompositeType Ty,
1022 unsigned VK, unsigned VIndex,
1023 MDNode *VTableHolder,
1028 assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
1029 "function types should be subroutines");
1030 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
1032 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
1034 getNonCompileUnitScope(Context),
1035 MDString::get(VMContext, Name),
1036 MDString::get(VMContext, Name),
1037 MDString::get(VMContext, LinkageName),
1038 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1040 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1041 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1042 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
1043 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
1045 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1046 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1049 Constant::getNullValue(Type::getInt32Ty(VMContext)),
1050 MDNode::getTemporary(VMContext, TElts),
1051 // FIXME: Do we want to use different scope/lines?
1052 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1054 MDNode *Node = MDNode::get(VMContext, Elts);
1056 AllSubprograms.push_back(Node);
1057 DISubprogram S(Node);
1058 assert(S.Verify() && "createMethod should return a valid DISubprogram");
1062 /// createNameSpace - This creates new descriptor for a namespace
1063 /// with the specified parent scope.
1064 DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
1065 DIFile File, unsigned LineNo) {
1067 GetTagConstant(VMContext, dwarf::DW_TAG_namespace),
1069 getNonCompileUnitScope(Scope),
1070 MDString::get(VMContext, Name),
1071 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1073 DINameSpace R(MDNode::get(VMContext, Elts));
1074 assert(R.Verify() &&
1075 "createNameSpace should return a verifiable DINameSpace");
1079 /// createLexicalBlockFile - This creates a new MDNode that encapsulates
1080 /// an existing scope with a new filename.
1081 DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
1084 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1088 DILexicalBlockFile R(MDNode::get(VMContext, Elts));
1091 "createLexicalBlockFile should return a verifiable DILexicalBlockFile");
1095 DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
1096 unsigned Line, unsigned Col) {
1097 // Defeat MDNode uniqing for lexical blocks by using unique id.
1098 static unsigned int unique_id = 0;
1100 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1102 getNonCompileUnitScope(Scope),
1103 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
1104 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1105 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
1107 DILexicalBlock R(MDNode::get(VMContext, Elts));
1108 assert(R.Verify() &&
1109 "createLexicalBlock should return a verifiable DILexicalBlock");
1113 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1114 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1115 Instruction *InsertBefore) {
1116 assert(Storage && "no storage passed to dbg.declare");
1117 assert(VarInfo.Verify() && "empty DIVariable passed to dbg.declare");
1119 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1121 Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1122 return CallInst::Create(DeclareFn, Args, "", InsertBefore);
1125 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1126 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1127 BasicBlock *InsertAtEnd) {
1128 assert(Storage && "no storage passed to dbg.declare");
1129 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.declare");
1131 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1133 Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1135 // If this block already has a terminator then insert this intrinsic
1136 // before the terminator.
1137 if (TerminatorInst *T = InsertAtEnd->getTerminator())
1138 return CallInst::Create(DeclareFn, Args, "", T);
1140 return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
1143 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1144 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1146 Instruction *InsertBefore) {
1147 assert(V && "no value passed to dbg.value");
1148 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
1150 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1152 Value *Args[] = { MDNode::get(V->getContext(), V),
1153 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1155 return CallInst::Create(ValueFn, Args, "", InsertBefore);
1158 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1159 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1161 BasicBlock *InsertAtEnd) {
1162 assert(V && "no value passed to dbg.value");
1163 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
1165 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1167 Value *Args[] = { MDNode::get(V->getContext(), V),
1168 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1170 return CallInst::Create(ValueFn, Args, "", InsertAtEnd);