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) {
107 assert(((Lang <= dwarf::DW_LANG_OCaml && Lang >= dwarf::DW_LANG_C89) ||
108 (Lang <= dwarf::DW_LANG_hi_user && Lang >= dwarf::DW_LANG_lo_user)) &&
109 "Invalid Language tag");
110 assert(!Filename.empty() &&
111 "Unable to create compile unit without filename");
112 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
113 TempEnumTypes = MDNode::getTemporary(VMContext, TElts);
115 TempRetainTypes = MDNode::getTemporary(VMContext, TElts);
117 TempSubprograms = MDNode::getTemporary(VMContext, TElts);
119 TempGVs = MDNode::getTemporary(VMContext, TElts);
121 TempImportedModules = MDNode::getTemporary(VMContext, TElts);
124 GetTagConstant(VMContext, dwarf::DW_TAG_compile_unit),
125 createFilePathPair(VMContext, Filename, Directory),
126 ConstantInt::get(Type::getInt32Ty(VMContext), Lang),
127 MDString::get(VMContext, Producer),
128 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
129 MDString::get(VMContext, Flags),
130 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeVer),
136 MDString::get(VMContext, SplitName),
137 ConstantInt::get(Type::getInt32Ty(VMContext), Kind)
140 MDNode *CUNode = MDNode::get(VMContext, Elts);
142 // Create a named metadata so that it is easier to find cu in a module.
143 NamedMDNode *NMD = M.getOrInsertNamedMetadata("llvm.dbg.cu");
144 NMD->addOperand(CUNode);
146 return DICompileUnit(CUNode);
149 static DIImportedEntity
150 createImportedModule(LLVMContext &C, dwarf::Tag Tag, DIScope Context,
151 Value *NS, unsigned Line, StringRef Name,
152 SmallVectorImpl<TrackingVH<MDNode>> &AllImportedModules) {
156 GetTagConstant(C, Tag),
159 ConstantInt::get(Type::getInt32Ty(C), Line),
161 R = MDNode::get(C, Elts);
164 GetTagConstant(C, Tag),
167 ConstantInt::get(Type::getInt32Ty(C), Line),
168 MDString::get(C, Name)
170 R = MDNode::get(C, Elts);
172 DIImportedEntity M(R);
173 assert(M.Verify() && "Imported module should be valid");
174 AllImportedModules.push_back(TrackingVH<MDNode>(M));
178 DIImportedEntity DIBuilder::createImportedModule(DIScope Context,
181 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
182 Context, NS, Line, StringRef(), AllImportedModules);
185 DIImportedEntity DIBuilder::createImportedModule(DIScope Context,
188 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_module,
189 Context, NS, Line, StringRef(), AllImportedModules);
192 DIImportedEntity DIBuilder::createImportedDeclaration(DIScope Context,
194 unsigned Line, StringRef Name) {
195 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_declaration,
196 Context, Decl.getRef(), Line, Name,
200 DIImportedEntity DIBuilder::createImportedDeclaration(DIScope Context,
201 DIImportedEntity Imp,
202 unsigned Line, StringRef Name) {
203 return ::createImportedModule(VMContext, dwarf::DW_TAG_imported_declaration,
204 Context, Imp, Line, Name, AllImportedModules);
207 /// createFile - Create a file descriptor to hold debugging information
209 DIFile DIBuilder::createFile(StringRef Filename, StringRef Directory) {
211 GetTagConstant(VMContext, dwarf::DW_TAG_file_type),
212 createFilePathPair(VMContext, Filename, Directory)
214 return DIFile(MDNode::get(VMContext, Elts));
217 /// createEnumerator - Create a single enumerator value.
218 DIEnumerator DIBuilder::createEnumerator(StringRef Name, int64_t Val) {
219 assert(!Name.empty() && "Unable to create enumerator without name");
221 GetTagConstant(VMContext, dwarf::DW_TAG_enumerator),
222 MDString::get(VMContext, Name),
223 ConstantInt::get(Type::getInt64Ty(VMContext), Val)
225 return DIEnumerator(MDNode::get(VMContext, Elts));
228 /// \brief Create a DWARF unspecified type.
229 DIBasicType DIBuilder::createUnspecifiedType(StringRef Name) {
230 assert(!Name.empty() && "Unable to create type without name");
231 // Unspecified types are encoded in DIBasicType format. Line number, filename,
232 // size, alignment, offset and flags are always empty here.
234 GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_type),
237 MDString::get(VMContext, Name),
238 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
239 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
240 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
241 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
242 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
243 ConstantInt::get(Type::getInt32Ty(VMContext), 0) // Encoding
245 return DIBasicType(MDNode::get(VMContext, Elts));
248 /// \brief Create C++11 nullptr type.
249 DIBasicType DIBuilder::createNullPtrType() {
250 return createUnspecifiedType("decltype(nullptr)");
253 /// createBasicType - Create debugging information entry for a basic
254 /// type, e.g 'char'.
256 DIBuilder::createBasicType(StringRef Name, uint64_t SizeInBits,
257 uint64_t AlignInBits, unsigned Encoding) {
258 assert(!Name.empty() && "Unable to create type without name");
259 // Basic types are encoded in DIBasicType format. Line number, filename,
260 // offset and flags are always empty here.
262 GetTagConstant(VMContext, dwarf::DW_TAG_base_type),
263 nullptr, // File/directory name
265 MDString::get(VMContext, Name),
266 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
267 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
268 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
269 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
270 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags;
271 ConstantInt::get(Type::getInt32Ty(VMContext), Encoding)
273 return DIBasicType(MDNode::get(VMContext, Elts));
276 /// createQualifiedType - Create debugging information entry for a qualified
277 /// type, e.g. 'const int'.
278 DIDerivedType DIBuilder::createQualifiedType(unsigned Tag, DIType FromTy) {
279 // Qualified types are encoded in DIDerivedType format.
281 GetTagConstant(VMContext, Tag),
284 MDString::get(VMContext, StringRef()), // Empty name.
285 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
286 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
287 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
288 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
289 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
292 return DIDerivedType(MDNode::get(VMContext, Elts));
295 /// createPointerType - Create debugging information entry for a pointer.
297 DIBuilder::createPointerType(DIType PointeeTy, uint64_t SizeInBits,
298 uint64_t AlignInBits, StringRef Name) {
299 // Pointer types are encoded in DIDerivedType format.
301 GetTagConstant(VMContext, dwarf::DW_TAG_pointer_type),
304 MDString::get(VMContext, Name),
305 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
306 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
307 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
308 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
309 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
312 return DIDerivedType(MDNode::get(VMContext, Elts));
315 DIDerivedType DIBuilder::createMemberPointerType(DIType PointeeTy,
317 // Pointer types are encoded in DIDerivedType format.
319 GetTagConstant(VMContext, dwarf::DW_TAG_ptr_to_member_type),
323 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
324 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
325 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
326 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
327 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
331 return DIDerivedType(MDNode::get(VMContext, Elts));
334 /// createReferenceType - Create debugging information entry for a reference
336 DIDerivedType DIBuilder::createReferenceType(unsigned Tag, DIType RTy) {
337 assert(RTy.isType() && "Unable to create reference type");
338 // References are encoded in DIDerivedType format.
340 GetTagConstant(VMContext, Tag),
344 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
345 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
346 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
347 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
348 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
351 return DIDerivedType(MDNode::get(VMContext, Elts));
354 /// createTypedef - Create debugging information entry for a typedef.
355 DIDerivedType DIBuilder::createTypedef(DIType Ty, StringRef Name, DIFile File,
356 unsigned LineNo, DIDescriptor Context) {
357 // typedefs are encoded in DIDerivedType format.
359 GetTagConstant(VMContext, dwarf::DW_TAG_typedef),
361 DIScope(getNonCompileUnitScope(Context)).getRef(),
362 MDString::get(VMContext, Name),
363 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
364 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
365 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
366 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
367 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
370 return DIDerivedType(MDNode::get(VMContext, Elts));
373 /// createFriend - Create debugging information entry for a 'friend'.
374 DIDerivedType DIBuilder::createFriend(DIType Ty, DIType FriendTy) {
375 // typedefs are encoded in DIDerivedType format.
376 assert(Ty.isType() && "Invalid type!");
377 assert(FriendTy.isType() && "Invalid friend type!");
379 GetTagConstant(VMContext, dwarf::DW_TAG_friend),
383 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
384 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
385 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
386 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
387 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
390 return DIDerivedType(MDNode::get(VMContext, Elts));
393 /// createInheritance - Create debugging information entry to establish
394 /// inheritance relationship between two types.
395 DIDerivedType DIBuilder::createInheritance(DIType Ty, DIType BaseTy,
398 assert(Ty.isType() && "Unable to create inheritance");
399 // TAG_inheritance is encoded in DIDerivedType format.
401 GetTagConstant(VMContext, dwarf::DW_TAG_inheritance),
405 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
406 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
407 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
408 ConstantInt::get(Type::getInt64Ty(VMContext), BaseOffset),
409 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
412 return DIDerivedType(MDNode::get(VMContext, Elts));
415 /// createMemberType - Create debugging information entry for a member.
416 DIDerivedType DIBuilder::createMemberType(DIDescriptor Scope, StringRef Name,
417 DIFile File, unsigned LineNumber,
419 uint64_t AlignInBits,
420 uint64_t OffsetInBits, unsigned Flags,
422 // TAG_member is encoded in DIDerivedType format.
424 GetTagConstant(VMContext, dwarf::DW_TAG_member),
426 DIScope(getNonCompileUnitScope(Scope)).getRef(),
427 MDString::get(VMContext, Name),
428 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
429 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
430 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
431 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
432 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
435 return DIDerivedType(MDNode::get(VMContext, Elts));
438 /// createStaticMemberType - Create debugging information entry for a
439 /// C++ static data member.
441 DIBuilder::createStaticMemberType(DIDescriptor Scope, StringRef Name,
442 DIFile File, unsigned LineNumber,
443 DIType Ty, unsigned Flags,
445 // TAG_member is encoded in DIDerivedType format.
446 Flags |= DIDescriptor::FlagStaticMember;
448 GetTagConstant(VMContext, dwarf::DW_TAG_member),
450 DIScope(getNonCompileUnitScope(Scope)).getRef(),
451 MDString::get(VMContext, Name),
452 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
453 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
454 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
455 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
456 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
460 return DIDerivedType(MDNode::get(VMContext, Elts));
463 /// createObjCIVar - Create debugging information entry for Objective-C
464 /// instance variable.
466 DIBuilder::createObjCIVar(StringRef Name, DIFile File, unsigned LineNumber,
467 uint64_t SizeInBits, uint64_t AlignInBits,
468 uint64_t OffsetInBits, unsigned Flags, DIType Ty,
469 StringRef PropertyName, StringRef GetterName,
470 StringRef SetterName, unsigned PropertyAttributes) {
471 // TAG_member is encoded in DIDerivedType format.
473 GetTagConstant(VMContext, dwarf::DW_TAG_member),
475 getNonCompileUnitScope(File),
476 MDString::get(VMContext, Name),
477 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
478 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
479 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
480 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
481 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
483 MDString::get(VMContext, PropertyName),
484 MDString::get(VMContext, GetterName),
485 MDString::get(VMContext, SetterName),
486 ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes)
488 return DIDerivedType(MDNode::get(VMContext, Elts));
491 /// createObjCIVar - Create debugging information entry for Objective-C
492 /// instance variable.
493 DIDerivedType DIBuilder::createObjCIVar(StringRef Name, DIFile File,
496 uint64_t AlignInBits,
497 uint64_t OffsetInBits, unsigned Flags,
498 DIType Ty, MDNode *PropertyNode) {
499 // TAG_member is encoded in DIDerivedType format.
501 GetTagConstant(VMContext, dwarf::DW_TAG_member),
503 getNonCompileUnitScope(File),
504 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::getInt64Ty(VMContext), OffsetInBits),
509 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
513 return DIDerivedType(MDNode::get(VMContext, Elts));
516 /// createObjCProperty - Create debugging information entry for Objective-C
519 DIBuilder::createObjCProperty(StringRef Name, DIFile File, unsigned LineNumber,
520 StringRef GetterName, StringRef SetterName,
521 unsigned PropertyAttributes, DIType Ty) {
523 GetTagConstant(VMContext, dwarf::DW_TAG_APPLE_property),
524 MDString::get(VMContext, Name),
526 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
527 MDString::get(VMContext, GetterName),
528 MDString::get(VMContext, SetterName),
529 ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes),
532 return DIObjCProperty(MDNode::get(VMContext, Elts));
535 /// createTemplateTypeParameter - Create debugging information for template
537 DITemplateTypeParameter
538 DIBuilder::createTemplateTypeParameter(DIDescriptor Context, StringRef Name,
539 DIType Ty, MDNode *File, unsigned LineNo,
542 GetTagConstant(VMContext, dwarf::DW_TAG_template_type_parameter),
543 DIScope(getNonCompileUnitScope(Context)).getRef(),
544 MDString::get(VMContext, Name),
547 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
548 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
550 return DITemplateTypeParameter(MDNode::get(VMContext, Elts));
553 DITemplateValueParameter
554 DIBuilder::createTemplateValueParameter(unsigned Tag, DIDescriptor Context,
555 StringRef Name, DIType Ty,
556 Value *Val, MDNode *File,
560 GetTagConstant(VMContext, Tag),
561 DIScope(getNonCompileUnitScope(Context)).getRef(),
562 MDString::get(VMContext, Name),
566 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
567 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
569 return DITemplateValueParameter(MDNode::get(VMContext, Elts));
572 /// createTemplateValueParameter - Create debugging information for template
574 DITemplateValueParameter
575 DIBuilder::createTemplateValueParameter(DIDescriptor Context, StringRef Name,
576 DIType Ty, Value *Val,
577 MDNode *File, unsigned LineNo,
579 return createTemplateValueParameter(dwarf::DW_TAG_template_value_parameter,
580 Context, Name, Ty, Val, File, LineNo,
584 DITemplateValueParameter
585 DIBuilder::createTemplateTemplateParameter(DIDescriptor Context, StringRef Name,
586 DIType Ty, StringRef Val,
587 MDNode *File, unsigned LineNo,
589 return createTemplateValueParameter(
590 dwarf::DW_TAG_GNU_template_template_param, Context, Name, Ty,
591 MDString::get(VMContext, Val), File, LineNo, ColumnNo);
594 DITemplateValueParameter
595 DIBuilder::createTemplateParameterPack(DIDescriptor Context, StringRef Name,
596 DIType Ty, DIArray Val,
597 MDNode *File, unsigned LineNo,
599 return createTemplateValueParameter(dwarf::DW_TAG_GNU_template_parameter_pack,
600 Context, Name, Ty, Val, File, LineNo,
604 /// createClassType - Create debugging information entry for a class.
605 DICompositeType DIBuilder::createClassType(DIDescriptor Context, StringRef Name,
606 DIFile File, unsigned LineNumber,
608 uint64_t AlignInBits,
609 uint64_t OffsetInBits,
610 unsigned Flags, DIType DerivedFrom,
613 MDNode *TemplateParams,
614 StringRef UniqueIdentifier) {
615 assert((!Context || Context.isScope() || Context.isType()) &&
616 "createClassType should be called with a valid Context");
617 // TAG_class_type is encoded in DICompositeType format.
619 GetTagConstant(VMContext, dwarf::DW_TAG_class_type),
621 DIScope(getNonCompileUnitScope(Context)).getRef(),
622 MDString::get(VMContext, Name),
623 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
624 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
625 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
626 ConstantInt::get(Type::getInt32Ty(VMContext), OffsetInBits),
627 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
628 DerivedFrom.getRef(),
630 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
631 VTableHolder.getRef(),
633 UniqueIdentifier.empty() ? nullptr
634 : MDString::get(VMContext, UniqueIdentifier)
636 DICompositeType R(MDNode::get(VMContext, Elts));
637 assert(R.isCompositeType() &&
638 "createClassType should return a DICompositeType");
639 if (!UniqueIdentifier.empty())
644 /// createStructType - Create debugging information entry for a struct.
645 DICompositeType DIBuilder::createStructType(DIDescriptor Context,
646 StringRef Name, DIFile File,
649 uint64_t AlignInBits,
650 unsigned Flags, DIType DerivedFrom,
652 unsigned RunTimeLang,
654 StringRef UniqueIdentifier) {
655 // TAG_structure_type is encoded in DICompositeType format.
657 GetTagConstant(VMContext, dwarf::DW_TAG_structure_type),
659 DIScope(getNonCompileUnitScope(Context)).getRef(),
660 MDString::get(VMContext, Name),
661 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
662 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
663 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
664 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
665 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
666 DerivedFrom.getRef(),
668 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
669 VTableHolder.getRef(),
671 UniqueIdentifier.empty() ? nullptr
672 : MDString::get(VMContext, UniqueIdentifier)
674 DICompositeType R(MDNode::get(VMContext, Elts));
675 assert(R.isCompositeType() &&
676 "createStructType should return a DICompositeType");
677 if (!UniqueIdentifier.empty())
682 /// createUnionType - Create debugging information entry for an union.
683 DICompositeType DIBuilder::createUnionType(DIDescriptor Scope, StringRef Name,
684 DIFile File, unsigned LineNumber,
686 uint64_t AlignInBits, unsigned Flags,
688 unsigned RunTimeLang,
689 StringRef UniqueIdentifier) {
690 // TAG_union_type is encoded in DICompositeType format.
692 GetTagConstant(VMContext, dwarf::DW_TAG_union_type),
694 DIScope(getNonCompileUnitScope(Scope)).getRef(),
695 MDString::get(VMContext, Name),
696 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
697 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
698 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
699 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
700 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
703 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
706 UniqueIdentifier.empty() ? nullptr
707 : MDString::get(VMContext, UniqueIdentifier)
709 DICompositeType R(MDNode::get(VMContext, Elts));
710 if (!UniqueIdentifier.empty())
715 /// createSubroutineType - Create subroutine type.
716 DICompositeType DIBuilder::createSubroutineType(DIFile File,
717 DIArray ParameterTypes,
719 // TAG_subroutine_type is encoded in DICompositeType format.
721 GetTagConstant(VMContext, dwarf::DW_TAG_subroutine_type),
722 Constant::getNullValue(Type::getInt32Ty(VMContext)),
724 MDString::get(VMContext, ""),
725 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
726 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
727 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
728 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
729 ConstantInt::get(Type::getInt32Ty(VMContext), Flags), // Flags
732 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
735 nullptr // Type Identifer
737 return DICompositeType(MDNode::get(VMContext, Elts));
740 /// createEnumerationType - Create debugging information entry for an
742 DICompositeType DIBuilder::createEnumerationType(
743 DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
744 uint64_t SizeInBits, uint64_t AlignInBits, DIArray Elements,
745 DIType UnderlyingType, StringRef UniqueIdentifier) {
746 // TAG_enumeration_type is encoded in DICompositeType format.
748 GetTagConstant(VMContext, dwarf::DW_TAG_enumeration_type),
750 DIScope(getNonCompileUnitScope(Scope)).getRef(),
751 MDString::get(VMContext, Name),
752 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
753 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
754 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
755 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset
756 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
757 UnderlyingType.getRef(),
759 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
762 UniqueIdentifier.empty() ? nullptr
763 : MDString::get(VMContext, UniqueIdentifier)
765 DICompositeType CTy(MDNode::get(VMContext, Elts));
766 AllEnumTypes.push_back(CTy);
767 if (!UniqueIdentifier.empty())
772 /// createArrayType - Create debugging information entry for an array.
773 DICompositeType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
774 DIType Ty, DIArray Subscripts) {
775 // TAG_array_type is encoded in DICompositeType format.
777 GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
778 nullptr, // Filename/Directory,
780 MDString::get(VMContext, ""),
781 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
782 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
783 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
784 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset
785 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
788 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
791 nullptr // Type Identifer
793 return DICompositeType(MDNode::get(VMContext, Elts));
796 /// createVectorType - Create debugging information entry for a vector.
797 DICompositeType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits,
798 DIType Ty, DIArray Subscripts) {
799 // A vector is an array type with the FlagVector flag applied.
801 GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
802 nullptr, // Filename/Directory,
804 MDString::get(VMContext, ""),
805 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
806 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
807 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
808 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset
809 ConstantInt::get(Type::getInt32Ty(VMContext), DIType::FlagVector),
812 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
815 nullptr // Type Identifer
817 return DICompositeType(MDNode::get(VMContext, Elts));
820 /// createArtificialType - Create a new DIType with "artificial" flag set.
821 DIType DIBuilder::createArtificialType(DIType Ty) {
822 if (Ty.isArtificial())
825 SmallVector<Value *, 9> Elts;
827 assert (N && "Unexpected input DIType!");
828 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
829 Elts.push_back(N->getOperand(i));
831 unsigned CurFlags = Ty.getFlags();
832 CurFlags = CurFlags | DIType::FlagArtificial;
834 // Flags are stored at this slot.
835 // FIXME: Add an enum for this magic value.
836 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
838 return DIType(MDNode::get(VMContext, Elts));
841 /// createObjectPointerType - Create a new type with both the object pointer
842 /// and artificial flags set.
843 DIType DIBuilder::createObjectPointerType(DIType Ty) {
844 if (Ty.isObjectPointer())
847 SmallVector<Value *, 9> Elts;
849 assert (N && "Unexpected input DIType!");
850 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
851 Elts.push_back(N->getOperand(i));
853 unsigned CurFlags = Ty.getFlags();
854 CurFlags = CurFlags | (DIType::FlagObjectPointer | DIType::FlagArtificial);
856 // Flags are stored at this slot.
857 // FIXME: Add an enum for this magic value.
858 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
860 return DIType(MDNode::get(VMContext, Elts));
863 /// retainType - Retain DIType in a module even if it is not referenced
864 /// through debug info anchors.
865 void DIBuilder::retainType(DIType T) {
866 AllRetainTypes.push_back(TrackingVH<MDNode>(T));
869 /// createUnspecifiedParameter - Create unspeicified type descriptor
870 /// for the subroutine type.
871 DIDescriptor DIBuilder::createUnspecifiedParameter() {
873 GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_parameters)
875 return DIDescriptor(MDNode::get(VMContext, Elts));
878 /// createForwardDecl - Create a temporary forward-declared type that
879 /// can be RAUW'd if the full type is seen.
881 DIBuilder::createForwardDecl(unsigned Tag, StringRef Name, DIDescriptor Scope,
882 DIFile F, unsigned Line, unsigned RuntimeLang,
883 uint64_t SizeInBits, uint64_t AlignInBits,
884 StringRef UniqueIdentifier) {
885 // Create a temporary MDNode.
887 GetTagConstant(VMContext, Tag),
889 DIScope(getNonCompileUnitScope(Scope)).getRef(),
890 MDString::get(VMContext, Name),
891 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
892 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
893 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
894 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset
895 ConstantInt::get(Type::getInt32Ty(VMContext), DIDescriptor::FlagFwdDecl),
898 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
900 nullptr, //TemplateParams
901 UniqueIdentifier.empty() ? nullptr
902 : MDString::get(VMContext, UniqueIdentifier)
904 MDNode *Node = MDNode::get(VMContext, Elts);
905 DICompositeType RetTy(Node);
906 assert(RetTy.isCompositeType() &&
907 "createForwardDecl result should be a DIType");
908 if (!UniqueIdentifier.empty())
913 /// createForwardDecl - Create a temporary forward-declared type that
914 /// can be RAUW'd if the full type is seen.
915 DICompositeType DIBuilder::createReplaceableForwardDecl(
916 unsigned Tag, StringRef Name, DIDescriptor Scope, DIFile F, unsigned Line,
917 unsigned RuntimeLang, uint64_t SizeInBits, uint64_t AlignInBits,
918 StringRef UniqueIdentifier) {
919 // Create a temporary MDNode.
921 GetTagConstant(VMContext, Tag),
923 DIScope(getNonCompileUnitScope(Scope)).getRef(),
924 MDString::get(VMContext, Name),
925 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
926 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
927 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
928 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset
929 ConstantInt::get(Type::getInt32Ty(VMContext), DIDescriptor::FlagFwdDecl),
932 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
934 nullptr, //TemplateParams
935 UniqueIdentifier.empty() ? nullptr
936 : MDString::get(VMContext, UniqueIdentifier)
938 MDNode *Node = MDNode::getTemporary(VMContext, Elts);
939 DICompositeType RetTy(Node);
940 assert(RetTy.isCompositeType() &&
941 "createForwardDecl result should be a DIType");
942 if (!UniqueIdentifier.empty())
947 /// getOrCreateArray - Get a DIArray, create one if required.
948 DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) {
949 return DIArray(MDNode::get(VMContext, Elements));
952 /// getOrCreateSubrange - Create a descriptor for a value range. This
953 /// implicitly uniques the values returned.
954 DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
956 GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type),
957 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
958 ConstantInt::get(Type::getInt64Ty(VMContext), Count)
961 return DISubrange(MDNode::get(VMContext, Elts));
964 /// \brief Create a new descriptor for the specified global.
965 DIGlobalVariable DIBuilder::createGlobalVariable(StringRef Name,
966 StringRef LinkageName,
967 DIFile F, unsigned LineNumber,
968 DITypeRef Ty, bool isLocalToUnit,
971 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
972 Constant::getNullValue(Type::getInt32Ty(VMContext)),
974 MDString::get(VMContext, Name),
975 MDString::get(VMContext, Name),
976 MDString::get(VMContext, LinkageName),
978 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
980 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
981 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
985 MDNode *Node = MDNode::get(VMContext, Elts);
986 AllGVs.push_back(Node);
987 return DIGlobalVariable(Node);
990 /// \brief Create a new descriptor for the specified global.
991 DIGlobalVariable DIBuilder::createGlobalVariable(StringRef Name, DIFile F,
996 return createGlobalVariable(Name, Name, F, LineNumber, Ty, isLocalToUnit,
1000 /// createStaticVariable - Create a new descriptor for the specified static
1002 DIGlobalVariable DIBuilder::createStaticVariable(DIDescriptor Context,
1004 StringRef LinkageName,
1005 DIFile F, unsigned LineNumber,
1008 Value *Val, MDNode *Decl) {
1010 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
1011 Constant::getNullValue(Type::getInt32Ty(VMContext)),
1012 getNonCompileUnitScope(Context),
1013 MDString::get(VMContext, Name),
1014 MDString::get(VMContext, Name),
1015 MDString::get(VMContext, LinkageName),
1017 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
1019 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
1020 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
1024 MDNode *Node = MDNode::get(VMContext, Elts);
1025 AllGVs.push_back(Node);
1026 return DIGlobalVariable(Node);
1029 /// createVariable - Create a new descriptor for the specified variable.
1030 DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
1031 StringRef Name, DIFile File,
1032 unsigned LineNo, DITypeRef Ty,
1033 bool AlwaysPreserve, unsigned Flags,
1035 DIDescriptor Context(getNonCompileUnitScope(Scope));
1036 assert((!Context || Context.isScope()) &&
1037 "createLocalVariable should be called with a valid Context");
1039 GetTagConstant(VMContext, Tag),
1040 getNonCompileUnitScope(Scope),
1041 MDString::get(VMContext, Name),
1043 ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))),
1045 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1046 Constant::getNullValue(Type::getInt32Ty(VMContext))
1048 MDNode *Node = MDNode::get(VMContext, Elts);
1049 if (AlwaysPreserve) {
1050 // The optimizer may remove local variable. If there is an interest
1051 // to preserve variable info in such situation then stash it in a
1053 DISubprogram Fn(getDISubprogram(Scope));
1054 NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, Fn);
1055 FnLocals->addOperand(Node);
1057 DIVariable RetVar(Node);
1058 assert(RetVar.isVariable() &&
1059 "createLocalVariable should return a valid DIVariable");
1063 /// createComplexVariable - Create a new descriptor for the specified variable
1064 /// which has a complex address expression for its address.
1065 DIVariable DIBuilder::createComplexVariable(unsigned Tag, DIDescriptor Scope,
1066 StringRef Name, DIFile F,
1069 ArrayRef<Value *> Addr,
1071 SmallVector<Value *, 15> Elts;
1072 Elts.push_back(GetTagConstant(VMContext, Tag));
1073 Elts.push_back(getNonCompileUnitScope(Scope)),
1074 Elts.push_back(MDString::get(VMContext, Name));
1076 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext),
1077 (LineNo | (ArgNo << 24))));
1079 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
1080 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
1081 Elts.append(Addr.begin(), Addr.end());
1083 return DIVariable(MDNode::get(VMContext, Elts));
1086 /// createFunction - Create a new descriptor for the specified function.
1087 /// FIXME: this is added for dragonegg. Once we update dragonegg
1088 /// to call resolve function, this will be removed.
1089 DISubprogram DIBuilder::createFunction(DIScopeRef Context, StringRef Name,
1090 StringRef LinkageName, DIFile File,
1091 unsigned LineNo, DICompositeType Ty,
1092 bool isLocalToUnit, bool isDefinition,
1093 unsigned ScopeLine, unsigned Flags,
1094 bool isOptimized, Function *Fn,
1095 MDNode *TParams, MDNode *Decl) {
1096 // dragonegg does not generate identifier for types, so using an empty map
1097 // to resolve the context should be fine.
1098 DITypeIdentifierMap EmptyMap;
1099 return createFunction(Context.resolve(EmptyMap), Name, LinkageName, File,
1100 LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine,
1101 Flags, isOptimized, Fn, TParams, Decl);
1104 /// createFunction - Create a new descriptor for the specified function.
1105 DISubprogram DIBuilder::createFunction(DIDescriptor Context, StringRef Name,
1106 StringRef LinkageName, DIFile File,
1107 unsigned LineNo, DICompositeType Ty,
1108 bool isLocalToUnit, bool isDefinition,
1109 unsigned ScopeLine, unsigned Flags,
1110 bool isOptimized, Function *Fn,
1111 MDNode *TParams, MDNode *Decl) {
1112 assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
1113 "function types should be subroutines");
1114 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
1116 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
1118 DIScope(getNonCompileUnitScope(Context)).getRef(),
1119 MDString::get(VMContext, Name),
1120 MDString::get(VMContext, Name),
1121 MDString::get(VMContext, LinkageName),
1122 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1124 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1125 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1126 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
1127 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
1129 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1130 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1134 MDNode::getTemporary(VMContext, TElts),
1135 ConstantInt::get(Type::getInt32Ty(VMContext), ScopeLine)
1137 MDNode *Node = MDNode::get(VMContext, Elts);
1139 // Create a named metadata so that we do not lose this mdnode.
1141 AllSubprograms.push_back(Node);
1142 DISubprogram S(Node);
1143 assert(S.isSubprogram() &&
1144 "createFunction should return a valid DISubprogram");
1148 /// createMethod - Create a new descriptor for the specified C++ method.
1149 DISubprogram DIBuilder::createMethod(DIDescriptor Context, StringRef Name,
1150 StringRef LinkageName, DIFile F,
1151 unsigned LineNo, DICompositeType Ty,
1152 bool isLocalToUnit, bool isDefinition,
1153 unsigned VK, unsigned VIndex,
1154 DIType VTableHolder, unsigned Flags,
1155 bool isOptimized, Function *Fn,
1157 assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
1158 "function types should be subroutines");
1159 assert(getNonCompileUnitScope(Context) &&
1160 "Methods should have both a Context and a context that isn't "
1161 "the compile unit.");
1163 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
1165 DIScope(Context).getRef(),
1166 MDString::get(VMContext, Name),
1167 MDString::get(VMContext, Name),
1168 MDString::get(VMContext, LinkageName),
1169 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1171 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1172 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1173 ConstantInt::get(Type::getInt32Ty(VMContext), VK),
1174 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
1175 VTableHolder.getRef(),
1176 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1177 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1180 Constant::getNullValue(Type::getInt32Ty(VMContext)),
1182 // FIXME: Do we want to use different scope/lines?
1183 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1185 MDNode *Node = MDNode::get(VMContext, Elts);
1187 AllSubprograms.push_back(Node);
1188 DISubprogram S(Node);
1189 assert(S.isSubprogram() && "createMethod should return a valid DISubprogram");
1193 /// createNameSpace - This creates new descriptor for a namespace
1194 /// with the specified parent scope.
1195 DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
1196 DIFile File, unsigned LineNo) {
1198 GetTagConstant(VMContext, dwarf::DW_TAG_namespace),
1200 getNonCompileUnitScope(Scope),
1201 MDString::get(VMContext, Name),
1202 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1204 DINameSpace R(MDNode::get(VMContext, Elts));
1205 assert(R.Verify() &&
1206 "createNameSpace should return a verifiable DINameSpace");
1210 /// createLexicalBlockFile - This creates a new MDNode that encapsulates
1211 /// an existing scope with a new filename.
1212 DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
1215 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1219 DILexicalBlockFile R(MDNode::get(VMContext, Elts));
1222 "createLexicalBlockFile should return a verifiable DILexicalBlockFile");
1226 DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
1227 unsigned Line, unsigned Col,
1228 unsigned Discriminator) {
1229 // FIXME: This isn't thread safe nor the right way to defeat MDNode uniquing.
1230 // I believe the right way is to have a self-referential element in the node.
1231 // Also: why do we bother with line/column - they're not used and the
1232 // documentation (SourceLevelDebugging.rst) claims the line/col are necessary
1233 // for uniquing, yet then we have this other solution (because line/col were
1234 // inadequate) anyway. Remove all 3 and replace them with a self-reference.
1236 // Defeat MDNode uniquing for lexical blocks by using unique id.
1237 static unsigned int unique_id = 0;
1239 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1241 getNonCompileUnitScope(Scope),
1242 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
1243 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1244 ConstantInt::get(Type::getInt32Ty(VMContext), Discriminator),
1245 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
1247 DILexicalBlock R(MDNode::get(VMContext, Elts));
1248 assert(R.Verify() &&
1249 "createLexicalBlock should return a verifiable DILexicalBlock");
1253 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1254 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1255 Instruction *InsertBefore) {
1256 assert(Storage && "no storage passed to dbg.declare");
1257 assert(VarInfo.isVariable() &&
1258 "empty or invalid DIVariable passed to dbg.declare");
1260 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1262 Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1263 return CallInst::Create(DeclareFn, Args, "", InsertBefore);
1266 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1267 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1268 BasicBlock *InsertAtEnd) {
1269 assert(Storage && "no storage passed to dbg.declare");
1270 assert(VarInfo.isVariable() &&
1271 "empty or invalid DIVariable passed to dbg.declare");
1273 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1275 Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1277 // If this block already has a terminator then insert this intrinsic
1278 // before the terminator.
1279 if (TerminatorInst *T = InsertAtEnd->getTerminator())
1280 return CallInst::Create(DeclareFn, Args, "", T);
1282 return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
1285 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1286 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1288 Instruction *InsertBefore) {
1289 assert(V && "no value passed to dbg.value");
1290 assert(VarInfo.isVariable() &&
1291 "empty or invalid DIVariable passed to dbg.value");
1293 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1295 Value *Args[] = { MDNode::get(V->getContext(), V),
1296 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1298 return CallInst::Create(ValueFn, Args, "", InsertBefore);
1301 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1302 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1304 BasicBlock *InsertAtEnd) {
1305 assert(V && "no value passed to dbg.value");
1306 assert(VarInfo.isVariable() &&
1307 "empty or invalid DIVariable passed to dbg.value");
1309 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1311 Value *Args[] = { MDNode::get(V->getContext(), V),
1312 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1314 return CallInst::Create(ValueFn, Args, "", InsertAtEnd);