1 //===--- DIBuilder.cpp - Debug Information Builder ------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the DIBuilder.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/IR/DIBuilder.h"
14 #include "LLVMContextImpl.h"
15 #include "llvm/ADT/Optional.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/BinaryFormat/Dwarf.h"
18 #include "llvm/IR/Constants.h"
19 #include "llvm/IR/DebugInfo.h"
20 #include "llvm/IR/IRBuilder.h"
21 #include "llvm/IR/IntrinsicInst.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/Debug.h"
27 using namespace llvm::dwarf
;
30 UseDbgAddr("use-dbg-addr",
31 llvm::cl::desc("Use llvm.dbg.addr for all local variables"),
32 cl::init(false), cl::Hidden
);
34 DIBuilder::DIBuilder(Module
&m
, bool AllowUnresolvedNodes
, DICompileUnit
*CU
)
35 : M(m
), VMContext(M
.getContext()), CUNode(CU
),
36 DeclareFn(nullptr), ValueFn(nullptr), LabelFn(nullptr),
37 AllowUnresolvedNodes(AllowUnresolvedNodes
) {}
39 void DIBuilder::trackIfUnresolved(MDNode
*N
) {
45 assert(AllowUnresolvedNodes
&& "Cannot handle unresolved nodes");
46 UnresolvedNodes
.emplace_back(N
);
49 void DIBuilder::finalizeSubprogram(DISubprogram
*SP
) {
50 MDTuple
*Temp
= SP
->getRetainedNodes().get();
51 if (!Temp
|| !Temp
->isTemporary())
54 SmallVector
<Metadata
*, 16> RetainedNodes
;
56 auto PV
= PreservedVariables
.find(SP
);
57 if (PV
!= PreservedVariables
.end())
58 RetainedNodes
.append(PV
->second
.begin(), PV
->second
.end());
60 auto PL
= PreservedLabels
.find(SP
);
61 if (PL
!= PreservedLabels
.end())
62 RetainedNodes
.append(PL
->second
.begin(), PL
->second
.end());
64 DINodeArray Node
= getOrCreateArray(RetainedNodes
);
66 TempMDTuple(Temp
)->replaceAllUsesWith(Node
.get());
69 void DIBuilder::finalize() {
71 assert(!AllowUnresolvedNodes
&&
72 "creating type nodes without a CU is not supported");
76 CUNode
->replaceEnumTypes(MDTuple::get(VMContext
, AllEnumTypes
));
78 SmallVector
<Metadata
*, 16> RetainValues
;
79 // Declarations and definitions of the same type may be retained. Some
80 // clients RAUW these pairs, leaving duplicates in the retained types
81 // list. Use a set to remove the duplicates while we transform the
82 // TrackingVHs back into Values.
83 SmallPtrSet
<Metadata
*, 16> RetainSet
;
84 for (unsigned I
= 0, E
= AllRetainTypes
.size(); I
< E
; I
++)
85 if (RetainSet
.insert(AllRetainTypes
[I
]).second
)
86 RetainValues
.push_back(AllRetainTypes
[I
]);
88 if (!RetainValues
.empty())
89 CUNode
->replaceRetainedTypes(MDTuple::get(VMContext
, RetainValues
));
91 DISubprogramArray SPs
= MDTuple::get(VMContext
, AllSubprograms
);
93 finalizeSubprogram(SP
);
94 for (auto *N
: RetainValues
)
95 if (auto *SP
= dyn_cast
<DISubprogram
>(N
))
96 finalizeSubprogram(SP
);
99 CUNode
->replaceGlobalVariables(MDTuple::get(VMContext
, AllGVs
));
101 if (!AllImportedModules
.empty())
102 CUNode
->replaceImportedEntities(MDTuple::get(
103 VMContext
, SmallVector
<Metadata
*, 16>(AllImportedModules
.begin(),
104 AllImportedModules
.end())));
106 for (const auto &I
: AllMacrosPerParent
) {
107 // DIMacroNode's with nullptr parent are DICompileUnit direct children.
109 CUNode
->replaceMacros(MDTuple::get(VMContext
, I
.second
.getArrayRef()));
112 // Otherwise, it must be a temporary DIMacroFile that need to be resolved.
113 auto *TMF
= cast
<DIMacroFile
>(I
.first
);
114 auto *MF
= DIMacroFile::get(VMContext
, dwarf::DW_MACINFO_start_file
,
115 TMF
->getLine(), TMF
->getFile(),
116 getOrCreateMacroArray(I
.second
.getArrayRef()));
117 replaceTemporary(llvm::TempDIMacroNode(TMF
), MF
);
120 // Now that all temp nodes have been replaced or deleted, resolve remaining
122 for (const auto &N
: UnresolvedNodes
)
123 if (N
&& !N
->isResolved())
125 UnresolvedNodes
.clear();
127 // Can't handle unresolved nodes anymore.
128 AllowUnresolvedNodes
= false;
131 /// If N is compile unit return NULL otherwise return N.
132 static DIScope
*getNonCompileUnitScope(DIScope
*N
) {
133 if (!N
|| isa
<DICompileUnit
>(N
))
135 return cast
<DIScope
>(N
);
138 DICompileUnit
*DIBuilder::createCompileUnit(
139 unsigned Lang
, DIFile
*File
, StringRef Producer
, bool isOptimized
,
140 StringRef Flags
, unsigned RunTimeVer
, StringRef SplitName
,
141 DICompileUnit::DebugEmissionKind Kind
, uint64_t DWOId
,
142 bool SplitDebugInlining
, bool DebugInfoForProfiling
,
143 DICompileUnit::DebugNameTableKind NameTableKind
, bool RangesBaseAddress
,
144 StringRef SysRoot
, StringRef SDK
) {
146 assert(((Lang
<= dwarf::DW_LANG_Fortran08
&& Lang
>= dwarf::DW_LANG_C89
) ||
147 (Lang
<= dwarf::DW_LANG_hi_user
&& Lang
>= dwarf::DW_LANG_lo_user
)) &&
148 "Invalid Language tag");
150 assert(!CUNode
&& "Can only make one compile unit per DIBuilder instance");
151 CUNode
= DICompileUnit::getDistinct(
152 VMContext
, Lang
, File
, Producer
, isOptimized
, Flags
, RunTimeVer
,
153 SplitName
, Kind
, nullptr, nullptr, nullptr, nullptr, nullptr, DWOId
,
154 SplitDebugInlining
, DebugInfoForProfiling
, NameTableKind
,
155 RangesBaseAddress
, SysRoot
, SDK
);
157 // Create a named metadata so that it is easier to find cu in a module.
158 NamedMDNode
*NMD
= M
.getOrInsertNamedMetadata("llvm.dbg.cu");
159 NMD
->addOperand(CUNode
);
160 trackIfUnresolved(CUNode
);
164 static DIImportedEntity
*
165 createImportedModule(LLVMContext
&C
, dwarf::Tag Tag
, DIScope
*Context
,
166 Metadata
*NS
, DIFile
*File
, unsigned Line
, StringRef Name
,
167 SmallVectorImpl
<TrackingMDNodeRef
> &AllImportedModules
) {
169 assert(File
&& "Source location has line number but no file");
170 unsigned EntitiesCount
= C
.pImpl
->DIImportedEntitys
.size();
171 auto *M
= DIImportedEntity::get(C
, Tag
, Context
, cast_or_null
<DINode
>(NS
),
173 if (EntitiesCount
< C
.pImpl
->DIImportedEntitys
.size())
174 // A new Imported Entity was just added to the context.
175 // Add it to the Imported Modules list.
176 AllImportedModules
.emplace_back(M
);
180 DIImportedEntity
*DIBuilder::createImportedModule(DIScope
*Context
,
181 DINamespace
*NS
, DIFile
*File
,
183 return ::createImportedModule(VMContext
, dwarf::DW_TAG_imported_module
,
184 Context
, NS
, File
, Line
, StringRef(),
188 DIImportedEntity
*DIBuilder::createImportedModule(DIScope
*Context
,
189 DIImportedEntity
*NS
,
190 DIFile
*File
, unsigned Line
) {
191 return ::createImportedModule(VMContext
, dwarf::DW_TAG_imported_module
,
192 Context
, NS
, File
, Line
, StringRef(),
196 DIImportedEntity
*DIBuilder::createImportedModule(DIScope
*Context
, DIModule
*M
,
197 DIFile
*File
, unsigned Line
) {
198 return ::createImportedModule(VMContext
, dwarf::DW_TAG_imported_module
,
199 Context
, M
, File
, Line
, StringRef(),
203 DIImportedEntity
*DIBuilder::createImportedDeclaration(DIScope
*Context
,
208 // Make sure to use the unique identifier based metadata reference for
209 // types that have one.
210 return ::createImportedModule(VMContext
, dwarf::DW_TAG_imported_declaration
,
211 Context
, Decl
, File
, Line
, Name
,
215 DIFile
*DIBuilder::createFile(StringRef Filename
, StringRef Directory
,
216 Optional
<DIFile::ChecksumInfo
<StringRef
>> CS
,
217 Optional
<StringRef
> Source
) {
218 return DIFile::get(VMContext
, Filename
, Directory
, CS
, Source
);
221 DIMacro
*DIBuilder::createMacro(DIMacroFile
*Parent
, unsigned LineNumber
,
222 unsigned MacroType
, StringRef Name
,
224 assert(!Name
.empty() && "Unable to create macro without name");
225 assert((MacroType
== dwarf::DW_MACINFO_undef
||
226 MacroType
== dwarf::DW_MACINFO_define
) &&
227 "Unexpected macro type");
228 auto *M
= DIMacro::get(VMContext
, MacroType
, LineNumber
, Name
, Value
);
229 AllMacrosPerParent
[Parent
].insert(M
);
233 DIMacroFile
*DIBuilder::createTempMacroFile(DIMacroFile
*Parent
,
234 unsigned LineNumber
, DIFile
*File
) {
235 auto *MF
= DIMacroFile::getTemporary(VMContext
, dwarf::DW_MACINFO_start_file
,
236 LineNumber
, File
, DIMacroNodeArray())
238 AllMacrosPerParent
[Parent
].insert(MF
);
239 // Add the new temporary DIMacroFile to the macro per parent map as a parent.
240 // This is needed to assure DIMacroFile with no children to have an entry in
241 // the map. Otherwise, it will not be resolved in DIBuilder::finalize().
242 AllMacrosPerParent
.insert({MF
, {}});
246 DIEnumerator
*DIBuilder::createEnumerator(StringRef Name
, uint64_t Val
,
248 assert(!Name
.empty() && "Unable to create enumerator without name");
249 return DIEnumerator::get(VMContext
, APInt(64, Val
, !IsUnsigned
), IsUnsigned
,
253 DIEnumerator
*DIBuilder::createEnumerator(StringRef Name
, APSInt Value
) {
254 assert(!Name
.empty() && "Unable to create enumerator without name");
255 return DIEnumerator::get(VMContext
, APInt(Value
), Value
.isUnsigned(), Name
);
258 DIBasicType
*DIBuilder::createUnspecifiedType(StringRef Name
) {
259 assert(!Name
.empty() && "Unable to create type without name");
260 return DIBasicType::get(VMContext
, dwarf::DW_TAG_unspecified_type
, Name
);
263 DIBasicType
*DIBuilder::createNullPtrType() {
264 return createUnspecifiedType("decltype(nullptr)");
267 DIBasicType
*DIBuilder::createBasicType(StringRef Name
, uint64_t SizeInBits
,
269 DINode::DIFlags Flags
) {
270 assert(!Name
.empty() && "Unable to create type without name");
271 return DIBasicType::get(VMContext
, dwarf::DW_TAG_base_type
, Name
, SizeInBits
,
275 DIStringType
*DIBuilder::createStringType(StringRef Name
, uint64_t SizeInBits
) {
276 assert(!Name
.empty() && "Unable to create type without name");
277 return DIStringType::get(VMContext
, dwarf::DW_TAG_string_type
, Name
,
281 DIDerivedType
*DIBuilder::createQualifiedType(unsigned Tag
, DIType
*FromTy
) {
282 return DIDerivedType::get(VMContext
, Tag
, "", nullptr, 0, nullptr, FromTy
, 0,
283 0, 0, None
, DINode::FlagZero
);
286 DIDerivedType
*DIBuilder::createPointerType(
289 uint32_t AlignInBits
,
290 Optional
<unsigned> DWARFAddressSpace
,
292 // FIXME: Why is there a name here?
293 return DIDerivedType::get(VMContext
, dwarf::DW_TAG_pointer_type
, Name
,
294 nullptr, 0, nullptr, PointeeTy
, SizeInBits
,
295 AlignInBits
, 0, DWARFAddressSpace
,
299 DIDerivedType
*DIBuilder::createMemberPointerType(DIType
*PointeeTy
,
302 uint32_t AlignInBits
,
303 DINode::DIFlags Flags
) {
304 return DIDerivedType::get(VMContext
, dwarf::DW_TAG_ptr_to_member_type
, "",
305 nullptr, 0, nullptr, PointeeTy
, SizeInBits
,
306 AlignInBits
, 0, None
, Flags
, Base
);
309 DIDerivedType
*DIBuilder::createReferenceType(
310 unsigned Tag
, DIType
*RTy
,
312 uint32_t AlignInBits
,
313 Optional
<unsigned> DWARFAddressSpace
) {
314 assert(RTy
&& "Unable to create reference type");
315 return DIDerivedType::get(VMContext
, Tag
, "", nullptr, 0, nullptr, RTy
,
316 SizeInBits
, AlignInBits
, 0, DWARFAddressSpace
,
320 DIDerivedType
*DIBuilder::createTypedef(DIType
*Ty
, StringRef Name
,
321 DIFile
*File
, unsigned LineNo
,
323 uint32_t AlignInBits
) {
324 return DIDerivedType::get(VMContext
, dwarf::DW_TAG_typedef
, Name
, File
,
325 LineNo
, getNonCompileUnitScope(Context
), Ty
, 0,
326 AlignInBits
, 0, None
, DINode::FlagZero
);
329 DIDerivedType
*DIBuilder::createFriend(DIType
*Ty
, DIType
*FriendTy
) {
330 assert(Ty
&& "Invalid type!");
331 assert(FriendTy
&& "Invalid friend type!");
332 return DIDerivedType::get(VMContext
, dwarf::DW_TAG_friend
, "", nullptr, 0, Ty
,
333 FriendTy
, 0, 0, 0, None
, DINode::FlagZero
);
336 DIDerivedType
*DIBuilder::createInheritance(DIType
*Ty
, DIType
*BaseTy
,
338 uint32_t VBPtrOffset
,
339 DINode::DIFlags Flags
) {
340 assert(Ty
&& "Unable to create inheritance");
341 Metadata
*ExtraData
= ConstantAsMetadata::get(
342 ConstantInt::get(IntegerType::get(VMContext
, 32), VBPtrOffset
));
343 return DIDerivedType::get(VMContext
, dwarf::DW_TAG_inheritance
, "", nullptr,
344 0, Ty
, BaseTy
, 0, 0, BaseOffset
, None
,
348 DIDerivedType
*DIBuilder::createMemberType(
349 DIScope
*Scope
, StringRef Name
, DIFile
*File
, unsigned LineNumber
,
350 uint64_t SizeInBits
, uint32_t AlignInBits
, uint64_t OffsetInBits
,
351 DINode::DIFlags Flags
, DIType
*Ty
, DINodeArray Annotations
) {
352 return DIDerivedType::get(VMContext
, dwarf::DW_TAG_member
, Name
, File
,
353 LineNumber
, getNonCompileUnitScope(Scope
), Ty
,
354 SizeInBits
, AlignInBits
, OffsetInBits
, None
, Flags
,
355 nullptr, Annotations
);
358 static ConstantAsMetadata
*getConstantOrNull(Constant
*C
) {
360 return ConstantAsMetadata::get(C
);
364 DIDerivedType
*DIBuilder::createVariantMemberType(
365 DIScope
*Scope
, StringRef Name
, DIFile
*File
, unsigned LineNumber
,
366 uint64_t SizeInBits
, uint32_t AlignInBits
, uint64_t OffsetInBits
,
367 Constant
*Discriminant
, DINode::DIFlags Flags
, DIType
*Ty
) {
368 return DIDerivedType::get(VMContext
, dwarf::DW_TAG_member
, Name
, File
,
369 LineNumber
, getNonCompileUnitScope(Scope
), Ty
,
370 SizeInBits
, AlignInBits
, OffsetInBits
, None
, Flags
,
371 getConstantOrNull(Discriminant
));
374 DIDerivedType
*DIBuilder::createBitFieldMemberType(
375 DIScope
*Scope
, StringRef Name
, DIFile
*File
, unsigned LineNumber
,
376 uint64_t SizeInBits
, uint64_t OffsetInBits
, uint64_t StorageOffsetInBits
,
377 DINode::DIFlags Flags
, DIType
*Ty
, DINodeArray Annotations
) {
378 Flags
|= DINode::FlagBitField
;
379 return DIDerivedType::get(
380 VMContext
, dwarf::DW_TAG_member
, Name
, File
, LineNumber
,
381 getNonCompileUnitScope(Scope
), Ty
, SizeInBits
, /* AlignInBits */ 0,
382 OffsetInBits
, None
, Flags
,
383 ConstantAsMetadata::get(ConstantInt::get(IntegerType::get(VMContext
, 64),
384 StorageOffsetInBits
)),
389 DIBuilder::createStaticMemberType(DIScope
*Scope
, StringRef Name
, DIFile
*File
,
390 unsigned LineNumber
, DIType
*Ty
,
391 DINode::DIFlags Flags
, llvm::Constant
*Val
,
392 uint32_t AlignInBits
) {
393 Flags
|= DINode::FlagStaticMember
;
394 return DIDerivedType::get(VMContext
, dwarf::DW_TAG_member
, Name
, File
,
395 LineNumber
, getNonCompileUnitScope(Scope
), Ty
, 0,
396 AlignInBits
, 0, None
, Flags
,
397 getConstantOrNull(Val
));
401 DIBuilder::createObjCIVar(StringRef Name
, DIFile
*File
, unsigned LineNumber
,
402 uint64_t SizeInBits
, uint32_t AlignInBits
,
403 uint64_t OffsetInBits
, DINode::DIFlags Flags
,
404 DIType
*Ty
, MDNode
*PropertyNode
) {
405 return DIDerivedType::get(VMContext
, dwarf::DW_TAG_member
, Name
, File
,
406 LineNumber
, getNonCompileUnitScope(File
), Ty
,
407 SizeInBits
, AlignInBits
, OffsetInBits
, None
, Flags
,
412 DIBuilder::createObjCProperty(StringRef Name
, DIFile
*File
, unsigned LineNumber
,
413 StringRef GetterName
, StringRef SetterName
,
414 unsigned PropertyAttributes
, DIType
*Ty
) {
415 return DIObjCProperty::get(VMContext
, Name
, File
, LineNumber
, GetterName
,
416 SetterName
, PropertyAttributes
, Ty
);
419 DITemplateTypeParameter
*
420 DIBuilder::createTemplateTypeParameter(DIScope
*Context
, StringRef Name
,
421 DIType
*Ty
, bool isDefault
) {
422 assert((!Context
|| isa
<DICompileUnit
>(Context
)) && "Expected compile unit");
423 return DITemplateTypeParameter::get(VMContext
, Name
, Ty
, isDefault
);
426 static DITemplateValueParameter
*
427 createTemplateValueParameterHelper(LLVMContext
&VMContext
, unsigned Tag
,
428 DIScope
*Context
, StringRef Name
, DIType
*Ty
,
429 bool IsDefault
, Metadata
*MD
) {
430 assert((!Context
|| isa
<DICompileUnit
>(Context
)) && "Expected compile unit");
431 return DITemplateValueParameter::get(VMContext
, Tag
, Name
, Ty
, IsDefault
, MD
);
434 DITemplateValueParameter
*
435 DIBuilder::createTemplateValueParameter(DIScope
*Context
, StringRef Name
,
436 DIType
*Ty
, bool isDefault
,
438 return createTemplateValueParameterHelper(
439 VMContext
, dwarf::DW_TAG_template_value_parameter
, Context
, Name
, Ty
,
440 isDefault
, getConstantOrNull(Val
));
443 DITemplateValueParameter
*
444 DIBuilder::createTemplateTemplateParameter(DIScope
*Context
, StringRef Name
,
445 DIType
*Ty
, StringRef Val
) {
446 return createTemplateValueParameterHelper(
447 VMContext
, dwarf::DW_TAG_GNU_template_template_param
, Context
, Name
, Ty
,
448 false, MDString::get(VMContext
, Val
));
451 DITemplateValueParameter
*
452 DIBuilder::createTemplateParameterPack(DIScope
*Context
, StringRef Name
,
453 DIType
*Ty
, DINodeArray Val
) {
454 return createTemplateValueParameterHelper(
455 VMContext
, dwarf::DW_TAG_GNU_template_parameter_pack
, Context
, Name
, Ty
,
459 DICompositeType
*DIBuilder::createClassType(
460 DIScope
*Context
, StringRef Name
, DIFile
*File
, unsigned LineNumber
,
461 uint64_t SizeInBits
, uint32_t AlignInBits
, uint64_t OffsetInBits
,
462 DINode::DIFlags Flags
, DIType
*DerivedFrom
, DINodeArray Elements
,
463 DIType
*VTableHolder
, MDNode
*TemplateParams
, StringRef UniqueIdentifier
) {
464 assert((!Context
|| isa
<DIScope
>(Context
)) &&
465 "createClassType should be called with a valid Context");
467 auto *R
= DICompositeType::get(
468 VMContext
, dwarf::DW_TAG_structure_type
, Name
, File
, LineNumber
,
469 getNonCompileUnitScope(Context
), DerivedFrom
, SizeInBits
, AlignInBits
,
470 OffsetInBits
, Flags
, Elements
, 0, VTableHolder
,
471 cast_or_null
<MDTuple
>(TemplateParams
), UniqueIdentifier
);
472 trackIfUnresolved(R
);
476 DICompositeType
*DIBuilder::createStructType(
477 DIScope
*Context
, StringRef Name
, DIFile
*File
, unsigned LineNumber
,
478 uint64_t SizeInBits
, uint32_t AlignInBits
, DINode::DIFlags Flags
,
479 DIType
*DerivedFrom
, DINodeArray Elements
, unsigned RunTimeLang
,
480 DIType
*VTableHolder
, StringRef UniqueIdentifier
) {
481 auto *R
= DICompositeType::get(
482 VMContext
, dwarf::DW_TAG_structure_type
, Name
, File
, LineNumber
,
483 getNonCompileUnitScope(Context
), DerivedFrom
, SizeInBits
, AlignInBits
, 0,
484 Flags
, Elements
, RunTimeLang
, VTableHolder
, nullptr, UniqueIdentifier
);
485 trackIfUnresolved(R
);
489 DICompositeType
*DIBuilder::createUnionType(
490 DIScope
*Scope
, StringRef Name
, DIFile
*File
, unsigned LineNumber
,
491 uint64_t SizeInBits
, uint32_t AlignInBits
, DINode::DIFlags Flags
,
492 DINodeArray Elements
, unsigned RunTimeLang
, StringRef UniqueIdentifier
) {
493 auto *R
= DICompositeType::get(
494 VMContext
, dwarf::DW_TAG_union_type
, Name
, File
, LineNumber
,
495 getNonCompileUnitScope(Scope
), nullptr, SizeInBits
, AlignInBits
, 0, Flags
,
496 Elements
, RunTimeLang
, nullptr, nullptr, UniqueIdentifier
);
497 trackIfUnresolved(R
);
501 DICompositeType
*DIBuilder::createVariantPart(
502 DIScope
*Scope
, StringRef Name
, DIFile
*File
, unsigned LineNumber
,
503 uint64_t SizeInBits
, uint32_t AlignInBits
, DINode::DIFlags Flags
,
504 DIDerivedType
*Discriminator
, DINodeArray Elements
, StringRef UniqueIdentifier
) {
505 auto *R
= DICompositeType::get(
506 VMContext
, dwarf::DW_TAG_variant_part
, Name
, File
, LineNumber
,
507 getNonCompileUnitScope(Scope
), nullptr, SizeInBits
, AlignInBits
, 0, Flags
,
508 Elements
, 0, nullptr, nullptr, UniqueIdentifier
, Discriminator
);
509 trackIfUnresolved(R
);
513 DISubroutineType
*DIBuilder::createSubroutineType(DITypeRefArray ParameterTypes
,
514 DINode::DIFlags Flags
,
516 return DISubroutineType::get(VMContext
, Flags
, CC
, ParameterTypes
);
519 DICompositeType
*DIBuilder::createEnumerationType(
520 DIScope
*Scope
, StringRef Name
, DIFile
*File
, unsigned LineNumber
,
521 uint64_t SizeInBits
, uint32_t AlignInBits
, DINodeArray Elements
,
522 DIType
*UnderlyingType
, StringRef UniqueIdentifier
, bool IsScoped
) {
523 auto *CTy
= DICompositeType::get(
524 VMContext
, dwarf::DW_TAG_enumeration_type
, Name
, File
, LineNumber
,
525 getNonCompileUnitScope(Scope
), UnderlyingType
, SizeInBits
, AlignInBits
, 0,
526 IsScoped
? DINode::FlagEnumClass
: DINode::FlagZero
, Elements
, 0, nullptr,
527 nullptr, UniqueIdentifier
);
528 AllEnumTypes
.push_back(CTy
);
529 trackIfUnresolved(CTy
);
533 DIDerivedType
*DIBuilder::createSetType(DIScope
*Scope
, StringRef Name
,
534 DIFile
*File
, unsigned LineNo
,
536 uint32_t AlignInBits
, DIType
*Ty
) {
538 DIDerivedType::get(VMContext
, dwarf::DW_TAG_set_type
, Name
, File
, LineNo
,
539 getNonCompileUnitScope(Scope
), Ty
, SizeInBits
,
540 AlignInBits
, 0, None
, DINode::FlagZero
);
541 trackIfUnresolved(R
);
545 DICompositeType
*DIBuilder::createArrayType(
546 uint64_t Size
, uint32_t AlignInBits
, DIType
*Ty
, DINodeArray Subscripts
,
547 PointerUnion
<DIExpression
*, DIVariable
*> DL
,
548 PointerUnion
<DIExpression
*, DIVariable
*> AS
,
549 PointerUnion
<DIExpression
*, DIVariable
*> AL
,
550 PointerUnion
<DIExpression
*, DIVariable
*> RK
) {
551 auto *R
= DICompositeType::get(
552 VMContext
, dwarf::DW_TAG_array_type
, "", nullptr, 0,
553 nullptr, Ty
, Size
, AlignInBits
, 0, DINode::FlagZero
,
554 Subscripts
, 0, nullptr, nullptr, "", nullptr,
555 DL
.is
<DIExpression
*>() ? (Metadata
*)DL
.get
<DIExpression
*>()
556 : (Metadata
*)DL
.get
<DIVariable
*>(),
557 AS
.is
<DIExpression
*>() ? (Metadata
*)AS
.get
<DIExpression
*>()
558 : (Metadata
*)AS
.get
<DIVariable
*>(),
559 AL
.is
<DIExpression
*>() ? (Metadata
*)AL
.get
<DIExpression
*>()
560 : (Metadata
*)AL
.get
<DIVariable
*>(),
561 RK
.is
<DIExpression
*>() ? (Metadata
*)RK
.get
<DIExpression
*>()
562 : (Metadata
*)RK
.get
<DIVariable
*>());
563 trackIfUnresolved(R
);
567 DICompositeType
*DIBuilder::createVectorType(uint64_t Size
,
568 uint32_t AlignInBits
, DIType
*Ty
,
569 DINodeArray Subscripts
) {
570 auto *R
= DICompositeType::get(VMContext
, dwarf::DW_TAG_array_type
, "",
571 nullptr, 0, nullptr, Ty
, Size
, AlignInBits
, 0,
572 DINode::FlagVector
, Subscripts
, 0, nullptr);
573 trackIfUnresolved(R
);
577 DISubprogram
*DIBuilder::createArtificialSubprogram(DISubprogram
*SP
) {
578 auto NewSP
= SP
->cloneWithFlags(SP
->getFlags() | DINode::FlagArtificial
);
579 return MDNode::replaceWithDistinct(std::move(NewSP
));
582 static DIType
*createTypeWithFlags(const DIType
*Ty
,
583 DINode::DIFlags FlagsToSet
) {
584 auto NewTy
= Ty
->cloneWithFlags(Ty
->getFlags() | FlagsToSet
);
585 return MDNode::replaceWithUniqued(std::move(NewTy
));
588 DIType
*DIBuilder::createArtificialType(DIType
*Ty
) {
589 // FIXME: Restrict this to the nodes where it's valid.
590 if (Ty
->isArtificial())
592 return createTypeWithFlags(Ty
, DINode::FlagArtificial
);
595 DIType
*DIBuilder::createObjectPointerType(DIType
*Ty
) {
596 // FIXME: Restrict this to the nodes where it's valid.
597 if (Ty
->isObjectPointer())
599 DINode::DIFlags Flags
= DINode::FlagObjectPointer
| DINode::FlagArtificial
;
600 return createTypeWithFlags(Ty
, Flags
);
603 void DIBuilder::retainType(DIScope
*T
) {
604 assert(T
&& "Expected non-null type");
605 assert((isa
<DIType
>(T
) || (isa
<DISubprogram
>(T
) &&
606 cast
<DISubprogram
>(T
)->isDefinition() == false)) &&
607 "Expected type or subprogram declaration");
608 AllRetainTypes
.emplace_back(T
);
611 DIBasicType
*DIBuilder::createUnspecifiedParameter() { return nullptr; }
614 DIBuilder::createForwardDecl(unsigned Tag
, StringRef Name
, DIScope
*Scope
,
615 DIFile
*F
, unsigned Line
, unsigned RuntimeLang
,
616 uint64_t SizeInBits
, uint32_t AlignInBits
,
617 StringRef UniqueIdentifier
) {
618 // FIXME: Define in terms of createReplaceableForwardDecl() by calling
619 // replaceWithUniqued().
620 auto *RetTy
= DICompositeType::get(
621 VMContext
, Tag
, Name
, F
, Line
, getNonCompileUnitScope(Scope
), nullptr,
622 SizeInBits
, AlignInBits
, 0, DINode::FlagFwdDecl
, nullptr, RuntimeLang
,
623 nullptr, nullptr, UniqueIdentifier
);
624 trackIfUnresolved(RetTy
);
628 DICompositeType
*DIBuilder::createReplaceableCompositeType(
629 unsigned Tag
, StringRef Name
, DIScope
*Scope
, DIFile
*F
, unsigned Line
,
630 unsigned RuntimeLang
, uint64_t SizeInBits
, uint32_t AlignInBits
,
631 DINode::DIFlags Flags
, StringRef UniqueIdentifier
,
632 DINodeArray Annotations
) {
634 DICompositeType::getTemporary(
635 VMContext
, Tag
, Name
, F
, Line
, getNonCompileUnitScope(Scope
), nullptr,
636 SizeInBits
, AlignInBits
, 0, Flags
, nullptr, RuntimeLang
, nullptr,
637 nullptr, UniqueIdentifier
, nullptr, nullptr, nullptr, nullptr, nullptr,
640 trackIfUnresolved(RetTy
);
644 DINodeArray
DIBuilder::getOrCreateArray(ArrayRef
<Metadata
*> Elements
) {
645 return MDTuple::get(VMContext
, Elements
);
649 DIBuilder::getOrCreateMacroArray(ArrayRef
<Metadata
*> Elements
) {
650 return MDTuple::get(VMContext
, Elements
);
653 DITypeRefArray
DIBuilder::getOrCreateTypeArray(ArrayRef
<Metadata
*> Elements
) {
654 SmallVector
<llvm::Metadata
*, 16> Elts
;
655 for (unsigned i
= 0, e
= Elements
.size(); i
!= e
; ++i
) {
656 if (Elements
[i
] && isa
<MDNode
>(Elements
[i
]))
657 Elts
.push_back(cast
<DIType
>(Elements
[i
]));
659 Elts
.push_back(Elements
[i
]);
661 return DITypeRefArray(MDNode::get(VMContext
, Elts
));
664 DISubrange
*DIBuilder::getOrCreateSubrange(int64_t Lo
, int64_t Count
) {
665 auto *LB
= ConstantAsMetadata::get(
666 ConstantInt::getSigned(Type::getInt64Ty(VMContext
), Lo
));
667 auto *CountNode
= ConstantAsMetadata::get(
668 ConstantInt::getSigned(Type::getInt64Ty(VMContext
), Count
));
669 return DISubrange::get(VMContext
, CountNode
, LB
, nullptr, nullptr);
672 DISubrange
*DIBuilder::getOrCreateSubrange(int64_t Lo
, Metadata
*CountNode
) {
673 auto *LB
= ConstantAsMetadata::get(
674 ConstantInt::getSigned(Type::getInt64Ty(VMContext
), Lo
));
675 return DISubrange::get(VMContext
, CountNode
, LB
, nullptr, nullptr);
678 DISubrange
*DIBuilder::getOrCreateSubrange(Metadata
*CountNode
, Metadata
*LB
,
679 Metadata
*UB
, Metadata
*Stride
) {
680 return DISubrange::get(VMContext
, CountNode
, LB
, UB
, Stride
);
683 DIGenericSubrange
*DIBuilder::getOrCreateGenericSubrange(
684 DIGenericSubrange::BoundType CountNode
, DIGenericSubrange::BoundType LB
,
685 DIGenericSubrange::BoundType UB
, DIGenericSubrange::BoundType Stride
) {
686 auto ConvToMetadata
= [&](DIGenericSubrange::BoundType Bound
) -> Metadata
* {
687 return Bound
.is
<DIExpression
*>() ? (Metadata
*)Bound
.get
<DIExpression
*>()
688 : (Metadata
*)Bound
.get
<DIVariable
*>();
690 return DIGenericSubrange::get(VMContext
, ConvToMetadata(CountNode
),
691 ConvToMetadata(LB
), ConvToMetadata(UB
),
692 ConvToMetadata(Stride
));
695 static void checkGlobalVariableScope(DIScope
*Context
) {
698 dyn_cast_or_null
<DICompositeType
>(getNonCompileUnitScope(Context
)))
699 assert(CT
->getIdentifier().empty() &&
700 "Context of a global variable should not be a type with identifier");
704 DIGlobalVariableExpression
*DIBuilder::createGlobalVariableExpression(
705 DIScope
*Context
, StringRef Name
, StringRef LinkageName
, DIFile
*F
,
706 unsigned LineNumber
, DIType
*Ty
, bool IsLocalToUnit
,
707 bool isDefined
, DIExpression
*Expr
,
708 MDNode
*Decl
, MDTuple
*TemplateParams
, uint32_t AlignInBits
) {
709 checkGlobalVariableScope(Context
);
711 auto *GV
= DIGlobalVariable::getDistinct(
712 VMContext
, cast_or_null
<DIScope
>(Context
), Name
, LinkageName
, F
,
713 LineNumber
, Ty
, IsLocalToUnit
, isDefined
, cast_or_null
<DIDerivedType
>(Decl
),
714 TemplateParams
, AlignInBits
);
716 Expr
= createExpression();
717 auto *N
= DIGlobalVariableExpression::get(VMContext
, GV
, Expr
);
722 DIGlobalVariable
*DIBuilder::createTempGlobalVariableFwdDecl(
723 DIScope
*Context
, StringRef Name
, StringRef LinkageName
, DIFile
*F
,
724 unsigned LineNumber
, DIType
*Ty
, bool IsLocalToUnit
, MDNode
*Decl
,
725 MDTuple
*TemplateParams
, uint32_t AlignInBits
) {
726 checkGlobalVariableScope(Context
);
728 return DIGlobalVariable::getTemporary(
729 VMContext
, cast_or_null
<DIScope
>(Context
), Name
, LinkageName
, F
,
730 LineNumber
, Ty
, IsLocalToUnit
, false,
731 cast_or_null
<DIDerivedType
>(Decl
), TemplateParams
, AlignInBits
)
735 static DILocalVariable
*createLocalVariable(
736 LLVMContext
&VMContext
,
737 DenseMap
<MDNode
*, SmallVector
<TrackingMDNodeRef
, 1>> &PreservedVariables
,
738 DIScope
*Scope
, StringRef Name
, unsigned ArgNo
, DIFile
*File
,
739 unsigned LineNo
, DIType
*Ty
, bool AlwaysPreserve
, DINode::DIFlags Flags
,
740 uint32_t AlignInBits
) {
741 // FIXME: Why getNonCompileUnitScope()?
742 // FIXME: Why is "!Context" okay here?
743 // FIXME: Why doesn't this check for a subprogram or lexical block (AFAICT
744 // the only valid scopes)?
745 DIScope
*Context
= getNonCompileUnitScope(Scope
);
748 DILocalVariable::get(VMContext
, cast_or_null
<DILocalScope
>(Context
), Name
,
749 File
, LineNo
, Ty
, ArgNo
, Flags
, AlignInBits
);
750 if (AlwaysPreserve
) {
751 // The optimizer may remove local variables. If there is an interest
752 // to preserve variable info in such situation then stash it in a
754 DISubprogram
*Fn
= getDISubprogram(Scope
);
755 assert(Fn
&& "Missing subprogram for local variable");
756 PreservedVariables
[Fn
].emplace_back(Node
);
761 DILocalVariable
*DIBuilder::createAutoVariable(DIScope
*Scope
, StringRef Name
,
762 DIFile
*File
, unsigned LineNo
,
763 DIType
*Ty
, bool AlwaysPreserve
,
764 DINode::DIFlags Flags
,
765 uint32_t AlignInBits
) {
766 return createLocalVariable(VMContext
, PreservedVariables
, Scope
, Name
,
767 /* ArgNo */ 0, File
, LineNo
, Ty
, AlwaysPreserve
,
771 DILocalVariable
*DIBuilder::createParameterVariable(
772 DIScope
*Scope
, StringRef Name
, unsigned ArgNo
, DIFile
*File
,
773 unsigned LineNo
, DIType
*Ty
, bool AlwaysPreserve
, DINode::DIFlags Flags
) {
774 assert(ArgNo
&& "Expected non-zero argument number for parameter");
775 return createLocalVariable(VMContext
, PreservedVariables
, Scope
, Name
, ArgNo
,
776 File
, LineNo
, Ty
, AlwaysPreserve
, Flags
,
780 DILabel
*DIBuilder::createLabel(
781 DIScope
*Scope
, StringRef Name
, DIFile
*File
,
782 unsigned LineNo
, bool AlwaysPreserve
) {
783 DIScope
*Context
= getNonCompileUnitScope(Scope
);
786 DILabel::get(VMContext
, cast_or_null
<DILocalScope
>(Context
), Name
,
789 if (AlwaysPreserve
) {
790 /// The optimizer may remove labels. If there is an interest
791 /// to preserve label info in such situation then append it to
792 /// the list of retained nodes of the DISubprogram.
793 DISubprogram
*Fn
= getDISubprogram(Scope
);
794 assert(Fn
&& "Missing subprogram for label");
795 PreservedLabels
[Fn
].emplace_back(Node
);
800 DIExpression
*DIBuilder::createExpression(ArrayRef
<uint64_t> Addr
) {
801 return DIExpression::get(VMContext
, Addr
);
804 DIExpression
*DIBuilder::createExpression(ArrayRef
<int64_t> Signed
) {
805 // TODO: Remove the callers of this signed version and delete.
806 SmallVector
<uint64_t, 8> Addr(Signed
.begin(), Signed
.end());
807 return createExpression(Addr
);
810 template <class... Ts
>
811 static DISubprogram
*getSubprogram(bool IsDistinct
, Ts
&&... Args
) {
813 return DISubprogram::getDistinct(std::forward
<Ts
>(Args
)...);
814 return DISubprogram::get(std::forward
<Ts
>(Args
)...);
817 DISubprogram
*DIBuilder::createFunction(
818 DIScope
*Context
, StringRef Name
, StringRef LinkageName
, DIFile
*File
,
819 unsigned LineNo
, DISubroutineType
*Ty
, unsigned ScopeLine
,
820 DINode::DIFlags Flags
, DISubprogram::DISPFlags SPFlags
,
821 DITemplateParameterArray TParams
, DISubprogram
*Decl
,
822 DITypeArray ThrownTypes
) {
823 bool IsDefinition
= SPFlags
& DISubprogram::SPFlagDefinition
;
824 auto *Node
= getSubprogram(
825 /*IsDistinct=*/IsDefinition
, VMContext
, getNonCompileUnitScope(Context
),
826 Name
, LinkageName
, File
, LineNo
, Ty
, ScopeLine
, nullptr, 0, 0, Flags
,
827 SPFlags
, IsDefinition
? CUNode
: nullptr, TParams
, Decl
,
828 MDTuple::getTemporary(VMContext
, None
).release(), ThrownTypes
);
831 AllSubprograms
.push_back(Node
);
832 trackIfUnresolved(Node
);
836 DISubprogram
*DIBuilder::createTempFunctionFwdDecl(
837 DIScope
*Context
, StringRef Name
, StringRef LinkageName
, DIFile
*File
,
838 unsigned LineNo
, DISubroutineType
*Ty
, unsigned ScopeLine
,
839 DINode::DIFlags Flags
, DISubprogram::DISPFlags SPFlags
,
840 DITemplateParameterArray TParams
, DISubprogram
*Decl
,
841 DITypeArray ThrownTypes
) {
842 bool IsDefinition
= SPFlags
& DISubprogram::SPFlagDefinition
;
843 return DISubprogram::getTemporary(VMContext
, getNonCompileUnitScope(Context
),
844 Name
, LinkageName
, File
, LineNo
, Ty
,
845 ScopeLine
, nullptr, 0, 0, Flags
, SPFlags
,
846 IsDefinition
? CUNode
: nullptr, TParams
,
847 Decl
, nullptr, ThrownTypes
)
851 DISubprogram
*DIBuilder::createMethod(
852 DIScope
*Context
, StringRef Name
, StringRef LinkageName
, DIFile
*F
,
853 unsigned LineNo
, DISubroutineType
*Ty
, unsigned VIndex
, int ThisAdjustment
,
854 DIType
*VTableHolder
, DINode::DIFlags Flags
,
855 DISubprogram::DISPFlags SPFlags
, DITemplateParameterArray TParams
,
856 DITypeArray ThrownTypes
) {
857 assert(getNonCompileUnitScope(Context
) &&
858 "Methods should have both a Context and a context that isn't "
859 "the compile unit.");
860 // FIXME: Do we want to use different scope/lines?
861 bool IsDefinition
= SPFlags
& DISubprogram::SPFlagDefinition
;
862 auto *SP
= getSubprogram(
863 /*IsDistinct=*/IsDefinition
, VMContext
, cast
<DIScope
>(Context
), Name
,
864 LinkageName
, F
, LineNo
, Ty
, LineNo
, VTableHolder
, VIndex
, ThisAdjustment
,
865 Flags
, SPFlags
, IsDefinition
? CUNode
: nullptr, TParams
, nullptr,
866 nullptr, ThrownTypes
);
869 AllSubprograms
.push_back(SP
);
870 trackIfUnresolved(SP
);
874 DICommonBlock
*DIBuilder::createCommonBlock(
875 DIScope
*Scope
, DIGlobalVariable
*Decl
, StringRef Name
, DIFile
*File
,
877 return DICommonBlock::get(
878 VMContext
, Scope
, Decl
, Name
, File
, LineNo
);
881 DINamespace
*DIBuilder::createNameSpace(DIScope
*Scope
, StringRef Name
,
882 bool ExportSymbols
) {
884 // It is okay to *not* make anonymous top-level namespaces distinct, because
885 // all nodes that have an anonymous namespace as their parent scope are
886 // guaranteed to be unique and/or are linked to their containing
887 // DICompileUnit. This decision is an explicit tradeoff of link time versus
888 // memory usage versus code simplicity and may get revisited in the future.
889 return DINamespace::get(VMContext
, getNonCompileUnitScope(Scope
), Name
,
893 DIModule
*DIBuilder::createModule(DIScope
*Scope
, StringRef Name
,
894 StringRef ConfigurationMacros
,
895 StringRef IncludePath
, StringRef APINotesFile
,
896 DIFile
*File
, unsigned LineNo
, bool IsDecl
) {
897 return DIModule::get(VMContext
, File
, getNonCompileUnitScope(Scope
), Name
,
898 ConfigurationMacros
, IncludePath
, APINotesFile
, LineNo
,
902 DILexicalBlockFile
*DIBuilder::createLexicalBlockFile(DIScope
*Scope
,
904 unsigned Discriminator
) {
905 return DILexicalBlockFile::get(VMContext
, Scope
, File
, Discriminator
);
908 DILexicalBlock
*DIBuilder::createLexicalBlock(DIScope
*Scope
, DIFile
*File
,
909 unsigned Line
, unsigned Col
) {
910 // Make these distinct, to avoid merging two lexical blocks on the same
912 return DILexicalBlock::getDistinct(VMContext
, getNonCompileUnitScope(Scope
),
916 Instruction
*DIBuilder::insertDeclare(Value
*Storage
, DILocalVariable
*VarInfo
,
917 DIExpression
*Expr
, const DILocation
*DL
,
918 Instruction
*InsertBefore
) {
919 return insertDeclare(Storage
, VarInfo
, Expr
, DL
, InsertBefore
->getParent(),
923 Instruction
*DIBuilder::insertDeclare(Value
*Storage
, DILocalVariable
*VarInfo
,
924 DIExpression
*Expr
, const DILocation
*DL
,
925 BasicBlock
*InsertAtEnd
) {
926 // If this block already has a terminator then insert this intrinsic before
927 // the terminator. Otherwise, put it at the end of the block.
928 Instruction
*InsertBefore
= InsertAtEnd
->getTerminator();
929 return insertDeclare(Storage
, VarInfo
, Expr
, DL
, InsertAtEnd
, InsertBefore
);
932 Instruction
*DIBuilder::insertLabel(DILabel
*LabelInfo
, const DILocation
*DL
,
933 Instruction
*InsertBefore
) {
935 LabelInfo
, DL
, InsertBefore
? InsertBefore
->getParent() : nullptr,
939 Instruction
*DIBuilder::insertLabel(DILabel
*LabelInfo
, const DILocation
*DL
,
940 BasicBlock
*InsertAtEnd
) {
941 return insertLabel(LabelInfo
, DL
, InsertAtEnd
, nullptr);
944 Instruction
*DIBuilder::insertDbgValueIntrinsic(Value
*V
,
945 DILocalVariable
*VarInfo
,
947 const DILocation
*DL
,
948 Instruction
*InsertBefore
) {
949 return insertDbgValueIntrinsic(
950 V
, VarInfo
, Expr
, DL
, InsertBefore
? InsertBefore
->getParent() : nullptr,
954 Instruction
*DIBuilder::insertDbgValueIntrinsic(Value
*V
,
955 DILocalVariable
*VarInfo
,
957 const DILocation
*DL
,
958 BasicBlock
*InsertAtEnd
) {
959 return insertDbgValueIntrinsic(V
, VarInfo
, Expr
, DL
, InsertAtEnd
, nullptr);
962 /// Initialize IRBuilder for inserting dbg.declare and dbg.value intrinsics.
963 /// This abstracts over the various ways to specify an insert position.
964 static void initIRBuilder(IRBuilder
<> &Builder
, const DILocation
*DL
,
965 BasicBlock
*InsertBB
, Instruction
*InsertBefore
) {
967 Builder
.SetInsertPoint(InsertBefore
);
969 Builder
.SetInsertPoint(InsertBB
);
970 Builder
.SetCurrentDebugLocation(DL
);
973 static Value
*getDbgIntrinsicValueImpl(LLVMContext
&VMContext
, Value
*V
) {
974 assert(V
&& "no value passed to dbg intrinsic");
975 return MetadataAsValue::get(VMContext
, ValueAsMetadata::get(V
));
978 static Function
*getDeclareIntrin(Module
&M
) {
979 return Intrinsic::getDeclaration(&M
, UseDbgAddr
? Intrinsic::dbg_addr
980 : Intrinsic::dbg_declare
);
983 Instruction
*DIBuilder::insertDeclare(Value
*Storage
, DILocalVariable
*VarInfo
,
984 DIExpression
*Expr
, const DILocation
*DL
,
985 BasicBlock
*InsertBB
, Instruction
*InsertBefore
) {
986 assert(VarInfo
&& "empty or invalid DILocalVariable* passed to dbg.declare");
987 assert(DL
&& "Expected debug loc");
988 assert(DL
->getScope()->getSubprogram() ==
989 VarInfo
->getScope()->getSubprogram() &&
990 "Expected matching subprograms");
992 DeclareFn
= getDeclareIntrin(M
);
994 trackIfUnresolved(VarInfo
);
995 trackIfUnresolved(Expr
);
996 Value
*Args
[] = {getDbgIntrinsicValueImpl(VMContext
, Storage
),
997 MetadataAsValue::get(VMContext
, VarInfo
),
998 MetadataAsValue::get(VMContext
, Expr
)};
1000 IRBuilder
<> B(DL
->getContext());
1001 initIRBuilder(B
, DL
, InsertBB
, InsertBefore
);
1002 return B
.CreateCall(DeclareFn
, Args
);
1005 Instruction
*DIBuilder::insertDbgValueIntrinsic(
1006 Value
*V
, DILocalVariable
*VarInfo
, DIExpression
*Expr
,
1007 const DILocation
*DL
, BasicBlock
*InsertBB
, Instruction
*InsertBefore
) {
1008 assert(V
&& "no value passed to dbg.value");
1009 assert(VarInfo
&& "empty or invalid DILocalVariable* passed to dbg.value");
1010 assert(DL
&& "Expected debug loc");
1011 assert(DL
->getScope()->getSubprogram() ==
1012 VarInfo
->getScope()->getSubprogram() &&
1013 "Expected matching subprograms");
1015 ValueFn
= Intrinsic::getDeclaration(&M
, Intrinsic::dbg_value
);
1017 trackIfUnresolved(VarInfo
);
1018 trackIfUnresolved(Expr
);
1019 Value
*Args
[] = {getDbgIntrinsicValueImpl(VMContext
, V
),
1020 MetadataAsValue::get(VMContext
, VarInfo
),
1021 MetadataAsValue::get(VMContext
, Expr
)};
1023 IRBuilder
<> B(DL
->getContext());
1024 initIRBuilder(B
, DL
, InsertBB
, InsertBefore
);
1025 return B
.CreateCall(ValueFn
, Args
);
1028 Instruction
*DIBuilder::insertLabel(
1029 DILabel
*LabelInfo
, const DILocation
*DL
,
1030 BasicBlock
*InsertBB
, Instruction
*InsertBefore
) {
1031 assert(LabelInfo
&& "empty or invalid DILabel* passed to dbg.label");
1032 assert(DL
&& "Expected debug loc");
1033 assert(DL
->getScope()->getSubprogram() ==
1034 LabelInfo
->getScope()->getSubprogram() &&
1035 "Expected matching subprograms");
1037 LabelFn
= Intrinsic::getDeclaration(&M
, Intrinsic::dbg_label
);
1039 trackIfUnresolved(LabelInfo
);
1040 Value
*Args
[] = {MetadataAsValue::get(VMContext
, LabelInfo
)};
1042 IRBuilder
<> B(DL
->getContext());
1043 initIRBuilder(B
, DL
, InsertBB
, InsertBefore
);
1044 return B
.CreateCall(LabelFn
, Args
);
1047 void DIBuilder::replaceVTableHolder(DICompositeType
*&T
,
1048 DIType
*VTableHolder
) {
1050 TypedTrackingMDRef
<DICompositeType
> N(T
);
1051 N
->replaceVTableHolder(VTableHolder
);
1055 // If this didn't create a self-reference, just return.
1056 if (T
!= VTableHolder
)
1059 // Look for unresolved operands. T will drop RAUW support, orphaning any
1060 // cycles underneath it.
1061 if (T
->isResolved())
1062 for (const MDOperand
&O
: T
->operands())
1063 if (auto *N
= dyn_cast_or_null
<MDNode
>(O
))
1064 trackIfUnresolved(N
);
1067 void DIBuilder::replaceArrays(DICompositeType
*&T
, DINodeArray Elements
,
1068 DINodeArray TParams
) {
1070 TypedTrackingMDRef
<DICompositeType
> N(T
);
1072 N
->replaceElements(Elements
);
1074 N
->replaceTemplateParams(DITemplateParameterArray(TParams
));
1078 // If T isn't resolved, there's no problem.
1079 if (!T
->isResolved())
1082 // If T is resolved, it may be due to a self-reference cycle. Track the
1083 // arrays explicitly if they're unresolved, or else the cycles will be
1086 trackIfUnresolved(Elements
.get());
1088 trackIfUnresolved(TParams
.get());