1 //===--- DIBuilder.cpp - Debug Information Builder ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the DIBuilder.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/DIBuilder.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/DebugInfo.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/IntrinsicInst.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/Dwarf.h"
24 using namespace llvm::dwarf;
26 static Constant *GetTagConstant(LLVMContext &VMContext, unsigned Tag) {
27 assert((Tag & LLVMDebugVersionMask) == 0 &&
28 "Tag too large for debug encoding!");
29 return ConstantInt::get(Type::getInt32Ty(VMContext), Tag | LLVMDebugVersion);
32 DIBuilder::DIBuilder(Module &m)
33 : M(m), VMContext(M.getContext()), TheCU(0), TempEnumTypes(0),
34 TempRetainTypes(0), TempSubprograms(0), TempGVs(0), DeclareFn(0),
38 /// finalize - Construct any deferred debug info descriptors.
39 void DIBuilder::finalize() {
40 DIArray Enums = getOrCreateArray(AllEnumTypes);
41 DIType(TempEnumTypes).replaceAllUsesWith(Enums);
43 DIArray RetainTypes = getOrCreateArray(AllRetainTypes);
44 DIType(TempRetainTypes).replaceAllUsesWith(RetainTypes);
46 DIArray SPs = getOrCreateArray(AllSubprograms);
47 DIType(TempSubprograms).replaceAllUsesWith(SPs);
48 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
49 DISubprogram SP(SPs.getElement(i));
50 SmallVector<Value *, 4> Variables;
51 if (NamedMDNode *NMD = getFnSpecificMDNode(M, SP)) {
52 for (unsigned ii = 0, ee = NMD->getNumOperands(); ii != ee; ++ii)
53 Variables.push_back(NMD->getOperand(ii));
54 NMD->eraseFromParent();
56 if (MDNode *Temp = SP.getVariablesNodes()) {
57 DIArray AV = getOrCreateArray(Variables);
58 DIType(Temp).replaceAllUsesWith(AV);
62 DIArray GVs = getOrCreateArray(AllGVs);
63 DIType(TempGVs).replaceAllUsesWith(GVs);
66 /// getNonCompileUnitScope - If N is compile unit return NULL otherwise return
68 static MDNode *getNonCompileUnitScope(MDNode *N) {
69 if (DIDescriptor(N).isCompileUnit())
74 /// createCompileUnit - A CompileUnit provides an anchor for all debugging
75 /// information generated during this instance of compilation.
76 void DIBuilder::createCompileUnit(unsigned Lang, StringRef Filename,
77 StringRef Directory, StringRef Producer,
78 bool isOptimized, StringRef Flags,
79 unsigned RunTimeVer, StringRef SplitName) {
80 assert(((Lang <= dwarf::DW_LANG_Python && Lang >= dwarf::DW_LANG_C89) ||
81 (Lang <= dwarf::DW_LANG_hi_user && Lang >= dwarf::DW_LANG_lo_user)) &&
82 "Invalid Language tag");
83 assert(!Filename.empty() &&
84 "Unable to create compile unit without filename");
85 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
86 TempEnumTypes = MDNode::getTemporary(VMContext, TElts);
88 TempRetainTypes = MDNode::getTemporary(VMContext, TElts);
90 TempSubprograms = MDNode::getTemporary(VMContext, TElts);
92 TempGVs = MDNode::getTemporary(VMContext, TElts);
95 GetTagConstant(VMContext, dwarf::DW_TAG_compile_unit),
96 Constant::getNullValue(Type::getInt32Ty(VMContext)),
97 ConstantInt::get(Type::getInt32Ty(VMContext), Lang),
98 createFile(Filename, Directory),
99 MDString::get(VMContext, Producer),
100 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
101 MDString::get(VMContext, Flags),
102 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer),
107 MDString::get(VMContext, SplitName)
109 TheCU = DICompileUnit(MDNode::get(VMContext, Elts));
111 // Create a named metadata so that it is easier to find cu in a module.
112 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
113 NMD->addOperand(TheCU);
116 /// createFile - Create a file descriptor to hold debugging information
118 DIFile DIBuilder::createFile(StringRef Filename, StringRef Directory) {
119 assert(!Filename.empty() && "Unable to create file without name");
121 GetTagConstant(VMContext, dwarf::DW_TAG_file_type),
122 MDString::get(VMContext, Filename),
123 MDString::get(VMContext, Directory),
126 return DIFile(MDNode::get(VMContext, Elts));
129 /// createEnumerator - Create a single enumerator value.
130 DIEnumerator DIBuilder::createEnumerator(StringRef Name, uint64_t Val) {
131 assert(!Name.empty() && "Unable to create enumerator without name");
133 GetTagConstant(VMContext, dwarf::DW_TAG_enumerator),
134 MDString::get(VMContext, Name),
135 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
137 return DIEnumerator(MDNode::get(VMContext, Elts));
140 /// createNullPtrType - Create C++0x nullptr type.
141 DIType DIBuilder::createNullPtrType(StringRef Name) {
142 assert(!Name.empty() && "Unable to create type without name");
143 // nullptr is encoded in DIBasicType format. Line number, filename,
144 // ,size, alignment, offset and flags are always empty here.
146 GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_type),
148 MDString::get(VMContext, Name),
150 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
151 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
152 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
153 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
154 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
155 ConstantInt::get(Type::getInt32Ty(VMContext), 0) // Encoding
157 return DIType(MDNode::get(VMContext, Elts));
160 /// createBasicType - Create debugging information entry for a basic
161 /// type, e.g 'char'.
163 DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
164 uint64_t AlignInBits, unsigned Encoding) {
165 assert(!Name.empty() && "Unable to create type without name");
166 // Basic types are encoded in DIBasicType format. Line number, filename,
167 // offset and flags are always empty here.
169 GetTagConstant(VMContext, dwarf::DW_TAG_base_type),
171 MDString::get(VMContext, Name),
173 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
174 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
175 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
176 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
177 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
178 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
180 return DIBasicType(MDNode::get(VMContext, Elts));
183 /// createQualifiedType - Create debugging information entry for a qualified
184 /// type, e.g. 'const int'.
185 DIDerivedType DIBuilder::createQualifiedType(unsigned Tag, DIType FromTy) {
186 // Qualified types are encoded in DIDerivedType format.
188 GetTagConstant(VMContext, Tag),
190 MDString::get(VMContext, StringRef()), // Empty name.
192 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
193 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
194 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
195 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
196 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
199 return DIDerivedType(MDNode::get(VMContext, Elts));
202 /// createPointerType - Create debugging information entry for a pointer.
204 DIBuilder::createPointerType(DIType PointeeTy, uint64_t SizeInBits,
205 uint64_t AlignInBits, StringRef Name) {
206 // Pointer types are encoded in DIDerivedType format.
208 GetTagConstant(VMContext, dwarf::DW_TAG_pointer_type),
210 MDString::get(VMContext, Name),
212 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
213 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
214 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
215 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
216 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
219 return DIDerivedType(MDNode::get(VMContext, Elts));
222 DIDerivedType DIBuilder::createMemberPointerType(DIType PointeeTy, DIType Base) {
223 // Pointer types are encoded in DIDerivedType format.
225 GetTagConstant(VMContext, dwarf::DW_TAG_ptr_to_member_type),
229 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
230 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
231 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
232 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
233 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
237 return DIDerivedType(MDNode::get(VMContext, Elts));
240 /// createReferenceType - Create debugging information entry for a reference
242 DIDerivedType DIBuilder::createReferenceType(unsigned Tag, DIType RTy) {
243 assert(RTy.Verify() && "Unable to create reference type");
244 // References are encoded in DIDerivedType format.
246 GetTagConstant(VMContext, Tag),
250 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
251 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
252 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
253 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
254 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
257 return DIDerivedType(MDNode::get(VMContext, Elts));
260 /// createTypedef - Create debugging information entry for a typedef.
261 DIDerivedType DIBuilder::createTypedef(DIType Ty, StringRef Name, DIFile File,
262 unsigned LineNo, DIDescriptor Context) {
263 // typedefs are encoded in DIDerivedType format.
264 assert(Ty.Verify() && "Invalid typedef type!");
266 GetTagConstant(VMContext, dwarf::DW_TAG_typedef),
267 getNonCompileUnitScope(Context),
268 MDString::get(VMContext, Name),
270 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
271 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
272 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
273 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
274 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
277 return DIDerivedType(MDNode::get(VMContext, Elts));
280 /// createFriend - Create debugging information entry for a 'friend'.
281 DIType DIBuilder::createFriend(DIType Ty, DIType FriendTy) {
282 // typedefs are encoded in DIDerivedType format.
283 assert(Ty.Verify() && "Invalid type!");
284 assert(FriendTy.Verify() && "Invalid friend type!");
286 GetTagConstant(VMContext, dwarf::DW_TAG_friend),
290 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
291 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
292 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
293 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
294 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
297 return DIType(MDNode::get(VMContext, Elts));
300 /// createInheritance - Create debugging information entry to establish
301 /// inheritance relationship between two types.
302 DIDerivedType DIBuilder::createInheritance(
303 DIType Ty, DIType BaseTy, uint64_t BaseOffset, unsigned Flags) {
304 assert(Ty.Verify() && "Unable to create inheritance");
305 // TAG_inheritance is encoded in DIDerivedType format.
307 GetTagConstant(VMContext, dwarf::DW_TAG_inheritance),
311 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
312 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
313 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
314 ConstantInt::get(Type::getInt64Ty(VMContext), BaseOffset),
315 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
318 return DIDerivedType(MDNode::get(VMContext, Elts));
321 /// createMemberType - Create debugging information entry for a member.
322 DIDerivedType DIBuilder::createMemberType(
323 DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
324 uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
325 unsigned Flags, DIType Ty) {
326 // TAG_member is encoded in DIDerivedType format.
328 GetTagConstant(VMContext, dwarf::DW_TAG_member),
329 getNonCompileUnitScope(Scope),
330 MDString::get(VMContext, Name),
332 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
333 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
334 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
335 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
336 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
339 return DIDerivedType(MDNode::get(VMContext, Elts));
342 /// createStaticMemberType - Create debugging information entry for a
343 /// C++ static data member.
344 DIType DIBuilder::createStaticMemberType(DIDescriptor Scope, StringRef Name,
345 DIFile File, unsigned LineNumber,
346 DIType Ty, unsigned Flags,
348 // TAG_member is encoded in DIDerivedType format.
349 Flags |= DIDescriptor::FlagStaticMember;
351 GetTagConstant(VMContext, dwarf::DW_TAG_member),
352 getNonCompileUnitScope(Scope),
353 MDString::get(VMContext, Name),
355 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
356 ConstantInt::get(Type::getInt64Ty(VMContext), 0/*SizeInBits*/),
357 ConstantInt::get(Type::getInt64Ty(VMContext), 0/*AlignInBits*/),
358 ConstantInt::get(Type::getInt64Ty(VMContext), 0/*OffsetInBits*/),
359 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
363 return DIType(MDNode::get(VMContext, Elts));
366 /// createObjCIVar - Create debugging information entry for Objective-C
367 /// instance variable.
368 DIType DIBuilder::createObjCIVar(StringRef Name,
369 DIFile File, unsigned LineNumber,
370 uint64_t SizeInBits, uint64_t AlignInBits,
371 uint64_t OffsetInBits, unsigned Flags,
372 DIType Ty, StringRef PropertyName,
373 StringRef GetterName, StringRef SetterName,
374 unsigned PropertyAttributes) {
375 // TAG_member is encoded in DIDerivedType format.
377 GetTagConstant(VMContext, dwarf::DW_TAG_member),
378 getNonCompileUnitScope(File),
379 MDString::get(VMContext, Name),
381 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
382 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
383 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
384 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
385 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
387 MDString::get(VMContext, PropertyName),
388 MDString::get(VMContext, GetterName),
389 MDString::get(VMContext, SetterName),
390 ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes)
392 return DIType(MDNode::get(VMContext, Elts));
395 /// createObjCIVar - Create debugging information entry for Objective-C
396 /// instance variable.
397 DIType DIBuilder::createObjCIVar(StringRef Name,
398 DIFile File, unsigned LineNumber,
399 uint64_t SizeInBits, uint64_t AlignInBits,
400 uint64_t OffsetInBits, unsigned Flags,
401 DIType Ty, MDNode *PropertyNode) {
402 // TAG_member is encoded in DIDerivedType format.
404 GetTagConstant(VMContext, dwarf::DW_TAG_member),
405 getNonCompileUnitScope(File),
406 MDString::get(VMContext, Name),
408 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
409 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
410 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
411 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
412 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
416 return DIType(MDNode::get(VMContext, Elts));
419 /// createObjCProperty - Create debugging information entry for Objective-C
421 DIObjCProperty DIBuilder::createObjCProperty(StringRef Name,
422 DIFile File, unsigned LineNumber,
423 StringRef GetterName,
424 StringRef SetterName,
425 unsigned PropertyAttributes,
428 GetTagConstant(VMContext, dwarf::DW_TAG_APPLE_property),
429 MDString::get(VMContext, Name),
431 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
432 MDString::get(VMContext, GetterName),
433 MDString::get(VMContext, SetterName),
434 ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes),
437 return DIObjCProperty(MDNode::get(VMContext, Elts));
440 /// createTemplateTypeParameter - Create debugging information for template
442 DITemplateTypeParameter
443 DIBuilder::createTemplateTypeParameter(DIDescriptor Context, StringRef Name,
444 DIType Ty, MDNode *File, unsigned LineNo,
447 GetTagConstant(VMContext, dwarf::DW_TAG_template_type_parameter),
448 getNonCompileUnitScope(Context),
449 MDString::get(VMContext, Name),
452 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
453 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
455 return DITemplateTypeParameter(MDNode::get(VMContext, Elts));
458 /// createTemplateValueParameter - Create debugging information for template
460 DITemplateValueParameter
461 DIBuilder::createTemplateValueParameter(DIDescriptor Context, StringRef Name,
462 DIType Ty, uint64_t Val,
463 MDNode *File, unsigned LineNo,
466 GetTagConstant(VMContext, dwarf::DW_TAG_template_value_parameter),
467 getNonCompileUnitScope(Context),
468 MDString::get(VMContext, Name),
470 ConstantInt::get(Type::getInt64Ty(VMContext), Val),
472 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
473 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
475 return DITemplateValueParameter(MDNode::get(VMContext, Elts));
478 /// createClassType - Create debugging information entry for a class.
479 DIType DIBuilder::createClassType(DIDescriptor Context, StringRef Name,
480 DIFile File, unsigned LineNumber,
481 uint64_t SizeInBits, uint64_t AlignInBits,
482 uint64_t OffsetInBits, unsigned Flags,
483 DIType DerivedFrom, DIArray Elements,
484 MDNode *VTableHolder,
485 MDNode *TemplateParams) {
486 assert((!Context || Context.Verify()) &&
487 "createClassType should be called with a valid Context");
488 // TAG_class_type is encoded in DICompositeType format.
490 GetTagConstant(VMContext, dwarf::DW_TAG_class_type),
491 getNonCompileUnitScope(Context),
492 MDString::get(VMContext, Name),
494 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
495 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
496 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
497 ConstantInt::get(Type::getInt32Ty(VMContext), OffsetInBits),
498 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
501 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
505 DIType R(MDNode::get(VMContext, Elts));
506 assert(R.Verify() && "createClassType should return a verifiable DIType");
510 /// createStructType - Create debugging information entry for a struct.
511 DICompositeType DIBuilder::createStructType(DIDescriptor Context,
512 StringRef Name, DIFile File,
515 uint64_t AlignInBits,
516 unsigned Flags, DIType DerivedFrom,
518 unsigned RunTimeLang,
519 MDNode *VTableHolder) {
520 // TAG_structure_type is encoded in DICompositeType format.
522 GetTagConstant(VMContext, dwarf::DW_TAG_structure_type),
523 getNonCompileUnitScope(Context),
524 MDString::get(VMContext, Name),
526 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
527 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
528 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
529 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
530 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
533 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
537 DICompositeType R(MDNode::get(VMContext, Elts));
538 assert(R.Verify() && "createStructType should return a verifiable DIType");
542 /// createUnionType - Create debugging information entry for an union.
543 DICompositeType DIBuilder::createUnionType(
544 DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
545 uint64_t SizeInBits, uint64_t AlignInBits, unsigned Flags, DIArray Elements,
546 unsigned RunTimeLang) {
547 // TAG_union_type is encoded in DICompositeType format.
549 GetTagConstant(VMContext, dwarf::DW_TAG_union_type),
550 getNonCompileUnitScope(Scope),
551 MDString::get(VMContext, Name),
553 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
554 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
555 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
556 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
557 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
560 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
561 Constant::getNullValue(Type::getInt32Ty(VMContext))
563 return DICompositeType(MDNode::get(VMContext, Elts));
566 /// createSubroutineType - Create subroutine type.
568 DIBuilder::createSubroutineType(DIFile File, DIArray ParameterTypes) {
569 // TAG_subroutine_type is encoded in DICompositeType format.
571 GetTagConstant(VMContext, dwarf::DW_TAG_subroutine_type),
572 Constant::getNullValue(Type::getInt32Ty(VMContext)),
573 MDString::get(VMContext, ""),
574 Constant::getNullValue(Type::getInt32Ty(VMContext)),
575 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
576 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
577 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
578 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
579 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
582 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
583 Constant::getNullValue(Type::getInt32Ty(VMContext))
585 return DICompositeType(MDNode::get(VMContext, Elts));
588 /// createEnumerationType - Create debugging information entry for an
590 DICompositeType DIBuilder::createEnumerationType(
591 DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
592 uint64_t SizeInBits, uint64_t AlignInBits, DIArray Elements,
594 // TAG_enumeration_type is encoded in DICompositeType format.
596 GetTagConstant(VMContext, dwarf::DW_TAG_enumeration_type),
597 getNonCompileUnitScope(Scope),
598 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), 0),
607 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
608 Constant::getNullValue(Type::getInt32Ty(VMContext))
610 MDNode *Node = MDNode::get(VMContext, Elts);
611 AllEnumTypes.push_back(Node);
612 return DICompositeType(Node);
615 /// createArrayType - Create debugging information entry for an array.
616 DICompositeType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
617 DIType Ty, DIArray Subscripts) {
618 // TAG_array_type is encoded in DICompositeType format.
620 GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
622 MDString::get(VMContext, ""),
624 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
625 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
626 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
627 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
628 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
631 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
632 Constant::getNullValue(Type::getInt32Ty(VMContext))
634 return DICompositeType(MDNode::get(VMContext, Elts));
637 /// createVectorType - Create debugging information entry for a vector.
638 DIType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits,
639 DIType Ty, DIArray Subscripts) {
641 // A vector is an array type with the FlagVector flag applied.
643 GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
645 MDString::get(VMContext, ""),
647 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
648 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
649 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
650 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
651 ConstantInt::get(Type::getInt32Ty(VMContext), DIType::FlagVector),
654 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
655 Constant::getNullValue(Type::getInt32Ty(VMContext))
657 return DIType(MDNode::get(VMContext, Elts));
660 /// createArtificialType - Create a new DIType with "artificial" flag set.
661 DIType DIBuilder::createArtificialType(DIType Ty) {
662 if (Ty.isArtificial())
665 SmallVector<Value *, 9> Elts;
667 assert (N && "Unexpected input DIType!");
668 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
669 if (Value *V = N->getOperand(i))
672 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
675 unsigned CurFlags = Ty.getFlags();
676 CurFlags = CurFlags | DIType::FlagArtificial;
678 // Flags are stored at this slot.
679 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
681 return DIType(MDNode::get(VMContext, Elts));
684 /// createObjectPointerType - Create a new type with both the object pointer
685 /// and artificial flags set.
686 DIType DIBuilder::createObjectPointerType(DIType Ty) {
687 if (Ty.isObjectPointer())
690 SmallVector<Value *, 9> Elts;
692 assert (N && "Unexpected input DIType!");
693 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
694 if (Value *V = N->getOperand(i))
697 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
700 unsigned CurFlags = Ty.getFlags();
701 CurFlags = CurFlags | (DIType::FlagObjectPointer | DIType::FlagArtificial);
703 // Flags are stored at this slot.
704 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
706 return DIType(MDNode::get(VMContext, Elts));
709 /// retainType - Retain DIType in a module even if it is not referenced
710 /// through debug info anchors.
711 void DIBuilder::retainType(DIType T) {
712 AllRetainTypes.push_back(T);
715 /// createUnspecifiedParameter - Create unspeicified type descriptor
716 /// for the subroutine type.
717 DIDescriptor DIBuilder::createUnspecifiedParameter() {
719 GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_parameters)
721 return DIDescriptor(MDNode::get(VMContext, Elts));
724 /// createTemporaryType - Create a temporary forward-declared type.
725 DIType DIBuilder::createTemporaryType() {
726 // Give the temporary MDNode a tag. It doesn't matter what tag we
727 // use here as long as DIType accepts it.
728 Value *Elts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
729 MDNode *Node = MDNode::getTemporary(VMContext, Elts);
733 /// createTemporaryType - Create a temporary forward-declared type.
734 DIType DIBuilder::createTemporaryType(DIFile F) {
735 // Give the temporary MDNode a tag. It doesn't matter what tag we
736 // use here as long as DIType accepts it.
738 GetTagConstant(VMContext, DW_TAG_base_type),
743 MDNode *Node = MDNode::getTemporary(VMContext, Elts);
747 /// createForwardDecl - Create a temporary forward-declared type that
748 /// can be RAUW'd if the full type is seen.
749 DIType DIBuilder::createForwardDecl(unsigned Tag, StringRef Name,
750 DIDescriptor Scope, DIFile F,
751 unsigned Line, unsigned RuntimeLang,
753 uint64_t AlignInBits) {
754 // Create a temporary MDNode.
756 GetTagConstant(VMContext, Tag),
757 getNonCompileUnitScope(Scope),
758 MDString::get(VMContext, Name),
760 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
761 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
762 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
763 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
764 ConstantInt::get(Type::getInt32Ty(VMContext),
765 DIDescriptor::FlagFwdDecl),
768 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
770 MDNode *Node = MDNode::getTemporary(VMContext, Elts);
771 assert(DIType(Node).Verify() &&
772 "createForwardDecl result should be verifiable");
776 /// getOrCreateArray - Get a DIArray, create one if required.
777 DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) {
778 if (Elements.empty()) {
779 Value *Null = Constant::getNullValue(Type::getInt32Ty(VMContext));
780 return DIArray(MDNode::get(VMContext, Null));
782 return DIArray(MDNode::get(VMContext, Elements));
785 /// getOrCreateSubrange - Create a descriptor for a value range. This
786 /// implicitly uniques the values returned.
787 DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
789 GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type),
790 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
791 ConstantInt::get(Type::getInt64Ty(VMContext), Count)
794 return DISubrange(MDNode::get(VMContext, Elts));
797 /// createGlobalVariable - Create a new descriptor for the specified global.
798 DIGlobalVariable DIBuilder::
799 createGlobalVariable(StringRef Name, DIFile F, unsigned LineNumber,
800 DIType Ty, bool isLocalToUnit, Value *Val) {
802 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
803 Constant::getNullValue(Type::getInt32Ty(VMContext)),
805 MDString::get(VMContext, Name),
806 MDString::get(VMContext, Name),
807 MDString::get(VMContext, Name),
809 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
811 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
812 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
816 MDNode *Node = MDNode::get(VMContext, Elts);
817 AllGVs.push_back(Node);
818 return DIGlobalVariable(Node);
821 /// createStaticVariable - Create a new descriptor for the specified static
823 DIGlobalVariable DIBuilder::
824 createStaticVariable(DIDescriptor Context, StringRef Name,
825 StringRef LinkageName, DIFile F, unsigned LineNumber,
826 DIType Ty, bool isLocalToUnit, Value *Val, MDNode *Decl) {
828 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
829 Constant::getNullValue(Type::getInt32Ty(VMContext)),
830 getNonCompileUnitScope(Context),
831 MDString::get(VMContext, Name),
832 MDString::get(VMContext, Name),
833 MDString::get(VMContext, LinkageName),
835 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
837 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
838 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
842 MDNode *Node = MDNode::get(VMContext, Elts);
843 AllGVs.push_back(Node);
844 return DIGlobalVariable(Node);
847 /// createVariable - Create a new descriptor for the specified variable.
848 DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
849 StringRef Name, DIFile File,
850 unsigned LineNo, DIType Ty,
851 bool AlwaysPreserve, unsigned Flags,
853 DIDescriptor Context(getNonCompileUnitScope(Scope));
854 assert((!Context || Context.Verify()) &&
855 "createLocalVariable should be called with a valid Context");
856 assert(Ty.Verify() &&
857 "createLocalVariable should be called with a valid type");
859 GetTagConstant(VMContext, Tag),
860 getNonCompileUnitScope(Scope),
861 MDString::get(VMContext, Name),
863 ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))),
865 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
866 Constant::getNullValue(Type::getInt32Ty(VMContext))
868 MDNode *Node = MDNode::get(VMContext, Elts);
869 if (AlwaysPreserve) {
870 // The optimizer may remove local variable. If there is an interest
871 // to preserve variable info in such situation then stash it in a
873 DISubprogram Fn(getDISubprogram(Scope));
874 NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, Fn);
875 FnLocals->addOperand(Node);
877 assert(DIVariable(Node).Verify() &&
878 "createLocalVariable should return a verifiable DIVariable");
879 return DIVariable(Node);
882 /// createComplexVariable - Create a new descriptor for the specified variable
883 /// which has a complex address expression for its address.
884 DIVariable DIBuilder::createComplexVariable(unsigned Tag, DIDescriptor Scope,
885 StringRef Name, DIFile F,
887 DIType Ty, ArrayRef<Value *> Addr,
889 SmallVector<Value *, 15> Elts;
890 Elts.push_back(GetTagConstant(VMContext, Tag));
891 Elts.push_back(getNonCompileUnitScope(Scope)),
892 Elts.push_back(MDString::get(VMContext, Name));
894 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext),
895 (LineNo | (ArgNo << 24))));
897 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
898 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
899 Elts.append(Addr.begin(), Addr.end());
901 return DIVariable(MDNode::get(VMContext, Elts));
904 /// createFunction - Create a new descriptor for the specified function.
905 DISubprogram DIBuilder::createFunction(DIDescriptor Context,
907 StringRef LinkageName,
908 DIFile File, unsigned LineNo,
910 bool isLocalToUnit, bool isDefinition,
912 unsigned Flags, bool isOptimized,
916 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
918 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
919 Constant::getNullValue(Type::getInt32Ty(VMContext)),
920 getNonCompileUnitScope(Context),
921 MDString::get(VMContext, Name),
922 MDString::get(VMContext, Name),
923 MDString::get(VMContext, LinkageName),
925 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
927 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
928 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
929 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
930 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
932 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
933 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
937 MDNode::getTemporary(VMContext, TElts),
938 ConstantInt::get(Type::getInt32Ty(VMContext), ScopeLine)
940 MDNode *Node = MDNode::get(VMContext, Elts);
942 // Create a named metadata so that we do not lose this mdnode.
944 AllSubprograms.push_back(Node);
945 return DISubprogram(Node);
948 /// createMethod - Create a new descriptor for the specified C++ method.
949 DISubprogram DIBuilder::createMethod(DIDescriptor Context,
951 StringRef LinkageName,
953 unsigned LineNo, DIType Ty,
956 unsigned VK, unsigned VIndex,
957 MDNode *VTableHolder,
962 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
964 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
965 Constant::getNullValue(Type::getInt32Ty(VMContext)),
966 getNonCompileUnitScope(Context),
967 MDString::get(VMContext, Name),
968 MDString::get(VMContext, Name),
969 MDString::get(VMContext, LinkageName),
971 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
973 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
974 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
975 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
976 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
978 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
979 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
982 Constant::getNullValue(Type::getInt32Ty(VMContext)),
983 MDNode::getTemporary(VMContext, TElts),
984 // FIXME: Do we want to use different scope/lines?
985 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
987 MDNode *Node = MDNode::get(VMContext, Elts);
989 AllSubprograms.push_back(Node);
990 return DISubprogram(Node);
993 /// createNameSpace - This creates new descriptor for a namespace
994 /// with the specified parent scope.
995 DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
996 DIFile File, unsigned LineNo) {
998 GetTagConstant(VMContext, dwarf::DW_TAG_namespace),
999 getNonCompileUnitScope(Scope),
1000 MDString::get(VMContext, Name),
1002 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1004 DINameSpace R(MDNode::get(VMContext, Elts));
1005 assert(R.Verify() &&
1006 "createNameSpace should return a verifiable DINameSpace");
1010 /// createLexicalBlockFile - This creates a new MDNode that encapsulates
1011 /// an existing scope with a new filename.
1012 DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
1015 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1019 DILexicalBlockFile R(MDNode::get(VMContext, Elts));
1022 "createLexicalBlockFile should return a verifiable DILexicalBlockFile");
1026 DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
1027 unsigned Line, unsigned Col) {
1028 // Defeat MDNode uniqing for lexical blocks by using unique id.
1029 static unsigned int unique_id = 0;
1031 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1032 getNonCompileUnitScope(Scope),
1033 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
1034 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1036 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
1038 DILexicalBlock R(MDNode::get(VMContext, Elts));
1039 assert(R.Verify() &&
1040 "createLexicalBlock should return a verifiable DILexicalBlock");
1044 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1045 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1046 Instruction *InsertBefore) {
1047 assert(Storage && "no storage passed to dbg.declare");
1048 assert(VarInfo.Verify() && "empty DIVariable passed to dbg.declare");
1050 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1052 Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1053 return CallInst::Create(DeclareFn, Args, "", InsertBefore);
1056 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1057 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1058 BasicBlock *InsertAtEnd) {
1059 assert(Storage && "no storage passed to dbg.declare");
1060 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.declare");
1062 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1064 Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1066 // If this block already has a terminator then insert this intrinsic
1067 // before the terminator.
1068 if (TerminatorInst *T = InsertAtEnd->getTerminator())
1069 return CallInst::Create(DeclareFn, Args, "", T);
1071 return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
1074 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1075 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1077 Instruction *InsertBefore) {
1078 assert(V && "no value passed to dbg.value");
1079 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
1081 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1083 Value *Args[] = { MDNode::get(V->getContext(), V),
1084 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1086 return CallInst::Create(ValueFn, Args, "", InsertBefore);
1089 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1090 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1092 BasicBlock *InsertAtEnd) {
1093 assert(V && "no value passed to dbg.value");
1094 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
1096 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1098 Value *Args[] = { MDNode::get(V->getContext(), V),
1099 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1101 return CallInst::Create(ValueFn, Args, "", InsertAtEnd);