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 MDString::get(VMContext, Filename),
99 MDString::get(VMContext, Directory),
100 MDString::get(VMContext, Producer),
101 // isMain field can be removed when we remove the legacy debug info.
102 ConstantInt::get(Type::getInt1Ty(VMContext), true), // isMain
103 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
104 MDString::get(VMContext, Flags),
105 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer),
110 MDString::get(VMContext, SplitName)
112 TheCU = DICompileUnit(MDNode::get(VMContext, Elts));
114 // Create a named metadata so that it is easier to find cu in a module.
115 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
116 NMD->addOperand(TheCU);
119 /// createFile - Create a file descriptor to hold debugging information
121 DIFile DIBuilder::createFile(StringRef Filename, StringRef Directory) {
122 assert(TheCU && "Unable to create DW_TAG_file_type without CompileUnit");
123 assert(!Filename.empty() && "Unable to create file without name");
125 GetTagConstant(VMContext, dwarf::DW_TAG_file_type),
126 MDString::get(VMContext, Filename),
127 MDString::get(VMContext, Directory),
130 return DIFile(MDNode::get(VMContext, Elts));
133 /// createEnumerator - Create a single enumerator value.
134 DIEnumerator DIBuilder::createEnumerator(StringRef Name, uint64_t Val) {
135 assert(!Name.empty() && "Unable to create enumerator without name");
137 GetTagConstant(VMContext, dwarf::DW_TAG_enumerator),
138 MDString::get(VMContext, Name),
139 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
141 return DIEnumerator(MDNode::get(VMContext, Elts));
144 /// createNullPtrType - Create C++0x nullptr type.
145 DIType DIBuilder::createNullPtrType(StringRef Name) {
146 assert(!Name.empty() && "Unable to create type without name");
147 // nullptr is encoded in DIBasicType format. Line number, filename,
148 // ,size, alignment, offset and flags are always empty here.
150 GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_type),
152 MDString::get(VMContext, Name),
154 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
155 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
156 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
157 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
158 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
159 ConstantInt::get(Type::getInt32Ty(VMContext), 0) // Encoding
161 return DIType(MDNode::get(VMContext, Elts));
164 /// createBasicType - Create debugging information entry for a basic
165 /// type, e.g 'char'.
167 DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
168 uint64_t AlignInBits, unsigned Encoding) {
169 assert(!Name.empty() && "Unable to create type without name");
170 // Basic types are encoded in DIBasicType format. Line number, filename,
171 // offset and flags are always empty here.
173 GetTagConstant(VMContext, dwarf::DW_TAG_base_type),
175 MDString::get(VMContext, Name),
177 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
178 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
179 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
180 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
181 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
182 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
184 return DIBasicType(MDNode::get(VMContext, Elts));
187 /// createQualifiedType - Create debugging information entry for a qualified
188 /// type, e.g. 'const int'.
189 DIDerivedType DIBuilder::createQualifiedType(unsigned Tag, DIType FromTy) {
190 // Qualified types are encoded in DIDerivedType format.
192 GetTagConstant(VMContext, Tag),
194 MDString::get(VMContext, StringRef()), // Empty name.
196 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
197 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
198 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
199 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
200 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
203 return DIDerivedType(MDNode::get(VMContext, Elts));
206 /// createPointerType - Create debugging information entry for a pointer.
208 DIBuilder::createPointerType(DIType PointeeTy, uint64_t SizeInBits,
209 uint64_t AlignInBits, StringRef Name) {
210 // Pointer types are encoded in DIDerivedType format.
212 GetTagConstant(VMContext, dwarf::DW_TAG_pointer_type),
214 MDString::get(VMContext, Name),
216 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
217 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
218 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
219 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
220 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
223 return DIDerivedType(MDNode::get(VMContext, Elts));
226 DIDerivedType DIBuilder::createMemberPointerType(DIType PointeeTy, DIType Base) {
227 // Pointer types are encoded in DIDerivedType format.
229 GetTagConstant(VMContext, dwarf::DW_TAG_ptr_to_member_type),
233 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
234 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
235 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
236 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
237 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
241 return DIDerivedType(MDNode::get(VMContext, Elts));
244 /// createReferenceType - Create debugging information entry for a reference
246 DIDerivedType DIBuilder::createReferenceType(unsigned Tag, DIType RTy) {
247 assert(RTy.Verify() && "Unable to create reference type");
248 // References are encoded in DIDerivedType format.
250 GetTagConstant(VMContext, Tag),
254 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
255 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
256 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
257 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
258 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
261 return DIDerivedType(MDNode::get(VMContext, Elts));
264 /// createTypedef - Create debugging information entry for a typedef.
265 DIDerivedType DIBuilder::createTypedef(DIType Ty, StringRef Name, DIFile File,
266 unsigned LineNo, DIDescriptor Context) {
267 // typedefs are encoded in DIDerivedType format.
268 assert(Ty.Verify() && "Invalid typedef type!");
270 GetTagConstant(VMContext, dwarf::DW_TAG_typedef),
271 getNonCompileUnitScope(Context),
272 MDString::get(VMContext, Name),
274 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
275 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
276 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
277 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
278 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
281 return DIDerivedType(MDNode::get(VMContext, Elts));
284 /// createFriend - Create debugging information entry for a 'friend'.
285 DIType DIBuilder::createFriend(DIType Ty, DIType FriendTy) {
286 // typedefs are encoded in DIDerivedType format.
287 assert(Ty.Verify() && "Invalid type!");
288 assert(FriendTy.Verify() && "Invalid friend type!");
290 GetTagConstant(VMContext, dwarf::DW_TAG_friend),
294 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
295 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
296 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
297 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
298 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
301 return DIType(MDNode::get(VMContext, Elts));
304 /// createInheritance - Create debugging information entry to establish
305 /// inheritance relationship between two types.
306 DIDerivedType DIBuilder::createInheritance(
307 DIType Ty, DIType BaseTy, uint64_t BaseOffset, unsigned Flags) {
308 assert(Ty.Verify() && "Unable to create inheritance");
309 // TAG_inheritance is encoded in DIDerivedType format.
311 GetTagConstant(VMContext, dwarf::DW_TAG_inheritance),
315 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
316 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
317 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
318 ConstantInt::get(Type::getInt64Ty(VMContext), BaseOffset),
319 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
322 return DIDerivedType(MDNode::get(VMContext, Elts));
325 /// createMemberType - Create debugging information entry for a member.
326 DIDerivedType DIBuilder::createMemberType(
327 DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
328 uint64_t SizeInBits, uint64_t AlignInBits, uint64_t OffsetInBits,
329 unsigned Flags, DIType Ty) {
330 // TAG_member is encoded in DIDerivedType format.
332 GetTagConstant(VMContext, dwarf::DW_TAG_member),
333 getNonCompileUnitScope(Scope),
334 MDString::get(VMContext, Name),
336 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
337 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
338 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
339 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
340 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
343 return DIDerivedType(MDNode::get(VMContext, Elts));
346 /// createStaticMemberType - Create debugging information entry for a
347 /// C++ static data member.
348 DIType DIBuilder::createStaticMemberType(DIDescriptor Scope, StringRef Name,
349 DIFile File, unsigned LineNumber,
350 DIType Ty, unsigned Flags,
352 // TAG_member is encoded in DIDerivedType format.
353 Flags |= DIDescriptor::FlagStaticMember;
355 GetTagConstant(VMContext, dwarf::DW_TAG_member),
356 getNonCompileUnitScope(Scope),
357 MDString::get(VMContext, Name),
359 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
360 ConstantInt::get(Type::getInt64Ty(VMContext), 0/*SizeInBits*/),
361 ConstantInt::get(Type::getInt64Ty(VMContext), 0/*AlignInBits*/),
362 ConstantInt::get(Type::getInt64Ty(VMContext), 0/*OffsetInBits*/),
363 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
367 return DIType(MDNode::get(VMContext, Elts));
370 /// createObjCIVar - Create debugging information entry for Objective-C
371 /// instance variable.
372 DIType DIBuilder::createObjCIVar(StringRef Name,
373 DIFile File, unsigned LineNumber,
374 uint64_t SizeInBits, uint64_t AlignInBits,
375 uint64_t OffsetInBits, unsigned Flags,
376 DIType Ty, StringRef PropertyName,
377 StringRef GetterName, StringRef SetterName,
378 unsigned PropertyAttributes) {
379 // TAG_member is encoded in DIDerivedType format.
381 GetTagConstant(VMContext, dwarf::DW_TAG_member),
382 getNonCompileUnitScope(File),
383 MDString::get(VMContext, Name),
385 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
386 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
387 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
388 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
389 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
391 MDString::get(VMContext, PropertyName),
392 MDString::get(VMContext, GetterName),
393 MDString::get(VMContext, SetterName),
394 ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes)
396 return DIType(MDNode::get(VMContext, Elts));
399 /// createObjCIVar - Create debugging information entry for Objective-C
400 /// instance variable.
401 DIType DIBuilder::createObjCIVar(StringRef Name,
402 DIFile File, unsigned LineNumber,
403 uint64_t SizeInBits, uint64_t AlignInBits,
404 uint64_t OffsetInBits, unsigned Flags,
405 DIType Ty, MDNode *PropertyNode) {
406 // TAG_member is encoded in DIDerivedType format.
408 GetTagConstant(VMContext, dwarf::DW_TAG_member),
409 getNonCompileUnitScope(File),
410 MDString::get(VMContext, Name),
412 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
413 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
414 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
415 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
416 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
420 return DIType(MDNode::get(VMContext, Elts));
423 /// createObjCProperty - Create debugging information entry for Objective-C
425 DIObjCProperty DIBuilder::createObjCProperty(StringRef Name,
426 DIFile File, unsigned LineNumber,
427 StringRef GetterName,
428 StringRef SetterName,
429 unsigned PropertyAttributes,
432 GetTagConstant(VMContext, dwarf::DW_TAG_APPLE_property),
433 MDString::get(VMContext, Name),
435 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
436 MDString::get(VMContext, GetterName),
437 MDString::get(VMContext, SetterName),
438 ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes),
441 return DIObjCProperty(MDNode::get(VMContext, Elts));
444 /// createTemplateTypeParameter - Create debugging information for template
446 DITemplateTypeParameter
447 DIBuilder::createTemplateTypeParameter(DIDescriptor Context, StringRef Name,
448 DIType Ty, MDNode *File, unsigned LineNo,
451 GetTagConstant(VMContext, dwarf::DW_TAG_template_type_parameter),
452 getNonCompileUnitScope(Context),
453 MDString::get(VMContext, Name),
456 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
457 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
459 return DITemplateTypeParameter(MDNode::get(VMContext, Elts));
462 /// createTemplateValueParameter - Create debugging information for template
464 DITemplateValueParameter
465 DIBuilder::createTemplateValueParameter(DIDescriptor Context, StringRef Name,
466 DIType Ty, uint64_t Val,
467 MDNode *File, unsigned LineNo,
470 GetTagConstant(VMContext, dwarf::DW_TAG_template_value_parameter),
471 getNonCompileUnitScope(Context),
472 MDString::get(VMContext, Name),
474 ConstantInt::get(Type::getInt64Ty(VMContext), Val),
476 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
477 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
479 return DITemplateValueParameter(MDNode::get(VMContext, Elts));
482 /// createClassType - Create debugging information entry for a class.
483 DIType DIBuilder::createClassType(DIDescriptor Context, StringRef Name,
484 DIFile File, unsigned LineNumber,
485 uint64_t SizeInBits, uint64_t AlignInBits,
486 uint64_t OffsetInBits, unsigned Flags,
487 DIType DerivedFrom, DIArray Elements,
488 MDNode *VTableHolder,
489 MDNode *TemplateParams) {
490 // TAG_class_type is encoded in DICompositeType format.
492 GetTagConstant(VMContext, dwarf::DW_TAG_class_type),
493 getNonCompileUnitScope(Context),
494 MDString::get(VMContext, Name),
496 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
497 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
498 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
499 ConstantInt::get(Type::getInt32Ty(VMContext), OffsetInBits),
500 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
503 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
507 return DIType(MDNode::get(VMContext, Elts));
510 /// createStructType - Create debugging information entry for a struct.
511 DIType DIBuilder::createStructType(DIDescriptor Context, StringRef Name,
512 DIFile File, unsigned LineNumber,
513 uint64_t SizeInBits, uint64_t AlignInBits,
514 unsigned Flags, DIArray Elements,
515 unsigned RunTimeLang) {
516 // TAG_structure_type is encoded in DICompositeType format.
518 GetTagConstant(VMContext, dwarf::DW_TAG_structure_type),
519 getNonCompileUnitScope(Context),
520 MDString::get(VMContext, Name),
522 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
523 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
524 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
525 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
526 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
529 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
533 return DIType(MDNode::get(VMContext, Elts));
536 /// createUnionType - Create debugging information entry for an union.
537 DICompositeType DIBuilder::createUnionType(
538 DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
539 uint64_t SizeInBits, uint64_t AlignInBits, unsigned Flags, DIArray Elements,
540 unsigned RunTimeLang) {
541 // TAG_union_type is encoded in DICompositeType format.
543 GetTagConstant(VMContext, dwarf::DW_TAG_union_type),
544 getNonCompileUnitScope(Scope),
545 MDString::get(VMContext, Name),
547 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
548 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
549 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
550 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
551 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
554 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
555 Constant::getNullValue(Type::getInt32Ty(VMContext))
557 return DICompositeType(MDNode::get(VMContext, Elts));
560 /// createSubroutineType - Create subroutine type.
562 DIBuilder::createSubroutineType(DIFile File, DIArray ParameterTypes) {
563 // TAG_subroutine_type is encoded in DICompositeType format.
565 GetTagConstant(VMContext, dwarf::DW_TAG_subroutine_type),
566 Constant::getNullValue(Type::getInt32Ty(VMContext)),
567 MDString::get(VMContext, ""),
568 Constant::getNullValue(Type::getInt32Ty(VMContext)),
569 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
570 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
571 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
572 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
573 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
576 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
577 Constant::getNullValue(Type::getInt32Ty(VMContext))
579 return DICompositeType(MDNode::get(VMContext, Elts));
582 /// createEnumerationType - Create debugging information entry for an
584 DICompositeType DIBuilder::createEnumerationType(
585 DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
586 uint64_t SizeInBits, uint64_t AlignInBits, DIArray Elements,
588 // TAG_enumeration_type is encoded in DICompositeType format.
590 GetTagConstant(VMContext, dwarf::DW_TAG_enumeration_type),
591 getNonCompileUnitScope(Scope),
592 MDString::get(VMContext, Name),
594 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
595 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
596 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
597 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
598 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
601 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
602 Constant::getNullValue(Type::getInt32Ty(VMContext))
604 MDNode *Node = MDNode::get(VMContext, Elts);
605 AllEnumTypes.push_back(Node);
606 return DICompositeType(Node);
609 /// createArrayType - Create debugging information entry for an array.
610 DICompositeType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
611 DIType Ty, DIArray Subscripts) {
612 // TAG_array_type is encoded in DICompositeType format.
614 GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
616 MDString::get(VMContext, ""),
618 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
619 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
620 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
621 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
622 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
625 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
626 Constant::getNullValue(Type::getInt32Ty(VMContext))
628 return DICompositeType(MDNode::get(VMContext, Elts));
631 /// createVectorType - Create debugging information entry for a vector.
632 DIType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits,
633 DIType Ty, DIArray Subscripts) {
635 // A vector is an array type with the FlagVector flag applied.
637 GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
639 MDString::get(VMContext, ""),
641 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
642 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
643 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
644 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
645 ConstantInt::get(Type::getInt32Ty(VMContext), DIType::FlagVector),
648 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
649 Constant::getNullValue(Type::getInt32Ty(VMContext))
651 return DIType(MDNode::get(VMContext, Elts));
654 /// createArtificialType - Create a new DIType with "artificial" flag set.
655 DIType DIBuilder::createArtificialType(DIType Ty) {
656 if (Ty.isArtificial())
659 SmallVector<Value *, 9> Elts;
661 assert (N && "Unexpected input DIType!");
662 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
663 if (Value *V = N->getOperand(i))
666 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
669 unsigned CurFlags = Ty.getFlags();
670 CurFlags = CurFlags | DIType::FlagArtificial;
672 // Flags are stored at this slot.
673 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
675 return DIType(MDNode::get(VMContext, Elts));
678 /// createObjectPointerType - Create a new type with both the object pointer
679 /// and artificial flags set.
680 DIType DIBuilder::createObjectPointerType(DIType Ty) {
681 if (Ty.isObjectPointer())
684 SmallVector<Value *, 9> Elts;
686 assert (N && "Unexpected input DIType!");
687 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
688 if (Value *V = N->getOperand(i))
691 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
694 unsigned CurFlags = Ty.getFlags();
695 CurFlags = CurFlags | (DIType::FlagObjectPointer | DIType::FlagArtificial);
697 // Flags are stored at this slot.
698 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
700 return DIType(MDNode::get(VMContext, Elts));
703 /// retainType - Retain DIType in a module even if it is not referenced
704 /// through debug info anchors.
705 void DIBuilder::retainType(DIType T) {
706 AllRetainTypes.push_back(T);
709 /// createUnspecifiedParameter - Create unspeicified type descriptor
710 /// for the subroutine type.
711 DIDescriptor DIBuilder::createUnspecifiedParameter() {
713 GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_parameters)
715 return DIDescriptor(MDNode::get(VMContext, Elts));
718 /// createTemporaryType - Create a temporary forward-declared type.
719 DIType DIBuilder::createTemporaryType() {
720 // Give the temporary MDNode a tag. It doesn't matter what tag we
721 // use here as long as DIType accepts it.
722 Value *Elts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
723 MDNode *Node = MDNode::getTemporary(VMContext, Elts);
727 /// createTemporaryType - Create a temporary forward-declared type.
728 DIType DIBuilder::createTemporaryType(DIFile F) {
729 // Give the temporary MDNode a tag. It doesn't matter what tag we
730 // use here as long as DIType accepts it.
732 GetTagConstant(VMContext, DW_TAG_base_type),
737 MDNode *Node = MDNode::getTemporary(VMContext, Elts);
741 /// createForwardDecl - Create a temporary forward-declared type that
742 /// can be RAUW'd if the full type is seen.
743 DIType DIBuilder::createForwardDecl(unsigned Tag, StringRef Name,
744 DIDescriptor Scope, DIFile F,
745 unsigned Line, unsigned RuntimeLang,
747 uint64_t AlignInBits) {
748 // Create a temporary MDNode.
750 GetTagConstant(VMContext, Tag),
751 getNonCompileUnitScope(Scope),
752 MDString::get(VMContext, Name),
754 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
755 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
756 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
757 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
758 ConstantInt::get(Type::getInt32Ty(VMContext),
759 DIDescriptor::FlagFwdDecl),
762 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
764 MDNode *Node = MDNode::getTemporary(VMContext, Elts);
768 /// getOrCreateArray - Get a DIArray, create one if required.
769 DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) {
770 if (Elements.empty()) {
771 Value *Null = Constant::getNullValue(Type::getInt32Ty(VMContext));
772 return DIArray(MDNode::get(VMContext, Null));
774 return DIArray(MDNode::get(VMContext, Elements));
777 /// getOrCreateSubrange - Create a descriptor for a value range. This
778 /// implicitly uniques the values returned.
779 DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
781 GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type),
782 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
783 ConstantInt::get(Type::getInt64Ty(VMContext), Count)
786 return DISubrange(MDNode::get(VMContext, Elts));
789 /// createGlobalVariable - Create a new descriptor for the specified global.
790 DIGlobalVariable DIBuilder::
791 createGlobalVariable(StringRef Name, DIFile F, unsigned LineNumber,
792 DIType Ty, bool isLocalToUnit, Value *Val) {
794 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
795 Constant::getNullValue(Type::getInt32Ty(VMContext)),
797 MDString::get(VMContext, Name),
798 MDString::get(VMContext, Name),
799 MDString::get(VMContext, Name),
801 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
803 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
804 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
808 MDNode *Node = MDNode::get(VMContext, Elts);
809 AllGVs.push_back(Node);
810 return DIGlobalVariable(Node);
813 /// createStaticVariable - Create a new descriptor for the specified static
815 DIGlobalVariable DIBuilder::
816 createStaticVariable(DIDescriptor Context, StringRef Name,
817 StringRef LinkageName, DIFile F, unsigned LineNumber,
818 DIType Ty, bool isLocalToUnit, Value *Val, MDNode *Decl) {
820 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
821 Constant::getNullValue(Type::getInt32Ty(VMContext)),
822 getNonCompileUnitScope(Context),
823 MDString::get(VMContext, Name),
824 MDString::get(VMContext, Name),
825 MDString::get(VMContext, LinkageName),
827 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
829 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
830 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
834 MDNode *Node = MDNode::get(VMContext, Elts);
835 AllGVs.push_back(Node);
836 return DIGlobalVariable(Node);
839 /// createVariable - Create a new descriptor for the specified variable.
840 DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
841 StringRef Name, DIFile File,
842 unsigned LineNo, DIType Ty,
843 bool AlwaysPreserve, unsigned Flags,
846 GetTagConstant(VMContext, Tag),
847 getNonCompileUnitScope(Scope),
848 MDString::get(VMContext, Name),
850 ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))),
852 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
853 Constant::getNullValue(Type::getInt32Ty(VMContext))
855 MDNode *Node = MDNode::get(VMContext, Elts);
856 if (AlwaysPreserve) {
857 // The optimizer may remove local variable. If there is an interest
858 // to preserve variable info in such situation then stash it in a
860 DISubprogram Fn(getDISubprogram(Scope));
861 NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, Fn);
862 FnLocals->addOperand(Node);
864 return DIVariable(Node);
867 /// createComplexVariable - Create a new descriptor for the specified variable
868 /// which has a complex address expression for its address.
869 DIVariable DIBuilder::createComplexVariable(unsigned Tag, DIDescriptor Scope,
870 StringRef Name, DIFile F,
872 DIType Ty, ArrayRef<Value *> Addr,
874 SmallVector<Value *, 15> Elts;
875 Elts.push_back(GetTagConstant(VMContext, Tag));
876 Elts.push_back(getNonCompileUnitScope(Scope)),
877 Elts.push_back(MDString::get(VMContext, Name));
879 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext),
880 (LineNo | (ArgNo << 24))));
882 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
883 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
884 Elts.append(Addr.begin(), Addr.end());
886 return DIVariable(MDNode::get(VMContext, Elts));
889 /// createFunction - Create a new descriptor for the specified function.
890 DISubprogram DIBuilder::createFunction(DIDescriptor Context,
892 StringRef LinkageName,
893 DIFile File, unsigned LineNo,
895 bool isLocalToUnit, bool isDefinition,
897 unsigned Flags, bool isOptimized,
901 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
903 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
904 Constant::getNullValue(Type::getInt32Ty(VMContext)),
905 getNonCompileUnitScope(Context),
906 MDString::get(VMContext, Name),
907 MDString::get(VMContext, Name),
908 MDString::get(VMContext, LinkageName),
910 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
912 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
913 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
914 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
915 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
917 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
918 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
922 MDNode::getTemporary(VMContext, TElts),
923 ConstantInt::get(Type::getInt32Ty(VMContext), ScopeLine)
925 MDNode *Node = MDNode::get(VMContext, Elts);
927 // Create a named metadata so that we do not lose this mdnode.
929 AllSubprograms.push_back(Node);
930 return DISubprogram(Node);
933 /// createMethod - Create a new descriptor for the specified C++ method.
934 DISubprogram DIBuilder::createMethod(DIDescriptor Context,
936 StringRef LinkageName,
938 unsigned LineNo, DIType Ty,
941 unsigned VK, unsigned VIndex,
942 MDNode *VTableHolder,
947 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
949 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
950 Constant::getNullValue(Type::getInt32Ty(VMContext)),
951 getNonCompileUnitScope(Context),
952 MDString::get(VMContext, Name),
953 MDString::get(VMContext, Name),
954 MDString::get(VMContext, LinkageName),
956 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
958 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
959 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
960 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
961 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
963 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
964 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
967 Constant::getNullValue(Type::getInt32Ty(VMContext)),
968 MDNode::getTemporary(VMContext, TElts),
969 // FIXME: Do we want to use different scope/lines?
970 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
972 MDNode *Node = MDNode::get(VMContext, Elts);
974 AllSubprograms.push_back(Node);
975 return DISubprogram(Node);
978 /// createNameSpace - This creates new descriptor for a namespace
979 /// with the specified parent scope.
980 DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
981 DIFile File, unsigned LineNo) {
983 GetTagConstant(VMContext, dwarf::DW_TAG_namespace),
984 getNonCompileUnitScope(Scope),
985 MDString::get(VMContext, Name),
987 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
989 return DINameSpace(MDNode::get(VMContext, Elts));
992 /// createLexicalBlockFile - This creates a new MDNode that encapsulates
993 /// an existing scope with a new filename.
994 DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
997 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1001 return DILexicalBlockFile(MDNode::get(VMContext, Elts));
1004 DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
1005 unsigned Line, unsigned Col) {
1006 // Defeat MDNode uniqing for lexical blocks by using unique id.
1007 static unsigned int unique_id = 0;
1009 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1010 getNonCompileUnitScope(Scope),
1011 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
1012 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1014 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
1016 return DILexicalBlock(MDNode::get(VMContext, Elts));
1019 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1020 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1021 Instruction *InsertBefore) {
1022 assert(Storage && "no storage passed to dbg.declare");
1023 assert(VarInfo.Verify() && "empty DIVariable passed to dbg.declare");
1025 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1027 Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1028 return CallInst::Create(DeclareFn, Args, "", InsertBefore);
1031 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1032 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1033 BasicBlock *InsertAtEnd) {
1034 assert(Storage && "no storage passed to dbg.declare");
1035 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.declare");
1037 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1039 Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1041 // If this block already has a terminator then insert this intrinsic
1042 // before the terminator.
1043 if (TerminatorInst *T = InsertAtEnd->getTerminator())
1044 return CallInst::Create(DeclareFn, Args, "", T);
1046 return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
1049 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1050 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1052 Instruction *InsertBefore) {
1053 assert(V && "no value passed to dbg.value");
1054 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
1056 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1058 Value *Args[] = { MDNode::get(V->getContext(), V),
1059 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1061 return CallInst::Create(ValueFn, Args, "", InsertBefore);
1064 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1065 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1067 BasicBlock *InsertAtEnd) {
1068 assert(V && "no value passed to dbg.value");
1069 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
1071 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1073 Value *Args[] = { MDNode::get(V->getContext(), V),
1074 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1076 return CallInst::Create(ValueFn, Args, "", InsertAtEnd);