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) {
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);
87 Value *THElts[] = { TempEnumTypes };
88 MDNode *EnumHolder = MDNode::get(VMContext, THElts);
90 TempRetainTypes = MDNode::getTemporary(VMContext, TElts);
91 Value *TRElts[] = { TempRetainTypes };
92 MDNode *RetainHolder = MDNode::get(VMContext, TRElts);
94 TempSubprograms = MDNode::getTemporary(VMContext, TElts);
95 Value *TSElts[] = { TempSubprograms };
96 MDNode *SPHolder = MDNode::get(VMContext, TSElts);
98 TempGVs = MDNode::getTemporary(VMContext, TElts);
99 Value *TVElts[] = { TempGVs };
100 MDNode *GVHolder = MDNode::get(VMContext, TVElts);
103 GetTagConstant(VMContext, dwarf::DW_TAG_compile_unit),
104 Constant::getNullValue(Type::getInt32Ty(VMContext)),
105 ConstantInt::get(Type::getInt32Ty(VMContext), Lang),
106 MDString::get(VMContext, Filename),
107 MDString::get(VMContext, Directory),
108 MDString::get(VMContext, Producer),
109 // Deprecate isMain field.
110 ConstantInt::get(Type::getInt1Ty(VMContext), true), // isMain
111 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
112 MDString::get(VMContext, Flags),
113 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer),
119 TheCU = DICompileUnit(MDNode::get(VMContext, Elts));
121 // Create a named metadata so that it is easier to find cu in a module.
122 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
123 NMD->addOperand(TheCU);
126 /// createFile - Create a file descriptor to hold debugging information
128 DIFile DIBuilder::createFile(StringRef Filename, StringRef Directory) {
129 assert(TheCU && "Unable to create DW_TAG_file_type without CompileUnit");
130 assert(!Filename.empty() && "Unable to create file without name");
132 GetTagConstant(VMContext, dwarf::DW_TAG_file_type),
133 MDString::get(VMContext, Filename),
134 MDString::get(VMContext, Directory),
137 return DIFile(MDNode::get(VMContext, Elts));
140 /// createEnumerator - Create a single enumerator value.
141 DIEnumerator DIBuilder::createEnumerator(StringRef Name, uint64_t Val) {
142 assert(!Name.empty() && "Unable to create enumerator without name");
144 GetTagConstant(VMContext, dwarf::DW_TAG_enumerator),
145 MDString::get(VMContext, Name),
146 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
148 return DIEnumerator(MDNode::get(VMContext, Elts));
151 /// createNullPtrType - Create C++0x nullptr type.
152 DIType DIBuilder::createNullPtrType(StringRef Name) {
153 assert(!Name.empty() && "Unable to create type without name");
154 // nullptr is encoded in DIBasicType format. Line number, filename,
155 // ,size, alignment, offset and flags are always empty here.
157 GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_type),
159 MDString::get(VMContext, Name),
161 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
162 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
163 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
164 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
165 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
166 ConstantInt::get(Type::getInt32Ty(VMContext), 0) // Encoding
168 return DIType(MDNode::get(VMContext, Elts));
171 /// createBasicType - Create debugging information entry for a basic
172 /// type, e.g 'char'.
173 DIType DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
174 uint64_t AlignInBits,
176 assert(!Name.empty() && "Unable to create type without name");
177 // Basic types are encoded in DIBasicType format. Line number, filename,
178 // offset and flags are always empty here.
180 GetTagConstant(VMContext, dwarf::DW_TAG_base_type),
182 MDString::get(VMContext, Name),
184 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
185 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
186 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
187 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
188 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
189 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
191 return DIType(MDNode::get(VMContext, Elts));
194 /// createQualifiedType - Create debugging information entry for a qualified
195 /// type, e.g. 'const int'.
196 DIType DIBuilder::createQualifiedType(unsigned Tag, DIType FromTy) {
197 // Qualified types are encoded in DIDerivedType format.
199 GetTagConstant(VMContext, Tag),
201 MDString::get(VMContext, StringRef()), // Empty name.
203 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
204 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
205 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
206 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
207 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
210 return DIType(MDNode::get(VMContext, Elts));
213 /// createPointerType - Create debugging information entry for a pointer.
214 DIType DIBuilder::createPointerType(DIType PointeeTy, uint64_t SizeInBits,
215 uint64_t AlignInBits, StringRef Name) {
216 // Pointer types are encoded in DIDerivedType format.
218 GetTagConstant(VMContext, dwarf::DW_TAG_pointer_type),
220 MDString::get(VMContext, Name),
222 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
223 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
224 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
225 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
226 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
229 return DIType(MDNode::get(VMContext, Elts));
232 DIType DIBuilder::createMemberPointerType(DIType PointeeTy, DIType Base) {
233 // Pointer types are encoded in DIDerivedType format.
235 GetTagConstant(VMContext, dwarf::DW_TAG_ptr_to_member_type),
239 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
240 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
241 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
242 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
243 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
247 return DIType(MDNode::get(VMContext, Elts));
250 /// createReferenceType - Create debugging information entry for a reference
252 DIType DIBuilder::createReferenceType(unsigned Tag, DIType RTy) {
253 assert(RTy.Verify() && "Unable to create reference type");
254 // References are encoded in DIDerivedType format.
256 GetTagConstant(VMContext, Tag),
260 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
261 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
262 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
263 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
264 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
267 return DIType(MDNode::get(VMContext, Elts));
270 /// createTypedef - Create debugging information entry for a typedef.
271 DIType DIBuilder::createTypedef(DIType Ty, StringRef Name, DIFile File,
272 unsigned LineNo, DIDescriptor Context) {
273 // typedefs are encoded in DIDerivedType format.
274 assert(Ty.Verify() && "Invalid typedef type!");
276 GetTagConstant(VMContext, dwarf::DW_TAG_typedef),
277 getNonCompileUnitScope(Context),
278 MDString::get(VMContext, Name),
280 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
281 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
282 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
283 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
284 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
287 return DIType(MDNode::get(VMContext, Elts));
290 /// createFriend - Create debugging information entry for a 'friend'.
291 DIType DIBuilder::createFriend(DIType Ty, DIType FriendTy) {
292 // typedefs are encoded in DIDerivedType format.
293 assert(Ty.Verify() && "Invalid type!");
294 assert(FriendTy.Verify() && "Invalid friend type!");
296 GetTagConstant(VMContext, dwarf::DW_TAG_friend),
300 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
301 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
302 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
303 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
304 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
307 return DIType(MDNode::get(VMContext, Elts));
310 /// createInheritance - Create debugging information entry to establish
311 /// inheritance relationship between two types.
312 DIType DIBuilder::createInheritance(DIType Ty, DIType BaseTy,
313 uint64_t BaseOffset, unsigned Flags) {
314 assert(Ty.Verify() && "Unable to create inheritance");
315 // TAG_inheritance is encoded in DIDerivedType format.
317 GetTagConstant(VMContext, dwarf::DW_TAG_inheritance),
321 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
322 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
323 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
324 ConstantInt::get(Type::getInt64Ty(VMContext), BaseOffset),
325 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
328 return DIType(MDNode::get(VMContext, Elts));
331 /// createMemberType - Create debugging information entry for a member.
332 DIType DIBuilder::createMemberType(DIDescriptor Scope, StringRef Name,
333 DIFile File, unsigned LineNumber,
334 uint64_t SizeInBits, uint64_t AlignInBits,
335 uint64_t OffsetInBits, unsigned Flags,
337 // TAG_member is encoded in DIDerivedType format.
339 GetTagConstant(VMContext, dwarf::DW_TAG_member),
340 getNonCompileUnitScope(Scope),
341 MDString::get(VMContext, Name),
343 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
344 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
345 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
346 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
347 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
350 return DIType(MDNode::get(VMContext, Elts));
353 /// createObjCIVar - Create debugging information entry for Objective-C
354 /// instance variable.
355 DIType DIBuilder::createObjCIVar(StringRef Name,
356 DIFile File, unsigned LineNumber,
357 uint64_t SizeInBits, uint64_t AlignInBits,
358 uint64_t OffsetInBits, unsigned Flags,
359 DIType Ty, StringRef PropertyName,
360 StringRef GetterName, StringRef SetterName,
361 unsigned PropertyAttributes) {
362 // TAG_member is encoded in DIDerivedType format.
364 GetTagConstant(VMContext, dwarf::DW_TAG_member),
365 getNonCompileUnitScope(File),
366 MDString::get(VMContext, Name),
368 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
369 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
370 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
371 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
372 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
374 MDString::get(VMContext, PropertyName),
375 MDString::get(VMContext, GetterName),
376 MDString::get(VMContext, SetterName),
377 ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes)
379 return DIType(MDNode::get(VMContext, Elts));
382 /// createObjCIVar - Create debugging information entry for Objective-C
383 /// instance variable.
384 DIType DIBuilder::createObjCIVar(StringRef Name,
385 DIFile File, unsigned LineNumber,
386 uint64_t SizeInBits, uint64_t AlignInBits,
387 uint64_t OffsetInBits, unsigned Flags,
388 DIType Ty, MDNode *PropertyNode) {
389 // TAG_member is encoded in DIDerivedType format.
391 GetTagConstant(VMContext, dwarf::DW_TAG_member),
392 getNonCompileUnitScope(File),
393 MDString::get(VMContext, Name),
395 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
396 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
397 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
398 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
399 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
403 return DIType(MDNode::get(VMContext, Elts));
406 /// createObjCProperty - Create debugging information entry for Objective-C
408 DIObjCProperty DIBuilder::createObjCProperty(StringRef Name,
409 DIFile File, unsigned LineNumber,
410 StringRef GetterName,
411 StringRef SetterName,
412 unsigned PropertyAttributes,
415 GetTagConstant(VMContext, dwarf::DW_TAG_APPLE_property),
416 MDString::get(VMContext, Name),
418 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
419 MDString::get(VMContext, GetterName),
420 MDString::get(VMContext, SetterName),
421 ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes),
424 return DIObjCProperty(MDNode::get(VMContext, Elts));
427 /// createTemplateTypeParameter - Create debugging information for template
429 DITemplateTypeParameter
430 DIBuilder::createTemplateTypeParameter(DIDescriptor Context, StringRef Name,
431 DIType Ty, MDNode *File, unsigned LineNo,
434 GetTagConstant(VMContext, dwarf::DW_TAG_template_type_parameter),
435 getNonCompileUnitScope(Context),
436 MDString::get(VMContext, Name),
439 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
440 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
442 return DITemplateTypeParameter(MDNode::get(VMContext, Elts));
445 /// createTemplateValueParameter - Create debugging information for template
447 DITemplateValueParameter
448 DIBuilder::createTemplateValueParameter(DIDescriptor Context, StringRef Name,
449 DIType Ty, uint64_t Val,
450 MDNode *File, unsigned LineNo,
453 GetTagConstant(VMContext, dwarf::DW_TAG_template_value_parameter),
454 getNonCompileUnitScope(Context),
455 MDString::get(VMContext, Name),
457 ConstantInt::get(Type::getInt64Ty(VMContext), Val),
459 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
460 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
462 return DITemplateValueParameter(MDNode::get(VMContext, Elts));
465 /// createClassType - Create debugging information entry for a class.
466 DIType DIBuilder::createClassType(DIDescriptor Context, StringRef Name,
467 DIFile File, unsigned LineNumber,
468 uint64_t SizeInBits, uint64_t AlignInBits,
469 uint64_t OffsetInBits, unsigned Flags,
470 DIType DerivedFrom, DIArray Elements,
471 MDNode *VTableHolder,
472 MDNode *TemplateParams) {
473 // TAG_class_type is encoded in DICompositeType format.
475 GetTagConstant(VMContext, dwarf::DW_TAG_class_type),
476 getNonCompileUnitScope(Context),
477 MDString::get(VMContext, Name),
479 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
480 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
481 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
482 ConstantInt::get(Type::getInt32Ty(VMContext), OffsetInBits),
483 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
486 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
490 return DIType(MDNode::get(VMContext, Elts));
493 /// createStructType - Create debugging information entry for a struct.
494 DIType DIBuilder::createStructType(DIDescriptor Context, StringRef Name,
495 DIFile File, unsigned LineNumber,
496 uint64_t SizeInBits, uint64_t AlignInBits,
497 unsigned Flags, DIArray Elements,
498 unsigned RunTimeLang) {
499 // TAG_structure_type is encoded in DICompositeType format.
501 GetTagConstant(VMContext, dwarf::DW_TAG_structure_type),
502 getNonCompileUnitScope(Context),
503 MDString::get(VMContext, Name),
505 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
506 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
507 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
508 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
509 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
512 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
513 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
514 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
516 return DIType(MDNode::get(VMContext, Elts));
519 /// createUnionType - Create debugging information entry for an union.
520 DIType DIBuilder::createUnionType(DIDescriptor Scope, StringRef Name,
522 unsigned LineNumber, uint64_t SizeInBits,
523 uint64_t AlignInBits, unsigned Flags,
524 DIArray Elements, unsigned RunTimeLang) {
525 // TAG_union_type is encoded in DICompositeType format.
527 GetTagConstant(VMContext, dwarf::DW_TAG_union_type),
528 getNonCompileUnitScope(Scope),
529 MDString::get(VMContext, Name),
531 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
532 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
533 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
534 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
535 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
538 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
539 Constant::getNullValue(Type::getInt32Ty(VMContext))
541 return DIType(MDNode::get(VMContext, Elts));
544 /// createSubroutineType - Create subroutine type.
545 DIType DIBuilder::createSubroutineType(DIFile File, DIArray ParameterTypes) {
546 // TAG_subroutine_type is encoded in DICompositeType format.
548 GetTagConstant(VMContext, dwarf::DW_TAG_subroutine_type),
549 Constant::getNullValue(Type::getInt32Ty(VMContext)),
550 MDString::get(VMContext, ""),
551 Constant::getNullValue(Type::getInt32Ty(VMContext)),
552 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
553 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
554 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
555 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
556 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
559 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
560 Constant::getNullValue(Type::getInt32Ty(VMContext))
562 return DIType(MDNode::get(VMContext, Elts));
565 /// createEnumerationType - Create debugging information entry for an
567 DIType DIBuilder::createEnumerationType(DIDescriptor Scope, StringRef Name,
568 DIFile File, unsigned LineNumber,
570 uint64_t AlignInBits,
573 // TAG_enumeration_type is encoded in DICompositeType format.
575 GetTagConstant(VMContext, dwarf::DW_TAG_enumeration_type),
576 getNonCompileUnitScope(Scope),
577 MDString::get(VMContext, Name),
579 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
580 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
581 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
582 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
583 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
586 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
587 Constant::getNullValue(Type::getInt32Ty(VMContext))
589 MDNode *Node = MDNode::get(VMContext, Elts);
590 AllEnumTypes.push_back(Node);
594 /// createArrayType - Create debugging information entry for an array.
595 DIType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
596 DIType Ty, DIArray Subscripts) {
597 // TAG_array_type is encoded in DICompositeType format.
599 GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
601 MDString::get(VMContext, ""),
603 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
604 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
605 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
606 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
607 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
610 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
611 Constant::getNullValue(Type::getInt32Ty(VMContext))
613 return DIType(MDNode::get(VMContext, Elts));
616 /// createVectorType - Create debugging information entry for a vector.
617 DIType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits,
618 DIType Ty, DIArray Subscripts) {
619 // TAG_vector_type is encoded in DICompositeType format.
621 GetTagConstant(VMContext, dwarf::DW_TAG_vector_type),
623 MDString::get(VMContext, ""),
625 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
626 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
627 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
628 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
629 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
632 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
633 Constant::getNullValue(Type::getInt32Ty(VMContext))
635 return DIType(MDNode::get(VMContext, Elts));
638 /// createArtificialType - Create a new DIType with "artificial" flag set.
639 DIType DIBuilder::createArtificialType(DIType Ty) {
640 if (Ty.isArtificial())
643 SmallVector<Value *, 9> Elts;
645 assert (N && "Unexpected input DIType!");
646 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
647 if (Value *V = N->getOperand(i))
650 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
653 unsigned CurFlags = Ty.getFlags();
654 CurFlags = CurFlags | DIType::FlagArtificial;
656 // Flags are stored at this slot.
657 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
659 return DIType(MDNode::get(VMContext, Elts));
662 /// createArtificialType - Create a new DIType with "artificial" flag set.
663 DIType DIBuilder::createObjectPointerType(DIType Ty) {
664 if (Ty.isObjectPointer())
667 SmallVector<Value *, 9> Elts;
669 assert (N && "Unexpected input DIType!");
670 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
671 if (Value *V = N->getOperand(i))
674 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
677 unsigned CurFlags = Ty.getFlags();
678 CurFlags = CurFlags | (DIType::FlagObjectPointer | DIType::FlagArtificial);
680 // Flags are stored at this slot.
681 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
683 return DIType(MDNode::get(VMContext, Elts));
686 /// retainType - Retain DIType in a module even if it is not referenced
687 /// through debug info anchors.
688 void DIBuilder::retainType(DIType T) {
689 AllRetainTypes.push_back(T);
692 /// createUnspecifiedParameter - Create unspeicified type descriptor
693 /// for the subroutine type.
694 DIDescriptor DIBuilder::createUnspecifiedParameter() {
696 GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_parameters)
698 return DIDescriptor(MDNode::get(VMContext, Elts));
701 /// createTemporaryType - Create a temporary forward-declared type.
702 DIType DIBuilder::createTemporaryType() {
703 // Give the temporary MDNode a tag. It doesn't matter what tag we
704 // use here as long as DIType accepts it.
705 Value *Elts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
706 MDNode *Node = MDNode::getTemporary(VMContext, Elts);
710 /// createTemporaryType - Create a temporary forward-declared type.
711 DIType DIBuilder::createTemporaryType(DIFile F) {
712 // Give the temporary MDNode a tag. It doesn't matter what tag we
713 // use here as long as DIType accepts it.
715 GetTagConstant(VMContext, DW_TAG_base_type),
720 MDNode *Node = MDNode::getTemporary(VMContext, Elts);
724 /// createForwardDecl - Create a temporary forward-declared type that
725 /// can be RAUW'd if the full type is seen.
726 DIType DIBuilder::createForwardDecl(unsigned Tag, StringRef Name,
727 DIDescriptor Scope, DIFile F,
728 unsigned Line, unsigned RuntimeLang,
730 uint64_t AlignInBits) {
731 // Create a temporary MDNode.
733 GetTagConstant(VMContext, Tag),
734 getNonCompileUnitScope(Scope),
735 MDString::get(VMContext, Name),
737 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
738 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
739 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
740 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
741 ConstantInt::get(Type::getInt32Ty(VMContext),
742 DIDescriptor::FlagFwdDecl),
745 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
747 MDNode *Node = MDNode::getTemporary(VMContext, Elts);
751 /// getOrCreateArray - Get a DIArray, create one if required.
752 DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) {
753 if (Elements.empty()) {
754 Value *Null = Constant::getNullValue(Type::getInt32Ty(VMContext));
755 return DIArray(MDNode::get(VMContext, Null));
757 return DIArray(MDNode::get(VMContext, Elements));
760 /// getOrCreateSubrange - Create a descriptor for a value range. This
761 /// implicitly uniques the values returned.
762 DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
764 GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type),
765 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
766 ConstantInt::get(Type::getInt64Ty(VMContext), Count)
769 return DISubrange(MDNode::get(VMContext, Elts));
772 /// createGlobalVariable - Create a new descriptor for the specified global.
773 DIGlobalVariable DIBuilder::
774 createGlobalVariable(StringRef Name, DIFile F, unsigned LineNumber,
775 DIType Ty, bool isLocalToUnit, Value *Val) {
777 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
778 Constant::getNullValue(Type::getInt32Ty(VMContext)),
780 MDString::get(VMContext, Name),
781 MDString::get(VMContext, Name),
782 MDString::get(VMContext, Name),
784 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
786 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
787 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
790 MDNode *Node = MDNode::get(VMContext, Elts);
791 AllGVs.push_back(Node);
792 return DIGlobalVariable(Node);
795 /// createStaticVariable - Create a new descriptor for the specified static
797 DIGlobalVariable DIBuilder::
798 createStaticVariable(DIDescriptor Context, StringRef Name,
799 StringRef LinkageName, DIFile F, unsigned LineNumber,
800 DIType Ty, bool isLocalToUnit, Value *Val) {
802 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
803 Constant::getNullValue(Type::getInt32Ty(VMContext)),
804 getNonCompileUnitScope(Context),
805 MDString::get(VMContext, Name),
806 MDString::get(VMContext, Name),
807 MDString::get(VMContext, LinkageName),
809 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
811 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
812 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
815 MDNode *Node = MDNode::get(VMContext, Elts);
816 AllGVs.push_back(Node);
817 return DIGlobalVariable(Node);
820 /// createVariable - Create a new descriptor for the specified variable.
821 DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
822 StringRef Name, DIFile File,
823 unsigned LineNo, DIType Ty,
824 bool AlwaysPreserve, unsigned Flags,
827 GetTagConstant(VMContext, Tag),
828 getNonCompileUnitScope(Scope),
829 MDString::get(VMContext, Name),
831 ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))),
833 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
834 Constant::getNullValue(Type::getInt32Ty(VMContext))
836 MDNode *Node = MDNode::get(VMContext, Elts);
837 if (AlwaysPreserve) {
838 // The optimizer may remove local variable. If there is an interest
839 // to preserve variable info in such situation then stash it in a
841 DISubprogram Fn(getDISubprogram(Scope));
842 NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, Fn);
843 FnLocals->addOperand(Node);
845 return DIVariable(Node);
848 /// createComplexVariable - Create a new descriptor for the specified variable
849 /// which has a complex address expression for its address.
850 DIVariable DIBuilder::createComplexVariable(unsigned Tag, DIDescriptor Scope,
851 StringRef Name, DIFile F,
853 DIType Ty, ArrayRef<Value *> Addr,
855 SmallVector<Value *, 15> Elts;
856 Elts.push_back(GetTagConstant(VMContext, Tag));
857 Elts.push_back(getNonCompileUnitScope(Scope)),
858 Elts.push_back(MDString::get(VMContext, Name));
860 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext),
861 (LineNo | (ArgNo << 24))));
863 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
864 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
865 Elts.append(Addr.begin(), Addr.end());
867 return DIVariable(MDNode::get(VMContext, Elts));
870 /// createFunction - Create a new descriptor for the specified function.
871 DISubprogram DIBuilder::createFunction(DIDescriptor Context,
873 StringRef LinkageName,
874 DIFile File, unsigned LineNo,
876 bool isLocalToUnit, bool isDefinition,
878 unsigned Flags, bool isOptimized,
882 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
883 MDNode *Temp = MDNode::getTemporary(VMContext, TElts);
884 Value *TVElts[] = { Temp };
885 MDNode *THolder = MDNode::get(VMContext, TVElts);
888 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
889 Constant::getNullValue(Type::getInt32Ty(VMContext)),
890 getNonCompileUnitScope(Context),
891 MDString::get(VMContext, Name),
892 MDString::get(VMContext, Name),
893 MDString::get(VMContext, LinkageName),
895 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
897 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
898 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
899 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
900 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
902 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
903 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
908 ConstantInt::get(Type::getInt32Ty(VMContext), ScopeLine)
910 MDNode *Node = MDNode::get(VMContext, Elts);
912 // Create a named metadata so that we do not lose this mdnode.
913 AllSubprograms.push_back(Node);
914 return DISubprogram(Node);
917 /// createMethod - Create a new descriptor for the specified C++ method.
918 DISubprogram DIBuilder::createMethod(DIDescriptor Context,
920 StringRef LinkageName,
922 unsigned LineNo, DIType Ty,
925 unsigned VK, unsigned VIndex,
926 MDNode *VTableHolder,
931 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
932 MDNode *Temp = MDNode::getTemporary(VMContext, TElts);
933 Value *TVElts[] = { Temp };
934 MDNode *THolder = MDNode::get(VMContext, TVElts);
937 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
938 Constant::getNullValue(Type::getInt32Ty(VMContext)),
939 getNonCompileUnitScope(Context),
940 MDString::get(VMContext, Name),
941 MDString::get(VMContext, Name),
942 MDString::get(VMContext, LinkageName),
944 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
946 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
947 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
948 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
949 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
951 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
952 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
955 Constant::getNullValue(Type::getInt32Ty(VMContext)),
957 // FIXME: Do we want to use different scope/lines?
958 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
960 MDNode *Node = MDNode::get(VMContext, Elts);
961 return DISubprogram(Node);
964 /// createNameSpace - This creates new descriptor for a namespace
965 /// with the specified parent scope.
966 DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
967 DIFile File, unsigned LineNo) {
969 GetTagConstant(VMContext, dwarf::DW_TAG_namespace),
970 getNonCompileUnitScope(Scope),
971 MDString::get(VMContext, Name),
973 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
975 return DINameSpace(MDNode::get(VMContext, Elts));
978 /// createLexicalBlockFile - This creates a new MDNode that encapsulates
979 /// an existing scope with a new filename.
980 DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
983 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
987 return DILexicalBlockFile(MDNode::get(VMContext, Elts));
990 DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
991 unsigned Line, unsigned Col) {
992 // Defeat MDNode uniqing for lexical blocks by using unique id.
993 static unsigned int unique_id = 0;
995 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
996 getNonCompileUnitScope(Scope),
997 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
998 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1000 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
1002 return DILexicalBlock(MDNode::get(VMContext, Elts));
1005 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1006 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1007 Instruction *InsertBefore) {
1008 assert(Storage && "no storage passed to dbg.declare");
1009 assert(VarInfo.Verify() && "empty DIVariable passed to dbg.declare");
1011 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1013 Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1014 return CallInst::Create(DeclareFn, Args, "", InsertBefore);
1017 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1018 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1019 BasicBlock *InsertAtEnd) {
1020 assert(Storage && "no storage passed to dbg.declare");
1021 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.declare");
1023 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1025 Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1027 // If this block already has a terminator then insert this intrinsic
1028 // before the terminator.
1029 if (TerminatorInst *T = InsertAtEnd->getTerminator())
1030 return CallInst::Create(DeclareFn, Args, "", T);
1032 return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
1035 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1036 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1038 Instruction *InsertBefore) {
1039 assert(V && "no value passed to dbg.value");
1040 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
1042 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1044 Value *Args[] = { MDNode::get(V->getContext(), V),
1045 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1047 return CallInst::Create(ValueFn, Args, "", InsertBefore);
1050 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1051 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1053 BasicBlock *InsertAtEnd) {
1054 assert(V && "no value passed to dbg.value");
1055 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
1057 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1059 Value *Args[] = { MDNode::get(V->getContext(), V),
1060 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1062 return CallInst::Create(ValueFn, Args, "", InsertAtEnd);