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.
473 DIBuilder::createObjCIVar(StringRef Name, DIFile File, unsigned LineNumber,
474 uint64_t SizeInBits, uint64_t AlignInBits,
475 uint64_t OffsetInBits, unsigned Flags, DIType Ty,
476 StringRef PropertyName, StringRef GetterName,
477 StringRef SetterName, unsigned PropertyAttributes) {
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),
490 MDString::get(VMContext, PropertyName),
491 MDString::get(VMContext, GetterName),
492 MDString::get(VMContext, SetterName),
493 ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes)
495 return DIDerivedType(MDNode::get(VMContext, Elts));
498 /// createObjCIVar - Create debugging information entry for Objective-C
499 /// instance variable.
500 DIDerivedType DIBuilder::createObjCIVar(StringRef Name, DIFile File,
503 uint64_t AlignInBits,
504 uint64_t OffsetInBits, unsigned Flags,
505 DIType Ty, MDNode *PropertyNode) {
506 // TAG_member is encoded in DIDerivedType format.
508 GetTagConstant(VMContext, dwarf::DW_TAG_member),
510 getNonCompileUnitScope(File),
511 MDString::get(VMContext, Name),
512 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
513 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
514 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
515 ConstantInt::get(Type::getInt64Ty(VMContext), OffsetInBits),
516 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
520 return DIDerivedType(MDNode::get(VMContext, Elts));
523 /// createObjCProperty - Create debugging information entry for Objective-C
526 DIBuilder::createObjCProperty(StringRef Name, DIFile File, unsigned LineNumber,
527 StringRef GetterName, StringRef SetterName,
528 unsigned PropertyAttributes, DIType Ty) {
530 GetTagConstant(VMContext, dwarf::DW_TAG_APPLE_property),
531 MDString::get(VMContext, Name),
533 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
534 MDString::get(VMContext, GetterName),
535 MDString::get(VMContext, SetterName),
536 ConstantInt::get(Type::getInt32Ty(VMContext), PropertyAttributes),
539 return DIObjCProperty(MDNode::get(VMContext, Elts));
542 /// createTemplateTypeParameter - Create debugging information for template
544 DITemplateTypeParameter
545 DIBuilder::createTemplateTypeParameter(DIDescriptor Context, StringRef Name,
546 DIType Ty, MDNode *File, unsigned LineNo,
549 GetTagConstant(VMContext, dwarf::DW_TAG_template_type_parameter),
550 DIScope(getNonCompileUnitScope(Context)).getRef(),
551 MDString::get(VMContext, Name),
554 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
555 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
557 return DITemplateTypeParameter(MDNode::get(VMContext, Elts));
560 DITemplateValueParameter
561 DIBuilder::createTemplateValueParameter(unsigned Tag, DIDescriptor Context,
562 StringRef Name, DIType Ty,
563 Value *Val, MDNode *File,
567 GetTagConstant(VMContext, Tag),
568 DIScope(getNonCompileUnitScope(Context)).getRef(),
569 MDString::get(VMContext, Name),
573 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
574 ConstantInt::get(Type::getInt32Ty(VMContext), ColumnNo)
576 return DITemplateValueParameter(MDNode::get(VMContext, Elts));
579 /// createTemplateValueParameter - Create debugging information for template
581 DITemplateValueParameter
582 DIBuilder::createTemplateValueParameter(DIDescriptor Context, StringRef Name,
583 DIType Ty, Value *Val,
584 MDNode *File, unsigned LineNo,
586 return createTemplateValueParameter(dwarf::DW_TAG_template_value_parameter,
587 Context, Name, Ty, Val, File, LineNo,
591 DITemplateValueParameter
592 DIBuilder::createTemplateTemplateParameter(DIDescriptor Context, StringRef Name,
593 DIType Ty, StringRef Val,
594 MDNode *File, unsigned LineNo,
596 return createTemplateValueParameter(
597 dwarf::DW_TAG_GNU_template_template_param, Context, Name, Ty,
598 MDString::get(VMContext, Val), File, LineNo, ColumnNo);
601 DITemplateValueParameter
602 DIBuilder::createTemplateParameterPack(DIDescriptor Context, StringRef Name,
603 DIType Ty, DIArray Val,
604 MDNode *File, unsigned LineNo,
606 return createTemplateValueParameter(dwarf::DW_TAG_GNU_template_parameter_pack,
607 Context, Name, Ty, Val, File, LineNo,
611 /// createClassType - Create debugging information entry for a class.
612 DICompositeType DIBuilder::createClassType(DIDescriptor Context, StringRef Name,
613 DIFile File, unsigned LineNumber,
615 uint64_t AlignInBits,
616 uint64_t OffsetInBits,
617 unsigned Flags, DIType DerivedFrom,
620 MDNode *TemplateParams,
621 StringRef UniqueIdentifier) {
622 assert((!Context || Context.isScope() || Context.isType()) &&
623 "createClassType should be called with a valid Context");
624 // TAG_class_type is encoded in DICompositeType format.
626 GetTagConstant(VMContext, dwarf::DW_TAG_class_type),
628 DIScope(getNonCompileUnitScope(Context)).getRef(),
629 MDString::get(VMContext, Name),
630 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
631 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
632 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
633 ConstantInt::get(Type::getInt32Ty(VMContext), OffsetInBits),
634 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
635 DerivedFrom.getRef(),
637 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
638 VTableHolder.getRef(),
640 UniqueIdentifier.empty() ? nullptr
641 : MDString::get(VMContext, UniqueIdentifier)
643 DICompositeType R(MDNode::get(VMContext, Elts));
644 assert(R.isCompositeType() &&
645 "createClassType should return a DICompositeType");
646 if (!UniqueIdentifier.empty())
651 /// createStructType - Create debugging information entry for a struct.
652 DICompositeType DIBuilder::createStructType(DIDescriptor Context,
653 StringRef Name, DIFile File,
656 uint64_t AlignInBits,
657 unsigned Flags, DIType DerivedFrom,
659 unsigned RunTimeLang,
661 StringRef UniqueIdentifier) {
662 // TAG_structure_type is encoded in DICompositeType format.
664 GetTagConstant(VMContext, dwarf::DW_TAG_structure_type),
666 DIScope(getNonCompileUnitScope(Context)).getRef(),
667 MDString::get(VMContext, Name),
668 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
669 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
670 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
671 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
672 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
673 DerivedFrom.getRef(),
675 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
676 VTableHolder.getRef(),
678 UniqueIdentifier.empty() ? nullptr
679 : MDString::get(VMContext, UniqueIdentifier)
681 DICompositeType R(MDNode::get(VMContext, Elts));
682 assert(R.isCompositeType() &&
683 "createStructType should return a DICompositeType");
684 if (!UniqueIdentifier.empty())
689 /// createUnionType - Create debugging information entry for an union.
690 DICompositeType DIBuilder::createUnionType(DIDescriptor Scope, StringRef Name,
691 DIFile File, unsigned LineNumber,
693 uint64_t AlignInBits, unsigned Flags,
695 unsigned RunTimeLang,
696 StringRef UniqueIdentifier) {
697 // TAG_union_type is encoded in DICompositeType format.
699 GetTagConstant(VMContext, dwarf::DW_TAG_union_type),
701 DIScope(getNonCompileUnitScope(Scope)).getRef(),
702 MDString::get(VMContext, Name),
703 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
704 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
705 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
706 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
707 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
710 ConstantInt::get(Type::getInt32Ty(VMContext), RunTimeLang),
713 UniqueIdentifier.empty() ? nullptr
714 : MDString::get(VMContext, UniqueIdentifier)
716 DICompositeType R(MDNode::get(VMContext, Elts));
717 if (!UniqueIdentifier.empty())
722 /// createSubroutineType - Create subroutine type.
723 DICompositeType DIBuilder::createSubroutineType(DIFile File,
724 DIArray ParameterTypes,
726 // TAG_subroutine_type is encoded in DICompositeType format.
728 GetTagConstant(VMContext, dwarf::DW_TAG_subroutine_type),
729 Constant::getNullValue(Type::getInt32Ty(VMContext)),
731 MDString::get(VMContext, ""),
732 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
733 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Size
734 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Align
735 ConstantInt::get(Type::getInt64Ty(VMContext), 0), // Offset
736 ConstantInt::get(Type::getInt32Ty(VMContext), Flags), // Flags
739 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
742 nullptr // Type Identifer
744 return DICompositeType(MDNode::get(VMContext, Elts));
747 /// createEnumerationType - Create debugging information entry for an
749 DICompositeType DIBuilder::createEnumerationType(
750 DIDescriptor Scope, StringRef Name, DIFile File, unsigned LineNumber,
751 uint64_t SizeInBits, uint64_t AlignInBits, DIArray Elements,
752 DIType UnderlyingType, StringRef UniqueIdentifier) {
753 // TAG_enumeration_type is encoded in DICompositeType format.
755 GetTagConstant(VMContext, dwarf::DW_TAG_enumeration_type),
757 DIScope(getNonCompileUnitScope(Scope)).getRef(),
758 MDString::get(VMContext, Name),
759 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
760 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
761 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
762 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset
763 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
764 UnderlyingType.getRef(),
766 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
769 UniqueIdentifier.empty() ? nullptr
770 : MDString::get(VMContext, UniqueIdentifier)
772 DICompositeType CTy(MDNode::get(VMContext, Elts));
773 AllEnumTypes.push_back(CTy);
774 if (!UniqueIdentifier.empty())
779 /// createArrayType - Create debugging information entry for an array.
780 DICompositeType DIBuilder::createArrayType(uint64_t Size, uint64_t AlignInBits,
781 DIType Ty, DIArray Subscripts) {
782 // TAG_array_type is encoded in DICompositeType format.
784 GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
785 nullptr, // Filename/Directory,
787 MDString::get(VMContext, ""),
788 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
789 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
790 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
791 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset
792 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Flags
795 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
798 nullptr // Type Identifer
800 return DICompositeType(MDNode::get(VMContext, Elts));
803 /// createVectorType - Create debugging information entry for a vector.
804 DICompositeType DIBuilder::createVectorType(uint64_t Size, uint64_t AlignInBits,
805 DIType Ty, DIArray Subscripts) {
806 // A vector is an array type with the FlagVector flag applied.
808 GetTagConstant(VMContext, dwarf::DW_TAG_array_type),
809 nullptr, // Filename/Directory,
811 MDString::get(VMContext, ""),
812 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Line
813 ConstantInt::get(Type::getInt64Ty(VMContext), Size),
814 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
815 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset
816 ConstantInt::get(Type::getInt32Ty(VMContext), DIType::FlagVector),
819 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
822 nullptr // Type Identifer
824 return DICompositeType(MDNode::get(VMContext, Elts));
827 /// createArtificialType - Create a new DIType with "artificial" flag set.
828 DIType DIBuilder::createArtificialType(DIType Ty) {
829 if (Ty.isArtificial())
832 SmallVector<Value *, 9> Elts;
834 assert (N && "Unexpected input DIType!");
835 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
836 Elts.push_back(N->getOperand(i));
838 unsigned CurFlags = Ty.getFlags();
839 CurFlags = CurFlags | DIType::FlagArtificial;
841 // Flags are stored at this slot.
842 // FIXME: Add an enum for this magic value.
843 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
845 return DIType(MDNode::get(VMContext, Elts));
848 /// createObjectPointerType - Create a new type with both the object pointer
849 /// and artificial flags set.
850 DIType DIBuilder::createObjectPointerType(DIType Ty) {
851 if (Ty.isObjectPointer())
854 SmallVector<Value *, 9> Elts;
856 assert (N && "Unexpected input DIType!");
857 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
858 Elts.push_back(N->getOperand(i));
860 unsigned CurFlags = Ty.getFlags();
861 CurFlags = CurFlags | (DIType::FlagObjectPointer | DIType::FlagArtificial);
863 // Flags are stored at this slot.
864 // FIXME: Add an enum for this magic value.
865 Elts[8] = ConstantInt::get(Type::getInt32Ty(VMContext), CurFlags);
867 return DIType(MDNode::get(VMContext, Elts));
870 /// retainType - Retain DIType in a module even if it is not referenced
871 /// through debug info anchors.
872 void DIBuilder::retainType(DIType T) {
873 AllRetainTypes.push_back(TrackingVH<MDNode>(T));
876 /// createUnspecifiedParameter - Create unspeicified type descriptor
877 /// for the subroutine type.
878 DIDescriptor DIBuilder::createUnspecifiedParameter() {
880 GetTagConstant(VMContext, dwarf::DW_TAG_unspecified_parameters)
882 return DIDescriptor(MDNode::get(VMContext, Elts));
885 /// createForwardDecl - Create a temporary forward-declared type that
886 /// can be RAUW'd if the full type is seen.
888 DIBuilder::createForwardDecl(unsigned Tag, StringRef Name, DIDescriptor Scope,
889 DIFile F, unsigned Line, unsigned RuntimeLang,
890 uint64_t SizeInBits, uint64_t AlignInBits,
891 StringRef UniqueIdentifier) {
892 // Create a temporary MDNode.
894 GetTagConstant(VMContext, Tag),
896 DIScope(getNonCompileUnitScope(Scope)).getRef(),
897 MDString::get(VMContext, Name),
898 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
899 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
900 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
901 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset
902 ConstantInt::get(Type::getInt32Ty(VMContext), DIDescriptor::FlagFwdDecl),
905 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
907 nullptr, //TemplateParams
908 UniqueIdentifier.empty() ? nullptr
909 : MDString::get(VMContext, UniqueIdentifier)
911 MDNode *Node = MDNode::get(VMContext, Elts);
912 DICompositeType RetTy(Node);
913 assert(RetTy.isCompositeType() &&
914 "createForwardDecl result should be a DIType");
915 if (!UniqueIdentifier.empty())
920 /// createForwardDecl - Create a temporary forward-declared type that
921 /// can be RAUW'd if the full type is seen.
922 DICompositeType DIBuilder::createReplaceableForwardDecl(
923 unsigned Tag, StringRef Name, DIDescriptor Scope, DIFile F, unsigned Line,
924 unsigned RuntimeLang, uint64_t SizeInBits, uint64_t AlignInBits,
925 StringRef UniqueIdentifier) {
926 // Create a temporary MDNode.
928 GetTagConstant(VMContext, Tag),
930 DIScope(getNonCompileUnitScope(Scope)).getRef(),
931 MDString::get(VMContext, Name),
932 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
933 ConstantInt::get(Type::getInt64Ty(VMContext), SizeInBits),
934 ConstantInt::get(Type::getInt64Ty(VMContext), AlignInBits),
935 ConstantInt::get(Type::getInt32Ty(VMContext), 0), // Offset
936 ConstantInt::get(Type::getInt32Ty(VMContext), DIDescriptor::FlagFwdDecl),
939 ConstantInt::get(Type::getInt32Ty(VMContext), RuntimeLang),
941 nullptr, //TemplateParams
942 UniqueIdentifier.empty() ? nullptr
943 : MDString::get(VMContext, UniqueIdentifier)
945 MDNode *Node = MDNode::getTemporary(VMContext, Elts);
946 DICompositeType RetTy(Node);
947 assert(RetTy.isCompositeType() &&
948 "createForwardDecl result should be a DIType");
949 if (!UniqueIdentifier.empty())
954 /// getOrCreateArray - Get a DIArray, create one if required.
955 DIArray DIBuilder::getOrCreateArray(ArrayRef<Value *> Elements) {
956 return DIArray(MDNode::get(VMContext, Elements));
959 /// getOrCreateSubrange - Create a descriptor for a value range. This
960 /// implicitly uniques the values returned.
961 DISubrange DIBuilder::getOrCreateSubrange(int64_t Lo, int64_t Count) {
963 GetTagConstant(VMContext, dwarf::DW_TAG_subrange_type),
964 ConstantInt::get(Type::getInt64Ty(VMContext), Lo),
965 ConstantInt::get(Type::getInt64Ty(VMContext), Count)
968 return DISubrange(MDNode::get(VMContext, Elts));
971 /// \brief Create a new descriptor for the specified global.
972 DIGlobalVariable DIBuilder::createGlobalVariable(StringRef Name,
973 StringRef LinkageName,
974 DIFile F, unsigned LineNumber,
975 DITypeRef Ty, bool isLocalToUnit,
978 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
979 Constant::getNullValue(Type::getInt32Ty(VMContext)),
981 MDString::get(VMContext, Name),
982 MDString::get(VMContext, Name),
983 MDString::get(VMContext, LinkageName),
985 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
987 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
988 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
992 MDNode *Node = MDNode::get(VMContext, Elts);
993 AllGVs.push_back(Node);
994 return DIGlobalVariable(Node);
997 /// \brief Create a new descriptor for the specified global.
998 DIGlobalVariable DIBuilder::createGlobalVariable(StringRef Name, DIFile F,
1003 return createGlobalVariable(Name, Name, F, LineNumber, Ty, isLocalToUnit,
1007 /// createStaticVariable - Create a new descriptor for the specified static
1009 DIGlobalVariable DIBuilder::createStaticVariable(DIDescriptor Context,
1011 StringRef LinkageName,
1012 DIFile F, unsigned LineNumber,
1015 Value *Val, MDNode *Decl) {
1017 GetTagConstant(VMContext, dwarf::DW_TAG_variable),
1018 Constant::getNullValue(Type::getInt32Ty(VMContext)),
1019 getNonCompileUnitScope(Context),
1020 MDString::get(VMContext, Name),
1021 MDString::get(VMContext, Name),
1022 MDString::get(VMContext, LinkageName),
1024 ConstantInt::get(Type::getInt32Ty(VMContext), LineNumber),
1026 ConstantInt::get(Type::getInt32Ty(VMContext), isLocalToUnit),
1027 ConstantInt::get(Type::getInt32Ty(VMContext), 1), /* isDefinition*/
1031 MDNode *Node = MDNode::get(VMContext, Elts);
1032 AllGVs.push_back(Node);
1033 return DIGlobalVariable(Node);
1036 /// createVariable - Create a new descriptor for the specified variable.
1037 DIVariable DIBuilder::createLocalVariable(unsigned Tag, DIDescriptor Scope,
1038 StringRef Name, DIFile File,
1039 unsigned LineNo, DITypeRef Ty,
1040 bool AlwaysPreserve, unsigned Flags,
1042 DIDescriptor Context(getNonCompileUnitScope(Scope));
1043 assert((!Context || Context.isScope()) &&
1044 "createLocalVariable should be called with a valid Context");
1046 GetTagConstant(VMContext, Tag),
1047 getNonCompileUnitScope(Scope),
1048 MDString::get(VMContext, Name),
1050 ConstantInt::get(Type::getInt32Ty(VMContext), (LineNo | (ArgNo << 24))),
1052 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1053 Constant::getNullValue(Type::getInt32Ty(VMContext))
1055 MDNode *Node = MDNode::get(VMContext, Elts);
1056 if (AlwaysPreserve) {
1057 // The optimizer may remove local variable. If there is an interest
1058 // to preserve variable info in such situation then stash it in a
1060 DISubprogram Fn(getDISubprogram(Scope));
1061 NamedMDNode *FnLocals = getOrInsertFnSpecificMDNode(M, Fn);
1062 FnLocals->addOperand(Node);
1064 DIVariable RetVar(Node);
1065 assert(RetVar.isVariable() &&
1066 "createLocalVariable should return a valid DIVariable");
1070 /// createComplexVariable - Create a new descriptor for the specified variable
1071 /// which has a complex address expression for its address.
1072 DIVariable DIBuilder::createComplexVariable(unsigned Tag, DIDescriptor Scope,
1073 StringRef Name, DIFile F,
1076 ArrayRef<Value *> Addr,
1078 SmallVector<Value *, 15> Elts;
1079 Elts.push_back(GetTagConstant(VMContext, Tag));
1080 Elts.push_back(getNonCompileUnitScope(Scope)),
1081 Elts.push_back(MDString::get(VMContext, Name));
1083 Elts.push_back(ConstantInt::get(Type::getInt32Ty(VMContext),
1084 (LineNo | (ArgNo << 24))));
1086 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
1087 Elts.push_back(Constant::getNullValue(Type::getInt32Ty(VMContext)));
1088 Elts.append(Addr.begin(), Addr.end());
1090 return DIVariable(MDNode::get(VMContext, Elts));
1093 /// createFunction - Create a new descriptor for the specified function.
1094 /// FIXME: this is added for dragonegg. Once we update dragonegg
1095 /// to call resolve function, this will be removed.
1096 DISubprogram DIBuilder::createFunction(DIScopeRef Context, StringRef Name,
1097 StringRef LinkageName, DIFile File,
1098 unsigned LineNo, DICompositeType Ty,
1099 bool isLocalToUnit, bool isDefinition,
1100 unsigned ScopeLine, unsigned Flags,
1101 bool isOptimized, Function *Fn,
1102 MDNode *TParams, MDNode *Decl) {
1103 // dragonegg does not generate identifier for types, so using an empty map
1104 // to resolve the context should be fine.
1105 DITypeIdentifierMap EmptyMap;
1106 return createFunction(Context.resolve(EmptyMap), Name, LinkageName, File,
1107 LineNo, Ty, isLocalToUnit, isDefinition, ScopeLine,
1108 Flags, isOptimized, Fn, TParams, Decl);
1111 /// createFunction - Create a new descriptor for the specified function.
1112 DISubprogram DIBuilder::createFunction(DIDescriptor Context, StringRef Name,
1113 StringRef LinkageName, DIFile File,
1114 unsigned LineNo, DICompositeType Ty,
1115 bool isLocalToUnit, bool isDefinition,
1116 unsigned ScopeLine, unsigned Flags,
1117 bool isOptimized, Function *Fn,
1118 MDNode *TParams, MDNode *Decl) {
1119 assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
1120 "function types should be subroutines");
1121 Value *TElts[] = { GetTagConstant(VMContext, DW_TAG_base_type) };
1123 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
1125 DIScope(getNonCompileUnitScope(Context)).getRef(),
1126 MDString::get(VMContext, Name),
1127 MDString::get(VMContext, Name),
1128 MDString::get(VMContext, LinkageName),
1129 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1131 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1132 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1133 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
1134 ConstantInt::get(Type::getInt32Ty(VMContext), 0),
1136 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1137 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1141 MDNode::getTemporary(VMContext, TElts),
1142 ConstantInt::get(Type::getInt32Ty(VMContext), ScopeLine)
1144 MDNode *Node = MDNode::get(VMContext, Elts);
1146 // Create a named metadata so that we do not lose this mdnode.
1148 AllSubprograms.push_back(Node);
1149 DISubprogram S(Node);
1150 assert(S.isSubprogram() &&
1151 "createFunction should return a valid DISubprogram");
1155 /// createMethod - Create a new descriptor for the specified C++ method.
1156 DISubprogram DIBuilder::createMethod(DIDescriptor Context, StringRef Name,
1157 StringRef LinkageName, DIFile F,
1158 unsigned LineNo, DICompositeType Ty,
1159 bool isLocalToUnit, bool isDefinition,
1160 unsigned VK, unsigned VIndex,
1161 DIType VTableHolder, unsigned Flags,
1162 bool isOptimized, Function *Fn,
1164 assert(Ty.getTag() == dwarf::DW_TAG_subroutine_type &&
1165 "function types should be subroutines");
1166 assert(getNonCompileUnitScope(Context) &&
1167 "Methods should have both a Context and a context that isn't "
1168 "the compile unit.");
1170 GetTagConstant(VMContext, dwarf::DW_TAG_subprogram),
1172 DIScope(Context).getRef(),
1173 MDString::get(VMContext, Name),
1174 MDString::get(VMContext, Name),
1175 MDString::get(VMContext, LinkageName),
1176 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo),
1178 ConstantInt::get(Type::getInt1Ty(VMContext), isLocalToUnit),
1179 ConstantInt::get(Type::getInt1Ty(VMContext), isDefinition),
1180 ConstantInt::get(Type::getInt32Ty(VMContext), VK),
1181 ConstantInt::get(Type::getInt32Ty(VMContext), VIndex),
1182 VTableHolder.getRef(),
1183 ConstantInt::get(Type::getInt32Ty(VMContext), Flags),
1184 ConstantInt::get(Type::getInt1Ty(VMContext), isOptimized),
1187 Constant::getNullValue(Type::getInt32Ty(VMContext)),
1189 // FIXME: Do we want to use different scope/lines?
1190 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1192 MDNode *Node = MDNode::get(VMContext, Elts);
1194 AllSubprograms.push_back(Node);
1195 DISubprogram S(Node);
1196 assert(S.isSubprogram() && "createMethod should return a valid DISubprogram");
1200 /// createNameSpace - This creates new descriptor for a namespace
1201 /// with the specified parent scope.
1202 DINameSpace DIBuilder::createNameSpace(DIDescriptor Scope, StringRef Name,
1203 DIFile File, unsigned LineNo) {
1205 GetTagConstant(VMContext, dwarf::DW_TAG_namespace),
1207 getNonCompileUnitScope(Scope),
1208 MDString::get(VMContext, Name),
1209 ConstantInt::get(Type::getInt32Ty(VMContext), LineNo)
1211 DINameSpace R(MDNode::get(VMContext, Elts));
1212 assert(R.Verify() &&
1213 "createNameSpace should return a verifiable DINameSpace");
1217 /// createLexicalBlockFile - This creates a new MDNode that encapsulates
1218 /// an existing scope with a new filename.
1219 DILexicalBlockFile DIBuilder::createLexicalBlockFile(DIDescriptor Scope,
1222 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1226 DILexicalBlockFile R(MDNode::get(VMContext, Elts));
1229 "createLexicalBlockFile should return a verifiable DILexicalBlockFile");
1233 DILexicalBlock DIBuilder::createLexicalBlock(DIDescriptor Scope, DIFile File,
1234 unsigned Line, unsigned Col,
1235 unsigned Discriminator) {
1236 // FIXME: This isn't thread safe nor the right way to defeat MDNode uniquing.
1237 // I believe the right way is to have a self-referential element in the node.
1238 // Also: why do we bother with line/column - they're not used and the
1239 // documentation (SourceLevelDebugging.rst) claims the line/col are necessary
1240 // for uniquing, yet then we have this other solution (because line/col were
1241 // inadequate) anyway. Remove all 3 and replace them with a self-reference.
1243 // Defeat MDNode uniquing for lexical blocks by using unique id.
1244 static unsigned int unique_id = 0;
1246 GetTagConstant(VMContext, dwarf::DW_TAG_lexical_block),
1248 getNonCompileUnitScope(Scope),
1249 ConstantInt::get(Type::getInt32Ty(VMContext), Line),
1250 ConstantInt::get(Type::getInt32Ty(VMContext), Col),
1251 ConstantInt::get(Type::getInt32Ty(VMContext), Discriminator),
1252 ConstantInt::get(Type::getInt32Ty(VMContext), unique_id++)
1254 DILexicalBlock R(MDNode::get(VMContext, Elts));
1255 assert(R.Verify() &&
1256 "createLexicalBlock should return a verifiable DILexicalBlock");
1260 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1261 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1262 Instruction *InsertBefore) {
1263 assert(Storage && "no storage passed to dbg.declare");
1264 assert(VarInfo.isVariable() &&
1265 "empty or invalid DIVariable passed to dbg.declare");
1267 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1269 Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1270 return CallInst::Create(DeclareFn, Args, "", InsertBefore);
1273 /// insertDeclare - Insert a new llvm.dbg.declare intrinsic call.
1274 Instruction *DIBuilder::insertDeclare(Value *Storage, DIVariable VarInfo,
1275 BasicBlock *InsertAtEnd) {
1276 assert(Storage && "no storage passed to dbg.declare");
1277 assert(VarInfo.isVariable() &&
1278 "empty or invalid DIVariable passed to dbg.declare");
1280 DeclareFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_declare);
1282 Value *Args[] = { MDNode::get(Storage->getContext(), Storage), VarInfo };
1284 // If this block already has a terminator then insert this intrinsic
1285 // before the terminator.
1286 if (TerminatorInst *T = InsertAtEnd->getTerminator())
1287 return CallInst::Create(DeclareFn, Args, "", T);
1289 return CallInst::Create(DeclareFn, Args, "", InsertAtEnd);
1292 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1293 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1295 Instruction *InsertBefore) {
1296 assert(V && "no value passed to dbg.value");
1297 assert(VarInfo.isVariable() &&
1298 "empty or invalid DIVariable passed to dbg.value");
1300 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1302 Value *Args[] = { MDNode::get(V->getContext(), V),
1303 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1305 return CallInst::Create(ValueFn, Args, "", InsertBefore);
1308 /// insertDbgValueIntrinsic - Insert a new llvm.dbg.value intrinsic call.
1309 Instruction *DIBuilder::insertDbgValueIntrinsic(Value *V, uint64_t Offset,
1311 BasicBlock *InsertAtEnd) {
1312 assert(V && "no value passed to dbg.value");
1313 assert(VarInfo.isVariable() &&
1314 "empty or invalid DIVariable passed to dbg.value");
1316 ValueFn = Intrinsic::getDeclaration(&M, Intrinsic::dbg_value);
1318 Value *Args[] = { MDNode::get(V->getContext(), V),
1319 ConstantInt::get(Type::getInt64Ty(V->getContext()), Offset),
1321 return CallInst::Create(ValueFn, Args, "", InsertAtEnd);