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 static MDNode *createFilePathPair(LLVMContext &VMContext, StringRef Filename,
75 StringRef Directory) {
76 assert(!Filename.empty() && "Unable to create file without name");
78 MDString::get(VMContext, Filename),
79 MDString::get(VMContext, Directory),
81 return MDNode::get(VMContext, Pair);
84 /// createCompileUnit - A CompileUnit provides an anchor for all debugging
85 /// information generated during this instance of compilation.
86 void DIBuilder::createCompileUnit(unsigned Lang, StringRef Filename,
87 StringRef Directory, StringRef Producer,
88 bool isOptimized, StringRef Flags,
89 unsigned RunTimeVer, StringRef SplitName) {
90 assert(((Lang <= dwarf::DW_LANG_Python && Lang >= dwarf::DW_LANG_C89) ||
91 (Lang <= dwarf::DW_LANG_hi_user && Lang >= dwarf::DW_LANG_lo_user)) &&
92 "Invalid Language tag");
93 assert(!Filename.empty() &&
94 "Unable to create compile unit without filename");
95 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
96 TempEnumTypes = MDNode::getTemporary(VMContext, TElts);
98 TempRetainTypes = MDNode::getTemporary(VMContext, TElts);
100 TempSubprograms = MDNode::getTemporary(VMContext, TElts);
102 TempGVs = MDNode::getTemporary(VMContext, TElts);
105 GetTagConstant(VMContext, dwarf::DW_TAG_compile_unit),
106 createFilePathPair(VMContext, Filename, Directory),
107 ConstantInt::get(Type::getInt32Ty(VMContext), Lang),
108 MDString::get(VMContext, Producer),
109 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
110 MDString::get(VMContext, Flags),
111 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer),
116 MDString::get(VMContext, SplitName)
118 TheCU = DICompileUnit(MDNode::get(VMContext, Elts));
120 // Create a named metadata so that it is easier to find cu in a module.
121 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
122 NMD->addOperand(TheCU);
125 /// createFile - Create a file descriptor to hold debugging information
127 DIFile DIBuilder::createFile(StringRef Filename, StringRef Directory) {
129 GetTagConstant(VMContext, dwarf::DW_TAG_file_type),
130 createFilePathPair(VMContext, Filename, Directory)
132 return DIFile(MDNode::get(VMContext, Elts));
135 /// createEnumerator - Create a single enumerator value.
136 DIEnumerator DIBuilder::createEnumerator(StringRef Name, uint64_t Val) {
137 assert(!Name.empty() && "Unable to create enumerator without name");
139 GetTagConstant(VMContext, dwarf::DW_TAG_enumerator),
140 MDString::get(VMContext, Name),
141 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
143 return DIEnumerator(MDNode::get(VMContext, Elts));
146 /// createNullPtrType - Create C++0x nullptr type.
147 DIType DIBuilder::createNullPtrType(StringRef Name) {
148 assert(!Name.empty() && "Unable to create type without name");
149 // nullptr is encoded in DIBasicType format. Line number, filename,
150 // ,size, alignment, offset and flags are always empty here.
152 GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_type),
155 MDString::get(VMContext, Name),
156 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
157 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
158 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
159 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
160 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
161 ConstantInt::get(Type::getInt32Ty(VMContext), 0) // Encoding
163 return DIType(MDNode::get(VMContext, Elts));
166 /// createBasicType - Create debugging information entry for a basic
167 /// type, e.g 'char'.
169 DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
170 uint64_t AlignInBits, unsigned Encoding) {
171 assert(!Name.empty() && "Unable to create type without name");
172 // Basic types are encoded in DIBasicType format. Line number, filename,
173 // offset and flags are always empty here.
175 GetTagConstant(VMContext, dwarf::DW_TAG_base_type),
176 NULL, // File/directory name
178 MDString::get(VMContext, Name),
179 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
180 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
181 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
182 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
183 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
184 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
186 return DIBasicType(MDNode::get(VMContext, Elts));
189 /// createQualifiedType - Create debugging information entry for a qualified
190 /// type, e.g. 'const int'.
191 DIDerivedType DIBuilder::createQualifiedType(unsigned Tag, DIType FromTy) {
192 // Qualified types are encoded in DIDerivedType format.
194 GetTagConstant(VMContext, Tag),
197 MDString::get(VMContext, StringRef()), // Empty name.
198 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
199 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
200 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
201 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
202 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
205 return DIDerivedType(MDNode::get(VMContext, Elts));
208 /// createPointerType - Create debugging information entry for a pointer.
210 DIBuilder::createPointerType(DIType PointeeTy, uint64_t SizeInBits,
211 uint64_t AlignInBits, StringRef Name) {
212 // Pointer types are encoded in DIDerivedType format.
214 GetTagConstant(VMContext, dwarf::DW_TAG_pointer_type),
217 MDString::get(VMContext, Name),
218 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
219 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
220 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
221 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
222 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
225 return DIDerivedType(MDNode::get(VMContext, Elts));
228 DIDerivedType DIBuilder::createMemberPointerType(DIType PointeeTy, DIType Base) {
229 // Pointer types are encoded in DIDerivedType format.
231 GetTagConstant(VMContext, dwarf::DW_TAG_ptr_to_member_type),
235 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
236 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
237 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
238 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
239 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
243 return DIDerivedType(MDNode::get(VMContext, Elts));
246 /// createReferenceType - Create debugging information entry for a reference
248 DIDerivedType DIBuilder::createReferenceType(unsigned Tag, DIType RTy) {
249 assert(RTy.Verify() && "Unable to create reference type");
250 // References are encoded in DIDerivedType format.
252 GetTagConstant(VMContext, Tag),
256 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
257 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
258 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
259 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
260 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
263 return DIDerivedType(MDNode::get(VMContext, Elts));
266 /// createTypedef - Create debugging information entry for a typedef.
267 DIDerivedType DIBuilder::createTypedef(DIType Ty, StringRef Name, DIFile File,
268 unsigned LineNo, DIDescriptor Context) {
269 // typedefs are encoded in DIDerivedType format.
270 assert(Ty.Verify() && "Invalid typedef type!");
272 GetTagConstant(VMContext, dwarf::DW_TAG_typedef),
274 getNonCompileUnitScope(Context),
275 MDString::get(VMContext, Name),
276 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
277 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
278 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
279 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
280 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
283 return DIDerivedType(MDNode::get(VMContext, Elts));
286 /// createFriend - Create debugging information entry for a 'friend'.
287 DIType DIBuilder::createFriend(DIType Ty, DIType FriendTy) {
288 // typedefs are encoded in DIDerivedType format.
289 assert(Ty.Verify() && "Invalid type!");
290 assert(FriendTy.Verify() && "Invalid friend type!");
292 GetTagConstant(VMContext, dwarf::DW_TAG_friend),
296 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
297 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
298 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
299 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
300 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
303 return DIType(MDNode::get(VMContext, Elts));
306 /// createInheritance - Create debugging information entry to establish
307 /// inheritance relationship between two types.
308 DIDerivedType DIBuilder::createInheritance(
309 DIType Ty, DIType BaseTy, uint64_t BaseOffset, unsigned Flags) {
310 assert(Ty.Verify() && "Unable to create inheritance");
311 // TAG_inheritance is encoded in DIDerivedType format.
313 GetTagConstant(VMContext, dwarf::DW_TAG_inheritance),
317 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
318 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
319 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
320 ConstantInt::get(Type::getInt64Ty(VMContext), BaseOffset),
321 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
324 return DIDerivedType(MDNode::get(VMContext, Elts));
327 /// createMemberType - Create debugging information entry for a member.
328 DIDerivedType DIBuilder::createMemberType(
329 DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
330 uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
331 unsigned Flags, DIType Ty) {
332 // TAG_member is encoded in DIDerivedType format.
334 GetTagConstant(VMContext, dwarf::DW_TAG_member),
336 getNonCompileUnitScope(Scope),
337 MDString::get(VMContext, Name),
338 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
339 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
340 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
341 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
342 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
345 return DIDerivedType(MDNode::get(VMContext, Elts));
348 /// createStaticMemberType - Create debugging information entry for a
349 /// C++ static data member.
350 DIType DIBuilder::createStaticMemberType(DIDescriptor Scope, StringRef Name,
351 DIFile File, unsigned LineNumber,
352 DIType Ty, unsigned Flags,
354 // TAG_member is encoded in DIDerivedType format.
355 Flags |= DIDescriptor::FlagStaticMember;
357 GetTagConstant(VMContext, dwarf::DW_TAG_member),
359 getNonCompileUnitScope(Scope),
360 MDString::get(VMContext, Name),
361 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
362 ConstantInt::get(Type::getInt64Ty(VMContext), 0/*SizeInBits*/),
363 ConstantInt::get(Type::getInt64Ty(VMContext), 0/*AlignInBits*/),
364 ConstantInt::get(Type::getInt64Ty(VMContext), 0/*OffsetInBits*/),
365 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
369 return DIType(MDNode::get(VMContext, Elts));
372 /// createObjCIVar - Create debugging information entry for Objective-C
373 /// instance variable.
374 DIType DIBuilder::createObjCIVar(StringRef Name,
375 DIFile File, unsigned LineNumber,
376 uint64_t SizeInBits, uint64_t AlignInBits,
377 uint64_t OffsetInBits, unsigned Flags,
378 DIType Ty, StringRef PropertyName,
379 StringRef GetterName, StringRef SetterName,
380 unsigned PropertyAttributes) {
381 // TAG_member is encoded in DIDerivedType format.
383 GetTagConstant(VMContext, dwarf::DW_TAG_member),
385 getNonCompileUnitScope(File),
386 MDString::get(VMContext, Name),
387 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
388 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
389 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
390 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
391 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
393 MDString::get(VMContext, PropertyName),
394 MDString::get(VMContext, GetterName),
395 MDString::get(VMContext, SetterName),
396 ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes)
398 return DIType(MDNode::get(VMContext, Elts));
401 /// createObjCIVar - Create debugging information entry for Objective-C
402 /// instance variable.
403 DIType DIBuilder::createObjCIVar(StringRef Name,
404 DIFile File, unsigned LineNumber,
405 uint64_t SizeInBits, uint64_t AlignInBits,
406 uint64_t OffsetInBits, unsigned Flags,
407 DIType Ty, MDNode *PropertyNode) {
408 // TAG_member is encoded in DIDerivedType format.
410 GetTagConstant(VMContext, dwarf::DW_TAG_member),
412 getNonCompileUnitScope(File),
413 MDString::get(VMContext, Name),
414 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
415 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
416 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
417 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
418 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
422 return DIType(MDNode::get(VMContext, Elts));
425 /// createObjCProperty - Create debugging information entry for Objective-C
427 DIObjCProperty DIBuilder::createObjCProperty(StringRef Name,
428 DIFile File, unsigned LineNumber,
429 StringRef GetterName,
430 StringRef SetterName,
431 unsigned PropertyAttributes,
434 GetTagConstant(VMContext, dwarf::DW_TAG_APPLE_property),
435 MDString::get(VMContext, Name),
437 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
438 MDString::get(VMContext, GetterName),
439 MDString::get(VMContext, SetterName),
440 ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes),
443 return DIObjCProperty(MDNode::get(VMContext, Elts));
446 /// createTemplateTypeParameter - Create debugging information for template
448 DITemplateTypeParameter
449 DIBuilder::createTemplateTypeParameter(DIDescriptor Context, StringRef Name,
450 DIType Ty, MDNode *File, unsigned LineNo,
453 GetTagConstant(VMContext, dwarf::DW_TAG_template_type_parameter),
454 getNonCompileUnitScope(Context),
455 MDString::get(VMContext, Name),
458 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
459 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
461 return DITemplateTypeParameter(MDNode::get(VMContext, Elts));
464 /// createTemplateValueParameter - Create debugging information for template
466 DITemplateValueParameter
467 DIBuilder::createTemplateValueParameter(DIDescriptor Context, StringRef Name,
468 DIType Ty, uint64_t Val,
469 MDNode *File, unsigned LineNo,
472 GetTagConstant(VMContext, dwarf::DW_TAG_template_value_parameter),
473 getNonCompileUnitScope(Context),
474 MDString::get(VMContext, Name),
476 ConstantInt::get(Type::getInt64Ty(VMContext), Val),
478 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
479 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
481 return DITemplateValueParameter(MDNode::get(VMContext, Elts));
484 /// createClassType - Create debugging information entry for a class.
485 DICompositeType DIBuilder::createClassType(DIDescriptor Context, StringRef Name,
486 DIFile File, unsigned LineNumber,
488 uint64_t AlignInBits,
489 uint64_t OffsetInBits,
490 unsigned Flags, DIType DerivedFrom,
492 MDNode *VTableHolder,
493 MDNode *TemplateParams) {
494 assert((!Context || Context.Verify()) &&
495 "createClassType should be called with a valid Context");
496 // TAG_class_type is encoded in DICompositeType format.
498 GetTagConstant(VMContext, dwarf::DW_TAG_class_type),
500 getNonCompileUnitScope(Context),
501 MDString::get(VMContext, Name),
502 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
503 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
504 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
505 ConstantInt::get(Type::getInt32Ty(VMContext), OffsetInBits),
506 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
509 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
513 DICompositeType R(MDNode::get(VMContext, Elts));
514 assert(R.Verify() && "createClassType should return a verifiable DIType");
518 /// createStructType - Create debugging information entry for a struct.
519 DICompositeType DIBuilder::createStructType(DIDescriptor Context,
520 StringRef Name, DIFile File,
523 uint64_t AlignInBits,
524 unsigned Flags, DIType DerivedFrom,
526 unsigned RunTimeLang,
527 MDNode *VTableHolder) {
528 // TAG_structure_type is encoded in DICompositeType format.
530 GetTagConstant(VMContext, dwarf::DW_TAG_structure_type),
532 getNonCompileUnitScope(Context),
533 MDString::get(VMContext, Name),
534 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
535 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
536 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
537 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
538 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
541 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
545 DICompositeType R(MDNode::get(VMContext, Elts));
546 assert(R.Verify() && "createStructType should return a verifiable DIType");
550 /// createUnionType - Create debugging information entry for an union.
551 DICompositeType DIBuilder::createUnionType(
552 DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
553 uint64_t SizeInBits, uint64_t AlignInBits, unsigned Flags, DIArray Elements,
554 unsigned RunTimeLang) {
555 // TAG_union_type is encoded in DICompositeType format.
557 GetTagConstant(VMContext, dwarf::DW_TAG_union_type),
559 getNonCompileUnitScope(Scope),
560 MDString::get(VMContext, Name),
561 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
562 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
563 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
564 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
565 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
568 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
569 Constant::getNullValue(Type::getInt32Ty(VMContext))
571 return DICompositeType(MDNode::get(VMContext, Elts));
574 /// createSubroutineType - Create subroutine type.
576 DIBuilder::createSubroutineType(DIFile File, DIArray ParameterTypes) {
577 // TAG_subroutine_type is encoded in DICompositeType format.
579 GetTagConstant(VMContext, dwarf::DW_TAG_subroutine_type),
580 Constant::getNullValue(Type::getInt32Ty(VMContext)),
581 Constant::getNullValue(Type::getInt32Ty(VMContext)),
582 MDString::get(VMContext, ""),
583 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
584 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
585 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
586 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
587 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
590 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
591 Constant::getNullValue(Type::getInt32Ty(VMContext))
593 return DICompositeType(MDNode::get(VMContext, Elts));
596 /// createEnumerationType - Create debugging information entry for an
598 DICompositeType DIBuilder::createEnumerationType(
599 DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
600 uint64_t SizeInBits, uint64_t AlignInBits, DIArray Elements,
602 // TAG_enumeration_type is encoded in DICompositeType format.
604 GetTagConstant(VMContext, dwarf::DW_TAG_enumeration_type),
606 getNonCompileUnitScope(Scope),
607 MDString::get(VMContext, Name),
608 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
609 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
610 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
611 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
612 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
615 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
616 Constant::getNullValue(Type::getInt32Ty(VMContext))
618 MDNode *Node = MDNode::get(VMContext, Elts);
619 AllEnumTypes.push_back(Node);
620 return DICompositeType(Node);
623 /// createArrayType - Create debugging information entry for an array.
624 DICompositeType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
625 DIType Ty, DIArray Subscripts) {
626 // TAG_array_type is encoded in DICompositeType format.
628 GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
629 NULL, // Filename/Directory,
631 MDString::get(VMContext, ""),
632 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
633 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
634 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
635 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
636 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
639 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
640 Constant::getNullValue(Type::getInt32Ty(VMContext))
642 return DICompositeType(MDNode::get(VMContext, Elts));
645 /// createVectorType - Create debugging information entry for a vector.
646 DIType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits,
647 DIType Ty, DIArray Subscripts) {
649 // A vector is an array type with the FlagVector flag applied.
651 GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
652 NULL, // Filename/Directory,
654 MDString::get(VMContext, ""),
655 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
656 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
657 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
658 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
659 ConstantInt::get(Type::getInt32Ty(VMContext), DIType::FlagVector),
662 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
663 Constant::getNullValue(Type::getInt32Ty(VMContext))
665 return DIType(MDNode::get(VMContext, Elts));
668 /// createArtificialType - Create a new DIType with "artificial" flag set.
669 DIType DIBuilder::createArtificialType(DIType Ty) {
670 if (Ty.isArtificial())
673 SmallVector<Value *, 9> Elts;
675 assert (N && "Unexpected input DIType!");
676 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
677 if (Value *V = N->getOperand(i))
680 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
683 unsigned CurFlags = Ty.getFlags();
684 CurFlags = CurFlags | DIType::FlagArtificial;
686 // Flags are stored at this slot.
687 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
689 return DIType(MDNode::get(VMContext, Elts));
692 /// createObjectPointerType - Create a new type with both the object pointer
693 /// and artificial flags set.
694 DIType DIBuilder::createObjectPointerType(DIType Ty) {
695 if (Ty.isObjectPointer())
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::FlagObjectPointer | 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 /// retainType - Retain DIType in a module even if it is not referenced
718 /// through debug info anchors.
719 void DIBuilder::retainType(DIType T) {
720 AllRetainTypes.push_back(T);
723 /// createUnspecifiedParameter - Create unspeicified type descriptor
724 /// for the subroutine type.
725 DIDescriptor DIBuilder::createUnspecifiedParameter() {
727 GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_parameters)
729 return DIDescriptor(MDNode::get(VMContext, Elts));
732 /// createForwardDecl - Create a temporary forward-declared type that
733 /// can be RAUW'd if the full type is seen.
734 DIType DIBuilder::createForwardDecl(unsigned Tag, StringRef Name,
735 DIDescriptor Scope, DIFile F,
736 unsigned Line, unsigned RuntimeLang,
738 uint64_t AlignInBits) {
739 // Create a temporary MDNode.
741 GetTagConstant(VMContext, Tag),
743 getNonCompileUnitScope(Scope),
744 MDString::get(VMContext, Name),
745 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
746 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
747 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
748 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
749 ConstantInt::get(Type::getInt32Ty(VMContext),
750 DIDescriptor::FlagFwdDecl),
753 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
755 MDNode *Node = MDNode::getTemporary(VMContext, Elts);
756 assert(DIType(Node).Verify() &&
757 "createForwardDecl result should be verifiable");
761 /// getOrCreateArray - Get a DIArray, create one if required.
762 DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) {
763 if (Elements.empty()) {
764 Value *Null = Constant::getNullValue(Type::getInt32Ty(VMContext));
765 return DIArray(MDNode::get(VMContext, Null));
767 return DIArray(MDNode::get(VMContext, Elements));
770 /// getOrCreateSubrange - Create a descriptor for a value range. This
771 /// implicitly uniques the values returned.
772 DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
774 GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type),
775 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
776 ConstantInt::get(Type::getInt64Ty(VMContext), Count)
779 return DISubrange(MDNode::get(VMContext, Elts));
782 /// \brief Create a new descriptor for the specified global.
783 DIGlobalVariable DIBuilder::
784 createGlobalVariable(StringRef Name, StringRef LinkageName, DIFile F,
785 unsigned LineNumber, DIType Ty, bool isLocalToUnit,
788 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
789 Constant::getNullValue(Type::getInt32Ty(VMContext)),
791 MDString::get(VMContext, Name),
792 MDString::get(VMContext, Name),
793 MDString::get(VMContext, LinkageName),
795 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
797 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
798 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
802 MDNode *Node = MDNode::get(VMContext, Elts);
803 AllGVs.push_back(Node);
804 return DIGlobalVariable(Node);
807 /// \brief Create a new descriptor for the specified global.
808 DIGlobalVariable DIBuilder::
809 createGlobalVariable(StringRef Name, DIFile F, unsigned LineNumber,
810 DIType Ty, bool isLocalToUnit, Value *Val) {
811 return createGlobalVariable(Name, Name, F, LineNumber, Ty, isLocalToUnit,
815 /// createStaticVariable - Create a new descriptor for the specified static
817 DIGlobalVariable DIBuilder::
818 createStaticVariable(DIDescriptor Context, StringRef Name,
819 StringRef LinkageName, DIFile F, unsigned LineNumber,
820 DIType Ty, bool isLocalToUnit, Value *Val, MDNode *Decl) {
822 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
823 Constant::getNullValue(Type::getInt32Ty(VMContext)),
824 getNonCompileUnitScope(Context),
825 MDString::get(VMContext, Name),
826 MDString::get(VMContext, Name),
827 MDString::get(VMContext, LinkageName),
829 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
831 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
832 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
836 MDNode *Node = MDNode::get(VMContext, Elts);
837 AllGVs.push_back(Node);
838 return DIGlobalVariable(Node);
841 /// createVariable - Create a new descriptor for the specified variable.
842 DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
843 StringRef Name, DIFile File,
844 unsigned LineNo, DIType Ty,
845 bool AlwaysPreserve, unsigned Flags,
847 DIDescriptor Context(getNonCompileUnitScope(Scope));
848 assert((!Context || Context.Verify()) &&
849 "createLocalVariable should be called with a valid Context");
850 assert(Ty.Verify() &&
851 "createLocalVariable should be called with a valid type");
853 GetTagConstant(VMContext, Tag),
854 getNonCompileUnitScope(Scope),
855 MDString::get(VMContext, Name),
857 ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))),
859 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
860 Constant::getNullValue(Type::getInt32Ty(VMContext))
862 MDNode *Node = MDNode::get(VMContext, Elts);
863 if (AlwaysPreserve) {
864 // The optimizer may remove local variable. If there is an interest
865 // to preserve variable info in such situation then stash it in a
867 DISubprogram Fn(getDISubprogram(Scope));
868 NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, Fn);
869 FnLocals->addOperand(Node);
871 assert(DIVariable(Node).Verify() &&
872 "createLocalVariable should return a verifiable DIVariable");
873 return DIVariable(Node);
876 /// createComplexVariable - Create a new descriptor for the specified variable
877 /// which has a complex address expression for its address.
878 DIVariable DIBuilder::createComplexVariable(unsigned Tag, DIDescriptor Scope,
879 StringRef Name, DIFile F,
881 DIType Ty, ArrayRef<Value *> Addr,
883 SmallVector<Value *, 15> Elts;
884 Elts.push_back(GetTagConstant(VMContext, Tag));
885 Elts.push_back(getNonCompileUnitScope(Scope)),
886 Elts.push_back(MDString::get(VMContext, Name));
888 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext),
889 (LineNo | (ArgNo << 24))));
891 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
892 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
893 Elts.append(Addr.begin(), Addr.end());
895 return DIVariable(MDNode::get(VMContext, Elts));
898 /// createFunction - Create a new descriptor for the specified function.
899 DISubprogram DIBuilder::createFunction(DIDescriptor Context,
901 StringRef LinkageName,
902 DIFile File, unsigned LineNo,
904 bool isLocalToUnit, bool isDefinition,
906 unsigned Flags, bool isOptimized,
910 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
912 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
914 getNonCompileUnitScope(Context),
915 MDString::get(VMContext, Name),
916 MDString::get(VMContext, Name),
917 MDString::get(VMContext, LinkageName),
918 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
920 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
921 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
922 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
923 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
925 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
926 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
930 MDNode::getTemporary(VMContext, TElts),
931 ConstantInt::get(Type::getInt32Ty(VMContext), ScopeLine)
933 MDNode *Node = MDNode::get(VMContext, Elts);
935 // Create a named metadata so that we do not lose this mdnode.
937 AllSubprograms.push_back(Node);
938 DISubprogram S(Node);
939 assert(S.Verify() && "createFunction should return a valid DISubprogram");
943 /// createMethod - Create a new descriptor for the specified C++ method.
944 DISubprogram DIBuilder::createMethod(DIDescriptor Context,
946 StringRef LinkageName,
948 unsigned LineNo, DIType Ty,
951 unsigned VK, unsigned VIndex,
952 MDNode *VTableHolder,
957 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
959 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
961 getNonCompileUnitScope(Context),
962 MDString::get(VMContext, Name),
963 MDString::get(VMContext, Name),
964 MDString::get(VMContext, LinkageName),
965 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
967 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
968 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
969 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
970 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
972 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
973 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
976 Constant::getNullValue(Type::getInt32Ty(VMContext)),
977 MDNode::getTemporary(VMContext, TElts),
978 // FIXME: Do we want to use different scope/lines?
979 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
981 MDNode *Node = MDNode::get(VMContext, Elts);
983 AllSubprograms.push_back(Node);
984 DISubprogram S(Node);
985 assert(S.Verify() && "createMethod should return a valid DISubprogram");
989 /// createNameSpace - This creates new descriptor for a namespace
990 /// with the specified parent scope.
991 DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
992 DIFile File, unsigned LineNo) {
994 GetTagConstant(VMContext, dwarf::DW_TAG_namespace),
996 getNonCompileUnitScope(Scope),
997 MDString::get(VMContext, Name),
998 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1000 DINameSpace R(MDNode::get(VMContext, Elts));
1001 assert(R.Verify() &&
1002 "createNameSpace should return a verifiable DINameSpace");
1006 /// createLexicalBlockFile - This creates a new MDNode that encapsulates
1007 /// an existing scope with a new filename.
1008 DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
1011 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1015 DILexicalBlockFile R(MDNode::get(VMContext, Elts));
1018 "createLexicalBlockFile should return a verifiable DILexicalBlockFile");
1022 DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
1023 unsigned Line, unsigned Col) {
1024 // Defeat MDNode uniqing for lexical blocks by using unique id.
1025 static unsigned int unique_id = 0;
1027 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1029 getNonCompileUnitScope(Scope),
1030 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
1031 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1032 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
1034 DILexicalBlock R(MDNode::get(VMContext, Elts));
1035 assert(R.Verify() &&
1036 "createLexicalBlock should return a verifiable DILexicalBlock");
1040 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1041 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1042 Instruction *InsertBefore) {
1043 assert(Storage && "no storage passed to dbg.declare");
1044 assert(VarInfo.Verify() && "empty DIVariable passed to dbg.declare");
1046 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1048 Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1049 return CallInst::Create(DeclareFn, Args, "", InsertBefore);
1052 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1053 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1054 BasicBlock *InsertAtEnd) {
1055 assert(Storage && "no storage passed to dbg.declare");
1056 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.declare");
1058 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1060 Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1062 // If this block already has a terminator then insert this intrinsic
1063 // before the terminator.
1064 if (TerminatorInst *T = InsertAtEnd->getTerminator())
1065 return CallInst::Create(DeclareFn, Args, "", T);
1067 return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
1070 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1071 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1073 Instruction *InsertBefore) {
1074 assert(V && "no value passed to dbg.value");
1075 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
1077 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1079 Value *Args[] = { MDNode::get(V->getContext(), V),
1080 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1082 return CallInst::Create(ValueFn, Args, "", InsertBefore);
1085 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1086 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1088 BasicBlock *InsertAtEnd) {
1089 assert(V && "no value passed to dbg.value");
1090 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
1092 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1094 Value *Args[] = { MDNode::get(V->getContext(), V),
1095 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1097 return CallInst::Create(ValueFn, Args, "", InsertAtEnd);