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/IR/DIBuilder.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/IR/Constants.h"
17 #include "llvm/IR/DebugInfo.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()), TempEnumTypes(nullptr),
34 TempRetainTypes(nullptr), TempSubprograms(nullptr), TempGVs(nullptr),
35 DeclareFn(nullptr), ValueFn(nullptr) {}
37 /// finalize - Construct any deferred debug info descriptors.
38 void DIBuilder::finalize() {
39 DIArray Enums = getOrCreateArray(AllEnumTypes);
40 DIType(TempEnumTypes).replaceAllUsesWith(Enums);
42 SmallVector<Value *, 16> RetainValues;
43 // Declarations and definitions of the same type may be retained. Some
44 // clients RAUW these pairs, leaving duplicates in the retained types
45 // list. Use a set to remove the duplicates while we transform the
46 // TrackingVHs back into Values.
47 SmallPtrSet<Value *, 16> RetainSet;
48 for (unsigned I = 0, E = AllRetainTypes.size(); I < E; I++)
49 if (RetainSet.insert(AllRetainTypes[I]))
50 RetainValues.push_back(AllRetainTypes[I]);
51 DIArray RetainTypes = getOrCreateArray(RetainValues);
52 DIType(TempRetainTypes).replaceAllUsesWith(RetainTypes);
54 DIArray SPs = getOrCreateArray(AllSubprograms);
55 DIType(TempSubprograms).replaceAllUsesWith(SPs);
56 for (unsigned i = 0, e = SPs.getNumElements(); i != e; ++i) {
57 DISubprogram SP(SPs.getElement(i));
58 SmallVector<Value *, 4> Variables;
59 if (NamedMDNode *NMD = getFnSpecificMDNode(M, SP)) {
60 for (unsigned ii = 0, ee = NMD->getNumOperands(); ii != ee; ++ii)
61 Variables.push_back(NMD->getOperand(ii));
62 NMD->eraseFromParent();
64 if (MDNode *Temp = SP.getVariablesNodes()) {
65 DIArray AV = getOrCreateArray(Variables);
66 DIType(Temp).replaceAllUsesWith(AV);
70 DIArray GVs = getOrCreateArray(AllGVs);
71 DIType(TempGVs).replaceAllUsesWith(GVs);
73 SmallVector<Value *, 16> RetainValuesI;
74 for (unsigned I = 0, E = AllImportedModules.size(); I < E; I++)
75 RetainValuesI.push_back(AllImportedModules[I]);
76 DIArray IMs = getOrCreateArray(RetainValuesI);
77 DIType(TempImportedModules).replaceAllUsesWith(IMs);
80 /// getNonCompileUnitScope - If N is compile unit return NULL otherwise return
82 static MDNode *getNonCompileUnitScope(MDNode *N) {
83 if (DIDescriptor(N).isCompileUnit())
88 static MDNode *createFilePathPair(LLVMContext &VMContext, StringRef Filename,
89 StringRef Directory) {
90 assert(!Filename.empty() && "Unable to create file without name");
92 MDString::get(VMContext, Filename),
93 MDString::get(VMContext, Directory)
95 return MDNode::get(VMContext, Pair);
98 /// createCompileUnit - A CompileUnit provides an anchor for all debugging
99 /// information generated during this instance of compilation.
100 DICompileUnit DIBuilder::createCompileUnit(unsigned Lang, StringRef Filename,
102 StringRef Producer, bool isOptimized,
103 StringRef Flags, unsigned RunTimeVer,
105 DebugEmissionKind Kind,
106 bool EmitDebugInfo) {
108 assert(((Lang <= dwarf::DW_LANG_OCaml && Lang >= dwarf::DW_LANG_C89) ||
109 (Lang <= dwarf::DW_LANG_hi_user && Lang >= dwarf::DW_LANG_lo_user)) &&
110 "Invalid Language tag");
111 assert(!Filename.empty() &&
112 "Unable to create compile unit without filename");
113 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
114 TempEnumTypes = MDNode::getTemporary(VMContext, TElts);
116 TempRetainTypes = MDNode::getTemporary(VMContext, TElts);
118 TempSubprograms = MDNode::getTemporary(VMContext, TElts);
120 TempGVs = MDNode::getTemporary(VMContext, TElts);
122 TempImportedModules = MDNode::getTemporary(VMContext, TElts);
125 GetTagConstant(VMContext, dwarf::DW_TAG_compile_unit),
126 createFilePathPair(VMContext, Filename, Directory),
127 ConstantInt::get(Type::getInt32Ty(VMContext), Lang),
128 MDString::get(VMContext, Producer),
129 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
130 MDString::get(VMContext, Flags),
131 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer),
137 MDString::get(VMContext, SplitName),
138 ConstantInt::get(Type::getInt32Ty(VMContext), Kind)
141 MDNode *CUNode = MDNode::get(VMContext, Elts);
143 // Create a named metadata so that it is easier to find cu in a module.
144 // Note that we only generate this when the caller wants to actually
145 // emit debug information. When we are only interested in tracking
146 // source line locations throughout the backend, we prevent codegen from
147 // emitting debug info in the final output by not generating llvm.dbg.cu.
149 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
150 NMD->addOperand(CUNode);
153 return DICompileUnit(CUNode);
156 static DIImportedEntity
157 createImportedModule(LLVMContext &C, dwarf::Tag Tag, DIScope Context,
158 Value *NS, unsigned Line, StringRef Name,
159 SmallVectorImpl<TrackingVH<MDNode>> &AllImportedModules) {
163 GetTagConstant(C, Tag),
166 ConstantInt::get(Type::getInt32Ty(C), Line),
168 R = MDNode::get(C, Elts);
171 GetTagConstant(C, Tag),
174 ConstantInt::get(Type::getInt32Ty(C), Line),
175 MDString::get(C, Name)
177 R = MDNode::get(C, Elts);
179 DIImportedEntity M(R);
180 assert(M.Verify() && "Imported module should be valid");
181 AllImportedModules.push_back(TrackingVH<MDNode>(M));
185 DIImportedEntity DIBuilder::createImportedModule(DIScope Context,
188 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
189 Context, NS, Line, StringRef(), AllImportedModules);
192 DIImportedEntity DIBuilder::createImportedModule(DIScope Context,
195 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
196 Context, NS, Line, StringRef(), AllImportedModules);
199 DIImportedEntity DIBuilder::createImportedDeclaration(DIScope Context,
201 unsigned Line, StringRef Name) {
202 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_declaration,
203 Context, Decl.getRef(), Line, Name,
207 DIImportedEntity DIBuilder::createImportedDeclaration(DIScope Context,
208 DIImportedEntity Imp,
209 unsigned Line, StringRef Name) {
210 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_declaration,
211 Context, Imp, Line, Name, AllImportedModules);
214 /// createFile - Create a file descriptor to hold debugging information
216 DIFile DIBuilder::createFile(StringRef Filename, StringRef Directory) {
218 GetTagConstant(VMContext, dwarf::DW_TAG_file_type),
219 createFilePathPair(VMContext, Filename, Directory)
221 return DIFile(MDNode::get(VMContext, Elts));
224 /// createEnumerator - Create a single enumerator value.
225 DIEnumerator DIBuilder::createEnumerator(StringRef Name, int64_t Val) {
226 assert(!Name.empty() && "Unable to create enumerator without name");
228 GetTagConstant(VMContext, dwarf::DW_TAG_enumerator),
229 MDString::get(VMContext, Name),
230 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
232 return DIEnumerator(MDNode::get(VMContext, Elts));
235 /// \brief Create a DWARF unspecified type.
236 DIBasicType DIBuilder::createUnspecifiedType(StringRef Name) {
237 assert(!Name.empty() && "Unable to create type without name");
238 // Unspecified types are encoded in DIBasicType format. Line number, filename,
239 // size, alignment, offset and flags are always empty here.
241 GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_type),
244 MDString::get(VMContext, Name),
245 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
246 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
247 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
248 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
249 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
250 ConstantInt::get(Type::getInt32Ty(VMContext), 0) // Encoding
252 return DIBasicType(MDNode::get(VMContext, Elts));
255 /// \brief Create C++11 nullptr type.
256 DIBasicType DIBuilder::createNullPtrType() {
257 return createUnspecifiedType("decltype(nullptr)");
260 /// createBasicType - Create debugging information entry for a basic
261 /// type, e.g 'char'.
263 DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
264 uint64_t AlignInBits, unsigned Encoding) {
265 assert(!Name.empty() && "Unable to create type without name");
266 // Basic types are encoded in DIBasicType format. Line number, filename,
267 // offset and flags are always empty here.
269 GetTagConstant(VMContext, dwarf::DW_TAG_base_type),
270 nullptr, // File/directory name
272 MDString::get(VMContext, Name),
273 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
274 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
275 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
276 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
277 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
278 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
280 return DIBasicType(MDNode::get(VMContext, Elts));
283 /// createQualifiedType - Create debugging information entry for a qualified
284 /// type, e.g. 'const int'.
285 DIDerivedType DIBuilder::createQualifiedType(unsigned Tag, DIType FromTy) {
286 // Qualified types are encoded in DIDerivedType format.
288 GetTagConstant(VMContext, Tag),
291 MDString::get(VMContext, StringRef()), // Empty name.
292 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
293 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
294 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
295 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
296 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
299 return DIDerivedType(MDNode::get(VMContext, Elts));
302 /// createPointerType - Create debugging information entry for a pointer.
304 DIBuilder::createPointerType(DIType PointeeTy, uint64_t SizeInBits,
305 uint64_t AlignInBits, StringRef Name) {
306 // Pointer types are encoded in DIDerivedType format.
308 GetTagConstant(VMContext, dwarf::DW_TAG_pointer_type),
311 MDString::get(VMContext, Name),
312 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
313 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
314 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
315 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
316 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
319 return DIDerivedType(MDNode::get(VMContext, Elts));
322 DIDerivedType DIBuilder::createMemberPointerType(DIType PointeeTy,
324 // Pointer types are encoded in DIDerivedType format.
326 GetTagConstant(VMContext, dwarf::DW_TAG_ptr_to_member_type),
330 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
331 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
332 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
333 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
334 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
338 return DIDerivedType(MDNode::get(VMContext, Elts));
341 /// createReferenceType - Create debugging information entry for a reference
343 DIDerivedType DIBuilder::createReferenceType(unsigned Tag, DIType RTy) {
344 assert(RTy.isType() && "Unable to create reference type");
345 // References are encoded in DIDerivedType format.
347 GetTagConstant(VMContext, Tag),
351 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
352 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
353 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
354 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
355 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
358 return DIDerivedType(MDNode::get(VMContext, Elts));
361 /// createTypedef - Create debugging information entry for a typedef.
362 DIDerivedType DIBuilder::createTypedef(DIType Ty, StringRef Name, DIFile File,
363 unsigned LineNo, DIDescriptor Context) {
364 // typedefs are encoded in DIDerivedType format.
366 GetTagConstant(VMContext, dwarf::DW_TAG_typedef),
368 DIScope(getNonCompileUnitScope(Context)).getRef(),
369 MDString::get(VMContext, Name),
370 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
371 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
372 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
373 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
374 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
377 return DIDerivedType(MDNode::get(VMContext, Elts));
380 /// createFriend - Create debugging information entry for a 'friend'.
381 DIDerivedType DIBuilder::createFriend(DIType Ty, DIType FriendTy) {
382 // typedefs are encoded in DIDerivedType format.
383 assert(Ty.isType() && "Invalid type!");
384 assert(FriendTy.isType() && "Invalid friend type!");
386 GetTagConstant(VMContext, dwarf::DW_TAG_friend),
390 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
391 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
392 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
393 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
394 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
397 return DIDerivedType(MDNode::get(VMContext, Elts));
400 /// createInheritance - Create debugging information entry to establish
401 /// inheritance relationship between two types.
402 DIDerivedType DIBuilder::createInheritance(DIType Ty, DIType BaseTy,
405 assert(Ty.isType() && "Unable to create inheritance");
406 // TAG_inheritance is encoded in DIDerivedType format.
408 GetTagConstant(VMContext, dwarf::DW_TAG_inheritance),
412 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
413 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
414 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
415 ConstantInt::get(Type::getInt64Ty(VMContext), BaseOffset),
416 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
419 return DIDerivedType(MDNode::get(VMContext, Elts));
422 /// createMemberType - Create debugging information entry for a member.
423 DIDerivedType DIBuilder::createMemberType(DIDescriptor Scope, StringRef Name,
424 DIFile File, unsigned LineNumber,
426 uint64_t AlignInBits,
427 uint64_t OffsetInBits, unsigned Flags,
429 // TAG_member is encoded in DIDerivedType format.
431 GetTagConstant(VMContext, dwarf::DW_TAG_member),
433 DIScope(getNonCompileUnitScope(Scope)).getRef(),
434 MDString::get(VMContext, Name),
435 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
436 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
437 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
438 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
439 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
442 return DIDerivedType(MDNode::get(VMContext, Elts));
445 /// createStaticMemberType - Create debugging information entry for a
446 /// C++ static data member.
448 DIBuilder::createStaticMemberType(DIDescriptor Scope, StringRef Name,
449 DIFile File, unsigned LineNumber,
450 DIType Ty, unsigned Flags,
452 // TAG_member is encoded in DIDerivedType format.
453 Flags |= DIDescriptor::FlagStaticMember;
455 GetTagConstant(VMContext, dwarf::DW_TAG_member),
457 DIScope(getNonCompileUnitScope(Scope)).getRef(),
458 MDString::get(VMContext, Name),
459 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
460 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
461 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
462 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
463 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
467 return DIDerivedType(MDNode::get(VMContext, Elts));
470 /// createObjCIVar - Create debugging information entry for Objective-C
471 /// instance variable.
472 DIDerivedType DIBuilder::createObjCIVar(StringRef Name, DIFile File,
475 uint64_t AlignInBits,
476 uint64_t OffsetInBits, unsigned Flags,
477 DIType Ty, MDNode *PropertyNode) {
478 // TAG_member is encoded in DIDerivedType format.
480 GetTagConstant(VMContext, dwarf::DW_TAG_member),
482 getNonCompileUnitScope(File),
483 MDString::get(VMContext, Name),
484 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
485 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
486 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
487 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
488 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
492 return DIDerivedType(MDNode::get(VMContext, Elts));
495 /// createObjCProperty - Create debugging information entry for Objective-C
498 DIBuilder::createObjCProperty(StringRef Name, DIFile File, unsigned LineNumber,
499 StringRef GetterName, StringRef SetterName,
500 unsigned PropertyAttributes, DIType Ty) {
502 GetTagConstant(VMContext, dwarf::DW_TAG_APPLE_property),
503 MDString::get(VMContext, Name),
505 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
506 MDString::get(VMContext, GetterName),
507 MDString::get(VMContext, SetterName),
508 ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes),
511 return DIObjCProperty(MDNode::get(VMContext, Elts));
514 /// createTemplateTypeParameter - Create debugging information for template
516 DITemplateTypeParameter
517 DIBuilder::createTemplateTypeParameter(DIDescriptor Context, StringRef Name,
518 DIType Ty, MDNode *File, unsigned LineNo,
521 GetTagConstant(VMContext, dwarf::DW_TAG_template_type_parameter),
522 DIScope(getNonCompileUnitScope(Context)).getRef(),
523 MDString::get(VMContext, Name),
526 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
527 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
529 return DITemplateTypeParameter(MDNode::get(VMContext, Elts));
532 DITemplateValueParameter
533 DIBuilder::createTemplateValueParameter(unsigned Tag, DIDescriptor Context,
534 StringRef Name, DIType Ty,
535 Value *Val, MDNode *File,
539 GetTagConstant(VMContext, Tag),
540 DIScope(getNonCompileUnitScope(Context)).getRef(),
541 MDString::get(VMContext, Name),
545 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
546 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
548 return DITemplateValueParameter(MDNode::get(VMContext, Elts));
551 /// createTemplateValueParameter - Create debugging information for template
553 DITemplateValueParameter
554 DIBuilder::createTemplateValueParameter(DIDescriptor Context, StringRef Name,
555 DIType Ty, Value *Val,
556 MDNode *File, unsigned LineNo,
558 return createTemplateValueParameter(dwarf::DW_TAG_template_value_parameter,
559 Context, Name, Ty, Val, File, LineNo,
563 DITemplateValueParameter
564 DIBuilder::createTemplateTemplateParameter(DIDescriptor Context, StringRef Name,
565 DIType Ty, StringRef Val,
566 MDNode *File, unsigned LineNo,
568 return createTemplateValueParameter(
569 dwarf::DW_TAG_GNU_template_template_param, Context, Name, Ty,
570 MDString::get(VMContext, Val), File, LineNo, ColumnNo);
573 DITemplateValueParameter
574 DIBuilder::createTemplateParameterPack(DIDescriptor Context, StringRef Name,
575 DIType Ty, DIArray Val,
576 MDNode *File, unsigned LineNo,
578 return createTemplateValueParameter(dwarf::DW_TAG_GNU_template_parameter_pack,
579 Context, Name, Ty, Val, File, LineNo,
583 /// createClassType - Create debugging information entry for a class.
584 DICompositeType DIBuilder::createClassType(DIDescriptor Context, StringRef Name,
585 DIFile File, unsigned LineNumber,
587 uint64_t AlignInBits,
588 uint64_t OffsetInBits,
589 unsigned Flags, DIType DerivedFrom,
592 MDNode *TemplateParams,
593 StringRef UniqueIdentifier) {
594 assert((!Context || Context.isScope() || Context.isType()) &&
595 "createClassType should be called with a valid Context");
596 // TAG_class_type is encoded in DICompositeType format.
598 GetTagConstant(VMContext, dwarf::DW_TAG_class_type),
600 DIScope(getNonCompileUnitScope(Context)).getRef(),
601 MDString::get(VMContext, Name),
602 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
603 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
604 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
605 ConstantInt::get(Type::getInt32Ty(VMContext), OffsetInBits),
606 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
607 DerivedFrom.getRef(),
609 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
610 VTableHolder.getRef(),
612 UniqueIdentifier.empty() ? nullptr
613 : MDString::get(VMContext, UniqueIdentifier)
615 DICompositeType R(MDNode::get(VMContext, Elts));
616 assert(R.isCompositeType() &&
617 "createClassType should return a DICompositeType");
618 if (!UniqueIdentifier.empty())
623 /// createStructType - Create debugging information entry for a struct.
624 DICompositeType DIBuilder::createStructType(DIDescriptor Context,
625 StringRef Name, DIFile File,
628 uint64_t AlignInBits,
629 unsigned Flags, DIType DerivedFrom,
631 unsigned RunTimeLang,
633 StringRef UniqueIdentifier) {
634 // TAG_structure_type is encoded in DICompositeType format.
636 GetTagConstant(VMContext, dwarf::DW_TAG_structure_type),
638 DIScope(getNonCompileUnitScope(Context)).getRef(),
639 MDString::get(VMContext, Name),
640 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
641 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
642 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
643 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
644 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
645 DerivedFrom.getRef(),
647 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
648 VTableHolder.getRef(),
650 UniqueIdentifier.empty() ? nullptr
651 : MDString::get(VMContext, UniqueIdentifier)
653 DICompositeType R(MDNode::get(VMContext, Elts));
654 assert(R.isCompositeType() &&
655 "createStructType should return a DICompositeType");
656 if (!UniqueIdentifier.empty())
661 /// createUnionType - Create debugging information entry for an union.
662 DICompositeType DIBuilder::createUnionType(DIDescriptor Scope, StringRef Name,
663 DIFile File, unsigned LineNumber,
665 uint64_t AlignInBits, unsigned Flags,
667 unsigned RunTimeLang,
668 StringRef UniqueIdentifier) {
669 // TAG_union_type is encoded in DICompositeType format.
671 GetTagConstant(VMContext, dwarf::DW_TAG_union_type),
673 DIScope(getNonCompileUnitScope(Scope)).getRef(),
674 MDString::get(VMContext, Name),
675 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
676 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
677 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
678 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
679 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
682 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
685 UniqueIdentifier.empty() ? nullptr
686 : MDString::get(VMContext, UniqueIdentifier)
688 DICompositeType R(MDNode::get(VMContext, Elts));
689 if (!UniqueIdentifier.empty())
694 /// createSubroutineType - Create subroutine type.
695 DISubroutineType DIBuilder::createSubroutineType(DIFile File,
696 DITypeArray ParameterTypes,
698 // TAG_subroutine_type is encoded in DICompositeType format.
700 GetTagConstant(VMContext, dwarf::DW_TAG_subroutine_type),
701 Constant::getNullValue(Type::getInt32Ty(VMContext)),
703 MDString::get(VMContext, ""),
704 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
705 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
706 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
707 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
708 ConstantInt::get(Type::getInt32Ty(VMContext), Flags), // Flags
711 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
714 nullptr // Type Identifer
716 return DISubroutineType(MDNode::get(VMContext, Elts));
719 /// createEnumerationType - Create debugging information entry for an
721 DICompositeType DIBuilder::createEnumerationType(
722 DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
723 uint64_t SizeInBits, uint64_t AlignInBits, DIArray Elements,
724 DIType UnderlyingType, StringRef UniqueIdentifier) {
725 // TAG_enumeration_type is encoded in DICompositeType format.
727 GetTagConstant(VMContext, dwarf::DW_TAG_enumeration_type),
729 DIScope(getNonCompileUnitScope(Scope)).getRef(),
730 MDString::get(VMContext, Name),
731 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
732 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
733 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
734 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset
735 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
736 UnderlyingType.getRef(),
738 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
741 UniqueIdentifier.empty() ? nullptr
742 : MDString::get(VMContext, UniqueIdentifier)
744 DICompositeType CTy(MDNode::get(VMContext, Elts));
745 AllEnumTypes.push_back(CTy);
746 if (!UniqueIdentifier.empty())
751 /// createArrayType - Create debugging information entry for an array.
752 DICompositeType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
753 DIType Ty, DIArray Subscripts) {
754 // TAG_array_type is encoded in DICompositeType format.
756 GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
757 nullptr, // Filename/Directory,
759 MDString::get(VMContext, ""),
760 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
761 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
762 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
763 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset
764 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
767 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
770 nullptr // Type Identifer
772 return DICompositeType(MDNode::get(VMContext, Elts));
775 /// createVectorType - Create debugging information entry for a vector.
776 DICompositeType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits,
777 DIType Ty, DIArray Subscripts) {
778 // A vector is an array type with the FlagVector flag applied.
780 GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
781 nullptr, // Filename/Directory,
783 MDString::get(VMContext, ""),
784 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
785 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
786 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
787 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset
788 ConstantInt::get(Type::getInt32Ty(VMContext), DIType::FlagVector),
791 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
794 nullptr // Type Identifer
796 return DICompositeType(MDNode::get(VMContext, Elts));
799 /// createArtificialType - Create a new DIType with "artificial" flag set.
800 DIType DIBuilder::createArtificialType(DIType Ty) {
801 if (Ty.isArtificial())
804 SmallVector<Value *, 9> Elts;
806 assert (N && "Unexpected input DIType!");
807 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
808 Elts.push_back(N->getOperand(i));
810 unsigned CurFlags = Ty.getFlags();
811 CurFlags = CurFlags | DIType::FlagArtificial;
813 // Flags are stored at this slot.
814 // FIXME: Add an enum for this magic value.
815 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
817 return DIType(MDNode::get(VMContext, Elts));
820 /// createObjectPointerType - Create a new type with both the object pointer
821 /// and artificial flags set.
822 DIType DIBuilder::createObjectPointerType(DIType Ty) {
823 if (Ty.isObjectPointer())
826 SmallVector<Value *, 9> Elts;
828 assert (N && "Unexpected input DIType!");
829 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
830 Elts.push_back(N->getOperand(i));
832 unsigned CurFlags = Ty.getFlags();
833 CurFlags = CurFlags | (DIType::FlagObjectPointer | DIType::FlagArtificial);
835 // Flags are stored at this slot.
836 // FIXME: Add an enum for this magic value.
837 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
839 return DIType(MDNode::get(VMContext, Elts));
842 /// retainType - Retain DIType in a module even if it is not referenced
843 /// through debug info anchors.
844 void DIBuilder::retainType(DIType T) {
845 AllRetainTypes.push_back(TrackingVH<MDNode>(T));
848 /// createUnspecifiedParameter - Create unspeicified type descriptor
849 /// for the subroutine type.
850 DIBasicType DIBuilder::createUnspecifiedParameter() {
851 return DIBasicType();
854 /// createForwardDecl - Create a permanent forward-declared type.
856 DIBuilder::createForwardDecl(unsigned Tag, StringRef Name, DIDescriptor Scope,
857 DIFile F, unsigned Line, unsigned RuntimeLang,
858 uint64_t SizeInBits, uint64_t AlignInBits,
859 StringRef UniqueIdentifier) {
860 // Create a temporary MDNode.
862 GetTagConstant(VMContext, Tag),
864 DIScope(getNonCompileUnitScope(Scope)).getRef(),
865 MDString::get(VMContext, Name),
866 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
867 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
868 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
869 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset
870 ConstantInt::get(Type::getInt32Ty(VMContext), DIDescriptor::FlagFwdDecl),
873 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
875 nullptr, //TemplateParams
876 UniqueIdentifier.empty() ? nullptr
877 : MDString::get(VMContext, UniqueIdentifier)
879 MDNode *Node = MDNode::get(VMContext, Elts);
880 DICompositeType RetTy(Node);
881 assert(RetTy.isCompositeType() &&
882 "createForwardDecl result should be a DIType");
883 if (!UniqueIdentifier.empty())
888 /// createReplaceableForwardDecl - Create a temporary forward-declared type that
889 /// can be RAUW'd if the full type is seen.
890 DICompositeType DIBuilder::createReplaceableForwardDecl(
891 unsigned Tag, StringRef Name, DIDescriptor Scope, DIFile F, unsigned Line,
892 unsigned RuntimeLang, uint64_t SizeInBits, uint64_t AlignInBits,
893 StringRef UniqueIdentifier) {
894 // Create a temporary MDNode.
896 GetTagConstant(VMContext, Tag),
898 DIScope(getNonCompileUnitScope(Scope)).getRef(),
899 MDString::get(VMContext, Name),
900 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
901 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
902 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
903 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset
904 ConstantInt::get(Type::getInt32Ty(VMContext), DIDescriptor::FlagFwdDecl),
907 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
909 nullptr, //TemplateParams
910 UniqueIdentifier.empty() ? nullptr
911 : MDString::get(VMContext, UniqueIdentifier)
913 MDNode *Node = MDNode::getTemporary(VMContext, Elts);
914 DICompositeType RetTy(Node);
915 assert(RetTy.isCompositeType() &&
916 "createReplaceableForwardDecl result should be a DIType");
917 if (!UniqueIdentifier.empty())
922 /// getOrCreateArray - Get a DIArray, create one if required.
923 DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) {
924 return DIArray(MDNode::get(VMContext, Elements));
927 /// getOrCreateTypeArray - Get a DITypeArray, create one if required.
928 DITypeArray DIBuilder::getOrCreateTypeArray(ArrayRef<Value *> Elements) {
929 SmallVector<llvm::Value *, 16> Elts;
930 for (unsigned i = 0, e = Elements.size(); i != e; ++i) {
931 if (Elements[i] && isa<MDNode>(Elements[i]))
932 Elts.push_back(DIType(cast<MDNode>(Elements[i])).getRef());
934 Elts.push_back(Elements[i]);
936 return DITypeArray(MDNode::get(VMContext, Elts));
939 /// getOrCreateSubrange - Create a descriptor for a value range. This
940 /// implicitly uniques the values returned.
941 DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
943 GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type),
944 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
945 ConstantInt::get(Type::getInt64Ty(VMContext), Count)
948 return DISubrange(MDNode::get(VMContext, Elts));
951 static DIGlobalVariable
952 createGlobalVariableHelper(LLVMContext &VMContext, DIDescriptor Context,
953 StringRef Name, StringRef LinkageName, DIFile F,
954 unsigned LineNumber, DITypeRef Ty, bool isLocalToUnit,
955 Value *Val, MDNode *Decl, bool isDefinition,
956 std::function<MDNode *(ArrayRef<Value *>)> CreateFunc) {
958 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
959 Constant::getNullValue(Type::getInt32Ty(VMContext)),
960 getNonCompileUnitScope(Context),
961 MDString::get(VMContext, Name),
962 MDString::get(VMContext, Name),
963 MDString::get(VMContext, LinkageName),
965 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
967 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
968 ConstantInt::get(Type::getInt32Ty(VMContext), isDefinition),
973 return DIGlobalVariable(CreateFunc(Elts));
976 /// createGlobalVariable - Create a new descriptor for the specified
978 DIGlobalVariable DIBuilder::createGlobalVariable(DIDescriptor Context,
980 StringRef LinkageName,
981 DIFile F, unsigned LineNumber,
984 Value *Val, MDNode *Decl) {
985 return createGlobalVariableHelper(VMContext, Context, Name, LinkageName, F,
986 LineNumber, Ty, isLocalToUnit, Val, Decl, true,
987 [&] (ArrayRef<Value *> Elts) -> MDNode * {
988 MDNode *Node = MDNode::get(VMContext, Elts);
989 AllGVs.push_back(Node);
994 /// createTempGlobalVariableFwdDecl - Create a new temporary descriptor for the
995 /// specified variable declarartion.
997 DIBuilder::createTempGlobalVariableFwdDecl(DIDescriptor Context,
999 StringRef LinkageName,
1000 DIFile F, unsigned LineNumber,
1003 Value *Val, MDNode *Decl) {
1004 return createGlobalVariableHelper(VMContext, Context, Name, LinkageName, F,
1005 LineNumber, Ty, isLocalToUnit, Val, Decl, false,
1006 [&] (ArrayRef<Value *> Elts) {
1007 return MDNode::getTemporary(VMContext, Elts);
1011 /// createVariable - Create a new descriptor for the specified variable.
1012 DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
1013 StringRef Name, DIFile File,
1014 unsigned LineNo, DITypeRef Ty,
1015 bool AlwaysPreserve, unsigned Flags,
1017 DIDescriptor Context(getNonCompileUnitScope(Scope));
1018 assert((!Context || Context.isScope()) &&
1019 "createLocalVariable should be called with a valid Context");
1021 GetTagConstant(VMContext, Tag),
1022 getNonCompileUnitScope(Scope),
1023 MDString::get(VMContext, Name),
1025 ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))),
1027 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1028 Constant::getNullValue(Type::getInt32Ty(VMContext))
1030 MDNode *Node = MDNode::get(VMContext, Elts);
1031 if (AlwaysPreserve) {
1032 // The optimizer may remove local variable. If there is an interest
1033 // to preserve variable info in such situation then stash it in a
1035 DISubprogram Fn(getDISubprogram(Scope));
1036 NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, Fn);
1037 FnLocals->addOperand(Node);
1039 DIVariable RetVar(Node);
1040 assert(RetVar.isVariable() &&
1041 "createLocalVariable should return a valid DIVariable");
1045 /// createExpression - Create a new descriptor for the specified
1046 /// variable which has a complex address expression for its address.
1047 /// @param Addr An array of complex address operations.
1048 DIExpression DIBuilder::createExpression(ArrayRef<int64_t> Addr) {
1049 SmallVector<llvm::Value *, 16> Elts;
1050 Elts.push_back(GetTagConstant(VMContext, DW_TAG_expression));
1052 llvm::Type *Int64Ty = Type::getInt64Ty(VMContext);
1053 for (int64_t I : Addr)
1054 Elts.push_back(ConstantInt::get(Int64Ty, I));
1056 return DIExpression(MDNode::get(VMContext, Elts));
1059 /// createFunction - Create a new descriptor for the specified function.
1060 /// FIXME: this is added for dragonegg. Once we update dragonegg
1061 /// to call resolve function, this will be removed.
1062 DISubprogram DIBuilder::createFunction(DIScopeRef Context, StringRef Name,
1063 StringRef LinkageName, DIFile File,
1064 unsigned LineNo, DICompositeType Ty,
1065 bool isLocalToUnit, bool isDefinition,
1066 unsigned ScopeLine, unsigned Flags,
1067 bool isOptimized, Function *Fn,
1068 MDNode *TParams, MDNode *Decl) {
1069 // dragonegg does not generate identifier for types, so using an empty map
1070 // to resolve the context should be fine.
1071 DITypeIdentifierMap EmptyMap;
1072 return createFunction(Context.resolve(EmptyMap), Name, LinkageName, File,
1073 LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine,
1074 Flags, isOptimized, Fn, TParams, Decl);
1078 createFunctionHelper(LLVMContext &VMContext, DIDescriptor Context, StringRef Name,
1079 StringRef LinkageName, DIFile File, unsigned LineNo,
1080 DICompositeType Ty, bool isLocalToUnit, bool isDefinition,
1081 unsigned ScopeLine, unsigned Flags, bool isOptimized,
1082 Function *Fn, MDNode *TParams, MDNode *Decl,
1083 std::function<MDNode *(ArrayRef<Value *>)> CreateFunc) {
1084 assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
1085 "function types should be subroutines");
1086 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
1088 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
1090 DIScope(getNonCompileUnitScope(Context)).getRef(),
1091 MDString::get(VMContext, Name),
1092 MDString::get(VMContext, Name),
1093 MDString::get(VMContext, LinkageName),
1094 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1096 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1097 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1098 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
1099 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
1101 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1102 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1106 MDNode::getTemporary(VMContext, TElts),
1107 ConstantInt::get(Type::getInt32Ty(VMContext), ScopeLine)
1110 DISubprogram S(CreateFunc(Elts));
1111 assert(S.isSubprogram() &&
1112 "createFunction should return a valid DISubprogram");
1117 /// createFunction - Create a new descriptor for the specified function.
1118 DISubprogram DIBuilder::createFunction(DIDescriptor Context, StringRef Name,
1119 StringRef LinkageName, DIFile File,
1120 unsigned LineNo, DICompositeType Ty,
1121 bool isLocalToUnit, bool isDefinition,
1122 unsigned ScopeLine, unsigned Flags,
1123 bool isOptimized, Function *Fn,
1124 MDNode *TParams, MDNode *Decl) {
1125 return createFunctionHelper(VMContext, Context, Name, LinkageName, File,
1126 LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine,
1127 Flags, isOptimized, Fn, TParams, Decl,
1128 [&] (ArrayRef<Value *> Elts) -> MDNode *{
1129 MDNode *Node = MDNode::get(VMContext, Elts);
1130 // Create a named metadata so that we
1131 // do not lose this mdnode.
1133 AllSubprograms.push_back(Node);
1138 /// createTempFunctionFwdDecl - Create a new temporary descriptor for
1139 /// the specified function declaration.
1141 DIBuilder::createTempFunctionFwdDecl(DIDescriptor Context, StringRef Name,
1142 StringRef LinkageName, DIFile File,
1143 unsigned LineNo, DICompositeType Ty,
1144 bool isLocalToUnit, bool isDefinition,
1145 unsigned ScopeLine, unsigned Flags,
1146 bool isOptimized, Function *Fn,
1147 MDNode *TParams, MDNode *Decl) {
1148 return createFunctionHelper(VMContext, Context, Name, LinkageName, File,
1149 LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine,
1150 Flags, isOptimized, Fn, TParams, Decl,
1151 [&] (ArrayRef<Value *> Elts) {
1152 return MDNode::getTemporary(VMContext, Elts);
1156 /// createMethod - Create a new descriptor for the specified C++ method.
1157 DISubprogram DIBuilder::createMethod(DIDescriptor Context, StringRef Name,
1158 StringRef LinkageName, DIFile F,
1159 unsigned LineNo, DICompositeType Ty,
1160 bool isLocalToUnit, bool isDefinition,
1161 unsigned VK, unsigned VIndex,
1162 DIType VTableHolder, unsigned Flags,
1163 bool isOptimized, Function *Fn,
1165 assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
1166 "function types should be subroutines");
1167 assert(getNonCompileUnitScope(Context) &&
1168 "Methods should have both a Context and a context that isn't "
1169 "the compile unit.");
1171 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
1173 DIScope(Context).getRef(),
1174 MDString::get(VMContext, Name),
1175 MDString::get(VMContext, Name),
1176 MDString::get(VMContext, LinkageName),
1177 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1179 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1180 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1181 ConstantInt::get(Type::getInt32Ty(VMContext), VK),
1182 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
1183 VTableHolder.getRef(),
1184 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1185 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1188 Constant::getNullValue(Type::getInt32Ty(VMContext)),
1190 // FIXME: Do we want to use different scope/lines?
1191 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1193 MDNode *Node = MDNode::get(VMContext, Elts);
1195 AllSubprograms.push_back(Node);
1196 DISubprogram S(Node);
1197 assert(S.isSubprogram() && "createMethod should return a valid DISubprogram");
1201 /// createNameSpace - This creates new descriptor for a namespace
1202 /// with the specified parent scope.
1203 DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
1204 DIFile File, unsigned LineNo) {
1206 GetTagConstant(VMContext, dwarf::DW_TAG_namespace),
1208 getNonCompileUnitScope(Scope),
1209 MDString::get(VMContext, Name),
1210 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1212 DINameSpace R(MDNode::get(VMContext, Elts));
1213 assert(R.Verify() &&
1214 "createNameSpace should return a verifiable DINameSpace");
1218 /// createLexicalBlockFile - This creates a new MDNode that encapsulates
1219 /// an existing scope with a new filename.
1220 DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
1222 unsigned Discriminator) {
1224 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1227 ConstantInt::get(Type::getInt32Ty(VMContext), Discriminator),
1229 DILexicalBlockFile R(MDNode::get(VMContext, Elts));
1232 "createLexicalBlockFile should return a verifiable DILexicalBlockFile");
1236 DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
1237 unsigned Line, unsigned Col) {
1238 // FIXME: This isn't thread safe nor the right way to defeat MDNode uniquing.
1239 // I believe the right way is to have a self-referential element in the node.
1240 // Also: why do we bother with line/column - they're not used and the
1241 // documentation (SourceLevelDebugging.rst) claims the line/col are necessary
1242 // for uniquing, yet then we have this other solution (because line/col were
1243 // inadequate) anyway. Remove all 3 and replace them with a self-reference.
1245 // Defeat MDNode uniquing for lexical blocks by using unique id.
1246 static unsigned int unique_id = 0;
1248 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1250 getNonCompileUnitScope(Scope),
1251 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
1252 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1253 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
1255 DILexicalBlock R(MDNode::get(VMContext, Elts));
1256 assert(R.Verify() &&
1257 "createLexicalBlock should return a verifiable DILexicalBlock");
1261 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1262 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1264 Instruction *InsertBefore) {
1265 assert(Storage && "no storage passed to dbg.declare");
1266 assert(VarInfo.isVariable() &&
1267 "empty or invalid DIVariable passed to dbg.declare");
1269 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1271 Value *Args[] = {MDNode::get(Storage->getContext(), Storage), VarInfo, Expr};
1272 return CallInst::Create(DeclareFn, Args, "", InsertBefore);
1275 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1276 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1278 BasicBlock *InsertAtEnd) {
1279 assert(Storage && "no storage passed to dbg.declare");
1280 assert(VarInfo.isVariable() &&
1281 "empty or invalid DIVariable passed to dbg.declare");
1283 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1285 Value *Args[] = {MDNode::get(Storage->getContext(), Storage), VarInfo, Expr};
1287 // If this block already has a terminator then insert this intrinsic
1288 // before the terminator.
1289 if (TerminatorInst *T = InsertAtEnd->getTerminator())
1290 return CallInst::Create(DeclareFn, Args, "", T);
1292 return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
1295 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1296 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1299 Instruction *InsertBefore) {
1300 assert(V && "no value passed to dbg.value");
1301 assert(VarInfo.isVariable() &&
1302 "empty or invalid DIVariable passed to dbg.value");
1304 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1306 Value *Args[] = {MDNode::get(V->getContext(), V),
1307 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1309 return CallInst::Create(ValueFn, Args, "", InsertBefore);
1312 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1313 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1316 BasicBlock *InsertAtEnd) {
1317 assert(V && "no value passed to dbg.value");
1318 assert(VarInfo.isVariable() &&
1319 "empty or invalid DIVariable passed to dbg.value");
1321 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1323 Value *Args[] = {MDNode::get(V->getContext(), V),
1324 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1326 return CallInst::Create(ValueFn, Args, "", InsertAtEnd);