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/APInt.h"
16 #include "llvm/ADT/APSInt.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/Module.h"
22 #include "llvm/Support/CommandLine.h"
26 using namespace llvm::dwarf
;
28 DIBuilder::DIBuilder(Module
&m
, bool AllowUnresolvedNodes
, DICompileUnit
*CU
)
29 : M(m
), VMContext(M
.getContext()), CUNode(CU
), DeclareFn(nullptr),
30 ValueFn(nullptr), LabelFn(nullptr), AssignFn(nullptr),
31 AllowUnresolvedNodes(AllowUnresolvedNodes
) {
33 if (const auto &ETs
= CUNode
->getEnumTypes())
34 EnumTypes
.assign(ETs
.begin(), ETs
.end());
35 if (const auto &RTs
= CUNode
->getRetainedTypes())
36 AllRetainTypes
.assign(RTs
.begin(), RTs
.end());
37 if (const auto &GVs
= CUNode
->getGlobalVariables())
38 AllGVs
.assign(GVs
.begin(), GVs
.end());
39 if (const auto &IMs
= CUNode
->getImportedEntities())
40 ImportedModules
.assign(IMs
.begin(), IMs
.end());
41 if (const auto &MNs
= CUNode
->getMacros())
42 AllMacrosPerParent
.insert({nullptr, {MNs
.begin(), MNs
.end()}});
46 void DIBuilder::trackIfUnresolved(MDNode
*N
) {
52 assert(AllowUnresolvedNodes
&& "Cannot handle unresolved nodes");
53 UnresolvedNodes
.emplace_back(N
);
56 void DIBuilder::finalizeSubprogram(DISubprogram
*SP
) {
57 auto PN
= SubprogramTrackedNodes
.find(SP
);
58 if (PN
!= SubprogramTrackedNodes
.end())
59 SP
->replaceRetainedNodes(
60 MDTuple::get(VMContext
, SmallVector
<Metadata
*, 16>(PN
->second
.begin(),
64 void DIBuilder::finalize() {
66 assert(!AllowUnresolvedNodes
&&
67 "creating type nodes without a CU is not supported");
71 if (!EnumTypes
.empty())
72 CUNode
->replaceEnumTypes(MDTuple::get(
73 VMContext
, SmallVector
<Metadata
*, 16>(EnumTypes
.begin(),
76 SmallVector
<Metadata
*, 16> RetainValues
;
77 // Declarations and definitions of the same type may be retained. Some
78 // clients RAUW these pairs, leaving duplicates in the retained types
79 // list. Use a set to remove the duplicates while we transform the
80 // TrackingVHs back into Values.
81 SmallPtrSet
<Metadata
*, 16> RetainSet
;
82 for (unsigned I
= 0, E
= AllRetainTypes
.size(); I
< E
; I
++)
83 if (RetainSet
.insert(AllRetainTypes
[I
]).second
)
84 RetainValues
.push_back(AllRetainTypes
[I
]);
86 if (!RetainValues
.empty())
87 CUNode
->replaceRetainedTypes(MDTuple::get(VMContext
, RetainValues
));
89 for (auto *SP
: AllSubprograms
)
90 finalizeSubprogram(SP
);
91 for (auto *N
: RetainValues
)
92 if (auto *SP
= dyn_cast
<DISubprogram
>(N
))
93 finalizeSubprogram(SP
);
96 CUNode
->replaceGlobalVariables(MDTuple::get(VMContext
, AllGVs
));
98 if (!ImportedModules
.empty())
99 CUNode
->replaceImportedEntities(MDTuple::get(
100 VMContext
, SmallVector
<Metadata
*, 16>(ImportedModules
.begin(),
101 ImportedModules
.end())));
103 for (const auto &I
: AllMacrosPerParent
) {
104 // DIMacroNode's with nullptr parent are DICompileUnit direct children.
106 CUNode
->replaceMacros(MDTuple::get(VMContext
, I
.second
.getArrayRef()));
109 // Otherwise, it must be a temporary DIMacroFile that need to be resolved.
110 auto *TMF
= cast
<DIMacroFile
>(I
.first
);
111 auto *MF
= DIMacroFile::get(VMContext
, dwarf::DW_MACINFO_start_file
,
112 TMF
->getLine(), TMF
->getFile(),
113 getOrCreateMacroArray(I
.second
.getArrayRef()));
114 replaceTemporary(llvm::TempDIMacroNode(TMF
), MF
);
117 // Now that all temp nodes have been replaced or deleted, resolve remaining
119 for (const auto &N
: UnresolvedNodes
)
120 if (N
&& !N
->isResolved())
122 UnresolvedNodes
.clear();
124 // Can't handle unresolved nodes anymore.
125 AllowUnresolvedNodes
= false;
128 /// If N is compile unit return NULL otherwise return N.
129 static DIScope
*getNonCompileUnitScope(DIScope
*N
) {
130 if (!N
|| isa
<DICompileUnit
>(N
))
132 return cast
<DIScope
>(N
);
135 DICompileUnit
*DIBuilder::createCompileUnit(
136 unsigned Lang
, DIFile
*File
, StringRef Producer
, bool isOptimized
,
137 StringRef Flags
, unsigned RunTimeVer
, StringRef SplitName
,
138 DICompileUnit::DebugEmissionKind Kind
, uint64_t DWOId
,
139 bool SplitDebugInlining
, bool DebugInfoForProfiling
,
140 DICompileUnit::DebugNameTableKind NameTableKind
, bool RangesBaseAddress
,
141 StringRef SysRoot
, StringRef SDK
) {
143 assert(((Lang
<= dwarf::DW_LANG_Mojo
&& Lang
>= dwarf::DW_LANG_C89
) ||
144 (Lang
<= dwarf::DW_LANG_hi_user
&& Lang
>= dwarf::DW_LANG_lo_user
)) &&
145 "Invalid Language tag");
147 assert(!CUNode
&& "Can only make one compile unit per DIBuilder instance");
148 CUNode
= DICompileUnit::getDistinct(
149 VMContext
, Lang
, File
, Producer
, isOptimized
, Flags
, RunTimeVer
,
150 SplitName
, Kind
, nullptr, nullptr, nullptr, nullptr, nullptr, DWOId
,
151 SplitDebugInlining
, DebugInfoForProfiling
, NameTableKind
,
152 RangesBaseAddress
, SysRoot
, SDK
);
154 // Create a named metadata so that it is easier to find cu in a module.
155 NamedMDNode
*NMD
= M
.getOrInsertNamedMetadata("llvm.dbg.cu");
156 NMD
->addOperand(CUNode
);
157 trackIfUnresolved(CUNode
);
161 static DIImportedEntity
*
162 createImportedModule(LLVMContext
&C
, dwarf::Tag Tag
, DIScope
*Context
,
163 Metadata
*NS
, DIFile
*File
, unsigned Line
, StringRef Name
,
164 DINodeArray Elements
,
165 SmallVectorImpl
<TrackingMDNodeRef
> &ImportedModules
) {
167 assert(File
&& "Source location has line number but no file");
168 unsigned EntitiesCount
= C
.pImpl
->DIImportedEntitys
.size();
169 auto *M
= DIImportedEntity::get(C
, Tag
, Context
, cast_or_null
<DINode
>(NS
),
170 File
, Line
, Name
, Elements
);
171 if (EntitiesCount
< C
.pImpl
->DIImportedEntitys
.size())
172 // A new Imported Entity was just added to the context.
173 // Add it to the Imported Modules list.
174 ImportedModules
.emplace_back(M
);
178 DIImportedEntity
*DIBuilder::createImportedModule(DIScope
*Context
,
179 DINamespace
*NS
, DIFile
*File
,
181 DINodeArray Elements
) {
182 return ::createImportedModule(VMContext
, dwarf::DW_TAG_imported_module
,
183 Context
, NS
, File
, Line
, StringRef(), Elements
,
184 getImportTrackingVector(Context
));
187 DIImportedEntity
*DIBuilder::createImportedModule(DIScope
*Context
,
188 DIImportedEntity
*NS
,
189 DIFile
*File
, unsigned Line
,
190 DINodeArray Elements
) {
191 return ::createImportedModule(VMContext
, dwarf::DW_TAG_imported_module
,
192 Context
, NS
, File
, Line
, StringRef(), Elements
,
193 getImportTrackingVector(Context
));
196 DIImportedEntity
*DIBuilder::createImportedModule(DIScope
*Context
, DIModule
*M
,
197 DIFile
*File
, unsigned Line
,
198 DINodeArray Elements
) {
199 return ::createImportedModule(VMContext
, dwarf::DW_TAG_imported_module
,
200 Context
, M
, File
, Line
, StringRef(), Elements
,
201 getImportTrackingVector(Context
));
205 DIBuilder::createImportedDeclaration(DIScope
*Context
, DINode
*Decl
,
206 DIFile
*File
, unsigned Line
,
207 StringRef Name
, DINodeArray Elements
) {
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
, Elements
,
212 getImportTrackingVector(Context
));
215 DIFile
*DIBuilder::createFile(StringRef Filename
, StringRef Directory
,
216 std::optional
<DIFile::ChecksumInfo
<StringRef
>> CS
,
217 std::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
, const 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 DIStringType
*DIBuilder::createStringType(StringRef Name
,
282 DIVariable
*StringLength
,
283 DIExpression
*StrLocationExp
) {
284 assert(!Name
.empty() && "Unable to create type without name");
285 return DIStringType::get(VMContext
, dwarf::DW_TAG_string_type
, Name
,
286 StringLength
, nullptr, StrLocationExp
, 0, 0, 0);
289 DIStringType
*DIBuilder::createStringType(StringRef Name
,
290 DIExpression
*StringLengthExp
,
291 DIExpression
*StrLocationExp
) {
292 assert(!Name
.empty() && "Unable to create type without name");
293 return DIStringType::get(VMContext
, dwarf::DW_TAG_string_type
, Name
, nullptr,
294 StringLengthExp
, StrLocationExp
, 0, 0, 0);
297 DIDerivedType
*DIBuilder::createQualifiedType(unsigned Tag
, DIType
*FromTy
) {
298 return DIDerivedType::get(VMContext
, Tag
, "", nullptr, 0, nullptr, FromTy
, 0,
299 0, 0, std::nullopt
, DINode::FlagZero
);
303 DIBuilder::createPointerType(DIType
*PointeeTy
, uint64_t SizeInBits
,
304 uint32_t AlignInBits
,
305 std::optional
<unsigned> DWARFAddressSpace
,
306 StringRef Name
, DINodeArray Annotations
) {
307 // FIXME: Why is there a name here?
308 return DIDerivedType::get(VMContext
, dwarf::DW_TAG_pointer_type
, Name
,
309 nullptr, 0, nullptr, PointeeTy
, SizeInBits
,
310 AlignInBits
, 0, DWARFAddressSpace
, DINode::FlagZero
,
311 nullptr, Annotations
);
314 DIDerivedType
*DIBuilder::createMemberPointerType(DIType
*PointeeTy
,
317 uint32_t AlignInBits
,
318 DINode::DIFlags Flags
) {
319 return DIDerivedType::get(VMContext
, dwarf::DW_TAG_ptr_to_member_type
, "",
320 nullptr, 0, nullptr, PointeeTy
, SizeInBits
,
321 AlignInBits
, 0, std::nullopt
, Flags
, Base
);
325 DIBuilder::createReferenceType(unsigned Tag
, DIType
*RTy
, uint64_t SizeInBits
,
326 uint32_t AlignInBits
,
327 std::optional
<unsigned> DWARFAddressSpace
) {
328 assert(RTy
&& "Unable to create reference type");
329 return DIDerivedType::get(VMContext
, Tag
, "", nullptr, 0, nullptr, RTy
,
330 SizeInBits
, AlignInBits
, 0, DWARFAddressSpace
,
334 DIDerivedType
*DIBuilder::createTypedef(DIType
*Ty
, StringRef Name
,
335 DIFile
*File
, unsigned LineNo
,
336 DIScope
*Context
, uint32_t AlignInBits
,
337 DINode::DIFlags Flags
,
338 DINodeArray Annotations
) {
340 DIDerivedType::get(VMContext
, dwarf::DW_TAG_typedef
, Name
, File
, LineNo
,
341 getNonCompileUnitScope(Context
), Ty
, 0, AlignInBits
, 0,
342 std::nullopt
, Flags
, nullptr, Annotations
);
343 if (isa_and_nonnull
<DILocalScope
>(Context
))
344 getSubprogramNodesTrackingVector(Context
).emplace_back(T
);
348 DIDerivedType
*DIBuilder::createFriend(DIType
*Ty
, DIType
*FriendTy
) {
349 assert(Ty
&& "Invalid type!");
350 assert(FriendTy
&& "Invalid friend type!");
351 return DIDerivedType::get(VMContext
, dwarf::DW_TAG_friend
, "", nullptr, 0, Ty
,
352 FriendTy
, 0, 0, 0, std::nullopt
, DINode::FlagZero
);
355 DIDerivedType
*DIBuilder::createInheritance(DIType
*Ty
, DIType
*BaseTy
,
357 uint32_t VBPtrOffset
,
358 DINode::DIFlags Flags
) {
359 assert(Ty
&& "Unable to create inheritance");
360 Metadata
*ExtraData
= ConstantAsMetadata::get(
361 ConstantInt::get(IntegerType::get(VMContext
, 32), VBPtrOffset
));
362 return DIDerivedType::get(VMContext
, dwarf::DW_TAG_inheritance
, "", nullptr,
363 0, Ty
, BaseTy
, 0, 0, BaseOffset
, std::nullopt
,
367 DIDerivedType
*DIBuilder::createMemberType(
368 DIScope
*Scope
, StringRef Name
, DIFile
*File
, unsigned LineNumber
,
369 uint64_t SizeInBits
, uint32_t AlignInBits
, uint64_t OffsetInBits
,
370 DINode::DIFlags Flags
, DIType
*Ty
, DINodeArray Annotations
) {
371 return DIDerivedType::get(VMContext
, dwarf::DW_TAG_member
, Name
, File
,
372 LineNumber
, getNonCompileUnitScope(Scope
), Ty
,
373 SizeInBits
, AlignInBits
, OffsetInBits
, std::nullopt
,
374 Flags
, nullptr, Annotations
);
377 static ConstantAsMetadata
*getConstantOrNull(Constant
*C
) {
379 return ConstantAsMetadata::get(C
);
383 DIDerivedType
*DIBuilder::createVariantMemberType(
384 DIScope
*Scope
, StringRef Name
, DIFile
*File
, unsigned LineNumber
,
385 uint64_t SizeInBits
, uint32_t AlignInBits
, uint64_t OffsetInBits
,
386 Constant
*Discriminant
, DINode::DIFlags Flags
, DIType
*Ty
) {
387 return DIDerivedType::get(VMContext
, dwarf::DW_TAG_member
, Name
, File
,
388 LineNumber
, getNonCompileUnitScope(Scope
), Ty
,
389 SizeInBits
, AlignInBits
, OffsetInBits
, std::nullopt
,
390 Flags
, getConstantOrNull(Discriminant
));
393 DIDerivedType
*DIBuilder::createBitFieldMemberType(
394 DIScope
*Scope
, StringRef Name
, DIFile
*File
, unsigned LineNumber
,
395 uint64_t SizeInBits
, uint64_t OffsetInBits
, uint64_t StorageOffsetInBits
,
396 DINode::DIFlags Flags
, DIType
*Ty
, DINodeArray Annotations
) {
397 Flags
|= DINode::FlagBitField
;
398 return DIDerivedType::get(
399 VMContext
, dwarf::DW_TAG_member
, Name
, File
, LineNumber
,
400 getNonCompileUnitScope(Scope
), Ty
, SizeInBits
, /*AlignInBits=*/0,
401 OffsetInBits
, std::nullopt
, Flags
,
402 ConstantAsMetadata::get(ConstantInt::get(IntegerType::get(VMContext
, 64),
403 StorageOffsetInBits
)),
408 DIBuilder::createStaticMemberType(DIScope
*Scope
, StringRef Name
, DIFile
*File
,
409 unsigned LineNumber
, DIType
*Ty
,
410 DINode::DIFlags Flags
, llvm::Constant
*Val
,
411 uint32_t AlignInBits
) {
412 Flags
|= DINode::FlagStaticMember
;
413 return DIDerivedType::get(VMContext
, dwarf::DW_TAG_member
, Name
, File
,
414 LineNumber
, getNonCompileUnitScope(Scope
), Ty
, 0,
415 AlignInBits
, 0, std::nullopt
, Flags
,
416 getConstantOrNull(Val
));
420 DIBuilder::createObjCIVar(StringRef Name
, DIFile
*File
, unsigned LineNumber
,
421 uint64_t SizeInBits
, uint32_t AlignInBits
,
422 uint64_t OffsetInBits
, DINode::DIFlags Flags
,
423 DIType
*Ty
, MDNode
*PropertyNode
) {
424 return DIDerivedType::get(VMContext
, dwarf::DW_TAG_member
, Name
, File
,
425 LineNumber
, getNonCompileUnitScope(File
), Ty
,
426 SizeInBits
, AlignInBits
, OffsetInBits
, std::nullopt
,
427 Flags
, PropertyNode
);
431 DIBuilder::createObjCProperty(StringRef Name
, DIFile
*File
, unsigned LineNumber
,
432 StringRef GetterName
, StringRef SetterName
,
433 unsigned PropertyAttributes
, DIType
*Ty
) {
434 return DIObjCProperty::get(VMContext
, Name
, File
, LineNumber
, GetterName
,
435 SetterName
, PropertyAttributes
, Ty
);
438 DITemplateTypeParameter
*
439 DIBuilder::createTemplateTypeParameter(DIScope
*Context
, StringRef Name
,
440 DIType
*Ty
, bool isDefault
) {
441 assert((!Context
|| isa
<DICompileUnit
>(Context
)) && "Expected compile unit");
442 return DITemplateTypeParameter::get(VMContext
, Name
, Ty
, isDefault
);
445 static DITemplateValueParameter
*
446 createTemplateValueParameterHelper(LLVMContext
&VMContext
, unsigned Tag
,
447 DIScope
*Context
, StringRef Name
, DIType
*Ty
,
448 bool IsDefault
, Metadata
*MD
) {
449 assert((!Context
|| isa
<DICompileUnit
>(Context
)) && "Expected compile unit");
450 return DITemplateValueParameter::get(VMContext
, Tag
, Name
, Ty
, IsDefault
, MD
);
453 DITemplateValueParameter
*
454 DIBuilder::createTemplateValueParameter(DIScope
*Context
, StringRef Name
,
455 DIType
*Ty
, bool isDefault
,
457 return createTemplateValueParameterHelper(
458 VMContext
, dwarf::DW_TAG_template_value_parameter
, Context
, Name
, Ty
,
459 isDefault
, getConstantOrNull(Val
));
462 DITemplateValueParameter
*
463 DIBuilder::createTemplateTemplateParameter(DIScope
*Context
, StringRef Name
,
464 DIType
*Ty
, StringRef Val
,
466 return createTemplateValueParameterHelper(
467 VMContext
, dwarf::DW_TAG_GNU_template_template_param
, Context
, Name
, Ty
,
468 IsDefault
, MDString::get(VMContext
, Val
));
471 DITemplateValueParameter
*
472 DIBuilder::createTemplateParameterPack(DIScope
*Context
, StringRef Name
,
473 DIType
*Ty
, DINodeArray Val
) {
474 return createTemplateValueParameterHelper(
475 VMContext
, dwarf::DW_TAG_GNU_template_parameter_pack
, Context
, Name
, Ty
,
479 DICompositeType
*DIBuilder::createClassType(
480 DIScope
*Context
, StringRef Name
, DIFile
*File
, unsigned LineNumber
,
481 uint64_t SizeInBits
, uint32_t AlignInBits
, uint64_t OffsetInBits
,
482 DINode::DIFlags Flags
, DIType
*DerivedFrom
, DINodeArray Elements
,
483 DIType
*VTableHolder
, MDNode
*TemplateParams
, StringRef UniqueIdentifier
) {
484 assert((!Context
|| isa
<DIScope
>(Context
)) &&
485 "createClassType should be called with a valid Context");
487 auto *R
= DICompositeType::get(
488 VMContext
, dwarf::DW_TAG_structure_type
, Name
, File
, LineNumber
,
489 getNonCompileUnitScope(Context
), DerivedFrom
, SizeInBits
, AlignInBits
,
490 OffsetInBits
, Flags
, Elements
, 0, VTableHolder
,
491 cast_or_null
<MDTuple
>(TemplateParams
), UniqueIdentifier
);
492 trackIfUnresolved(R
);
493 if (isa_and_nonnull
<DILocalScope
>(Context
))
494 getSubprogramNodesTrackingVector(Context
).emplace_back(R
);
498 DICompositeType
*DIBuilder::createStructType(
499 DIScope
*Context
, StringRef Name
, DIFile
*File
, unsigned LineNumber
,
500 uint64_t SizeInBits
, uint32_t AlignInBits
, DINode::DIFlags Flags
,
501 DIType
*DerivedFrom
, DINodeArray Elements
, unsigned RunTimeLang
,
502 DIType
*VTableHolder
, StringRef UniqueIdentifier
) {
503 auto *R
= DICompositeType::get(
504 VMContext
, dwarf::DW_TAG_structure_type
, Name
, File
, LineNumber
,
505 getNonCompileUnitScope(Context
), DerivedFrom
, SizeInBits
, AlignInBits
, 0,
506 Flags
, Elements
, RunTimeLang
, VTableHolder
, nullptr, UniqueIdentifier
);
507 trackIfUnresolved(R
);
508 if (isa_and_nonnull
<DILocalScope
>(Context
))
509 getSubprogramNodesTrackingVector(Context
).emplace_back(R
);
513 DICompositeType
*DIBuilder::createUnionType(
514 DIScope
*Scope
, StringRef Name
, DIFile
*File
, unsigned LineNumber
,
515 uint64_t SizeInBits
, uint32_t AlignInBits
, DINode::DIFlags Flags
,
516 DINodeArray Elements
, unsigned RunTimeLang
, StringRef UniqueIdentifier
) {
517 auto *R
= DICompositeType::get(
518 VMContext
, dwarf::DW_TAG_union_type
, Name
, File
, LineNumber
,
519 getNonCompileUnitScope(Scope
), nullptr, SizeInBits
, AlignInBits
, 0, Flags
,
520 Elements
, RunTimeLang
, nullptr, nullptr, UniqueIdentifier
);
521 trackIfUnresolved(R
);
522 if (isa_and_nonnull
<DILocalScope
>(Scope
))
523 getSubprogramNodesTrackingVector(Scope
).emplace_back(R
);
528 DIBuilder::createVariantPart(DIScope
*Scope
, StringRef Name
, DIFile
*File
,
529 unsigned LineNumber
, uint64_t SizeInBits
,
530 uint32_t AlignInBits
, DINode::DIFlags Flags
,
531 DIDerivedType
*Discriminator
, DINodeArray Elements
,
532 StringRef UniqueIdentifier
) {
533 auto *R
= DICompositeType::get(
534 VMContext
, dwarf::DW_TAG_variant_part
, Name
, File
, LineNumber
,
535 getNonCompileUnitScope(Scope
), nullptr, SizeInBits
, AlignInBits
, 0, Flags
,
536 Elements
, 0, nullptr, nullptr, UniqueIdentifier
, Discriminator
);
537 trackIfUnresolved(R
);
541 DISubroutineType
*DIBuilder::createSubroutineType(DITypeRefArray ParameterTypes
,
542 DINode::DIFlags Flags
,
544 return DISubroutineType::get(VMContext
, Flags
, CC
, ParameterTypes
);
547 DICompositeType
*DIBuilder::createEnumerationType(
548 DIScope
*Scope
, StringRef Name
, DIFile
*File
, unsigned LineNumber
,
549 uint64_t SizeInBits
, uint32_t AlignInBits
, DINodeArray Elements
,
550 DIType
*UnderlyingType
, StringRef UniqueIdentifier
, bool IsScoped
) {
551 auto *CTy
= DICompositeType::get(
552 VMContext
, dwarf::DW_TAG_enumeration_type
, Name
, File
, LineNumber
,
553 getNonCompileUnitScope(Scope
), UnderlyingType
, SizeInBits
, AlignInBits
, 0,
554 IsScoped
? DINode::FlagEnumClass
: DINode::FlagZero
, Elements
, 0, nullptr,
555 nullptr, UniqueIdentifier
);
556 if (isa_and_nonnull
<DILocalScope
>(Scope
))
557 getSubprogramNodesTrackingVector(Scope
).emplace_back(CTy
);
559 EnumTypes
.emplace_back(CTy
);
560 trackIfUnresolved(CTy
);
564 DIDerivedType
*DIBuilder::createSetType(DIScope
*Scope
, StringRef Name
,
565 DIFile
*File
, unsigned LineNo
,
567 uint32_t AlignInBits
, DIType
*Ty
) {
569 DIDerivedType::get(VMContext
, dwarf::DW_TAG_set_type
, Name
, File
, LineNo
,
570 getNonCompileUnitScope(Scope
), Ty
, SizeInBits
,
571 AlignInBits
, 0, std::nullopt
, DINode::FlagZero
);
572 trackIfUnresolved(R
);
577 DIBuilder::createArrayType(uint64_t Size
, uint32_t AlignInBits
, DIType
*Ty
,
578 DINodeArray Subscripts
,
579 PointerUnion
<DIExpression
*, DIVariable
*> DL
,
580 PointerUnion
<DIExpression
*, DIVariable
*> AS
,
581 PointerUnion
<DIExpression
*, DIVariable
*> AL
,
582 PointerUnion
<DIExpression
*, DIVariable
*> RK
) {
583 auto *R
= DICompositeType::get(
584 VMContext
, dwarf::DW_TAG_array_type
, "", nullptr, 0, nullptr, Ty
, Size
,
585 AlignInBits
, 0, DINode::FlagZero
, Subscripts
, 0, nullptr, nullptr, "",
587 isa
<DIExpression
*>(DL
) ? (Metadata
*)cast
<DIExpression
*>(DL
)
588 : (Metadata
*)cast
<DIVariable
*>(DL
),
589 isa
<DIExpression
*>(AS
) ? (Metadata
*)cast
<DIExpression
*>(AS
)
590 : (Metadata
*)cast
<DIVariable
*>(AS
),
591 isa
<DIExpression
*>(AL
) ? (Metadata
*)cast
<DIExpression
*>(AL
)
592 : (Metadata
*)cast
<DIVariable
*>(AL
),
593 isa
<DIExpression
*>(RK
) ? (Metadata
*)cast
<DIExpression
*>(RK
)
594 : (Metadata
*)cast
<DIVariable
*>(RK
));
595 trackIfUnresolved(R
);
599 DICompositeType
*DIBuilder::createVectorType(uint64_t Size
,
600 uint32_t AlignInBits
, DIType
*Ty
,
601 DINodeArray Subscripts
) {
602 auto *R
= DICompositeType::get(VMContext
, dwarf::DW_TAG_array_type
, "",
603 nullptr, 0, nullptr, Ty
, Size
, AlignInBits
, 0,
604 DINode::FlagVector
, Subscripts
, 0, nullptr);
605 trackIfUnresolved(R
);
609 DISubprogram
*DIBuilder::createArtificialSubprogram(DISubprogram
*SP
) {
610 auto NewSP
= SP
->cloneWithFlags(SP
->getFlags() | DINode::FlagArtificial
);
611 return MDNode::replaceWithDistinct(std::move(NewSP
));
614 static DIType
*createTypeWithFlags(const DIType
*Ty
,
615 DINode::DIFlags FlagsToSet
) {
616 auto NewTy
= Ty
->cloneWithFlags(Ty
->getFlags() | FlagsToSet
);
617 return MDNode::replaceWithUniqued(std::move(NewTy
));
620 DIType
*DIBuilder::createArtificialType(DIType
*Ty
) {
621 // FIXME: Restrict this to the nodes where it's valid.
622 if (Ty
->isArtificial())
624 return createTypeWithFlags(Ty
, DINode::FlagArtificial
);
627 DIType
*DIBuilder::createObjectPointerType(DIType
*Ty
) {
628 // FIXME: Restrict this to the nodes where it's valid.
629 if (Ty
->isObjectPointer())
631 DINode::DIFlags Flags
= DINode::FlagObjectPointer
| DINode::FlagArtificial
;
632 return createTypeWithFlags(Ty
, Flags
);
635 void DIBuilder::retainType(DIScope
*T
) {
636 assert(T
&& "Expected non-null type");
637 assert((isa
<DIType
>(T
) || (isa
<DISubprogram
>(T
) &&
638 cast
<DISubprogram
>(T
)->isDefinition() == false)) &&
639 "Expected type or subprogram declaration");
640 if (!isa_and_nonnull
<DILocalScope
>(T
->getScope()))
641 AllRetainTypes
.emplace_back(T
);
644 DIBasicType
*DIBuilder::createUnspecifiedParameter() { return nullptr; }
647 DIBuilder::createForwardDecl(unsigned Tag
, StringRef Name
, DIScope
*Scope
,
648 DIFile
*F
, unsigned Line
, unsigned RuntimeLang
,
649 uint64_t SizeInBits
, uint32_t AlignInBits
,
650 StringRef UniqueIdentifier
) {
651 // FIXME: Define in terms of createReplaceableForwardDecl() by calling
652 // replaceWithUniqued().
653 auto *RetTy
= DICompositeType::get(
654 VMContext
, Tag
, Name
, F
, Line
, getNonCompileUnitScope(Scope
), nullptr,
655 SizeInBits
, AlignInBits
, 0, DINode::FlagFwdDecl
, nullptr, RuntimeLang
,
656 nullptr, nullptr, UniqueIdentifier
);
657 trackIfUnresolved(RetTy
);
658 if (isa_and_nonnull
<DILocalScope
>(Scope
))
659 getSubprogramNodesTrackingVector(Scope
).emplace_back(RetTy
);
663 DICompositeType
*DIBuilder::createReplaceableCompositeType(
664 unsigned Tag
, StringRef Name
, DIScope
*Scope
, DIFile
*F
, unsigned Line
,
665 unsigned RuntimeLang
, uint64_t SizeInBits
, uint32_t AlignInBits
,
666 DINode::DIFlags Flags
, StringRef UniqueIdentifier
,
667 DINodeArray Annotations
) {
669 DICompositeType::getTemporary(
670 VMContext
, Tag
, Name
, F
, Line
, getNonCompileUnitScope(Scope
), nullptr,
671 SizeInBits
, AlignInBits
, 0, Flags
, nullptr, RuntimeLang
, nullptr,
672 nullptr, UniqueIdentifier
, nullptr, nullptr, nullptr, nullptr,
673 nullptr, Annotations
)
675 trackIfUnresolved(RetTy
);
676 if (isa_and_nonnull
<DILocalScope
>(Scope
))
677 getSubprogramNodesTrackingVector(Scope
).emplace_back(RetTy
);
681 DINodeArray
DIBuilder::getOrCreateArray(ArrayRef
<Metadata
*> Elements
) {
682 return MDTuple::get(VMContext
, Elements
);
686 DIBuilder::getOrCreateMacroArray(ArrayRef
<Metadata
*> Elements
) {
687 return MDTuple::get(VMContext
, Elements
);
690 DITypeRefArray
DIBuilder::getOrCreateTypeArray(ArrayRef
<Metadata
*> Elements
) {
691 SmallVector
<llvm::Metadata
*, 16> Elts
;
692 for (Metadata
*E
: Elements
) {
693 if (isa_and_nonnull
<MDNode
>(E
))
694 Elts
.push_back(cast
<DIType
>(E
));
698 return DITypeRefArray(MDNode::get(VMContext
, Elts
));
701 DISubrange
*DIBuilder::getOrCreateSubrange(int64_t Lo
, int64_t Count
) {
702 auto *LB
= ConstantAsMetadata::get(
703 ConstantInt::getSigned(Type::getInt64Ty(VMContext
), Lo
));
704 auto *CountNode
= ConstantAsMetadata::get(
705 ConstantInt::getSigned(Type::getInt64Ty(VMContext
), Count
));
706 return DISubrange::get(VMContext
, CountNode
, LB
, nullptr, nullptr);
709 DISubrange
*DIBuilder::getOrCreateSubrange(int64_t Lo
, Metadata
*CountNode
) {
710 auto *LB
= ConstantAsMetadata::get(
711 ConstantInt::getSigned(Type::getInt64Ty(VMContext
), Lo
));
712 return DISubrange::get(VMContext
, CountNode
, LB
, nullptr, nullptr);
715 DISubrange
*DIBuilder::getOrCreateSubrange(Metadata
*CountNode
, Metadata
*LB
,
716 Metadata
*UB
, Metadata
*Stride
) {
717 return DISubrange::get(VMContext
, CountNode
, LB
, UB
, Stride
);
720 DIGenericSubrange
*DIBuilder::getOrCreateGenericSubrange(
721 DIGenericSubrange::BoundType CountNode
, DIGenericSubrange::BoundType LB
,
722 DIGenericSubrange::BoundType UB
, DIGenericSubrange::BoundType Stride
) {
723 auto ConvToMetadata
= [&](DIGenericSubrange::BoundType Bound
) -> Metadata
* {
724 return isa
<DIExpression
*>(Bound
) ? (Metadata
*)cast
<DIExpression
*>(Bound
)
725 : (Metadata
*)cast
<DIVariable
*>(Bound
);
727 return DIGenericSubrange::get(VMContext
, ConvToMetadata(CountNode
),
728 ConvToMetadata(LB
), ConvToMetadata(UB
),
729 ConvToMetadata(Stride
));
732 static void checkGlobalVariableScope(DIScope
*Context
) {
735 dyn_cast_or_null
<DICompositeType
>(getNonCompileUnitScope(Context
)))
736 assert(CT
->getIdentifier().empty() &&
737 "Context of a global variable should not be a type with identifier");
741 DIGlobalVariableExpression
*DIBuilder::createGlobalVariableExpression(
742 DIScope
*Context
, StringRef Name
, StringRef LinkageName
, DIFile
*F
,
743 unsigned LineNumber
, DIType
*Ty
, bool IsLocalToUnit
, bool isDefined
,
744 DIExpression
*Expr
, MDNode
*Decl
, MDTuple
*TemplateParams
,
745 uint32_t AlignInBits
, DINodeArray Annotations
) {
746 checkGlobalVariableScope(Context
);
748 auto *GV
= DIGlobalVariable::getDistinct(
749 VMContext
, cast_or_null
<DIScope
>(Context
), Name
, LinkageName
, F
,
750 LineNumber
, Ty
, IsLocalToUnit
, isDefined
,
751 cast_or_null
<DIDerivedType
>(Decl
), TemplateParams
, AlignInBits
,
754 Expr
= createExpression();
755 auto *N
= DIGlobalVariableExpression::get(VMContext
, GV
, Expr
);
760 DIGlobalVariable
*DIBuilder::createTempGlobalVariableFwdDecl(
761 DIScope
*Context
, StringRef Name
, StringRef LinkageName
, DIFile
*F
,
762 unsigned LineNumber
, DIType
*Ty
, bool IsLocalToUnit
, MDNode
*Decl
,
763 MDTuple
*TemplateParams
, uint32_t AlignInBits
) {
764 checkGlobalVariableScope(Context
);
766 return DIGlobalVariable::getTemporary(
767 VMContext
, cast_or_null
<DIScope
>(Context
), Name
, LinkageName
, F
,
768 LineNumber
, Ty
, IsLocalToUnit
, false,
769 cast_or_null
<DIDerivedType
>(Decl
), TemplateParams
, AlignInBits
,
774 static DILocalVariable
*createLocalVariable(
775 LLVMContext
&VMContext
,
776 SmallVectorImpl
<TrackingMDNodeRef
> &PreservedNodes
,
777 DIScope
*Context
, StringRef Name
, unsigned ArgNo
, DIFile
*File
,
778 unsigned LineNo
, DIType
*Ty
, bool AlwaysPreserve
, DINode::DIFlags Flags
,
779 uint32_t AlignInBits
, DINodeArray Annotations
= nullptr) {
780 // FIXME: Why doesn't this check for a subprogram or lexical block (AFAICT
781 // the only valid scopes)?
782 auto *Scope
= cast
<DILocalScope
>(Context
);
783 auto *Node
= DILocalVariable::get(VMContext
, Scope
, Name
, File
, LineNo
, Ty
,
784 ArgNo
, Flags
, AlignInBits
, Annotations
);
785 if (AlwaysPreserve
) {
786 // The optimizer may remove local variables. If there is an interest
787 // to preserve variable info in such situation then stash it in a
789 PreservedNodes
.emplace_back(Node
);
794 DILocalVariable
*DIBuilder::createAutoVariable(DIScope
*Scope
, StringRef Name
,
795 DIFile
*File
, unsigned LineNo
,
796 DIType
*Ty
, bool AlwaysPreserve
,
797 DINode::DIFlags Flags
,
798 uint32_t AlignInBits
) {
799 assert(Scope
&& isa
<DILocalScope
>(Scope
) &&
800 "Unexpected scope for a local variable.");
801 return createLocalVariable(
802 VMContext
, getSubprogramNodesTrackingVector(Scope
), Scope
, Name
,
803 /* ArgNo */ 0, File
, LineNo
, Ty
, AlwaysPreserve
, Flags
, AlignInBits
);
806 DILocalVariable
*DIBuilder::createParameterVariable(
807 DIScope
*Scope
, StringRef Name
, unsigned ArgNo
, DIFile
*File
,
808 unsigned LineNo
, DIType
*Ty
, bool AlwaysPreserve
, DINode::DIFlags Flags
,
809 DINodeArray Annotations
) {
810 assert(ArgNo
&& "Expected non-zero argument number for parameter");
811 assert(Scope
&& isa
<DILocalScope
>(Scope
) &&
812 "Unexpected scope for a local variable.");
813 return createLocalVariable(
814 VMContext
, getSubprogramNodesTrackingVector(Scope
), Scope
, Name
, ArgNo
,
815 File
, LineNo
, Ty
, AlwaysPreserve
, Flags
, /*AlignInBits=*/0, Annotations
);
818 DILabel
*DIBuilder::createLabel(DIScope
*Context
, StringRef Name
, DIFile
*File
,
819 unsigned LineNo
, bool AlwaysPreserve
) {
820 auto *Scope
= cast
<DILocalScope
>(Context
);
821 auto *Node
= DILabel::get(VMContext
, Scope
, Name
, File
, LineNo
);
823 if (AlwaysPreserve
) {
824 /// The optimizer may remove labels. If there is an interest
825 /// to preserve label info in such situation then append it to
826 /// the list of retained nodes of the DISubprogram.
827 getSubprogramNodesTrackingVector(Scope
).emplace_back(Node
);
832 DIExpression
*DIBuilder::createExpression(ArrayRef
<uint64_t> Addr
) {
833 return DIExpression::get(VMContext
, Addr
);
836 template <class... Ts
>
837 static DISubprogram
*getSubprogram(bool IsDistinct
, Ts
&&...Args
) {
839 return DISubprogram::getDistinct(std::forward
<Ts
>(Args
)...);
840 return DISubprogram::get(std::forward
<Ts
>(Args
)...);
843 DISubprogram
*DIBuilder::createFunction(
844 DIScope
*Context
, StringRef Name
, StringRef LinkageName
, DIFile
*File
,
845 unsigned LineNo
, DISubroutineType
*Ty
, unsigned ScopeLine
,
846 DINode::DIFlags Flags
, DISubprogram::DISPFlags SPFlags
,
847 DITemplateParameterArray TParams
, DISubprogram
*Decl
,
848 DITypeArray ThrownTypes
, DINodeArray Annotations
,
849 StringRef TargetFuncName
) {
850 bool IsDefinition
= SPFlags
& DISubprogram::SPFlagDefinition
;
851 auto *Node
= getSubprogram(
852 /*IsDistinct=*/IsDefinition
, VMContext
, getNonCompileUnitScope(Context
),
853 Name
, LinkageName
, File
, LineNo
, Ty
, ScopeLine
, nullptr, 0, 0, Flags
,
854 SPFlags
, IsDefinition
? CUNode
: nullptr, TParams
, Decl
, nullptr,
855 ThrownTypes
, Annotations
, TargetFuncName
);
858 AllSubprograms
.push_back(Node
);
859 trackIfUnresolved(Node
);
863 DISubprogram
*DIBuilder::createTempFunctionFwdDecl(
864 DIScope
*Context
, StringRef Name
, StringRef LinkageName
, DIFile
*File
,
865 unsigned LineNo
, DISubroutineType
*Ty
, unsigned ScopeLine
,
866 DINode::DIFlags Flags
, DISubprogram::DISPFlags SPFlags
,
867 DITemplateParameterArray TParams
, DISubprogram
*Decl
,
868 DITypeArray ThrownTypes
) {
869 bool IsDefinition
= SPFlags
& DISubprogram::SPFlagDefinition
;
870 return DISubprogram::getTemporary(VMContext
, getNonCompileUnitScope(Context
),
871 Name
, LinkageName
, File
, LineNo
, Ty
,
872 ScopeLine
, nullptr, 0, 0, Flags
, SPFlags
,
873 IsDefinition
? CUNode
: nullptr, TParams
,
874 Decl
, nullptr, ThrownTypes
)
878 DISubprogram
*DIBuilder::createMethod(
879 DIScope
*Context
, StringRef Name
, StringRef LinkageName
, DIFile
*F
,
880 unsigned LineNo
, DISubroutineType
*Ty
, unsigned VIndex
, int ThisAdjustment
,
881 DIType
*VTableHolder
, DINode::DIFlags Flags
,
882 DISubprogram::DISPFlags SPFlags
, DITemplateParameterArray TParams
,
883 DITypeArray ThrownTypes
) {
884 assert(getNonCompileUnitScope(Context
) &&
885 "Methods should have both a Context and a context that isn't "
886 "the compile unit.");
887 // FIXME: Do we want to use different scope/lines?
888 bool IsDefinition
= SPFlags
& DISubprogram::SPFlagDefinition
;
889 auto *SP
= getSubprogram(
890 /*IsDistinct=*/IsDefinition
, VMContext
, cast
<DIScope
>(Context
), Name
,
891 LinkageName
, F
, LineNo
, Ty
, LineNo
, VTableHolder
, VIndex
, ThisAdjustment
,
892 Flags
, SPFlags
, IsDefinition
? CUNode
: nullptr, TParams
, nullptr,
893 nullptr, ThrownTypes
);
896 AllSubprograms
.push_back(SP
);
897 trackIfUnresolved(SP
);
901 DICommonBlock
*DIBuilder::createCommonBlock(DIScope
*Scope
,
902 DIGlobalVariable
*Decl
,
903 StringRef Name
, DIFile
*File
,
905 return DICommonBlock::get(VMContext
, Scope
, Decl
, Name
, File
, LineNo
);
908 DINamespace
*DIBuilder::createNameSpace(DIScope
*Scope
, StringRef Name
,
909 bool ExportSymbols
) {
911 // It is okay to *not* make anonymous top-level namespaces distinct, because
912 // all nodes that have an anonymous namespace as their parent scope are
913 // guaranteed to be unique and/or are linked to their containing
914 // DICompileUnit. This decision is an explicit tradeoff of link time versus
915 // memory usage versus code simplicity and may get revisited in the future.
916 return DINamespace::get(VMContext
, getNonCompileUnitScope(Scope
), Name
,
920 DIModule
*DIBuilder::createModule(DIScope
*Scope
, StringRef Name
,
921 StringRef ConfigurationMacros
,
922 StringRef IncludePath
, StringRef APINotesFile
,
923 DIFile
*File
, unsigned LineNo
, bool IsDecl
) {
924 return DIModule::get(VMContext
, File
, getNonCompileUnitScope(Scope
), Name
,
925 ConfigurationMacros
, IncludePath
, APINotesFile
, LineNo
,
929 DILexicalBlockFile
*DIBuilder::createLexicalBlockFile(DIScope
*Scope
,
931 unsigned Discriminator
) {
932 return DILexicalBlockFile::get(VMContext
, Scope
, File
, Discriminator
);
935 DILexicalBlock
*DIBuilder::createLexicalBlock(DIScope
*Scope
, DIFile
*File
,
936 unsigned Line
, unsigned Col
) {
937 // Make these distinct, to avoid merging two lexical blocks on the same
939 return DILexicalBlock::getDistinct(VMContext
, getNonCompileUnitScope(Scope
),
943 Instruction
*DIBuilder::insertDeclare(Value
*Storage
, DILocalVariable
*VarInfo
,
944 DIExpression
*Expr
, const DILocation
*DL
,
945 Instruction
*InsertBefore
) {
946 return insertDeclare(Storage
, VarInfo
, Expr
, DL
, InsertBefore
->getParent(),
950 Instruction
*DIBuilder::insertDeclare(Value
*Storage
, DILocalVariable
*VarInfo
,
951 DIExpression
*Expr
, const DILocation
*DL
,
952 BasicBlock
*InsertAtEnd
) {
953 // If this block already has a terminator then insert this intrinsic before
954 // the terminator. Otherwise, put it at the end of the block.
955 Instruction
*InsertBefore
= InsertAtEnd
->getTerminator();
956 return insertDeclare(Storage
, VarInfo
, Expr
, DL
, InsertAtEnd
, InsertBefore
);
960 DIBuilder::insertDbgAssign(Instruction
*LinkedInstr
, Value
*Val
,
961 DILocalVariable
*SrcVar
, DIExpression
*ValExpr
,
962 Value
*Addr
, DIExpression
*AddrExpr
,
963 const DILocation
*DL
) {
964 LLVMContext
&Ctx
= LinkedInstr
->getContext();
965 Module
*M
= LinkedInstr
->getModule();
967 AssignFn
= Intrinsic::getDeclaration(M
, Intrinsic::dbg_assign
);
969 auto *Link
= LinkedInstr
->getMetadata(LLVMContext::MD_DIAssignID
);
970 assert(Link
&& "Linked instruction must have DIAssign metadata attached");
972 std::array
<Value
*, 6> Args
= {
973 MetadataAsValue::get(Ctx
, ValueAsMetadata::get(Val
)),
974 MetadataAsValue::get(Ctx
, SrcVar
),
975 MetadataAsValue::get(Ctx
, ValExpr
),
976 MetadataAsValue::get(Ctx
, Link
),
977 MetadataAsValue::get(Ctx
, ValueAsMetadata::get(Addr
)),
978 MetadataAsValue::get(Ctx
, AddrExpr
),
982 B
.SetCurrentDebugLocation(DL
);
984 auto *DVI
= cast
<DbgAssignIntrinsic
>(B
.CreateCall(AssignFn
, Args
));
985 DVI
->insertAfter(LinkedInstr
);
989 Instruction
*DIBuilder::insertLabel(DILabel
*LabelInfo
, const DILocation
*DL
,
990 Instruction
*InsertBefore
) {
991 return insertLabel(LabelInfo
, DL
,
992 InsertBefore
? InsertBefore
->getParent() : nullptr,
996 Instruction
*DIBuilder::insertLabel(DILabel
*LabelInfo
, const DILocation
*DL
,
997 BasicBlock
*InsertAtEnd
) {
998 return insertLabel(LabelInfo
, DL
, InsertAtEnd
, nullptr);
1001 Instruction
*DIBuilder::insertDbgValueIntrinsic(Value
*V
,
1002 DILocalVariable
*VarInfo
,
1004 const DILocation
*DL
,
1005 Instruction
*InsertBefore
) {
1006 return insertDbgValueIntrinsic(
1007 V
, VarInfo
, Expr
, DL
, InsertBefore
? InsertBefore
->getParent() : nullptr,
1011 Instruction
*DIBuilder::insertDbgValueIntrinsic(Value
*V
,
1012 DILocalVariable
*VarInfo
,
1014 const DILocation
*DL
,
1015 BasicBlock
*InsertAtEnd
) {
1016 return insertDbgValueIntrinsic(V
, VarInfo
, Expr
, DL
, InsertAtEnd
, nullptr);
1019 /// Initialize IRBuilder for inserting dbg.declare and dbg.value intrinsics.
1020 /// This abstracts over the various ways to specify an insert position.
1021 static void initIRBuilder(IRBuilder
<> &Builder
, const DILocation
*DL
,
1022 BasicBlock
*InsertBB
, Instruction
*InsertBefore
) {
1024 Builder
.SetInsertPoint(InsertBefore
);
1026 Builder
.SetInsertPoint(InsertBB
);
1027 Builder
.SetCurrentDebugLocation(DL
);
1030 static Value
*getDbgIntrinsicValueImpl(LLVMContext
&VMContext
, Value
*V
) {
1031 assert(V
&& "no value passed to dbg intrinsic");
1032 return MetadataAsValue::get(VMContext
, ValueAsMetadata::get(V
));
1035 static Function
*getDeclareIntrin(Module
&M
) {
1036 return Intrinsic::getDeclaration(&M
, Intrinsic::dbg_declare
);
1039 Instruction
*DIBuilder::insertDbgValueIntrinsic(
1040 llvm::Value
*Val
, DILocalVariable
*VarInfo
, DIExpression
*Expr
,
1041 const DILocation
*DL
, BasicBlock
*InsertBB
, Instruction
*InsertBefore
) {
1043 ValueFn
= Intrinsic::getDeclaration(&M
, Intrinsic::dbg_value
);
1044 return insertDbgIntrinsic(ValueFn
, Val
, VarInfo
, Expr
, DL
, InsertBB
,
1048 Instruction
*DIBuilder::insertDeclare(Value
*Storage
, DILocalVariable
*VarInfo
,
1049 DIExpression
*Expr
, const DILocation
*DL
,
1050 BasicBlock
*InsertBB
,
1051 Instruction
*InsertBefore
) {
1052 assert(VarInfo
&& "empty or invalid DILocalVariable* passed to dbg.declare");
1053 assert(DL
&& "Expected debug loc");
1054 assert(DL
->getScope()->getSubprogram() ==
1055 VarInfo
->getScope()->getSubprogram() &&
1056 "Expected matching subprograms");
1058 DeclareFn
= getDeclareIntrin(M
);
1060 trackIfUnresolved(VarInfo
);
1061 trackIfUnresolved(Expr
);
1062 Value
*Args
[] = {getDbgIntrinsicValueImpl(VMContext
, Storage
),
1063 MetadataAsValue::get(VMContext
, VarInfo
),
1064 MetadataAsValue::get(VMContext
, Expr
)};
1066 IRBuilder
<> B(DL
->getContext());
1067 initIRBuilder(B
, DL
, InsertBB
, InsertBefore
);
1068 return B
.CreateCall(DeclareFn
, Args
);
1071 Instruction
*DIBuilder::insertDbgIntrinsic(llvm::Function
*IntrinsicFn
,
1072 Value
*V
, DILocalVariable
*VarInfo
,
1074 const DILocation
*DL
,
1075 BasicBlock
*InsertBB
,
1076 Instruction
*InsertBefore
) {
1077 assert(IntrinsicFn
&& "must pass a non-null intrinsic function");
1078 assert(V
&& "must pass a value to a dbg intrinsic");
1080 "empty or invalid DILocalVariable* passed to debug intrinsic");
1081 assert(DL
&& "Expected debug loc");
1082 assert(DL
->getScope()->getSubprogram() ==
1083 VarInfo
->getScope()->getSubprogram() &&
1084 "Expected matching subprograms");
1086 trackIfUnresolved(VarInfo
);
1087 trackIfUnresolved(Expr
);
1088 Value
*Args
[] = {getDbgIntrinsicValueImpl(VMContext
, V
),
1089 MetadataAsValue::get(VMContext
, VarInfo
),
1090 MetadataAsValue::get(VMContext
, Expr
)};
1092 IRBuilder
<> B(DL
->getContext());
1093 initIRBuilder(B
, DL
, InsertBB
, InsertBefore
);
1094 return B
.CreateCall(IntrinsicFn
, Args
);
1097 Instruction
*DIBuilder::insertLabel(DILabel
*LabelInfo
, const DILocation
*DL
,
1098 BasicBlock
*InsertBB
,
1099 Instruction
*InsertBefore
) {
1100 assert(LabelInfo
&& "empty or invalid DILabel* passed to dbg.label");
1101 assert(DL
&& "Expected debug loc");
1102 assert(DL
->getScope()->getSubprogram() ==
1103 LabelInfo
->getScope()->getSubprogram() &&
1104 "Expected matching subprograms");
1106 LabelFn
= Intrinsic::getDeclaration(&M
, Intrinsic::dbg_label
);
1108 trackIfUnresolved(LabelInfo
);
1109 Value
*Args
[] = {MetadataAsValue::get(VMContext
, LabelInfo
)};
1111 IRBuilder
<> B(DL
->getContext());
1112 initIRBuilder(B
, DL
, InsertBB
, InsertBefore
);
1113 return B
.CreateCall(LabelFn
, Args
);
1116 void DIBuilder::replaceVTableHolder(DICompositeType
*&T
, DIType
*VTableHolder
) {
1118 TypedTrackingMDRef
<DICompositeType
> N(T
);
1119 N
->replaceVTableHolder(VTableHolder
);
1123 // If this didn't create a self-reference, just return.
1124 if (T
!= VTableHolder
)
1127 // Look for unresolved operands. T will drop RAUW support, orphaning any
1128 // cycles underneath it.
1129 if (T
->isResolved())
1130 for (const MDOperand
&O
: T
->operands())
1131 if (auto *N
= dyn_cast_or_null
<MDNode
>(O
))
1132 trackIfUnresolved(N
);
1135 void DIBuilder::replaceArrays(DICompositeType
*&T
, DINodeArray Elements
,
1136 DINodeArray TParams
) {
1138 TypedTrackingMDRef
<DICompositeType
> N(T
);
1140 N
->replaceElements(Elements
);
1142 N
->replaceTemplateParams(DITemplateParameterArray(TParams
));
1146 // If T isn't resolved, there's no problem.
1147 if (!T
->isResolved())
1150 // If T is resolved, it may be due to a self-reference cycle. Track the
1151 // arrays explicitly if they're unresolved, or else the cycles will be
1154 trackIfUnresolved(Elements
.get());
1156 trackIfUnresolved(TParams
.get());