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 DIImportedEntity DIBuilder::createImportedModule(DIScope Context,
135 GetTagConstant(VMContext, dwarf::DW_TAG_imported_module),
138 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
140 DIImportedEntity M(MDNode::get(VMContext, Elts));
141 assert(M.Verify() && "Imported module should be valid");
142 AllImportedModules.push_back(M);
146 DIImportedEntity DIBuilder::createImportedDeclaration(DIScope Context,
150 GetTagConstant(VMContext, dwarf::DW_TAG_imported_declaration),
153 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
155 DIImportedEntity M(MDNode::get(VMContext, Elts));
156 assert(M.Verify() && "Imported module should be valid");
157 AllImportedModules.push_back(M);
161 /// createFile - Create a file descriptor to hold debugging information
163 DIFile DIBuilder::createFile(StringRef Filename, StringRef Directory) {
165 GetTagConstant(VMContext, dwarf::DW_TAG_file_type),
166 createFilePathPair(VMContext, Filename, Directory)
168 return DIFile(MDNode::get(VMContext, Elts));
171 /// createEnumerator - Create a single enumerator value.
172 DIEnumerator DIBuilder::createEnumerator(StringRef Name, uint64_t Val) {
173 assert(!Name.empty() && "Unable to create enumerator without name");
175 GetTagConstant(VMContext, dwarf::DW_TAG_enumerator),
176 MDString::get(VMContext, Name),
177 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
179 return DIEnumerator(MDNode::get(VMContext, Elts));
182 /// createNullPtrType - Create C++0x nullptr type.
183 DIType DIBuilder::createNullPtrType(StringRef Name) {
184 assert(!Name.empty() && "Unable to create type without name");
185 // nullptr is encoded in DIBasicType format. Line number, filename,
186 // ,size, alignment, offset and flags are always empty here.
188 GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_type),
191 MDString::get(VMContext, 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;
197 ConstantInt::get(Type::getInt32Ty(VMContext), 0) // Encoding
199 return DIType(MDNode::get(VMContext, Elts));
202 /// createBasicType - Create debugging information entry for a basic
203 /// type, e.g 'char'.
205 DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
206 uint64_t AlignInBits, unsigned Encoding) {
207 assert(!Name.empty() && "Unable to create type without name");
208 // Basic types are encoded in DIBasicType format. Line number, filename,
209 // offset and flags are always empty here.
211 GetTagConstant(VMContext, dwarf::DW_TAG_base_type),
212 NULL, // File/directory name
214 MDString::get(VMContext, Name),
215 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
216 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
217 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
218 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
219 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
220 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
222 return DIBasicType(MDNode::get(VMContext, Elts));
225 /// createQualifiedType - Create debugging information entry for a qualified
226 /// type, e.g. 'const int'.
227 DIDerivedType DIBuilder::createQualifiedType(unsigned Tag, DIType FromTy) {
228 // Qualified types are encoded in DIDerivedType format.
230 GetTagConstant(VMContext, Tag),
233 MDString::get(VMContext, StringRef()), // Empty name.
234 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
235 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
236 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
237 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
238 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
241 return DIDerivedType(MDNode::get(VMContext, Elts));
244 /// createPointerType - Create debugging information entry for a pointer.
246 DIBuilder::createPointerType(DIType PointeeTy, uint64_t SizeInBits,
247 uint64_t AlignInBits, StringRef Name) {
248 // Pointer types are encoded in DIDerivedType format.
250 GetTagConstant(VMContext, dwarf::DW_TAG_pointer_type),
253 MDString::get(VMContext, Name),
254 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
255 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
256 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
257 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
258 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
261 return DIDerivedType(MDNode::get(VMContext, Elts));
264 DIDerivedType DIBuilder::createMemberPointerType(DIType PointeeTy,
266 // Pointer types are encoded in DIDerivedType format.
268 GetTagConstant(VMContext, dwarf::DW_TAG_ptr_to_member_type),
272 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
273 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
274 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
275 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
276 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
280 return DIDerivedType(MDNode::get(VMContext, Elts));
283 /// createReferenceType - Create debugging information entry for a reference
285 DIDerivedType DIBuilder::createReferenceType(unsigned Tag, DIType RTy) {
286 assert(RTy.Verify() && "Unable to create reference type");
287 // References are encoded in DIDerivedType format.
289 GetTagConstant(VMContext, Tag),
293 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
294 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
295 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
296 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
297 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
300 return DIDerivedType(MDNode::get(VMContext, Elts));
303 /// createTypedef - Create debugging information entry for a typedef.
304 DIDerivedType DIBuilder::createTypedef(DIType Ty, StringRef Name, DIFile File,
305 unsigned LineNo, DIDescriptor Context) {
306 // typedefs are encoded in DIDerivedType format.
307 assert(Ty.Verify() && "Invalid typedef type!");
309 GetTagConstant(VMContext, dwarf::DW_TAG_typedef),
311 getNonCompileUnitScope(Context),
312 MDString::get(VMContext, Name),
313 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
314 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
315 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
316 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
317 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
320 return DIDerivedType(MDNode::get(VMContext, Elts));
323 /// createFriend - Create debugging information entry for a 'friend'.
324 DIType DIBuilder::createFriend(DIType Ty, DIType FriendTy) {
325 // typedefs are encoded in DIDerivedType format.
326 assert(Ty.Verify() && "Invalid type!");
327 assert(FriendTy.Verify() && "Invalid friend type!");
329 GetTagConstant(VMContext, dwarf::DW_TAG_friend),
333 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
334 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
335 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
336 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
337 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
340 return DIType(MDNode::get(VMContext, Elts));
343 /// createInheritance - Create debugging information entry to establish
344 /// inheritance relationship between two types.
345 DIDerivedType DIBuilder::createInheritance(
346 DIType Ty, DIType BaseTy, uint64_t BaseOffset, unsigned Flags) {
347 assert(Ty.Verify() && "Unable to create inheritance");
348 // TAG_inheritance is encoded in DIDerivedType format.
350 GetTagConstant(VMContext, dwarf::DW_TAG_inheritance),
354 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
355 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
356 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
357 ConstantInt::get(Type::getInt64Ty(VMContext), BaseOffset),
358 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
361 return DIDerivedType(MDNode::get(VMContext, Elts));
364 /// createMemberType - Create debugging information entry for a member.
365 DIDerivedType DIBuilder::createMemberType(
366 DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
367 uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
368 unsigned Flags, DIType Ty) {
369 // TAG_member is encoded in DIDerivedType format.
371 GetTagConstant(VMContext, dwarf::DW_TAG_member),
373 getNonCompileUnitScope(Scope),
374 MDString::get(VMContext, Name),
375 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
376 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
377 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
378 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
379 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
382 return DIDerivedType(MDNode::get(VMContext, Elts));
385 /// createStaticMemberType - Create debugging information entry for a
386 /// C++ static data member.
387 DIType DIBuilder::createStaticMemberType(DIDescriptor Scope, StringRef Name,
388 DIFile File, unsigned LineNumber,
389 DIType Ty, unsigned Flags,
391 // TAG_member is encoded in DIDerivedType format.
392 Flags |= DIDescriptor::FlagStaticMember;
394 GetTagConstant(VMContext, dwarf::DW_TAG_member),
396 getNonCompileUnitScope(Scope),
397 MDString::get(VMContext, Name),
398 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
399 ConstantInt::get(Type::getInt64Ty(VMContext), 0/*SizeInBits*/),
400 ConstantInt::get(Type::getInt64Ty(VMContext), 0/*AlignInBits*/),
401 ConstantInt::get(Type::getInt64Ty(VMContext), 0/*OffsetInBits*/),
402 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
406 return DIType(MDNode::get(VMContext, Elts));
409 /// createObjCIVar - Create debugging information entry for Objective-C
410 /// instance variable.
411 DIType DIBuilder::createObjCIVar(StringRef Name,
412 DIFile File, unsigned LineNumber,
413 uint64_t SizeInBits, uint64_t AlignInBits,
414 uint64_t OffsetInBits, unsigned Flags,
415 DIType Ty, StringRef PropertyName,
416 StringRef GetterName, StringRef SetterName,
417 unsigned PropertyAttributes) {
418 // TAG_member is encoded in DIDerivedType format.
420 GetTagConstant(VMContext, dwarf::DW_TAG_member),
422 getNonCompileUnitScope(File),
423 MDString::get(VMContext, Name),
424 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
425 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
426 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
427 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
428 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
430 MDString::get(VMContext, PropertyName),
431 MDString::get(VMContext, GetterName),
432 MDString::get(VMContext, SetterName),
433 ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes)
435 return DIType(MDNode::get(VMContext, Elts));
438 /// createObjCIVar - Create debugging information entry for Objective-C
439 /// instance variable.
440 DIType DIBuilder::createObjCIVar(StringRef Name,
441 DIFile File, unsigned LineNumber,
442 uint64_t SizeInBits, uint64_t AlignInBits,
443 uint64_t OffsetInBits, unsigned Flags,
444 DIType Ty, MDNode *PropertyNode) {
445 // TAG_member is encoded in DIDerivedType format.
447 GetTagConstant(VMContext, dwarf::DW_TAG_member),
449 getNonCompileUnitScope(File),
450 MDString::get(VMContext, Name),
451 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
452 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
453 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
454 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
455 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
459 return DIType(MDNode::get(VMContext, Elts));
462 /// createObjCProperty - Create debugging information entry for Objective-C
464 DIObjCProperty DIBuilder::createObjCProperty(StringRef Name,
465 DIFile File, unsigned LineNumber,
466 StringRef GetterName,
467 StringRef SetterName,
468 unsigned PropertyAttributes,
471 GetTagConstant(VMContext, dwarf::DW_TAG_APPLE_property),
472 MDString::get(VMContext, Name),
474 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
475 MDString::get(VMContext, GetterName),
476 MDString::get(VMContext, SetterName),
477 ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes),
480 return DIObjCProperty(MDNode::get(VMContext, Elts));
483 /// createTemplateTypeParameter - Create debugging information for template
485 DITemplateTypeParameter
486 DIBuilder::createTemplateTypeParameter(DIDescriptor Context, StringRef Name,
487 DIType Ty, MDNode *File, unsigned LineNo,
490 GetTagConstant(VMContext, dwarf::DW_TAG_template_type_parameter),
491 getNonCompileUnitScope(Context),
492 MDString::get(VMContext, Name),
495 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
496 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
498 return DITemplateTypeParameter(MDNode::get(VMContext, Elts));
501 /// createTemplateValueParameter - Create debugging information for template
503 DITemplateValueParameter
504 DIBuilder::createTemplateValueParameter(DIDescriptor Context, StringRef Name,
505 DIType Ty, uint64_t Val,
506 MDNode *File, unsigned LineNo,
509 GetTagConstant(VMContext, dwarf::DW_TAG_template_value_parameter),
510 getNonCompileUnitScope(Context),
511 MDString::get(VMContext, Name),
513 ConstantInt::get(Type::getInt64Ty(VMContext), Val),
515 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
516 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
518 return DITemplateValueParameter(MDNode::get(VMContext, Elts));
521 /// createClassType - Create debugging information entry for a class.
522 DICompositeType DIBuilder::createClassType(DIDescriptor Context, StringRef Name,
523 DIFile File, unsigned LineNumber,
525 uint64_t AlignInBits,
526 uint64_t OffsetInBits,
527 unsigned Flags, DIType DerivedFrom,
529 MDNode *VTableHolder,
530 MDNode *TemplateParams) {
531 assert((!Context || Context.Verify()) &&
532 "createClassType should be called with a valid Context");
533 // TAG_class_type is encoded in DICompositeType format.
535 GetTagConstant(VMContext, dwarf::DW_TAG_class_type),
537 getNonCompileUnitScope(Context),
538 MDString::get(VMContext, Name),
539 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
540 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
541 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
542 ConstantInt::get(Type::getInt32Ty(VMContext), OffsetInBits),
543 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
546 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
550 DICompositeType R(MDNode::get(VMContext, Elts));
551 assert(R.Verify() && "createClassType should return a verifiable DIType");
555 /// createStructType - Create debugging information entry for a struct.
556 DICompositeType DIBuilder::createStructType(DIDescriptor Context,
557 StringRef Name, DIFile File,
560 uint64_t AlignInBits,
561 unsigned Flags, DIType DerivedFrom,
563 unsigned RunTimeLang,
564 MDNode *VTableHolder) {
565 // TAG_structure_type is encoded in DICompositeType format.
567 GetTagConstant(VMContext, dwarf::DW_TAG_structure_type),
569 getNonCompileUnitScope(Context),
570 MDString::get(VMContext, Name),
571 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
572 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
573 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
574 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
575 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
578 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
582 DICompositeType R(MDNode::get(VMContext, Elts));
583 assert(R.Verify() && "createStructType should return a verifiable DIType");
587 /// createUnionType - Create debugging information entry for an union.
588 DICompositeType DIBuilder::createUnionType(DIDescriptor Scope, StringRef Name,
589 DIFile File, unsigned LineNumber,
591 uint64_t AlignInBits, unsigned Flags,
593 unsigned RunTimeLang) {
594 // TAG_union_type is encoded in DICompositeType format.
596 GetTagConstant(VMContext, dwarf::DW_TAG_union_type),
598 getNonCompileUnitScope(Scope),
599 MDString::get(VMContext, Name),
600 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
601 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
602 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
603 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
604 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
607 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
608 Constant::getNullValue(Type::getInt32Ty(VMContext)),
611 return DICompositeType(MDNode::get(VMContext, Elts));
614 /// createSubroutineType - Create subroutine type.
616 DIBuilder::createSubroutineType(DIFile File, DIArray ParameterTypes) {
617 // TAG_subroutine_type is encoded in DICompositeType format.
619 GetTagConstant(VMContext, dwarf::DW_TAG_subroutine_type),
620 Constant::getNullValue(Type::getInt32Ty(VMContext)),
621 Constant::getNullValue(Type::getInt32Ty(VMContext)),
622 MDString::get(VMContext, ""),
623 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
624 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
625 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
626 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
627 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
630 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
631 Constant::getNullValue(Type::getInt32Ty(VMContext))
633 return DICompositeType(MDNode::get(VMContext, Elts));
636 /// createEnumerationType - Create debugging information entry for an
638 DICompositeType DIBuilder::createEnumerationType(
639 DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
640 uint64_t SizeInBits, uint64_t AlignInBits, DIArray Elements,
641 DIType UnderlyingType) {
642 // TAG_enumeration_type is encoded in DICompositeType format.
644 GetTagConstant(VMContext, dwarf::DW_TAG_enumeration_type),
646 getNonCompileUnitScope(Scope),
647 MDString::get(VMContext, Name),
648 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
649 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
650 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
651 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
652 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
655 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
656 Constant::getNullValue(Type::getInt32Ty(VMContext))
658 MDNode *Node = MDNode::get(VMContext, Elts);
659 AllEnumTypes.push_back(Node);
660 return DICompositeType(Node);
663 /// createArrayType - Create debugging information entry for an array.
664 DICompositeType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
665 DIType Ty, DIArray Subscripts) {
666 // TAG_array_type is encoded in DICompositeType format.
668 GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
669 NULL, // Filename/Directory,
671 MDString::get(VMContext, ""),
672 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
673 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
674 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
675 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
676 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
679 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
680 Constant::getNullValue(Type::getInt32Ty(VMContext))
682 return DICompositeType(MDNode::get(VMContext, Elts));
685 /// createVectorType - Create debugging information entry for a vector.
686 DIType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits,
687 DIType Ty, DIArray Subscripts) {
689 // A vector is an array type with the FlagVector flag applied.
691 GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
692 NULL, // Filename/Directory,
694 MDString::get(VMContext, ""),
695 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
696 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
697 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
698 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
699 ConstantInt::get(Type::getInt32Ty(VMContext), DIType::FlagVector),
702 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
703 Constant::getNullValue(Type::getInt32Ty(VMContext))
705 return DIType(MDNode::get(VMContext, Elts));
708 /// createArtificialType - Create a new DIType with "artificial" flag set.
709 DIType DIBuilder::createArtificialType(DIType Ty) {
710 if (Ty.isArtificial())
713 SmallVector<Value *, 9> Elts;
715 assert (N && "Unexpected input DIType!");
716 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
717 if (Value *V = N->getOperand(i))
720 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
723 unsigned CurFlags = Ty.getFlags();
724 CurFlags = CurFlags | DIType::FlagArtificial;
726 // Flags are stored at this slot.
727 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
729 return DIType(MDNode::get(VMContext, Elts));
732 /// createObjectPointerType - Create a new type with both the object pointer
733 /// and artificial flags set.
734 DIType DIBuilder::createObjectPointerType(DIType Ty) {
735 if (Ty.isObjectPointer())
738 SmallVector<Value *, 9> Elts;
740 assert (N && "Unexpected input DIType!");
741 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
742 if (Value *V = N->getOperand(i))
745 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
748 unsigned CurFlags = Ty.getFlags();
749 CurFlags = CurFlags | (DIType::FlagObjectPointer | DIType::FlagArtificial);
751 // Flags are stored at this slot.
752 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
754 return DIType(MDNode::get(VMContext, Elts));
757 /// retainType - Retain DIType in a module even if it is not referenced
758 /// through debug info anchors.
759 void DIBuilder::retainType(DIType T) {
760 AllRetainTypes.push_back(T);
763 /// createUnspecifiedParameter - Create unspeicified type descriptor
764 /// for the subroutine type.
765 DIDescriptor DIBuilder::createUnspecifiedParameter() {
767 GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_parameters)
769 return DIDescriptor(MDNode::get(VMContext, Elts));
772 /// createForwardDecl - Create a temporary forward-declared type that
773 /// can be RAUW'd if the full type is seen.
774 DIType DIBuilder::createForwardDecl(unsigned Tag, StringRef Name,
775 DIDescriptor Scope, DIFile F,
776 unsigned Line, unsigned RuntimeLang,
778 uint64_t AlignInBits) {
779 // Create a temporary MDNode.
781 GetTagConstant(VMContext, Tag),
783 getNonCompileUnitScope(Scope),
784 MDString::get(VMContext, Name),
785 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
786 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
787 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
788 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
789 ConstantInt::get(Type::getInt32Ty(VMContext),
790 DIDescriptor::FlagFwdDecl),
793 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
795 MDNode *Node = MDNode::getTemporary(VMContext, Elts);
796 assert(DIType(Node).Verify() &&
797 "createForwardDecl result should be verifiable");
801 /// getOrCreateArray - Get a DIArray, create one if required.
802 DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) {
803 if (Elements.empty()) {
804 Value *Null = Constant::getNullValue(Type::getInt32Ty(VMContext));
805 return DIArray(MDNode::get(VMContext, Null));
807 return DIArray(MDNode::get(VMContext, Elements));
810 /// getOrCreateSubrange - Create a descriptor for a value range. This
811 /// implicitly uniques the values returned.
812 DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
814 GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type),
815 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
816 ConstantInt::get(Type::getInt64Ty(VMContext), Count)
819 return DISubrange(MDNode::get(VMContext, Elts));
822 /// \brief Create a new descriptor for the specified global.
823 DIGlobalVariable DIBuilder::
824 createGlobalVariable(StringRef Name, StringRef LinkageName, DIFile F,
825 unsigned LineNumber, DIType Ty, bool isLocalToUnit,
828 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
829 Constant::getNullValue(Type::getInt32Ty(VMContext)),
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 /// \brief Create a new descriptor for the specified global.
848 DIGlobalVariable DIBuilder::
849 createGlobalVariable(StringRef Name, DIFile F, unsigned LineNumber,
850 DIType Ty, bool isLocalToUnit, Value *Val) {
851 return createGlobalVariable(Name, Name, F, LineNumber, Ty, isLocalToUnit,
855 /// createStaticVariable - Create a new descriptor for the specified static
857 DIGlobalVariable DIBuilder::
858 createStaticVariable(DIDescriptor Context, StringRef Name,
859 StringRef LinkageName, DIFile F, unsigned LineNumber,
860 DIType Ty, bool isLocalToUnit, Value *Val, MDNode *Decl) {
862 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
863 Constant::getNullValue(Type::getInt32Ty(VMContext)),
864 getNonCompileUnitScope(Context),
865 MDString::get(VMContext, Name),
866 MDString::get(VMContext, Name),
867 MDString::get(VMContext, LinkageName),
869 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
871 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
872 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
876 MDNode *Node = MDNode::get(VMContext, Elts);
877 AllGVs.push_back(Node);
878 return DIGlobalVariable(Node);
881 /// createVariable - Create a new descriptor for the specified variable.
882 DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
883 StringRef Name, DIFile File,
884 unsigned LineNo, DIType Ty,
885 bool AlwaysPreserve, unsigned Flags,
887 DIDescriptor Context(getNonCompileUnitScope(Scope));
888 assert((!Context || Context.Verify()) &&
889 "createLocalVariable should be called with a valid Context");
890 assert(Ty.Verify() &&
891 "createLocalVariable should be called with a valid type");
893 GetTagConstant(VMContext, Tag),
894 getNonCompileUnitScope(Scope),
895 MDString::get(VMContext, Name),
897 ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))),
899 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
900 Constant::getNullValue(Type::getInt32Ty(VMContext))
902 MDNode *Node = MDNode::get(VMContext, Elts);
903 if (AlwaysPreserve) {
904 // The optimizer may remove local variable. If there is an interest
905 // to preserve variable info in such situation then stash it in a
907 DISubprogram Fn(getDISubprogram(Scope));
908 NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, Fn);
909 FnLocals->addOperand(Node);
911 assert(DIVariable(Node).Verify() &&
912 "createLocalVariable should return a verifiable DIVariable");
913 return DIVariable(Node);
916 /// createComplexVariable - Create a new descriptor for the specified variable
917 /// which has a complex address expression for its address.
918 DIVariable DIBuilder::createComplexVariable(unsigned Tag, DIDescriptor Scope,
919 StringRef Name, DIFile F,
921 DIType Ty, ArrayRef<Value *> Addr,
923 SmallVector<Value *, 15> Elts;
924 Elts.push_back(GetTagConstant(VMContext, Tag));
925 Elts.push_back(getNonCompileUnitScope(Scope)),
926 Elts.push_back(MDString::get(VMContext, Name));
928 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext),
929 (LineNo | (ArgNo << 24))));
931 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
932 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
933 Elts.append(Addr.begin(), Addr.end());
935 return DIVariable(MDNode::get(VMContext, Elts));
938 /// createFunction - Create a new descriptor for the specified function.
939 DISubprogram DIBuilder::createFunction(DIDescriptor Context,
941 StringRef LinkageName,
942 DIFile File, unsigned LineNo,
944 bool isLocalToUnit, bool isDefinition,
946 unsigned Flags, bool isOptimized,
950 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
952 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
954 getNonCompileUnitScope(Context),
955 MDString::get(VMContext, Name),
956 MDString::get(VMContext, Name),
957 MDString::get(VMContext, LinkageName),
958 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
960 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
961 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
962 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
963 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
965 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
966 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
970 MDNode::getTemporary(VMContext, TElts),
971 ConstantInt::get(Type::getInt32Ty(VMContext), ScopeLine)
973 MDNode *Node = MDNode::get(VMContext, Elts);
975 // Create a named metadata so that we do not lose this mdnode.
977 AllSubprograms.push_back(Node);
978 DISubprogram S(Node);
979 assert(S.Verify() && "createFunction should return a valid DISubprogram");
983 /// createMethod - Create a new descriptor for the specified C++ method.
984 DISubprogram DIBuilder::createMethod(DIDescriptor Context,
986 StringRef LinkageName,
988 unsigned LineNo, DIType Ty,
991 unsigned VK, unsigned VIndex,
992 MDNode *VTableHolder,
997 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
999 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
1001 getNonCompileUnitScope(Context),
1002 MDString::get(VMContext, Name),
1003 MDString::get(VMContext, Name),
1004 MDString::get(VMContext, LinkageName),
1005 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1007 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1008 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1009 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
1010 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
1012 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1013 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1016 Constant::getNullValue(Type::getInt32Ty(VMContext)),
1017 MDNode::getTemporary(VMContext, TElts),
1018 // FIXME: Do we want to use different scope/lines?
1019 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1021 MDNode *Node = MDNode::get(VMContext, Elts);
1023 AllSubprograms.push_back(Node);
1024 DISubprogram S(Node);
1025 assert(S.Verify() && "createMethod should return a valid DISubprogram");
1029 /// createNameSpace - This creates new descriptor for a namespace
1030 /// with the specified parent scope.
1031 DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
1032 DIFile File, unsigned LineNo) {
1034 GetTagConstant(VMContext, dwarf::DW_TAG_namespace),
1036 getNonCompileUnitScope(Scope),
1037 MDString::get(VMContext, Name),
1038 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1040 DINameSpace R(MDNode::get(VMContext, Elts));
1041 assert(R.Verify() &&
1042 "createNameSpace should return a verifiable DINameSpace");
1046 /// createLexicalBlockFile - This creates a new MDNode that encapsulates
1047 /// an existing scope with a new filename.
1048 DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
1051 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1055 DILexicalBlockFile R(MDNode::get(VMContext, Elts));
1058 "createLexicalBlockFile should return a verifiable DILexicalBlockFile");
1062 DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
1063 unsigned Line, unsigned Col) {
1064 // Defeat MDNode uniqing for lexical blocks by using unique id.
1065 static unsigned int unique_id = 0;
1067 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1069 getNonCompileUnitScope(Scope),
1070 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
1071 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1072 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
1074 DILexicalBlock R(MDNode::get(VMContext, Elts));
1075 assert(R.Verify() &&
1076 "createLexicalBlock should return a verifiable DILexicalBlock");
1080 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1081 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1082 Instruction *InsertBefore) {
1083 assert(Storage && "no storage passed to dbg.declare");
1084 assert(VarInfo.Verify() && "empty DIVariable passed to dbg.declare");
1086 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1088 Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1089 return CallInst::Create(DeclareFn, Args, "", InsertBefore);
1092 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1093 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1094 BasicBlock *InsertAtEnd) {
1095 assert(Storage && "no storage passed to dbg.declare");
1096 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.declare");
1098 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1100 Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1102 // If this block already has a terminator then insert this intrinsic
1103 // before the terminator.
1104 if (TerminatorInst *T = InsertAtEnd->getTerminator())
1105 return CallInst::Create(DeclareFn, Args, "", T);
1107 return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
1110 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1111 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1113 Instruction *InsertBefore) {
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, "", InsertBefore);
1125 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1126 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1128 BasicBlock *InsertAtEnd) {
1129 assert(V && "no value passed to dbg.value");
1130 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
1132 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1134 Value *Args[] = { MDNode::get(V->getContext(), V),
1135 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1137 return CallInst::Create(ValueFn, Args, "", InsertAtEnd);