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 DIImportedModule DIBuilder::createImportedModule(DIScope Context,
135 GetTagConstant(VMContext, dwarf::DW_TAG_imported_module),
138 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
140 DIImportedModule M(MDNode::get(VMContext, Elts));
141 assert(M.Verify() && "Imported module should be valid");
142 AllImportedModules.push_back(M);
146 /// createFile - Create a file descriptor to hold debugging information
148 DIFile DIBuilder::createFile(StringRef Filename, StringRef Directory) {
150 GetTagConstant(VMContext, dwarf::DW_TAG_file_type),
151 createFilePathPair(VMContext, Filename, Directory)
153 return DIFile(MDNode::get(VMContext, Elts));
156 /// createEnumerator - Create a single enumerator value.
157 DIEnumerator DIBuilder::createEnumerator(StringRef Name, uint64_t Val) {
158 assert(!Name.empty() && "Unable to create enumerator without name");
160 GetTagConstant(VMContext, dwarf::DW_TAG_enumerator),
161 MDString::get(VMContext, Name),
162 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
164 return DIEnumerator(MDNode::get(VMContext, Elts));
167 /// createNullPtrType - Create C++0x nullptr type.
168 DIType DIBuilder::createNullPtrType(StringRef Name) {
169 assert(!Name.empty() && "Unable to create type without name");
170 // nullptr is encoded in DIBasicType format. Line number, filename,
171 // ,size, alignment, offset and flags are always empty here.
173 GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_type),
176 MDString::get(VMContext, Name),
177 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
178 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
179 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
180 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
181 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
182 ConstantInt::get(Type::getInt32Ty(VMContext), 0) // Encoding
184 return DIType(MDNode::get(VMContext, Elts));
187 /// createBasicType - Create debugging information entry for a basic
188 /// type, e.g 'char'.
190 DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
191 uint64_t AlignInBits, unsigned Encoding) {
192 assert(!Name.empty() && "Unable to create type without name");
193 // Basic types are encoded in DIBasicType format. Line number, filename,
194 // offset and flags are always empty here.
196 GetTagConstant(VMContext, dwarf::DW_TAG_base_type),
197 NULL, // File/directory name
199 MDString::get(VMContext, Name),
200 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
201 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
202 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
203 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
204 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
205 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
207 return DIBasicType(MDNode::get(VMContext, Elts));
210 /// createQualifiedType - Create debugging information entry for a qualified
211 /// type, e.g. 'const int'.
212 DIDerivedType DIBuilder::createQualifiedType(unsigned Tag, DIType FromTy) {
213 // Qualified types are encoded in DIDerivedType format.
215 GetTagConstant(VMContext, Tag),
218 MDString::get(VMContext, StringRef()), // Empty name.
219 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
220 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
221 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
222 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
223 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
226 return DIDerivedType(MDNode::get(VMContext, Elts));
229 /// createPointerType - Create debugging information entry for a pointer.
231 DIBuilder::createPointerType(DIType PointeeTy, uint64_t SizeInBits,
232 uint64_t AlignInBits, StringRef Name) {
233 // Pointer types are encoded in DIDerivedType format.
235 GetTagConstant(VMContext, dwarf::DW_TAG_pointer_type),
238 MDString::get(VMContext, Name),
239 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
240 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
241 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
242 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
243 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
246 return DIDerivedType(MDNode::get(VMContext, Elts));
249 DIDerivedType DIBuilder::createMemberPointerType(DIType PointeeTy,
251 // Pointer types are encoded in DIDerivedType format.
253 GetTagConstant(VMContext, dwarf::DW_TAG_ptr_to_member_type),
257 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
258 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
259 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
260 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
261 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
265 return DIDerivedType(MDNode::get(VMContext, Elts));
268 /// createReferenceType - Create debugging information entry for a reference
270 DIDerivedType DIBuilder::createReferenceType(unsigned Tag, DIType RTy) {
271 assert(RTy.Verify() && "Unable to create reference type");
272 // References are encoded in DIDerivedType format.
274 GetTagConstant(VMContext, Tag),
278 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
279 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
280 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
281 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
282 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
285 return DIDerivedType(MDNode::get(VMContext, Elts));
288 /// createTypedef - Create debugging information entry for a typedef.
289 DIDerivedType DIBuilder::createTypedef(DIType Ty, StringRef Name, DIFile File,
290 unsigned LineNo, DIDescriptor Context) {
291 // typedefs are encoded in DIDerivedType format.
292 assert(Ty.Verify() && "Invalid typedef type!");
294 GetTagConstant(VMContext, dwarf::DW_TAG_typedef),
296 getNonCompileUnitScope(Context),
297 MDString::get(VMContext, Name),
298 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
299 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
300 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
301 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
302 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
305 return DIDerivedType(MDNode::get(VMContext, Elts));
308 /// createFriend - Create debugging information entry for a 'friend'.
309 DIType DIBuilder::createFriend(DIType Ty, DIType FriendTy) {
310 // typedefs are encoded in DIDerivedType format.
311 assert(Ty.Verify() && "Invalid type!");
312 assert(FriendTy.Verify() && "Invalid friend type!");
314 GetTagConstant(VMContext, dwarf::DW_TAG_friend),
318 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
319 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
320 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
321 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
322 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
325 return DIType(MDNode::get(VMContext, Elts));
328 /// createInheritance - Create debugging information entry to establish
329 /// inheritance relationship between two types.
330 DIDerivedType DIBuilder::createInheritance(
331 DIType Ty, DIType BaseTy, uint64_t BaseOffset, unsigned Flags) {
332 assert(Ty.Verify() && "Unable to create inheritance");
333 // TAG_inheritance is encoded in DIDerivedType format.
335 GetTagConstant(VMContext, dwarf::DW_TAG_inheritance),
339 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
340 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
341 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
342 ConstantInt::get(Type::getInt64Ty(VMContext), BaseOffset),
343 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
346 return DIDerivedType(MDNode::get(VMContext, Elts));
349 /// createMemberType - Create debugging information entry for a member.
350 DIDerivedType DIBuilder::createMemberType(
351 DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
352 uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
353 unsigned Flags, DIType Ty) {
354 // TAG_member is encoded in DIDerivedType format.
356 GetTagConstant(VMContext, dwarf::DW_TAG_member),
358 getNonCompileUnitScope(Scope),
359 MDString::get(VMContext, Name),
360 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
361 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
362 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
363 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
364 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
367 return DIDerivedType(MDNode::get(VMContext, Elts));
370 /// createStaticMemberType - Create debugging information entry for a
371 /// C++ static data member.
372 DIType DIBuilder::createStaticMemberType(DIDescriptor Scope, StringRef Name,
373 DIFile File, unsigned LineNumber,
374 DIType Ty, unsigned Flags,
376 // TAG_member is encoded in DIDerivedType format.
377 Flags |= DIDescriptor::FlagStaticMember;
379 GetTagConstant(VMContext, dwarf::DW_TAG_member),
381 getNonCompileUnitScope(Scope),
382 MDString::get(VMContext, Name),
383 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
384 ConstantInt::get(Type::getInt64Ty(VMContext), 0/*SizeInBits*/),
385 ConstantInt::get(Type::getInt64Ty(VMContext), 0/*AlignInBits*/),
386 ConstantInt::get(Type::getInt64Ty(VMContext), 0/*OffsetInBits*/),
387 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
391 return DIType(MDNode::get(VMContext, Elts));
394 /// createObjCIVar - Create debugging information entry for Objective-C
395 /// instance variable.
396 DIType DIBuilder::createObjCIVar(StringRef Name,
397 DIFile File, unsigned LineNumber,
398 uint64_t SizeInBits, uint64_t AlignInBits,
399 uint64_t OffsetInBits, unsigned Flags,
400 DIType Ty, StringRef PropertyName,
401 StringRef GetterName, StringRef SetterName,
402 unsigned PropertyAttributes) {
403 // TAG_member is encoded in DIDerivedType format.
405 GetTagConstant(VMContext, dwarf::DW_TAG_member),
407 getNonCompileUnitScope(File),
408 MDString::get(VMContext, Name),
409 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
410 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
411 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
412 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
413 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
415 MDString::get(VMContext, PropertyName),
416 MDString::get(VMContext, GetterName),
417 MDString::get(VMContext, SetterName),
418 ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes)
420 return DIType(MDNode::get(VMContext, Elts));
423 /// createObjCIVar - Create debugging information entry for Objective-C
424 /// instance variable.
425 DIType DIBuilder::createObjCIVar(StringRef Name,
426 DIFile File, unsigned LineNumber,
427 uint64_t SizeInBits, uint64_t AlignInBits,
428 uint64_t OffsetInBits, unsigned Flags,
429 DIType Ty, MDNode *PropertyNode) {
430 // TAG_member is encoded in DIDerivedType format.
432 GetTagConstant(VMContext, dwarf::DW_TAG_member),
434 getNonCompileUnitScope(File),
435 MDString::get(VMContext, Name),
436 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
437 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
438 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
439 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
440 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
444 return DIType(MDNode::get(VMContext, Elts));
447 /// createObjCProperty - Create debugging information entry for Objective-C
449 DIObjCProperty DIBuilder::createObjCProperty(StringRef Name,
450 DIFile File, unsigned LineNumber,
451 StringRef GetterName,
452 StringRef SetterName,
453 unsigned PropertyAttributes,
456 GetTagConstant(VMContext, dwarf::DW_TAG_APPLE_property),
457 MDString::get(VMContext, Name),
459 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
460 MDString::get(VMContext, GetterName),
461 MDString::get(VMContext, SetterName),
462 ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes),
465 return DIObjCProperty(MDNode::get(VMContext, Elts));
468 /// createTemplateTypeParameter - Create debugging information for template
470 DITemplateTypeParameter
471 DIBuilder::createTemplateTypeParameter(DIDescriptor Context, StringRef Name,
472 DIType Ty, MDNode *File, unsigned LineNo,
475 GetTagConstant(VMContext, dwarf::DW_TAG_template_type_parameter),
476 getNonCompileUnitScope(Context),
477 MDString::get(VMContext, Name),
480 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
481 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
483 return DITemplateTypeParameter(MDNode::get(VMContext, Elts));
486 /// createTemplateValueParameter - Create debugging information for template
488 DITemplateValueParameter
489 DIBuilder::createTemplateValueParameter(DIDescriptor Context, StringRef Name,
490 DIType Ty, uint64_t Val,
491 MDNode *File, unsigned LineNo,
494 GetTagConstant(VMContext, dwarf::DW_TAG_template_value_parameter),
495 getNonCompileUnitScope(Context),
496 MDString::get(VMContext, Name),
498 ConstantInt::get(Type::getInt64Ty(VMContext), Val),
500 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
501 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
503 return DITemplateValueParameter(MDNode::get(VMContext, Elts));
506 /// createClassType - Create debugging information entry for a class.
507 DICompositeType DIBuilder::createClassType(DIDescriptor Context, StringRef Name,
508 DIFile File, unsigned LineNumber,
510 uint64_t AlignInBits,
511 uint64_t OffsetInBits,
512 unsigned Flags, DIType DerivedFrom,
514 MDNode *VTableHolder,
515 MDNode *TemplateParams) {
516 assert((!Context || Context.Verify()) &&
517 "createClassType should be called with a valid Context");
518 // TAG_class_type is encoded in DICompositeType format.
520 GetTagConstant(VMContext, dwarf::DW_TAG_class_type),
522 getNonCompileUnitScope(Context),
523 MDString::get(VMContext, Name),
524 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
525 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
526 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
527 ConstantInt::get(Type::getInt32Ty(VMContext), OffsetInBits),
528 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
531 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
535 DICompositeType R(MDNode::get(VMContext, Elts));
536 assert(R.Verify() && "createClassType should return a verifiable DIType");
540 /// createStructType - Create debugging information entry for a struct.
541 DICompositeType DIBuilder::createStructType(DIDescriptor Context,
542 StringRef Name, DIFile File,
545 uint64_t AlignInBits,
546 unsigned Flags, DIType DerivedFrom,
548 unsigned RunTimeLang,
549 MDNode *VTableHolder) {
550 // TAG_structure_type is encoded in DICompositeType format.
552 GetTagConstant(VMContext, dwarf::DW_TAG_structure_type),
554 getNonCompileUnitScope(Context),
555 MDString::get(VMContext, Name),
556 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
557 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
558 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
559 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
560 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
563 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
567 DICompositeType R(MDNode::get(VMContext, Elts));
568 assert(R.Verify() && "createStructType should return a verifiable DIType");
572 /// createUnionType - Create debugging information entry for an union.
573 DICompositeType DIBuilder::createUnionType(DIDescriptor Scope, StringRef Name,
574 DIFile File, unsigned LineNumber,
576 uint64_t AlignInBits, unsigned Flags,
578 unsigned RunTimeLang) {
579 // TAG_union_type is encoded in DICompositeType format.
581 GetTagConstant(VMContext, dwarf::DW_TAG_union_type),
583 getNonCompileUnitScope(Scope),
584 MDString::get(VMContext, Name),
585 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
586 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
587 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
588 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
589 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
592 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
593 Constant::getNullValue(Type::getInt32Ty(VMContext)),
596 return DICompositeType(MDNode::get(VMContext, Elts));
599 /// createSubroutineType - Create subroutine type.
601 DIBuilder::createSubroutineType(DIFile File, DIArray ParameterTypes) {
602 // TAG_subroutine_type is encoded in DICompositeType format.
604 GetTagConstant(VMContext, dwarf::DW_TAG_subroutine_type),
605 Constant::getNullValue(Type::getInt32Ty(VMContext)),
606 Constant::getNullValue(Type::getInt32Ty(VMContext)),
607 MDString::get(VMContext, ""),
608 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
609 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
610 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
611 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
612 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
615 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
616 Constant::getNullValue(Type::getInt32Ty(VMContext))
618 return DICompositeType(MDNode::get(VMContext, Elts));
621 /// createEnumerationType - Create debugging information entry for an
623 DICompositeType DIBuilder::createEnumerationType(
624 DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
625 uint64_t SizeInBits, uint64_t AlignInBits, DIArray Elements,
626 DIType UnderlyingType) {
627 // TAG_enumeration_type is encoded in DICompositeType format.
629 GetTagConstant(VMContext, dwarf::DW_TAG_enumeration_type),
631 getNonCompileUnitScope(Scope),
632 MDString::get(VMContext, Name),
633 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
634 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
635 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
636 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
637 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
640 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
641 Constant::getNullValue(Type::getInt32Ty(VMContext))
643 MDNode *Node = MDNode::get(VMContext, Elts);
644 AllEnumTypes.push_back(Node);
645 return DICompositeType(Node);
648 /// createArrayType - Create debugging information entry for an array.
649 DICompositeType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
650 DIType Ty, DIArray Subscripts) {
651 // TAG_array_type is encoded in DICompositeType format.
653 GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
654 NULL, // Filename/Directory,
656 MDString::get(VMContext, ""),
657 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
658 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
659 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
660 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
661 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
664 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
665 Constant::getNullValue(Type::getInt32Ty(VMContext))
667 return DICompositeType(MDNode::get(VMContext, Elts));
670 /// createVectorType - Create debugging information entry for a vector.
671 DIType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits,
672 DIType Ty, DIArray Subscripts) {
674 // A vector is an array type with the FlagVector flag applied.
676 GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
677 NULL, // Filename/Directory,
679 MDString::get(VMContext, ""),
680 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
681 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
682 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
683 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
684 ConstantInt::get(Type::getInt32Ty(VMContext), DIType::FlagVector),
687 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
688 Constant::getNullValue(Type::getInt32Ty(VMContext))
690 return DIType(MDNode::get(VMContext, Elts));
693 /// createArtificialType - Create a new DIType with "artificial" flag set.
694 DIType DIBuilder::createArtificialType(DIType Ty) {
695 if (Ty.isArtificial())
698 SmallVector<Value *, 9> Elts;
700 assert (N && "Unexpected input DIType!");
701 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
702 if (Value *V = N->getOperand(i))
705 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
708 unsigned CurFlags = Ty.getFlags();
709 CurFlags = CurFlags | DIType::FlagArtificial;
711 // Flags are stored at this slot.
712 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
714 return DIType(MDNode::get(VMContext, Elts));
717 /// createObjectPointerType - Create a new type with both the object pointer
718 /// and artificial flags set.
719 DIType DIBuilder::createObjectPointerType(DIType Ty) {
720 if (Ty.isObjectPointer())
723 SmallVector<Value *, 9> Elts;
725 assert (N && "Unexpected input DIType!");
726 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
727 if (Value *V = N->getOperand(i))
730 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
733 unsigned CurFlags = Ty.getFlags();
734 CurFlags = CurFlags | (DIType::FlagObjectPointer | DIType::FlagArtificial);
736 // Flags are stored at this slot.
737 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
739 return DIType(MDNode::get(VMContext, Elts));
742 /// retainType - Retain DIType in a module even if it is not referenced
743 /// through debug info anchors.
744 void DIBuilder::retainType(DIType T) {
745 AllRetainTypes.push_back(T);
748 /// createUnspecifiedParameter - Create unspeicified type descriptor
749 /// for the subroutine type.
750 DIDescriptor DIBuilder::createUnspecifiedParameter() {
752 GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_parameters)
754 return DIDescriptor(MDNode::get(VMContext, Elts));
757 /// createForwardDecl - Create a temporary forward-declared type that
758 /// can be RAUW'd if the full type is seen.
759 DIType DIBuilder::createForwardDecl(unsigned Tag, StringRef Name,
760 DIDescriptor Scope, DIFile F,
761 unsigned Line, unsigned RuntimeLang,
763 uint64_t AlignInBits) {
764 // Create a temporary MDNode.
766 GetTagConstant(VMContext, Tag),
768 getNonCompileUnitScope(Scope),
769 MDString::get(VMContext, Name),
770 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
771 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
772 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
773 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
774 ConstantInt::get(Type::getInt32Ty(VMContext),
775 DIDescriptor::FlagFwdDecl),
778 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
780 MDNode *Node = MDNode::getTemporary(VMContext, Elts);
781 assert(DIType(Node).Verify() &&
782 "createForwardDecl result should be verifiable");
786 /// getOrCreateArray - Get a DIArray, create one if required.
787 DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) {
788 if (Elements.empty()) {
789 Value *Null = Constant::getNullValue(Type::getInt32Ty(VMContext));
790 return DIArray(MDNode::get(VMContext, Null));
792 return DIArray(MDNode::get(VMContext, Elements));
795 /// getOrCreateSubrange - Create a descriptor for a value range. This
796 /// implicitly uniques the values returned.
797 DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
799 GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type),
800 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
801 ConstantInt::get(Type::getInt64Ty(VMContext), Count)
804 return DISubrange(MDNode::get(VMContext, Elts));
807 /// \brief Create a new descriptor for the specified global.
808 DIGlobalVariable DIBuilder::
809 createGlobalVariable(StringRef Name, StringRef LinkageName, DIFile F,
810 unsigned LineNumber, DIType Ty, bool isLocalToUnit,
813 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
814 Constant::getNullValue(Type::getInt32Ty(VMContext)),
816 MDString::get(VMContext, Name),
817 MDString::get(VMContext, Name),
818 MDString::get(VMContext, LinkageName),
820 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
822 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
823 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
827 MDNode *Node = MDNode::get(VMContext, Elts);
828 AllGVs.push_back(Node);
829 return DIGlobalVariable(Node);
832 /// \brief Create a new descriptor for the specified global.
833 DIGlobalVariable DIBuilder::
834 createGlobalVariable(StringRef Name, DIFile F, unsigned LineNumber,
835 DIType Ty, bool isLocalToUnit, Value *Val) {
836 return createGlobalVariable(Name, Name, F, LineNumber, Ty, isLocalToUnit,
840 /// createStaticVariable - Create a new descriptor for the specified static
842 DIGlobalVariable DIBuilder::
843 createStaticVariable(DIDescriptor Context, StringRef Name,
844 StringRef LinkageName, DIFile F, unsigned LineNumber,
845 DIType Ty, bool isLocalToUnit, Value *Val, MDNode *Decl) {
847 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
848 Constant::getNullValue(Type::getInt32Ty(VMContext)),
849 getNonCompileUnitScope(Context),
850 MDString::get(VMContext, Name),
851 MDString::get(VMContext, Name),
852 MDString::get(VMContext, LinkageName),
854 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
856 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
857 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
861 MDNode *Node = MDNode::get(VMContext, Elts);
862 AllGVs.push_back(Node);
863 return DIGlobalVariable(Node);
866 /// createVariable - Create a new descriptor for the specified variable.
867 DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
868 StringRef Name, DIFile File,
869 unsigned LineNo, DIType Ty,
870 bool AlwaysPreserve, unsigned Flags,
872 DIDescriptor Context(getNonCompileUnitScope(Scope));
873 assert((!Context || Context.Verify()) &&
874 "createLocalVariable should be called with a valid Context");
875 assert(Ty.Verify() &&
876 "createLocalVariable should be called with a valid type");
878 GetTagConstant(VMContext, Tag),
879 getNonCompileUnitScope(Scope),
880 MDString::get(VMContext, Name),
882 ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))),
884 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
885 Constant::getNullValue(Type::getInt32Ty(VMContext))
887 MDNode *Node = MDNode::get(VMContext, Elts);
888 if (AlwaysPreserve) {
889 // The optimizer may remove local variable. If there is an interest
890 // to preserve variable info in such situation then stash it in a
892 DISubprogram Fn(getDISubprogram(Scope));
893 NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, Fn);
894 FnLocals->addOperand(Node);
896 assert(DIVariable(Node).Verify() &&
897 "createLocalVariable should return a verifiable DIVariable");
898 return DIVariable(Node);
901 /// createComplexVariable - Create a new descriptor for the specified variable
902 /// which has a complex address expression for its address.
903 DIVariable DIBuilder::createComplexVariable(unsigned Tag, DIDescriptor Scope,
904 StringRef Name, DIFile F,
906 DIType Ty, ArrayRef<Value *> Addr,
908 SmallVector<Value *, 15> Elts;
909 Elts.push_back(GetTagConstant(VMContext, Tag));
910 Elts.push_back(getNonCompileUnitScope(Scope)),
911 Elts.push_back(MDString::get(VMContext, Name));
913 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext),
914 (LineNo | (ArgNo << 24))));
916 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
917 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
918 Elts.append(Addr.begin(), Addr.end());
920 return DIVariable(MDNode::get(VMContext, Elts));
923 /// createFunction - Create a new descriptor for the specified function.
924 DISubprogram DIBuilder::createFunction(DIDescriptor Context,
926 StringRef LinkageName,
927 DIFile File, unsigned LineNo,
929 bool isLocalToUnit, bool isDefinition,
931 unsigned Flags, bool isOptimized,
935 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
937 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
939 getNonCompileUnitScope(Context),
940 MDString::get(VMContext, Name),
941 MDString::get(VMContext, Name),
942 MDString::get(VMContext, LinkageName),
943 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
945 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
946 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
947 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
948 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
950 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
951 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
955 MDNode::getTemporary(VMContext, TElts),
956 ConstantInt::get(Type::getInt32Ty(VMContext), ScopeLine)
958 MDNode *Node = MDNode::get(VMContext, Elts);
960 // Create a named metadata so that we do not lose this mdnode.
962 AllSubprograms.push_back(Node);
963 DISubprogram S(Node);
964 assert(S.Verify() && "createFunction should return a valid DISubprogram");
968 /// createMethod - Create a new descriptor for the specified C++ method.
969 DISubprogram DIBuilder::createMethod(DIDescriptor Context,
971 StringRef LinkageName,
973 unsigned LineNo, DIType Ty,
976 unsigned VK, unsigned VIndex,
977 MDNode *VTableHolder,
982 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
984 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
986 getNonCompileUnitScope(Context),
987 MDString::get(VMContext, Name),
988 MDString::get(VMContext, Name),
989 MDString::get(VMContext, LinkageName),
990 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
992 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
993 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
994 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
995 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
997 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
998 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1001 Constant::getNullValue(Type::getInt32Ty(VMContext)),
1002 MDNode::getTemporary(VMContext, TElts),
1003 // FIXME: Do we want to use different scope/lines?
1004 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1006 MDNode *Node = MDNode::get(VMContext, Elts);
1008 AllSubprograms.push_back(Node);
1009 DISubprogram S(Node);
1010 assert(S.Verify() && "createMethod should return a valid DISubprogram");
1014 /// createNameSpace - This creates new descriptor for a namespace
1015 /// with the specified parent scope.
1016 DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
1017 DIFile File, unsigned LineNo) {
1019 GetTagConstant(VMContext, dwarf::DW_TAG_namespace),
1021 getNonCompileUnitScope(Scope),
1022 MDString::get(VMContext, Name),
1023 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1025 DINameSpace R(MDNode::get(VMContext, Elts));
1026 assert(R.Verify() &&
1027 "createNameSpace should return a verifiable DINameSpace");
1031 /// createLexicalBlockFile - This creates a new MDNode that encapsulates
1032 /// an existing scope with a new filename.
1033 DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
1036 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1040 DILexicalBlockFile R(MDNode::get(VMContext, Elts));
1043 "createLexicalBlockFile should return a verifiable DILexicalBlockFile");
1047 DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
1048 unsigned Line, unsigned Col) {
1049 // Defeat MDNode uniqing for lexical blocks by using unique id.
1050 static unsigned int unique_id = 0;
1052 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1054 getNonCompileUnitScope(Scope),
1055 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
1056 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1057 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
1059 DILexicalBlock R(MDNode::get(VMContext, Elts));
1060 assert(R.Verify() &&
1061 "createLexicalBlock should return a verifiable DILexicalBlock");
1065 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1066 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1067 Instruction *InsertBefore) {
1068 assert(Storage && "no storage passed to dbg.declare");
1069 assert(VarInfo.Verify() && "empty DIVariable passed to dbg.declare");
1071 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1073 Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1074 return CallInst::Create(DeclareFn, Args, "", InsertBefore);
1077 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1078 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1079 BasicBlock *InsertAtEnd) {
1080 assert(Storage && "no storage passed to dbg.declare");
1081 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.declare");
1083 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1085 Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1087 // If this block already has a terminator then insert this intrinsic
1088 // before the terminator.
1089 if (TerminatorInst *T = InsertAtEnd->getTerminator())
1090 return CallInst::Create(DeclareFn, Args, "", T);
1092 return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
1095 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1096 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1098 Instruction *InsertBefore) {
1099 assert(V && "no value passed to dbg.value");
1100 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
1102 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1104 Value *Args[] = { MDNode::get(V->getContext(), V),
1105 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1107 return CallInst::Create(ValueFn, Args, "", InsertBefore);
1110 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1111 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1113 BasicBlock *InsertAtEnd) {
1114 assert(V && "no value passed to dbg.value");
1115 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
1117 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1119 Value *Args[] = { MDNode::get(V->getContext(), V),
1120 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1122 return CallInst::Create(ValueFn, Args, "", InsertAtEnd);