1 //===--- DIBuilder.cpp - Debug Information Builder ------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the DIBuilder.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/DIBuilder.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/DebugInfo.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/IntrinsicInst.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/Dwarf.h"
24 using namespace llvm::dwarf;
26 static Constant *GetTagConstant(LLVMContext &VMContext, unsigned Tag) {
27 assert((Tag & LLVMDebugVersionMask) == 0 &&
28 "Tag too large for debug encoding!");
29 return ConstantInt::get(Type::getInt32Ty(VMContext), Tag | LLVMDebugVersion);
32 DIBuilder::DIBuilder(Module &m)
33 : M(m), VMContext(M.getContext()), TheCU(0), TempEnumTypes(0),
34 TempRetainTypes(0), TempSubprograms(0), TempGVs(0), DeclareFn(0),
38 /// finalize - Construct any deferred debug info descriptors.
39 void DIBuilder::finalize() {
40 DIArray Enums = getOrCreateArray(AllEnumTypes);
41 DIType(TempEnumTypes).replaceAllUsesWith(Enums);
43 DIArray RetainTypes = getOrCreateArray(AllRetainTypes);
44 DIType(TempRetainTypes).replaceAllUsesWith(RetainTypes);
46 DIArray SPs = getOrCreateArray(AllSubprograms);
47 DIType(TempSubprograms).replaceAllUsesWith(SPs);
48 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
49 DISubprogram SP(SPs.getElement(i));
50 SmallVector<Value *, 4> Variables;
51 if (NamedMDNode *NMD = getFnSpecificMDNode(M, SP)) {
52 for (unsigned ii = 0, ee = NMD->getNumOperands(); ii != ee; ++ii)
53 Variables.push_back(NMD->getOperand(ii));
54 NMD->eraseFromParent();
56 if (MDNode *Temp = SP.getVariablesNodes()) {
57 DIArray AV = getOrCreateArray(Variables);
58 DIType(Temp).replaceAllUsesWith(AV);
62 DIArray GVs = getOrCreateArray(AllGVs);
63 DIType(TempGVs).replaceAllUsesWith(GVs);
65 DIArray IMs = getOrCreateArray(AllImportedModules);
66 DIType(TempImportedModules).replaceAllUsesWith(IMs);
69 /// getNonCompileUnitScope - If N is compile unit return NULL otherwise return
71 static MDNode *getNonCompileUnitScope(MDNode *N) {
72 if (DIDescriptor(N).isCompileUnit())
77 static MDNode *createFilePathPair(LLVMContext &VMContext, StringRef Filename,
78 StringRef Directory) {
79 assert(!Filename.empty() && "Unable to create file without name");
81 MDString::get(VMContext, Filename),
82 MDString::get(VMContext, Directory),
84 return MDNode::get(VMContext, Pair);
87 /// createCompileUnit - A CompileUnit provides an anchor for all debugging
88 /// information generated during this instance of compilation.
89 void DIBuilder::createCompileUnit(unsigned Lang, StringRef Filename,
90 StringRef Directory, StringRef Producer,
91 bool isOptimized, StringRef Flags,
92 unsigned RunTimeVer, StringRef SplitName) {
93 assert(((Lang <= dwarf::DW_LANG_Python && Lang >= dwarf::DW_LANG_C89) ||
94 (Lang <= dwarf::DW_LANG_hi_user && Lang >= dwarf::DW_LANG_lo_user)) &&
95 "Invalid Language tag");
96 assert(!Filename.empty() &&
97 "Unable to create compile unit without filename");
98 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
99 TempEnumTypes = MDNode::getTemporary(VMContext, TElts);
101 TempRetainTypes = MDNode::getTemporary(VMContext, TElts);
103 TempSubprograms = MDNode::getTemporary(VMContext, TElts);
105 TempGVs = MDNode::getTemporary(VMContext, TElts);
107 TempImportedModules = MDNode::getTemporary(VMContext, TElts);
110 GetTagConstant(VMContext, dwarf::DW_TAG_compile_unit),
111 createFilePathPair(VMContext, Filename, Directory),
112 ConstantInt::get(Type::getInt32Ty(VMContext), Lang),
113 MDString::get(VMContext, Producer),
114 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
115 MDString::get(VMContext, Flags),
116 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer),
122 MDString::get(VMContext, SplitName)
124 TheCU = DICompileUnit(MDNode::get(VMContext, Elts));
126 // Create a named metadata so that it is easier to find cu in a module.
127 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
128 NMD->addOperand(TheCU);
131 DIImportedModule DIBuilder::createImportedModule(DIScope Context,
135 GetTagConstant(VMContext, dwarf::DW_TAG_imported_module),
138 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
140 DIImportedModule M(MDNode::get(VMContext, Elts));
141 assert(M.Verify() && "Imported module should be valid");
142 AllImportedModules.push_back(M);
146 /// createFile - Create a file descriptor to hold debugging information
148 DIFile DIBuilder::createFile(StringRef Filename, StringRef Directory) {
150 GetTagConstant(VMContext, dwarf::DW_TAG_file_type),
151 createFilePathPair(VMContext, Filename, Directory)
153 return DIFile(MDNode::get(VMContext, Elts));
156 /// createEnumerator - Create a single enumerator value.
157 DIEnumerator DIBuilder::createEnumerator(StringRef Name, uint64_t Val) {
158 assert(!Name.empty() && "Unable to create enumerator without name");
160 GetTagConstant(VMContext, dwarf::DW_TAG_enumerator),
161 MDString::get(VMContext, Name),
162 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
164 return DIEnumerator(MDNode::get(VMContext, Elts));
167 /// createNullPtrType - Create C++0x nullptr type.
168 DIType DIBuilder::createNullPtrType(StringRef Name) {
169 assert(!Name.empty() && "Unable to create type without name");
170 // nullptr is encoded in DIBasicType format. Line number, filename,
171 // ,size, alignment, offset and flags are always empty here.
173 GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_type),
176 MDString::get(VMContext, Name),
177 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
178 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
179 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
180 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
181 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
182 ConstantInt::get(Type::getInt32Ty(VMContext), 0) // Encoding
184 return DIType(MDNode::get(VMContext, Elts));
187 /// createBasicType - Create debugging information entry for a basic
188 /// type, e.g 'char'.
190 DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
191 uint64_t AlignInBits, unsigned Encoding) {
192 assert(!Name.empty() && "Unable to create type without name");
193 // Basic types are encoded in DIBasicType format. Line number, filename,
194 // offset and flags are always empty here.
196 GetTagConstant(VMContext, dwarf::DW_TAG_base_type),
197 NULL, // File/directory name
199 MDString::get(VMContext, Name),
200 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
201 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
202 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
203 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
204 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
205 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
207 return DIBasicType(MDNode::get(VMContext, Elts));
210 /// createQualifiedType - Create debugging information entry for a qualified
211 /// type, e.g. 'const int'.
212 DIDerivedType DIBuilder::createQualifiedType(unsigned Tag, DIType FromTy) {
213 // Qualified types are encoded in DIDerivedType format.
215 GetTagConstant(VMContext, Tag),
218 MDString::get(VMContext, StringRef()), // Empty name.
219 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
220 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
221 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
222 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
223 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
226 return DIDerivedType(MDNode::get(VMContext, Elts));
229 /// createPointerType - Create debugging information entry for a pointer.
231 DIBuilder::createPointerType(DIType PointeeTy, uint64_t SizeInBits,
232 uint64_t AlignInBits, StringRef Name) {
233 // Pointer types are encoded in DIDerivedType format.
235 GetTagConstant(VMContext, dwarf::DW_TAG_pointer_type),
238 MDString::get(VMContext, Name),
239 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
240 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
241 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
242 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
243 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
246 return DIDerivedType(MDNode::get(VMContext, Elts));
249 DIDerivedType DIBuilder::createMemberPointerType(DIType PointeeTy, DIType Base) {
250 // Pointer types are encoded in DIDerivedType format.
252 GetTagConstant(VMContext, dwarf::DW_TAG_ptr_to_member_type),
256 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
257 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
258 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
259 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
260 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
264 return DIDerivedType(MDNode::get(VMContext, Elts));
267 /// createReferenceType - Create debugging information entry for a reference
269 DIDerivedType DIBuilder::createReferenceType(unsigned Tag, DIType RTy) {
270 assert(RTy.Verify() && "Unable to create reference type");
271 // References are encoded in DIDerivedType format.
273 GetTagConstant(VMContext, Tag),
277 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
278 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
279 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
280 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
281 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
284 return DIDerivedType(MDNode::get(VMContext, Elts));
287 /// createTypedef - Create debugging information entry for a typedef.
288 DIDerivedType DIBuilder::createTypedef(DIType Ty, StringRef Name, DIFile File,
289 unsigned LineNo, DIDescriptor Context) {
290 // typedefs are encoded in DIDerivedType format.
291 assert(Ty.Verify() && "Invalid typedef type!");
293 GetTagConstant(VMContext, dwarf::DW_TAG_typedef),
295 getNonCompileUnitScope(Context),
296 MDString::get(VMContext, Name),
297 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
298 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
299 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
300 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
301 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
304 return DIDerivedType(MDNode::get(VMContext, Elts));
307 /// createFriend - Create debugging information entry for a 'friend'.
308 DIType DIBuilder::createFriend(DIType Ty, DIType FriendTy) {
309 // typedefs are encoded in DIDerivedType format.
310 assert(Ty.Verify() && "Invalid type!");
311 assert(FriendTy.Verify() && "Invalid friend type!");
313 GetTagConstant(VMContext, dwarf::DW_TAG_friend),
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), 0), // Offset
321 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
324 return DIType(MDNode::get(VMContext, Elts));
327 /// createInheritance - Create debugging information entry to establish
328 /// inheritance relationship between two types.
329 DIDerivedType DIBuilder::createInheritance(
330 DIType Ty, DIType BaseTy, uint64_t BaseOffset, unsigned Flags) {
331 assert(Ty.Verify() && "Unable to create inheritance");
332 // TAG_inheritance is encoded in DIDerivedType format.
334 GetTagConstant(VMContext, dwarf::DW_TAG_inheritance),
338 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
339 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
340 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
341 ConstantInt::get(Type::getInt64Ty(VMContext), BaseOffset),
342 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
345 return DIDerivedType(MDNode::get(VMContext, Elts));
348 /// createMemberType - Create debugging information entry for a member.
349 DIDerivedType DIBuilder::createMemberType(
350 DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
351 uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
352 unsigned Flags, DIType Ty) {
353 // TAG_member is encoded in DIDerivedType format.
355 GetTagConstant(VMContext, dwarf::DW_TAG_member),
357 getNonCompileUnitScope(Scope),
358 MDString::get(VMContext, Name),
359 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
360 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
361 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
362 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
363 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
366 return DIDerivedType(MDNode::get(VMContext, Elts));
369 /// createStaticMemberType - Create debugging information entry for a
370 /// C++ static data member.
371 DIType DIBuilder::createStaticMemberType(DIDescriptor Scope, StringRef Name,
372 DIFile File, unsigned LineNumber,
373 DIType Ty, unsigned Flags,
375 // TAG_member is encoded in DIDerivedType format.
376 Flags |= DIDescriptor::FlagStaticMember;
378 GetTagConstant(VMContext, dwarf::DW_TAG_member),
380 getNonCompileUnitScope(Scope),
381 MDString::get(VMContext, Name),
382 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
383 ConstantInt::get(Type::getInt64Ty(VMContext), 0/*SizeInBits*/),
384 ConstantInt::get(Type::getInt64Ty(VMContext), 0/*AlignInBits*/),
385 ConstantInt::get(Type::getInt64Ty(VMContext), 0/*OffsetInBits*/),
386 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
390 return DIType(MDNode::get(VMContext, Elts));
393 /// createObjCIVar - Create debugging information entry for Objective-C
394 /// instance variable.
395 DIType DIBuilder::createObjCIVar(StringRef Name,
396 DIFile File, unsigned LineNumber,
397 uint64_t SizeInBits, uint64_t AlignInBits,
398 uint64_t OffsetInBits, unsigned Flags,
399 DIType Ty, StringRef PropertyName,
400 StringRef GetterName, StringRef SetterName,
401 unsigned PropertyAttributes) {
402 // TAG_member is encoded in DIDerivedType format.
404 GetTagConstant(VMContext, dwarf::DW_TAG_member),
406 getNonCompileUnitScope(File),
407 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),
414 MDString::get(VMContext, PropertyName),
415 MDString::get(VMContext, GetterName),
416 MDString::get(VMContext, SetterName),
417 ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes)
419 return DIType(MDNode::get(VMContext, Elts));
422 /// createObjCIVar - Create debugging information entry for Objective-C
423 /// instance variable.
424 DIType DIBuilder::createObjCIVar(StringRef Name,
425 DIFile File, unsigned LineNumber,
426 uint64_t SizeInBits, uint64_t AlignInBits,
427 uint64_t OffsetInBits, unsigned Flags,
428 DIType Ty, MDNode *PropertyNode) {
429 // TAG_member is encoded in DIDerivedType format.
431 GetTagConstant(VMContext, dwarf::DW_TAG_member),
433 getNonCompileUnitScope(File),
434 MDString::get(VMContext, Name),
435 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
436 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
437 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
438 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
439 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
443 return DIType(MDNode::get(VMContext, Elts));
446 /// createObjCProperty - Create debugging information entry for Objective-C
448 DIObjCProperty DIBuilder::createObjCProperty(StringRef Name,
449 DIFile File, unsigned LineNumber,
450 StringRef GetterName,
451 StringRef SetterName,
452 unsigned PropertyAttributes,
455 GetTagConstant(VMContext, dwarf::DW_TAG_APPLE_property),
456 MDString::get(VMContext, Name),
458 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
459 MDString::get(VMContext, GetterName),
460 MDString::get(VMContext, SetterName),
461 ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes),
464 return DIObjCProperty(MDNode::get(VMContext, Elts));
467 /// createTemplateTypeParameter - Create debugging information for template
469 DITemplateTypeParameter
470 DIBuilder::createTemplateTypeParameter(DIDescriptor Context, StringRef Name,
471 DIType Ty, MDNode *File, unsigned LineNo,
474 GetTagConstant(VMContext, dwarf::DW_TAG_template_type_parameter),
475 getNonCompileUnitScope(Context),
476 MDString::get(VMContext, Name),
479 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
480 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
482 return DITemplateTypeParameter(MDNode::get(VMContext, Elts));
485 /// createTemplateValueParameter - Create debugging information for template
487 DITemplateValueParameter
488 DIBuilder::createTemplateValueParameter(DIDescriptor Context, StringRef Name,
489 DIType Ty, uint64_t Val,
490 MDNode *File, unsigned LineNo,
493 GetTagConstant(VMContext, dwarf::DW_TAG_template_value_parameter),
494 getNonCompileUnitScope(Context),
495 MDString::get(VMContext, Name),
497 ConstantInt::get(Type::getInt64Ty(VMContext), Val),
499 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
500 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
502 return DITemplateValueParameter(MDNode::get(VMContext, Elts));
505 /// createClassType - Create debugging information entry for a class.
506 DICompositeType DIBuilder::createClassType(DIDescriptor Context, StringRef Name,
507 DIFile File, unsigned LineNumber,
509 uint64_t AlignInBits,
510 uint64_t OffsetInBits,
511 unsigned Flags, DIType DerivedFrom,
513 MDNode *VTableHolder,
514 MDNode *TemplateParams) {
515 assert((!Context || Context.Verify()) &&
516 "createClassType should be called with a valid Context");
517 // TAG_class_type is encoded in DICompositeType format.
519 GetTagConstant(VMContext, dwarf::DW_TAG_class_type),
521 getNonCompileUnitScope(Context),
522 MDString::get(VMContext, Name),
523 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
524 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
525 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
526 ConstantInt::get(Type::getInt32Ty(VMContext), OffsetInBits),
527 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
530 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
534 DICompositeType R(MDNode::get(VMContext, Elts));
535 assert(R.Verify() && "createClassType should return a verifiable DIType");
539 /// createStructType - Create debugging information entry for a struct.
540 DICompositeType DIBuilder::createStructType(DIDescriptor Context,
541 StringRef Name, DIFile File,
544 uint64_t AlignInBits,
545 unsigned Flags, DIType DerivedFrom,
547 unsigned RunTimeLang,
548 MDNode *VTableHolder) {
549 // TAG_structure_type is encoded in DICompositeType format.
551 GetTagConstant(VMContext, dwarf::DW_TAG_structure_type),
553 getNonCompileUnitScope(Context),
554 MDString::get(VMContext, Name),
555 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
556 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
557 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
558 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
559 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
562 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
566 DICompositeType R(MDNode::get(VMContext, Elts));
567 assert(R.Verify() && "createStructType should return a verifiable DIType");
571 /// createUnionType - Create debugging information entry for an union.
572 DICompositeType DIBuilder::createUnionType(DIDescriptor Scope, StringRef Name,
573 DIFile File, unsigned LineNumber,
575 uint64_t AlignInBits, unsigned Flags,
577 unsigned RunTimeLang) {
578 // TAG_union_type is encoded in DICompositeType format.
580 GetTagConstant(VMContext, dwarf::DW_TAG_union_type),
582 getNonCompileUnitScope(Scope),
583 MDString::get(VMContext, Name),
584 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
585 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
586 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
587 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
588 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
591 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
592 Constant::getNullValue(Type::getInt32Ty(VMContext)),
595 return DICompositeType(MDNode::get(VMContext, Elts));
598 /// createSubroutineType - Create subroutine type.
600 DIBuilder::createSubroutineType(DIFile File, DIArray ParameterTypes) {
601 // TAG_subroutine_type is encoded in DICompositeType format.
603 GetTagConstant(VMContext, dwarf::DW_TAG_subroutine_type),
604 Constant::getNullValue(Type::getInt32Ty(VMContext)),
605 Constant::getNullValue(Type::getInt32Ty(VMContext)),
606 MDString::get(VMContext, ""),
607 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
608 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
609 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
610 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
611 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
614 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
615 Constant::getNullValue(Type::getInt32Ty(VMContext))
617 return DICompositeType(MDNode::get(VMContext, Elts));
620 /// createEnumerationType - Create debugging information entry for an
622 DICompositeType DIBuilder::createEnumerationType(
623 DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
624 uint64_t SizeInBits, uint64_t AlignInBits, DIArray Elements,
626 // TAG_enumeration_type is encoded in DICompositeType format.
628 GetTagConstant(VMContext, dwarf::DW_TAG_enumeration_type),
630 getNonCompileUnitScope(Scope),
631 MDString::get(VMContext, Name),
632 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
633 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
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 MDNode *Node = MDNode::get(VMContext, Elts);
643 AllEnumTypes.push_back(Node);
644 return DICompositeType(Node);
647 /// createArrayType - Create debugging information entry for an array.
648 DICompositeType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
649 DIType Ty, DIArray Subscripts) {
650 // TAG_array_type is encoded in DICompositeType format.
652 GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
653 NULL, // Filename/Directory,
655 MDString::get(VMContext, ""),
656 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
657 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
658 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
659 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
660 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
663 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
664 Constant::getNullValue(Type::getInt32Ty(VMContext))
666 return DICompositeType(MDNode::get(VMContext, Elts));
669 /// createVectorType - Create debugging information entry for a vector.
670 DIType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits,
671 DIType Ty, DIArray Subscripts) {
673 // A vector is an array type with the FlagVector flag applied.
675 GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
676 NULL, // Filename/Directory,
678 MDString::get(VMContext, ""),
679 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
680 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
681 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
682 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
683 ConstantInt::get(Type::getInt32Ty(VMContext), DIType::FlagVector),
686 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
687 Constant::getNullValue(Type::getInt32Ty(VMContext))
689 return DIType(MDNode::get(VMContext, Elts));
692 /// createArtificialType - Create a new DIType with "artificial" flag set.
693 DIType DIBuilder::createArtificialType(DIType Ty) {
694 if (Ty.isArtificial())
697 SmallVector<Value *, 9> Elts;
699 assert (N && "Unexpected input DIType!");
700 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
701 if (Value *V = N->getOperand(i))
704 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
707 unsigned CurFlags = Ty.getFlags();
708 CurFlags = CurFlags | DIType::FlagArtificial;
710 // Flags are stored at this slot.
711 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
713 return DIType(MDNode::get(VMContext, Elts));
716 /// createObjectPointerType - Create a new type with both the object pointer
717 /// and artificial flags set.
718 DIType DIBuilder::createObjectPointerType(DIType Ty) {
719 if (Ty.isObjectPointer())
722 SmallVector<Value *, 9> Elts;
724 assert (N && "Unexpected input DIType!");
725 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
726 if (Value *V = N->getOperand(i))
729 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
732 unsigned CurFlags = Ty.getFlags();
733 CurFlags = CurFlags | (DIType::FlagObjectPointer | DIType::FlagArtificial);
735 // Flags are stored at this slot.
736 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
738 return DIType(MDNode::get(VMContext, Elts));
741 /// retainType - Retain DIType in a module even if it is not referenced
742 /// through debug info anchors.
743 void DIBuilder::retainType(DIType T) {
744 AllRetainTypes.push_back(T);
747 /// createUnspecifiedParameter - Create unspeicified type descriptor
748 /// for the subroutine type.
749 DIDescriptor DIBuilder::createUnspecifiedParameter() {
751 GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_parameters)
753 return DIDescriptor(MDNode::get(VMContext, Elts));
756 /// createForwardDecl - Create a temporary forward-declared type that
757 /// can be RAUW'd if the full type is seen.
758 DIType DIBuilder::createForwardDecl(unsigned Tag, StringRef Name,
759 DIDescriptor Scope, DIFile F,
760 unsigned Line, unsigned RuntimeLang,
762 uint64_t AlignInBits) {
763 // Create a temporary MDNode.
765 GetTagConstant(VMContext, Tag),
767 getNonCompileUnitScope(Scope),
768 MDString::get(VMContext, Name),
769 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
770 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
771 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
772 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
773 ConstantInt::get(Type::getInt32Ty(VMContext),
774 DIDescriptor::FlagFwdDecl),
777 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
779 MDNode *Node = MDNode::getTemporary(VMContext, Elts);
780 assert(DIType(Node).Verify() &&
781 "createForwardDecl result should be verifiable");
785 /// getOrCreateArray - Get a DIArray, create one if required.
786 DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) {
787 if (Elements.empty()) {
788 Value *Null = Constant::getNullValue(Type::getInt32Ty(VMContext));
789 return DIArray(MDNode::get(VMContext, Null));
791 return DIArray(MDNode::get(VMContext, Elements));
794 /// getOrCreateSubrange - Create a descriptor for a value range. This
795 /// implicitly uniques the values returned.
796 DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
798 GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type),
799 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
800 ConstantInt::get(Type::getInt64Ty(VMContext), Count)
803 return DISubrange(MDNode::get(VMContext, Elts));
806 /// \brief Create a new descriptor for the specified global.
807 DIGlobalVariable DIBuilder::
808 createGlobalVariable(StringRef Name, StringRef LinkageName, DIFile F,
809 unsigned LineNumber, DIType Ty, bool isLocalToUnit,
812 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
813 Constant::getNullValue(Type::getInt32Ty(VMContext)),
815 MDString::get(VMContext, Name),
816 MDString::get(VMContext, Name),
817 MDString::get(VMContext, LinkageName),
819 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
821 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
822 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
826 MDNode *Node = MDNode::get(VMContext, Elts);
827 AllGVs.push_back(Node);
828 return DIGlobalVariable(Node);
831 /// \brief Create a new descriptor for the specified global.
832 DIGlobalVariable DIBuilder::
833 createGlobalVariable(StringRef Name, DIFile F, unsigned LineNumber,
834 DIType Ty, bool isLocalToUnit, Value *Val) {
835 return createGlobalVariable(Name, Name, F, LineNumber, Ty, isLocalToUnit,
839 /// createStaticVariable - Create a new descriptor for the specified static
841 DIGlobalVariable DIBuilder::
842 createStaticVariable(DIDescriptor Context, StringRef Name,
843 StringRef LinkageName, DIFile F, unsigned LineNumber,
844 DIType Ty, bool isLocalToUnit, Value *Val, MDNode *Decl) {
846 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
847 Constant::getNullValue(Type::getInt32Ty(VMContext)),
848 getNonCompileUnitScope(Context),
849 MDString::get(VMContext, Name),
850 MDString::get(VMContext, Name),
851 MDString::get(VMContext, LinkageName),
853 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
855 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
856 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
860 MDNode *Node = MDNode::get(VMContext, Elts);
861 AllGVs.push_back(Node);
862 return DIGlobalVariable(Node);
865 /// createVariable - Create a new descriptor for the specified variable.
866 DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
867 StringRef Name, DIFile File,
868 unsigned LineNo, DIType Ty,
869 bool AlwaysPreserve, unsigned Flags,
871 DIDescriptor Context(getNonCompileUnitScope(Scope));
872 assert((!Context || Context.Verify()) &&
873 "createLocalVariable should be called with a valid Context");
874 assert(Ty.Verify() &&
875 "createLocalVariable should be called with a valid type");
877 GetTagConstant(VMContext, Tag),
878 getNonCompileUnitScope(Scope),
879 MDString::get(VMContext, Name),
881 ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))),
883 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
884 Constant::getNullValue(Type::getInt32Ty(VMContext))
886 MDNode *Node = MDNode::get(VMContext, Elts);
887 if (AlwaysPreserve) {
888 // The optimizer may remove local variable. If there is an interest
889 // to preserve variable info in such situation then stash it in a
891 DISubprogram Fn(getDISubprogram(Scope));
892 NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, Fn);
893 FnLocals->addOperand(Node);
895 assert(DIVariable(Node).Verify() &&
896 "createLocalVariable should return a verifiable DIVariable");
897 return DIVariable(Node);
900 /// createComplexVariable - Create a new descriptor for the specified variable
901 /// which has a complex address expression for its address.
902 DIVariable DIBuilder::createComplexVariable(unsigned Tag, DIDescriptor Scope,
903 StringRef Name, DIFile F,
905 DIType Ty, ArrayRef<Value *> Addr,
907 SmallVector<Value *, 15> Elts;
908 Elts.push_back(GetTagConstant(VMContext, Tag));
909 Elts.push_back(getNonCompileUnitScope(Scope)),
910 Elts.push_back(MDString::get(VMContext, Name));
912 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext),
913 (LineNo | (ArgNo << 24))));
915 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
916 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
917 Elts.append(Addr.begin(), Addr.end());
919 return DIVariable(MDNode::get(VMContext, Elts));
922 /// createFunction - Create a new descriptor for the specified function.
923 DISubprogram DIBuilder::createFunction(DIDescriptor Context,
925 StringRef LinkageName,
926 DIFile File, unsigned LineNo,
928 bool isLocalToUnit, bool isDefinition,
930 unsigned Flags, bool isOptimized,
934 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
936 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
938 getNonCompileUnitScope(Context),
939 MDString::get(VMContext, Name),
940 MDString::get(VMContext, Name),
941 MDString::get(VMContext, LinkageName),
942 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
944 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
945 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
946 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
947 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
949 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
950 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
954 MDNode::getTemporary(VMContext, TElts),
955 ConstantInt::get(Type::getInt32Ty(VMContext), ScopeLine)
957 MDNode *Node = MDNode::get(VMContext, Elts);
959 // Create a named metadata so that we do not lose this mdnode.
961 AllSubprograms.push_back(Node);
962 DISubprogram S(Node);
963 assert(S.Verify() && "createFunction should return a valid DISubprogram");
967 /// createMethod - Create a new descriptor for the specified C++ method.
968 DISubprogram DIBuilder::createMethod(DIDescriptor Context,
970 StringRef LinkageName,
972 unsigned LineNo, DIType Ty,
975 unsigned VK, unsigned VIndex,
976 MDNode *VTableHolder,
981 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
983 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
985 getNonCompileUnitScope(Context),
986 MDString::get(VMContext, Name),
987 MDString::get(VMContext, Name),
988 MDString::get(VMContext, LinkageName),
989 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
991 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
992 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
993 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
994 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
996 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
997 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1000 Constant::getNullValue(Type::getInt32Ty(VMContext)),
1001 MDNode::getTemporary(VMContext, TElts),
1002 // FIXME: Do we want to use different scope/lines?
1003 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1005 MDNode *Node = MDNode::get(VMContext, Elts);
1007 AllSubprograms.push_back(Node);
1008 DISubprogram S(Node);
1009 assert(S.Verify() && "createMethod should return a valid DISubprogram");
1013 /// createNameSpace - This creates new descriptor for a namespace
1014 /// with the specified parent scope.
1015 DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
1016 DIFile File, unsigned LineNo) {
1018 GetTagConstant(VMContext, dwarf::DW_TAG_namespace),
1020 getNonCompileUnitScope(Scope),
1021 MDString::get(VMContext, Name),
1022 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1024 DINameSpace R(MDNode::get(VMContext, Elts));
1025 assert(R.Verify() &&
1026 "createNameSpace should return a verifiable DINameSpace");
1030 /// createLexicalBlockFile - This creates a new MDNode that encapsulates
1031 /// an existing scope with a new filename.
1032 DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
1035 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1039 DILexicalBlockFile R(MDNode::get(VMContext, Elts));
1042 "createLexicalBlockFile should return a verifiable DILexicalBlockFile");
1046 DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
1047 unsigned Line, unsigned Col) {
1048 // Defeat MDNode uniqing for lexical blocks by using unique id.
1049 static unsigned int unique_id = 0;
1051 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1053 getNonCompileUnitScope(Scope),
1054 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
1055 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1056 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
1058 DILexicalBlock R(MDNode::get(VMContext, Elts));
1059 assert(R.Verify() &&
1060 "createLexicalBlock should return a verifiable DILexicalBlock");
1064 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1065 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1066 Instruction *InsertBefore) {
1067 assert(Storage && "no storage passed to dbg.declare");
1068 assert(VarInfo.Verify() && "empty DIVariable passed to dbg.declare");
1070 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1072 Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1073 return CallInst::Create(DeclareFn, Args, "", InsertBefore);
1076 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1077 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1078 BasicBlock *InsertAtEnd) {
1079 assert(Storage && "no storage passed to dbg.declare");
1080 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.declare");
1082 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1084 Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1086 // If this block already has a terminator then insert this intrinsic
1087 // before the terminator.
1088 if (TerminatorInst *T = InsertAtEnd->getTerminator())
1089 return CallInst::Create(DeclareFn, Args, "", T);
1091 return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
1094 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1095 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1097 Instruction *InsertBefore) {
1098 assert(V && "no value passed to dbg.value");
1099 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
1101 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1103 Value *Args[] = { MDNode::get(V->getContext(), V),
1104 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1106 return CallInst::Create(ValueFn, Args, "", InsertBefore);
1109 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1110 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1112 BasicBlock *InsertAtEnd) {
1113 assert(V && "no value passed to dbg.value");
1114 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
1116 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1118 Value *Args[] = { MDNode::get(V->getContext(), V),
1119 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1121 return CallInst::Create(ValueFn, Args, "", InsertAtEnd);