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/Constants.h"
16 #include "llvm/DebugInfo.h"
17 #include "llvm/IntrinsicInst.h"
18 #include "llvm/Module.h"
19 #include "llvm/ADT/STLExtras.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 /// createReferenceType - Create debugging information entry for a reference
234 DIType DIBuilder::createReferenceType(unsigned Tag, DIType RTy) {
235 assert(RTy.Verify() && "Unable to create reference type");
236 // References are encoded in DIDerivedType format.
238 GetTagConstant(VMContext, Tag),
242 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
243 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
244 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
245 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
246 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
249 return DIType(MDNode::get(VMContext, Elts));
252 /// createTypedef - Create debugging information entry for a typedef.
253 DIType DIBuilder::createTypedef(DIType Ty, StringRef Name, DIFile File,
254 unsigned LineNo, DIDescriptor Context) {
255 // typedefs are encoded in DIDerivedType format.
256 assert(Ty.Verify() && "Invalid typedef type!");
258 GetTagConstant(VMContext, dwarf::DW_TAG_typedef),
259 getNonCompileUnitScope(Context),
260 MDString::get(VMContext, Name),
262 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
263 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
264 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
265 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
266 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
269 return DIType(MDNode::get(VMContext, Elts));
272 /// createFriend - Create debugging information entry for a 'friend'.
273 DIType DIBuilder::createFriend(DIType Ty, DIType FriendTy) {
274 // typedefs are encoded in DIDerivedType format.
275 assert(Ty.Verify() && "Invalid type!");
276 assert(FriendTy.Verify() && "Invalid friend type!");
278 GetTagConstant(VMContext, dwarf::DW_TAG_friend),
282 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
283 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
284 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
285 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
286 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
289 return DIType(MDNode::get(VMContext, Elts));
292 /// createInheritance - Create debugging information entry to establish
293 /// inheritance relationship between two types.
294 DIType DIBuilder::createInheritance(DIType Ty, DIType BaseTy,
295 uint64_t BaseOffset, unsigned Flags) {
296 assert(Ty.Verify() && "Unable to create inheritance");
297 // TAG_inheritance is encoded in DIDerivedType format.
299 GetTagConstant(VMContext, dwarf::DW_TAG_inheritance),
303 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
304 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
305 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
306 ConstantInt::get(Type::getInt64Ty(VMContext), BaseOffset),
307 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
310 return DIType(MDNode::get(VMContext, Elts));
313 /// createMemberType - Create debugging information entry for a member.
314 DIType DIBuilder::createMemberType(DIDescriptor Scope, StringRef Name,
315 DIFile File, unsigned LineNumber,
316 uint64_t SizeInBits, uint64_t AlignInBits,
317 uint64_t OffsetInBits, unsigned Flags,
319 // TAG_member is encoded in DIDerivedType format.
321 GetTagConstant(VMContext, dwarf::DW_TAG_member),
322 getNonCompileUnitScope(Scope),
323 MDString::get(VMContext, Name),
325 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
326 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
327 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
328 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
329 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
332 return DIType(MDNode::get(VMContext, Elts));
335 /// createObjCIVar - Create debugging information entry for Objective-C
336 /// instance variable.
337 DIType DIBuilder::createObjCIVar(StringRef Name,
338 DIFile File, unsigned LineNumber,
339 uint64_t SizeInBits, uint64_t AlignInBits,
340 uint64_t OffsetInBits, unsigned Flags,
341 DIType Ty, StringRef PropertyName,
342 StringRef GetterName, StringRef SetterName,
343 unsigned PropertyAttributes) {
344 // TAG_member is encoded in DIDerivedType format.
346 GetTagConstant(VMContext, dwarf::DW_TAG_member),
347 getNonCompileUnitScope(File),
348 MDString::get(VMContext, Name),
350 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
351 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
352 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
353 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
354 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
356 MDString::get(VMContext, PropertyName),
357 MDString::get(VMContext, GetterName),
358 MDString::get(VMContext, SetterName),
359 ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes)
361 return DIType(MDNode::get(VMContext, Elts));
364 /// createObjCIVar - Create debugging information entry for Objective-C
365 /// instance variable.
366 DIType DIBuilder::createObjCIVar(StringRef Name,
367 DIFile File, unsigned LineNumber,
368 uint64_t SizeInBits, uint64_t AlignInBits,
369 uint64_t OffsetInBits, unsigned Flags,
370 DIType Ty, MDNode *PropertyNode) {
371 // TAG_member is encoded in DIDerivedType format.
373 GetTagConstant(VMContext, dwarf::DW_TAG_member),
374 getNonCompileUnitScope(File),
375 MDString::get(VMContext, Name),
377 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
378 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
379 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
380 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
381 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
385 return DIType(MDNode::get(VMContext, Elts));
388 /// createObjCProperty - Create debugging information entry for Objective-C
390 DIObjCProperty DIBuilder::createObjCProperty(StringRef Name,
391 DIFile File, unsigned LineNumber,
392 StringRef GetterName,
393 StringRef SetterName,
394 unsigned PropertyAttributes,
397 GetTagConstant(VMContext, dwarf::DW_TAG_APPLE_property),
398 MDString::get(VMContext, Name),
400 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
401 MDString::get(VMContext, GetterName),
402 MDString::get(VMContext, SetterName),
403 ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes),
406 return DIObjCProperty(MDNode::get(VMContext, Elts));
409 /// createTemplateTypeParameter - Create debugging information for template
411 DITemplateTypeParameter
412 DIBuilder::createTemplateTypeParameter(DIDescriptor Context, StringRef Name,
413 DIType Ty, MDNode *File, unsigned LineNo,
416 GetTagConstant(VMContext, dwarf::DW_TAG_template_type_parameter),
417 getNonCompileUnitScope(Context),
418 MDString::get(VMContext, Name),
421 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
422 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
424 return DITemplateTypeParameter(MDNode::get(VMContext, Elts));
427 /// createTemplateValueParameter - Create debugging information for template
429 DITemplateValueParameter
430 DIBuilder::createTemplateValueParameter(DIDescriptor Context, StringRef Name,
431 DIType Ty, uint64_t Val,
432 MDNode *File, unsigned LineNo,
435 GetTagConstant(VMContext, dwarf::DW_TAG_template_value_parameter),
436 getNonCompileUnitScope(Context),
437 MDString::get(VMContext, Name),
439 ConstantInt::get(Type::getInt64Ty(VMContext), Val),
441 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
442 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
444 return DITemplateValueParameter(MDNode::get(VMContext, Elts));
447 /// createClassType - Create debugging information entry for a class.
448 DIType DIBuilder::createClassType(DIDescriptor Context, StringRef Name,
449 DIFile File, unsigned LineNumber,
450 uint64_t SizeInBits, uint64_t AlignInBits,
451 uint64_t OffsetInBits, unsigned Flags,
452 DIType DerivedFrom, DIArray Elements,
453 MDNode *VTableHolder,
454 MDNode *TemplateParams) {
455 // TAG_class_type is encoded in DICompositeType format.
457 GetTagConstant(VMContext, dwarf::DW_TAG_class_type),
458 getNonCompileUnitScope(Context),
459 MDString::get(VMContext, Name),
461 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
462 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
463 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
464 ConstantInt::get(Type::getInt32Ty(VMContext), OffsetInBits),
465 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
468 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
472 return DIType(MDNode::get(VMContext, Elts));
475 /// createStructType - Create debugging information entry for a struct.
476 DIType DIBuilder::createStructType(DIDescriptor Context, StringRef Name,
477 DIFile File, unsigned LineNumber,
478 uint64_t SizeInBits, uint64_t AlignInBits,
479 unsigned Flags, DIArray Elements,
480 unsigned RunTimeLang) {
481 // TAG_structure_type is encoded in DICompositeType format.
483 GetTagConstant(VMContext, dwarf::DW_TAG_structure_type),
484 getNonCompileUnitScope(Context),
485 MDString::get(VMContext, Name),
487 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
488 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
489 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
490 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
491 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
494 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
495 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
496 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
498 return DIType(MDNode::get(VMContext, Elts));
501 /// createUnionType - Create debugging information entry for an union.
502 DIType DIBuilder::createUnionType(DIDescriptor Scope, StringRef Name,
504 unsigned LineNumber, uint64_t SizeInBits,
505 uint64_t AlignInBits, unsigned Flags,
506 DIArray Elements, unsigned RunTimeLang) {
507 // TAG_union_type is encoded in DICompositeType format.
509 GetTagConstant(VMContext, dwarf::DW_TAG_union_type),
510 getNonCompileUnitScope(Scope),
511 MDString::get(VMContext, Name),
513 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
514 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
515 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
516 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
517 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
520 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
521 Constant::getNullValue(Type::getInt32Ty(VMContext))
523 return DIType(MDNode::get(VMContext, Elts));
526 /// createSubroutineType - Create subroutine type.
527 DIType DIBuilder::createSubroutineType(DIFile File, DIArray ParameterTypes) {
528 // TAG_subroutine_type is encoded in DICompositeType format.
530 GetTagConstant(VMContext, dwarf::DW_TAG_subroutine_type),
531 Constant::getNullValue(Type::getInt32Ty(VMContext)),
532 MDString::get(VMContext, ""),
533 Constant::getNullValue(Type::getInt32Ty(VMContext)),
534 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
535 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
536 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
537 ConstantInt::get(Type::getInt64Ty(VMContext), 0),
538 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
541 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
542 Constant::getNullValue(Type::getInt32Ty(VMContext))
544 return DIType(MDNode::get(VMContext, Elts));
547 /// createEnumerationType - Create debugging information entry for an
549 DIType DIBuilder::createEnumerationType(DIDescriptor Scope, StringRef Name,
550 DIFile File, unsigned LineNumber,
552 uint64_t AlignInBits,
555 // TAG_enumeration_type is encoded in DICompositeType format.
557 GetTagConstant(VMContext, dwarf::DW_TAG_enumeration_type),
558 getNonCompileUnitScope(Scope),
559 MDString::get(VMContext, Name),
561 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
562 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
563 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
564 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
565 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
568 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
569 Constant::getNullValue(Type::getInt32Ty(VMContext))
571 MDNode *Node = MDNode::get(VMContext, Elts);
572 AllEnumTypes.push_back(Node);
576 /// createArrayType - Create debugging information entry for an array.
577 DIType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
578 DIType Ty, DIArray Subscripts) {
579 // TAG_array_type is encoded in DICompositeType format.
581 GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
583 MDString::get(VMContext, ""),
585 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
586 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
587 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
588 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
589 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
592 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
593 Constant::getNullValue(Type::getInt32Ty(VMContext))
595 return DIType(MDNode::get(VMContext, Elts));
598 /// createVectorType - Create debugging information entry for a vector.
599 DIType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits,
600 DIType Ty, DIArray Subscripts) {
601 // TAG_vector_type is encoded in DICompositeType format.
603 GetTagConstant(VMContext, dwarf::DW_TAG_vector_type),
605 MDString::get(VMContext, ""),
607 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
608 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
609 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
610 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
611 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
614 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
615 Constant::getNullValue(Type::getInt32Ty(VMContext))
617 return DIType(MDNode::get(VMContext, Elts));
620 /// createArtificialType - Create a new DIType with "artificial" flag set.
621 DIType DIBuilder::createArtificialType(DIType Ty) {
622 if (Ty.isArtificial())
625 SmallVector<Value *, 9> Elts;
627 assert (N && "Unexpected input DIType!");
628 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
629 if (Value *V = N->getOperand(i))
632 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
635 unsigned CurFlags = Ty.getFlags();
636 CurFlags = CurFlags | DIType::FlagArtificial;
638 // Flags are stored at this slot.
639 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
641 return DIType(MDNode::get(VMContext, Elts));
644 /// createArtificialType - Create a new DIType with "artificial" flag set.
645 DIType DIBuilder::createObjectPointerType(DIType Ty) {
646 if (Ty.isObjectPointer())
649 SmallVector<Value *, 9> Elts;
651 assert (N && "Unexpected input DIType!");
652 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
653 if (Value *V = N->getOperand(i))
656 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
659 unsigned CurFlags = Ty.getFlags();
660 CurFlags = CurFlags | (DIType::FlagObjectPointer | DIType::FlagArtificial);
662 // Flags are stored at this slot.
663 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
665 return DIType(MDNode::get(VMContext, Elts));
668 /// retainType - Retain DIType in a module even if it is not referenced
669 /// through debug info anchors.
670 void DIBuilder::retainType(DIType T) {
671 AllRetainTypes.push_back(T);
674 /// createUnspecifiedParameter - Create unspeicified type descriptor
675 /// for the subroutine type.
676 DIDescriptor DIBuilder::createUnspecifiedParameter() {
678 GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_parameters)
680 return DIDescriptor(MDNode::get(VMContext, Elts));
683 /// createTemporaryType - Create a temporary forward-declared type.
684 DIType DIBuilder::createTemporaryType() {
685 // Give the temporary MDNode a tag. It doesn't matter what tag we
686 // use here as long as DIType accepts it.
687 Value *Elts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
688 MDNode *Node = MDNode::getTemporary(VMContext, Elts);
692 /// createTemporaryType - Create a temporary forward-declared type.
693 DIType DIBuilder::createTemporaryType(DIFile F) {
694 // Give the temporary MDNode a tag. It doesn't matter what tag we
695 // use here as long as DIType accepts it.
697 GetTagConstant(VMContext, DW_TAG_base_type),
702 MDNode *Node = MDNode::getTemporary(VMContext, Elts);
706 /// createForwardDecl - Create a temporary forward-declared type that
707 /// can be RAUW'd if the full type is seen.
708 DIType DIBuilder::createForwardDecl(unsigned Tag, StringRef Name,
709 DIDescriptor Scope, DIFile F,
710 unsigned Line, unsigned RuntimeLang,
712 uint64_t AlignInBits) {
713 // Create a temporary MDNode.
715 GetTagConstant(VMContext, Tag),
716 getNonCompileUnitScope(Scope),
717 MDString::get(VMContext, Name),
719 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
720 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
721 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
722 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
723 ConstantInt::get(Type::getInt32Ty(VMContext),
724 DIDescriptor::FlagFwdDecl),
727 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang)
729 MDNode *Node = MDNode::getTemporary(VMContext, Elts);
733 /// getOrCreateArray - Get a DIArray, create one if required.
734 DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) {
735 if (Elements.empty()) {
736 Value *Null = Constant::getNullValue(Type::getInt32Ty(VMContext));
737 return DIArray(MDNode::get(VMContext, Null));
739 return DIArray(MDNode::get(VMContext, Elements));
742 /// getOrCreateSubrange - Create a descriptor for a value range. This
743 /// implicitly uniques the values returned.
744 DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Hi) {
746 GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type),
747 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
748 ConstantInt::get(Type::getInt64Ty(VMContext), Hi)
751 return DISubrange(MDNode::get(VMContext, Elts));
754 /// createGlobalVariable - Create a new descriptor for the specified global.
755 DIGlobalVariable DIBuilder::
756 createGlobalVariable(StringRef Name, DIFile F, unsigned LineNumber,
757 DIType Ty, bool isLocalToUnit, Value *Val) {
759 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
760 Constant::getNullValue(Type::getInt32Ty(VMContext)),
762 MDString::get(VMContext, Name),
763 MDString::get(VMContext, Name),
764 MDString::get(VMContext, Name),
766 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
768 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
769 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
772 MDNode *Node = MDNode::get(VMContext, Elts);
773 AllGVs.push_back(Node);
774 return DIGlobalVariable(Node);
777 /// createStaticVariable - Create a new descriptor for the specified static
779 DIGlobalVariable DIBuilder::
780 createStaticVariable(DIDescriptor Context, StringRef Name,
781 StringRef LinkageName, DIFile F, unsigned LineNumber,
782 DIType Ty, bool isLocalToUnit, Value *Val) {
784 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
785 Constant::getNullValue(Type::getInt32Ty(VMContext)),
786 getNonCompileUnitScope(Context),
787 MDString::get(VMContext, Name),
788 MDString::get(VMContext, Name),
789 MDString::get(VMContext, LinkageName),
791 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
793 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
794 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
797 MDNode *Node = MDNode::get(VMContext, Elts);
798 AllGVs.push_back(Node);
799 return DIGlobalVariable(Node);
802 /// createVariable - Create a new descriptor for the specified variable.
803 DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
804 StringRef Name, DIFile File,
805 unsigned LineNo, DIType Ty,
806 bool AlwaysPreserve, unsigned Flags,
809 GetTagConstant(VMContext, Tag),
810 getNonCompileUnitScope(Scope),
811 MDString::get(VMContext, Name),
813 ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))),
815 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
816 Constant::getNullValue(Type::getInt32Ty(VMContext))
818 MDNode *Node = MDNode::get(VMContext, Elts);
819 if (AlwaysPreserve) {
820 // The optimizer may remove local variable. If there is an interest
821 // to preserve variable info in such situation then stash it in a
823 DISubprogram Fn(getDISubprogram(Scope));
824 NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, Fn);
825 FnLocals->addOperand(Node);
827 return DIVariable(Node);
830 /// createComplexVariable - Create a new descriptor for the specified variable
831 /// which has a complex address expression for its address.
832 DIVariable DIBuilder::createComplexVariable(unsigned Tag, DIDescriptor Scope,
833 StringRef Name, DIFile F,
835 DIType Ty, ArrayRef<Value *> Addr,
837 SmallVector<Value *, 15> Elts;
838 Elts.push_back(GetTagConstant(VMContext, Tag));
839 Elts.push_back(getNonCompileUnitScope(Scope)),
840 Elts.push_back(MDString::get(VMContext, Name));
842 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext),
843 (LineNo | (ArgNo << 24))));
845 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
846 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
847 Elts.append(Addr.begin(), Addr.end());
849 return DIVariable(MDNode::get(VMContext, Elts));
852 /// createFunction - Create a new descriptor for the specified function.
853 DISubprogram DIBuilder::createFunction(DIDescriptor Context,
855 StringRef LinkageName,
856 DIFile File, unsigned LineNo,
858 bool isLocalToUnit, bool isDefinition,
860 unsigned Flags, bool isOptimized,
864 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
865 MDNode *Temp = MDNode::getTemporary(VMContext, TElts);
866 Value *TVElts[] = { Temp };
867 MDNode *THolder = MDNode::get(VMContext, TVElts);
870 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
871 Constant::getNullValue(Type::getInt32Ty(VMContext)),
872 getNonCompileUnitScope(Context),
873 MDString::get(VMContext, Name),
874 MDString::get(VMContext, Name),
875 MDString::get(VMContext, LinkageName),
877 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
879 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
880 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
881 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
882 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
884 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
885 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
890 ConstantInt::get(Type::getInt32Ty(VMContext), ScopeLine)
892 MDNode *Node = MDNode::get(VMContext, Elts);
894 // Create a named metadata so that we do not lose this mdnode.
895 AllSubprograms.push_back(Node);
896 return DISubprogram(Node);
899 /// createMethod - Create a new descriptor for the specified C++ method.
900 DISubprogram DIBuilder::createMethod(DIDescriptor Context,
902 StringRef LinkageName,
904 unsigned LineNo, DIType Ty,
907 unsigned VK, unsigned VIndex,
908 MDNode *VTableHolder,
913 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
914 MDNode *Temp = MDNode::getTemporary(VMContext, TElts);
915 Value *TVElts[] = { Temp };
916 MDNode *THolder = MDNode::get(VMContext, TVElts);
919 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
920 Constant::getNullValue(Type::getInt32Ty(VMContext)),
921 getNonCompileUnitScope(Context),
922 MDString::get(VMContext, Name),
923 MDString::get(VMContext, Name),
924 MDString::get(VMContext, LinkageName),
926 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
928 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
929 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
930 ConstantInt::get(Type::getInt32Ty(VMContext), (unsigned)VK),
931 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
933 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
934 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
937 Constant::getNullValue(Type::getInt32Ty(VMContext)),
939 // FIXME: Do we want to use different scope/lines?
940 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
942 MDNode *Node = MDNode::get(VMContext, Elts);
943 return DISubprogram(Node);
946 /// createNameSpace - This creates new descriptor for a namespace
947 /// with the specified parent scope.
948 DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
949 DIFile File, unsigned LineNo) {
951 GetTagConstant(VMContext, dwarf::DW_TAG_namespace),
952 getNonCompileUnitScope(Scope),
953 MDString::get(VMContext, Name),
955 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
957 return DINameSpace(MDNode::get(VMContext, Elts));
960 /// createLexicalBlockFile - This creates a new MDNode that encapsulates
961 /// an existing scope with a new filename.
962 DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
965 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
969 return DILexicalBlockFile(MDNode::get(VMContext, Elts));
972 DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
973 unsigned Line, unsigned Col) {
974 // Defeat MDNode uniqing for lexical blocks by using unique id.
975 static unsigned int unique_id = 0;
977 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
978 getNonCompileUnitScope(Scope),
979 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
980 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
982 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
984 return DILexicalBlock(MDNode::get(VMContext, Elts));
987 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
988 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
989 Instruction *InsertBefore) {
990 assert(Storage && "no storage passed to dbg.declare");
991 assert(VarInfo.Verify() && "empty DIVariable passed to dbg.declare");
993 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
995 Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
996 return CallInst::Create(DeclareFn, Args, "", InsertBefore);
999 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1000 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1001 BasicBlock *InsertAtEnd) {
1002 assert(Storage && "no storage passed to dbg.declare");
1003 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.declare");
1005 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1007 Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1009 // If this block already has a terminator then insert this intrinsic
1010 // before the terminator.
1011 if (TerminatorInst *T = InsertAtEnd->getTerminator())
1012 return CallInst::Create(DeclareFn, Args, "", T);
1014 return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
1017 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1018 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1020 Instruction *InsertBefore) {
1021 assert(V && "no value passed to dbg.value");
1022 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
1024 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1026 Value *Args[] = { MDNode::get(V->getContext(), V),
1027 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1029 return CallInst::Create(ValueFn, Args, "", InsertBefore);
1032 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1033 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1035 BasicBlock *InsertAtEnd) {
1036 assert(V && "no value passed to dbg.value");
1037 assert(VarInfo.Verify() && "invalid DIVariable passed to dbg.value");
1039 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1041 Value *Args[] = { MDNode::get(V->getContext(), V),
1042 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1044 return CallInst::Create(ValueFn, Args, "", InsertAtEnd);