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"
25 using namespace llvm::dwarf
;
27 DIBuilder::DIBuilder(Module
&m
, bool AllowUnresolvedNodes
, DICompileUnit
*CU
)
28 : M(m
), VMContext(M
.getContext()), CUNode(CU
), DeclareFn(nullptr),
29 ValueFn(nullptr), LabelFn(nullptr), AssignFn(nullptr),
30 AllowUnresolvedNodes(AllowUnresolvedNodes
) {
32 if (const auto &ETs
= CUNode
->getEnumTypes())
33 AllEnumTypes
.assign(ETs
.begin(), ETs
.end());
34 if (const auto &RTs
= CUNode
->getRetainedTypes())
35 AllRetainTypes
.assign(RTs
.begin(), RTs
.end());
36 if (const auto &GVs
= CUNode
->getGlobalVariables())
37 AllGVs
.assign(GVs
.begin(), GVs
.end());
38 if (const auto &IMs
= CUNode
->getImportedEntities())
39 ImportedModules
.assign(IMs
.begin(), IMs
.end());
40 if (const auto &MNs
= CUNode
->getMacros())
41 AllMacrosPerParent
.insert({nullptr, {MNs
.begin(), MNs
.end()}});
45 void DIBuilder::trackIfUnresolved(MDNode
*N
) {
51 assert(AllowUnresolvedNodes
&& "Cannot handle unresolved nodes");
52 UnresolvedNodes
.emplace_back(N
);
55 void DIBuilder::finalizeSubprogram(DISubprogram
*SP
) {
56 auto PN
= SubprogramTrackedNodes
.find(SP
);
57 if (PN
!= SubprogramTrackedNodes
.end())
58 SP
->replaceRetainedNodes(
59 MDTuple::get(VMContext
, SmallVector
<Metadata
*, 16>(PN
->second
.begin(),
63 void DIBuilder::finalize() {
65 assert(!AllowUnresolvedNodes
&&
66 "creating type nodes without a CU is not supported");
70 if (!AllEnumTypes
.empty())
71 CUNode
->replaceEnumTypes(MDTuple::get(
72 VMContext
, SmallVector
<Metadata
*, 16>(AllEnumTypes
.begin(),
73 AllEnumTypes
.end())));
75 SmallVector
<Metadata
*, 16> RetainValues
;
76 // Declarations and definitions of the same type may be retained. Some
77 // clients RAUW these pairs, leaving duplicates in the retained types
78 // list. Use a set to remove the duplicates while we transform the
79 // TrackingVHs back into Values.
80 SmallPtrSet
<Metadata
*, 16> RetainSet
;
81 for (const TrackingMDNodeRef
&N
: AllRetainTypes
)
82 if (RetainSet
.insert(N
).second
)
83 RetainValues
.push_back(N
);
85 if (!RetainValues
.empty())
86 CUNode
->replaceRetainedTypes(MDTuple::get(VMContext
, RetainValues
));
88 for (auto *SP
: AllSubprograms
)
89 finalizeSubprogram(SP
);
90 for (auto *N
: RetainValues
)
91 if (auto *SP
= dyn_cast
<DISubprogram
>(N
))
92 finalizeSubprogram(SP
);
95 CUNode
->replaceGlobalVariables(MDTuple::get(VMContext
, AllGVs
));
97 if (!ImportedModules
.empty())
98 CUNode
->replaceImportedEntities(MDTuple::get(
99 VMContext
, SmallVector
<Metadata
*, 16>(ImportedModules
.begin(),
100 ImportedModules
.end())));
102 for (const auto &I
: AllMacrosPerParent
) {
103 // DIMacroNode's with nullptr parent are DICompileUnit direct children.
105 CUNode
->replaceMacros(MDTuple::get(VMContext
, I
.second
.getArrayRef()));
108 // Otherwise, it must be a temporary DIMacroFile that need to be resolved.
109 auto *TMF
= cast
<DIMacroFile
>(I
.first
);
110 auto *MF
= DIMacroFile::get(VMContext
, dwarf::DW_MACINFO_start_file
,
111 TMF
->getLine(), TMF
->getFile(),
112 getOrCreateMacroArray(I
.second
.getArrayRef()));
113 replaceTemporary(llvm::TempDIMacroNode(TMF
), MF
);
116 // Now that all temp nodes have been replaced or deleted, resolve remaining
118 for (const auto &N
: UnresolvedNodes
)
119 if (N
&& !N
->isResolved())
121 UnresolvedNodes
.clear();
123 // Can't handle unresolved nodes anymore.
124 AllowUnresolvedNodes
= false;
127 /// If N is compile unit return NULL otherwise return N.
128 static DIScope
*getNonCompileUnitScope(DIScope
*N
) {
129 if (!N
|| isa
<DICompileUnit
>(N
))
131 return cast
<DIScope
>(N
);
134 DICompileUnit
*DIBuilder::createCompileUnit(
135 unsigned Lang
, DIFile
*File
, StringRef Producer
, bool isOptimized
,
136 StringRef Flags
, unsigned RunTimeVer
, StringRef SplitName
,
137 DICompileUnit::DebugEmissionKind Kind
, uint64_t DWOId
,
138 bool SplitDebugInlining
, bool DebugInfoForProfiling
,
139 DICompileUnit::DebugNameTableKind NameTableKind
, bool RangesBaseAddress
,
140 StringRef SysRoot
, StringRef SDK
) {
142 assert(((Lang
<= dwarf::DW_LANG_Metal
&& Lang
>= dwarf::DW_LANG_C89
) ||
143 (Lang
<= dwarf::DW_LANG_hi_user
&& Lang
>= dwarf::DW_LANG_lo_user
)) &&
144 "Invalid Language tag");
146 assert(!CUNode
&& "Can only make one compile unit per DIBuilder instance");
147 CUNode
= DICompileUnit::getDistinct(
148 VMContext
, Lang
, File
, Producer
, isOptimized
, Flags
, RunTimeVer
,
149 SplitName
, Kind
, nullptr, nullptr, nullptr, nullptr, nullptr, DWOId
,
150 SplitDebugInlining
, DebugInfoForProfiling
, NameTableKind
,
151 RangesBaseAddress
, SysRoot
, SDK
);
153 // Create a named metadata so that it is easier to find cu in a module.
154 NamedMDNode
*NMD
= M
.getOrInsertNamedMetadata("llvm.dbg.cu");
155 NMD
->addOperand(CUNode
);
156 trackIfUnresolved(CUNode
);
160 static DIImportedEntity
*
161 createImportedModule(LLVMContext
&C
, dwarf::Tag Tag
, DIScope
*Context
,
162 Metadata
*NS
, DIFile
*File
, unsigned Line
, StringRef Name
,
163 DINodeArray Elements
,
164 SmallVectorImpl
<TrackingMDNodeRef
> &ImportedModules
) {
166 assert(File
&& "Source location has line number but no file");
167 unsigned EntitiesCount
= C
.pImpl
->DIImportedEntitys
.size();
168 auto *M
= DIImportedEntity::get(C
, Tag
, Context
, cast_or_null
<DINode
>(NS
),
169 File
, Line
, Name
, Elements
);
170 if (EntitiesCount
< C
.pImpl
->DIImportedEntitys
.size())
171 // A new Imported Entity was just added to the context.
172 // Add it to the Imported Modules list.
173 ImportedModules
.emplace_back(M
);
177 DIImportedEntity
*DIBuilder::createImportedModule(DIScope
*Context
,
178 DINamespace
*NS
, DIFile
*File
,
180 DINodeArray Elements
) {
181 return ::createImportedModule(VMContext
, dwarf::DW_TAG_imported_module
,
182 Context
, NS
, File
, Line
, StringRef(), Elements
,
183 getImportTrackingVector(Context
));
186 DIImportedEntity
*DIBuilder::createImportedModule(DIScope
*Context
,
187 DIImportedEntity
*NS
,
188 DIFile
*File
, unsigned Line
,
189 DINodeArray Elements
) {
190 return ::createImportedModule(VMContext
, dwarf::DW_TAG_imported_module
,
191 Context
, NS
, File
, Line
, StringRef(), Elements
,
192 getImportTrackingVector(Context
));
195 DIImportedEntity
*DIBuilder::createImportedModule(DIScope
*Context
, DIModule
*M
,
196 DIFile
*File
, unsigned Line
,
197 DINodeArray Elements
) {
198 return ::createImportedModule(VMContext
, dwarf::DW_TAG_imported_module
,
199 Context
, M
, File
, Line
, StringRef(), Elements
,
200 getImportTrackingVector(Context
));
204 DIBuilder::createImportedDeclaration(DIScope
*Context
, DINode
*Decl
,
205 DIFile
*File
, unsigned Line
,
206 StringRef Name
, DINodeArray Elements
) {
207 // Make sure to use the unique identifier based metadata reference for
208 // types that have one.
209 return ::createImportedModule(VMContext
, dwarf::DW_TAG_imported_declaration
,
210 Context
, Decl
, File
, Line
, Name
, Elements
,
211 getImportTrackingVector(Context
));
214 DIFile
*DIBuilder::createFile(StringRef Filename
, StringRef Directory
,
215 std::optional
<DIFile::ChecksumInfo
<StringRef
>> CS
,
216 std::optional
<StringRef
> Source
) {
217 return DIFile::get(VMContext
, Filename
, Directory
, CS
, Source
);
220 DIMacro
*DIBuilder::createMacro(DIMacroFile
*Parent
, unsigned LineNumber
,
221 unsigned MacroType
, StringRef Name
,
223 assert(!Name
.empty() && "Unable to create macro without name");
224 assert((MacroType
== dwarf::DW_MACINFO_undef
||
225 MacroType
== dwarf::DW_MACINFO_define
) &&
226 "Unexpected macro type");
227 auto *M
= DIMacro::get(VMContext
, MacroType
, LineNumber
, Name
, Value
);
228 AllMacrosPerParent
[Parent
].insert(M
);
232 DIMacroFile
*DIBuilder::createTempMacroFile(DIMacroFile
*Parent
,
233 unsigned LineNumber
, DIFile
*File
) {
234 auto *MF
= DIMacroFile::getTemporary(VMContext
, dwarf::DW_MACINFO_start_file
,
235 LineNumber
, File
, DIMacroNodeArray())
237 AllMacrosPerParent
[Parent
].insert(MF
);
238 // Add the new temporary DIMacroFile to the macro per parent map as a parent.
239 // This is needed to assure DIMacroFile with no children to have an entry in
240 // the map. Otherwise, it will not be resolved in DIBuilder::finalize().
241 AllMacrosPerParent
.insert({MF
, {}});
245 DIEnumerator
*DIBuilder::createEnumerator(StringRef Name
, uint64_t Val
,
247 assert(!Name
.empty() && "Unable to create enumerator without name");
248 return DIEnumerator::get(VMContext
, APInt(64, Val
, !IsUnsigned
), IsUnsigned
,
252 DIEnumerator
*DIBuilder::createEnumerator(StringRef Name
, const APSInt
&Value
) {
253 assert(!Name
.empty() && "Unable to create enumerator without name");
254 return DIEnumerator::get(VMContext
, APInt(Value
), Value
.isUnsigned(), Name
);
257 DIBasicType
*DIBuilder::createUnspecifiedType(StringRef Name
) {
258 assert(!Name
.empty() && "Unable to create type without name");
259 return DIBasicType::get(VMContext
, dwarf::DW_TAG_unspecified_type
, Name
);
262 DIBasicType
*DIBuilder::createNullPtrType() {
263 return createUnspecifiedType("decltype(nullptr)");
266 DIBasicType
*DIBuilder::createBasicType(StringRef Name
, uint64_t SizeInBits
,
268 DINode::DIFlags Flags
,
269 uint32_t NumExtraInhabitants
) {
270 assert(!Name
.empty() && "Unable to create type without name");
271 return DIBasicType::get(VMContext
, dwarf::DW_TAG_base_type
, Name
, SizeInBits
,
272 0, Encoding
, NumExtraInhabitants
, Flags
);
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
, std::nullopt
, DINode::FlagZero
);
302 DIDerivedType
*DIBuilder::createPtrAuthQualifiedType(
303 DIType
*FromTy
, unsigned Key
, bool IsAddressDiscriminated
,
304 unsigned ExtraDiscriminator
, bool IsaPointer
,
305 bool AuthenticatesNullValues
) {
306 return DIDerivedType::get(VMContext
, dwarf::DW_TAG_LLVM_ptrauth_type
, "",
307 nullptr, 0, nullptr, FromTy
, 0, 0, 0, std::nullopt
,
308 std::optional
<DIDerivedType::PtrAuthData
>(
309 std::in_place
, Key
, IsAddressDiscriminated
,
310 ExtraDiscriminator
, IsaPointer
,
311 AuthenticatesNullValues
),
316 DIBuilder::createPointerType(DIType
*PointeeTy
, uint64_t SizeInBits
,
317 uint32_t AlignInBits
,
318 std::optional
<unsigned> DWARFAddressSpace
,
319 StringRef Name
, DINodeArray Annotations
) {
320 // FIXME: Why is there a name here?
321 return DIDerivedType::get(VMContext
, dwarf::DW_TAG_pointer_type
, Name
,
322 nullptr, 0, nullptr, PointeeTy
, SizeInBits
,
323 AlignInBits
, 0, DWARFAddressSpace
, std::nullopt
,
324 DINode::FlagZero
, nullptr, Annotations
);
327 DIDerivedType
*DIBuilder::createMemberPointerType(DIType
*PointeeTy
,
330 uint32_t AlignInBits
,
331 DINode::DIFlags Flags
) {
332 return DIDerivedType::get(VMContext
, dwarf::DW_TAG_ptr_to_member_type
, "",
333 nullptr, 0, nullptr, PointeeTy
, SizeInBits
,
334 AlignInBits
, 0, std::nullopt
, std::nullopt
, Flags
,
339 DIBuilder::createReferenceType(unsigned Tag
, DIType
*RTy
, uint64_t SizeInBits
,
340 uint32_t AlignInBits
,
341 std::optional
<unsigned> DWARFAddressSpace
) {
342 assert(RTy
&& "Unable to create reference type");
343 return DIDerivedType::get(VMContext
, Tag
, "", nullptr, 0, nullptr, RTy
,
344 SizeInBits
, AlignInBits
, 0, DWARFAddressSpace
, {},
348 DIDerivedType
*DIBuilder::createTypedef(DIType
*Ty
, StringRef Name
,
349 DIFile
*File
, unsigned LineNo
,
350 DIScope
*Context
, uint32_t AlignInBits
,
351 DINode::DIFlags Flags
,
352 DINodeArray Annotations
) {
353 return DIDerivedType::get(VMContext
, dwarf::DW_TAG_typedef
, Name
, File
,
354 LineNo
, getNonCompileUnitScope(Context
), Ty
, 0,
355 AlignInBits
, 0, std::nullopt
, std::nullopt
, Flags
,
356 nullptr, Annotations
);
360 DIBuilder::createTemplateAlias(DIType
*Ty
, StringRef Name
, DIFile
*File
,
361 unsigned LineNo
, DIScope
*Context
,
362 DINodeArray TParams
, uint32_t AlignInBits
,
363 DINode::DIFlags Flags
, DINodeArray Annotations
) {
364 return DIDerivedType::get(VMContext
, dwarf::DW_TAG_template_alias
, Name
, File
,
365 LineNo
, getNonCompileUnitScope(Context
), Ty
, 0,
366 AlignInBits
, 0, std::nullopt
, std::nullopt
, Flags
,
367 TParams
.get(), Annotations
);
370 DIDerivedType
*DIBuilder::createFriend(DIType
*Ty
, DIType
*FriendTy
) {
371 assert(Ty
&& "Invalid type!");
372 assert(FriendTy
&& "Invalid friend type!");
373 return DIDerivedType::get(VMContext
, dwarf::DW_TAG_friend
, "", nullptr, 0, Ty
,
374 FriendTy
, 0, 0, 0, std::nullopt
, std::nullopt
,
378 DIDerivedType
*DIBuilder::createInheritance(DIType
*Ty
, DIType
*BaseTy
,
380 uint32_t VBPtrOffset
,
381 DINode::DIFlags Flags
) {
382 assert(Ty
&& "Unable to create inheritance");
383 Metadata
*ExtraData
= ConstantAsMetadata::get(
384 ConstantInt::get(IntegerType::get(VMContext
, 32), VBPtrOffset
));
385 return DIDerivedType::get(VMContext
, dwarf::DW_TAG_inheritance
, "", nullptr,
386 0, Ty
, BaseTy
, 0, 0, BaseOffset
, std::nullopt
,
387 std::nullopt
, Flags
, ExtraData
);
390 DIDerivedType
*DIBuilder::createMemberType(
391 DIScope
*Scope
, StringRef Name
, DIFile
*File
, unsigned LineNumber
,
392 uint64_t SizeInBits
, uint32_t AlignInBits
, uint64_t OffsetInBits
,
393 DINode::DIFlags Flags
, DIType
*Ty
, DINodeArray Annotations
) {
394 return DIDerivedType::get(VMContext
, dwarf::DW_TAG_member
, Name
, File
,
395 LineNumber
, getNonCompileUnitScope(Scope
), Ty
,
396 SizeInBits
, AlignInBits
, OffsetInBits
, std::nullopt
,
397 std::nullopt
, Flags
, nullptr, Annotations
);
400 static ConstantAsMetadata
*getConstantOrNull(Constant
*C
) {
402 return ConstantAsMetadata::get(C
);
406 DIDerivedType
*DIBuilder::createVariantMemberType(
407 DIScope
*Scope
, StringRef Name
, DIFile
*File
, unsigned LineNumber
,
408 uint64_t SizeInBits
, uint32_t AlignInBits
, uint64_t OffsetInBits
,
409 Constant
*Discriminant
, DINode::DIFlags Flags
, DIType
*Ty
) {
410 return DIDerivedType::get(
411 VMContext
, dwarf::DW_TAG_member
, Name
, File
, LineNumber
,
412 getNonCompileUnitScope(Scope
), Ty
, SizeInBits
, AlignInBits
, OffsetInBits
,
413 std::nullopt
, std::nullopt
, Flags
, getConstantOrNull(Discriminant
));
416 DIDerivedType
*DIBuilder::createBitFieldMemberType(
417 DIScope
*Scope
, StringRef Name
, DIFile
*File
, unsigned LineNumber
,
418 uint64_t SizeInBits
, uint64_t OffsetInBits
, uint64_t StorageOffsetInBits
,
419 DINode::DIFlags Flags
, DIType
*Ty
, DINodeArray Annotations
) {
420 Flags
|= DINode::FlagBitField
;
421 return DIDerivedType::get(
422 VMContext
, dwarf::DW_TAG_member
, Name
, File
, LineNumber
,
423 getNonCompileUnitScope(Scope
), Ty
, SizeInBits
, /*AlignInBits=*/0,
424 OffsetInBits
, std::nullopt
, std::nullopt
, Flags
,
425 ConstantAsMetadata::get(ConstantInt::get(IntegerType::get(VMContext
, 64),
426 StorageOffsetInBits
)),
431 DIBuilder::createStaticMemberType(DIScope
*Scope
, StringRef Name
, DIFile
*File
,
432 unsigned LineNumber
, DIType
*Ty
,
433 DINode::DIFlags Flags
, llvm::Constant
*Val
,
434 unsigned Tag
, uint32_t AlignInBits
) {
435 Flags
|= DINode::FlagStaticMember
;
436 return DIDerivedType::get(VMContext
, Tag
, Name
, File
, LineNumber
,
437 getNonCompileUnitScope(Scope
), Ty
, 0, AlignInBits
,
438 0, std::nullopt
, std::nullopt
, Flags
,
439 getConstantOrNull(Val
));
443 DIBuilder::createObjCIVar(StringRef Name
, DIFile
*File
, unsigned LineNumber
,
444 uint64_t SizeInBits
, uint32_t AlignInBits
,
445 uint64_t OffsetInBits
, DINode::DIFlags Flags
,
446 DIType
*Ty
, MDNode
*PropertyNode
) {
447 return DIDerivedType::get(VMContext
, dwarf::DW_TAG_member
, Name
, File
,
448 LineNumber
, getNonCompileUnitScope(File
), Ty
,
449 SizeInBits
, AlignInBits
, OffsetInBits
, std::nullopt
,
450 std::nullopt
, Flags
, PropertyNode
);
454 DIBuilder::createObjCProperty(StringRef Name
, DIFile
*File
, unsigned LineNumber
,
455 StringRef GetterName
, StringRef SetterName
,
456 unsigned PropertyAttributes
, DIType
*Ty
) {
457 return DIObjCProperty::get(VMContext
, Name
, File
, LineNumber
, GetterName
,
458 SetterName
, PropertyAttributes
, Ty
);
461 DITemplateTypeParameter
*
462 DIBuilder::createTemplateTypeParameter(DIScope
*Context
, StringRef Name
,
463 DIType
*Ty
, bool isDefault
) {
464 assert((!Context
|| isa
<DICompileUnit
>(Context
)) && "Expected compile unit");
465 return DITemplateTypeParameter::get(VMContext
, Name
, Ty
, isDefault
);
468 static DITemplateValueParameter
*
469 createTemplateValueParameterHelper(LLVMContext
&VMContext
, unsigned Tag
,
470 DIScope
*Context
, StringRef Name
, DIType
*Ty
,
471 bool IsDefault
, Metadata
*MD
) {
472 assert((!Context
|| isa
<DICompileUnit
>(Context
)) && "Expected compile unit");
473 return DITemplateValueParameter::get(VMContext
, Tag
, Name
, Ty
, IsDefault
, MD
);
476 DITemplateValueParameter
*
477 DIBuilder::createTemplateValueParameter(DIScope
*Context
, StringRef Name
,
478 DIType
*Ty
, bool isDefault
,
480 return createTemplateValueParameterHelper(
481 VMContext
, dwarf::DW_TAG_template_value_parameter
, Context
, Name
, Ty
,
482 isDefault
, getConstantOrNull(Val
));
485 DITemplateValueParameter
*
486 DIBuilder::createTemplateTemplateParameter(DIScope
*Context
, StringRef Name
,
487 DIType
*Ty
, StringRef Val
,
489 return createTemplateValueParameterHelper(
490 VMContext
, dwarf::DW_TAG_GNU_template_template_param
, Context
, Name
, Ty
,
491 IsDefault
, MDString::get(VMContext
, Val
));
494 DITemplateValueParameter
*
495 DIBuilder::createTemplateParameterPack(DIScope
*Context
, StringRef Name
,
496 DIType
*Ty
, DINodeArray Val
) {
497 return createTemplateValueParameterHelper(
498 VMContext
, dwarf::DW_TAG_GNU_template_parameter_pack
, Context
, Name
, Ty
,
502 DICompositeType
*DIBuilder::createClassType(
503 DIScope
*Context
, StringRef Name
, DIFile
*File
, unsigned LineNumber
,
504 uint64_t SizeInBits
, uint32_t AlignInBits
, uint64_t OffsetInBits
,
505 DINode::DIFlags Flags
, DIType
*DerivedFrom
, DINodeArray Elements
,
506 unsigned RunTimeLang
, DIType
*VTableHolder
, MDNode
*TemplateParams
,
507 StringRef UniqueIdentifier
) {
508 assert((!Context
|| isa
<DIScope
>(Context
)) &&
509 "createClassType should be called with a valid Context");
511 auto *R
= DICompositeType::get(
512 VMContext
, dwarf::DW_TAG_class_type
, Name
, File
, LineNumber
,
513 getNonCompileUnitScope(Context
), DerivedFrom
, SizeInBits
, AlignInBits
,
514 OffsetInBits
, Flags
, Elements
, RunTimeLang
, VTableHolder
,
515 cast_or_null
<MDTuple
>(TemplateParams
), UniqueIdentifier
);
516 trackIfUnresolved(R
);
520 DICompositeType
*DIBuilder::createStructType(
521 DIScope
*Context
, StringRef Name
, DIFile
*File
, unsigned LineNumber
,
522 uint64_t SizeInBits
, uint32_t AlignInBits
, DINode::DIFlags Flags
,
523 DIType
*DerivedFrom
, DINodeArray Elements
, unsigned RunTimeLang
,
524 DIType
*VTableHolder
, StringRef UniqueIdentifier
, DIType
*Specification
,
525 uint32_t NumExtraInhabitants
) {
526 auto *R
= DICompositeType::get(
527 VMContext
, dwarf::DW_TAG_structure_type
, Name
, File
, LineNumber
,
528 getNonCompileUnitScope(Context
), DerivedFrom
, SizeInBits
, AlignInBits
, 0,
529 Flags
, Elements
, RunTimeLang
, VTableHolder
, nullptr, UniqueIdentifier
,
530 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, Specification
,
531 NumExtraInhabitants
);
532 trackIfUnresolved(R
);
536 DICompositeType
*DIBuilder::createUnionType(
537 DIScope
*Scope
, StringRef Name
, DIFile
*File
, unsigned LineNumber
,
538 uint64_t SizeInBits
, uint32_t AlignInBits
, DINode::DIFlags Flags
,
539 DINodeArray Elements
, unsigned RunTimeLang
, StringRef UniqueIdentifier
) {
540 auto *R
= DICompositeType::get(
541 VMContext
, dwarf::DW_TAG_union_type
, Name
, File
, LineNumber
,
542 getNonCompileUnitScope(Scope
), nullptr, SizeInBits
, AlignInBits
, 0, Flags
,
543 Elements
, RunTimeLang
, nullptr, nullptr, UniqueIdentifier
);
544 trackIfUnresolved(R
);
549 DIBuilder::createVariantPart(DIScope
*Scope
, StringRef Name
, DIFile
*File
,
550 unsigned LineNumber
, uint64_t SizeInBits
,
551 uint32_t AlignInBits
, DINode::DIFlags Flags
,
552 DIDerivedType
*Discriminator
, DINodeArray Elements
,
553 StringRef UniqueIdentifier
) {
554 auto *R
= DICompositeType::get(
555 VMContext
, dwarf::DW_TAG_variant_part
, Name
, File
, LineNumber
,
556 getNonCompileUnitScope(Scope
), nullptr, SizeInBits
, AlignInBits
, 0, Flags
,
557 Elements
, 0, nullptr, nullptr, UniqueIdentifier
, Discriminator
);
558 trackIfUnresolved(R
);
562 DISubroutineType
*DIBuilder::createSubroutineType(DITypeRefArray ParameterTypes
,
563 DINode::DIFlags Flags
,
565 return DISubroutineType::get(VMContext
, Flags
, CC
, ParameterTypes
);
569 DIBuilder::createEnumerationType(DIScope
*Scope
, StringRef Name
, DIFile
*File
,
570 unsigned LineNumber
, uint64_t SizeInBits
,
571 uint32_t AlignInBits
, DINodeArray Elements
,
572 DIType
*UnderlyingType
, unsigned RunTimeLang
,
573 StringRef UniqueIdentifier
, bool IsScoped
) {
574 auto *CTy
= DICompositeType::get(
575 VMContext
, dwarf::DW_TAG_enumeration_type
, Name
, File
, LineNumber
,
576 getNonCompileUnitScope(Scope
), UnderlyingType
, SizeInBits
, AlignInBits
, 0,
577 IsScoped
? DINode::FlagEnumClass
: DINode::FlagZero
, Elements
,
578 RunTimeLang
, nullptr, nullptr, UniqueIdentifier
);
579 AllEnumTypes
.emplace_back(CTy
);
580 trackIfUnresolved(CTy
);
584 DIDerivedType
*DIBuilder::createSetType(DIScope
*Scope
, StringRef Name
,
585 DIFile
*File
, unsigned LineNo
,
587 uint32_t AlignInBits
, DIType
*Ty
) {
588 auto *R
= DIDerivedType::get(VMContext
, dwarf::DW_TAG_set_type
, Name
, File
,
589 LineNo
, getNonCompileUnitScope(Scope
), Ty
,
590 SizeInBits
, AlignInBits
, 0, std::nullopt
,
591 std::nullopt
, DINode::FlagZero
);
592 trackIfUnresolved(R
);
597 DIBuilder::createArrayType(uint64_t Size
, uint32_t AlignInBits
, DIType
*Ty
,
598 DINodeArray Subscripts
,
599 PointerUnion
<DIExpression
*, DIVariable
*> DL
,
600 PointerUnion
<DIExpression
*, DIVariable
*> AS
,
601 PointerUnion
<DIExpression
*, DIVariable
*> AL
,
602 PointerUnion
<DIExpression
*, DIVariable
*> RK
) {
603 auto *R
= DICompositeType::get(
604 VMContext
, dwarf::DW_TAG_array_type
, "", nullptr, 0, nullptr, Ty
, Size
,
605 AlignInBits
, 0, DINode::FlagZero
, Subscripts
, 0, nullptr, nullptr, "",
607 isa
<DIExpression
*>(DL
) ? (Metadata
*)cast
<DIExpression
*>(DL
)
608 : (Metadata
*)cast
<DIVariable
*>(DL
),
609 isa
<DIExpression
*>(AS
) ? (Metadata
*)cast
<DIExpression
*>(AS
)
610 : (Metadata
*)cast
<DIVariable
*>(AS
),
611 isa
<DIExpression
*>(AL
) ? (Metadata
*)cast
<DIExpression
*>(AL
)
612 : (Metadata
*)cast
<DIVariable
*>(AL
),
613 isa
<DIExpression
*>(RK
) ? (Metadata
*)cast
<DIExpression
*>(RK
)
614 : (Metadata
*)cast
<DIVariable
*>(RK
));
615 trackIfUnresolved(R
);
619 DICompositeType
*DIBuilder::createVectorType(uint64_t Size
,
620 uint32_t AlignInBits
, DIType
*Ty
,
621 DINodeArray Subscripts
) {
622 auto *R
= DICompositeType::get(VMContext
, dwarf::DW_TAG_array_type
, "",
623 nullptr, 0, nullptr, Ty
, Size
, AlignInBits
, 0,
624 DINode::FlagVector
, Subscripts
, 0, nullptr);
625 trackIfUnresolved(R
);
629 DISubprogram
*DIBuilder::createArtificialSubprogram(DISubprogram
*SP
) {
630 auto NewSP
= SP
->cloneWithFlags(SP
->getFlags() | DINode::FlagArtificial
);
631 return MDNode::replaceWithDistinct(std::move(NewSP
));
634 static DIType
*createTypeWithFlags(const DIType
*Ty
,
635 DINode::DIFlags FlagsToSet
) {
636 auto NewTy
= Ty
->cloneWithFlags(Ty
->getFlags() | FlagsToSet
);
637 return MDNode::replaceWithUniqued(std::move(NewTy
));
640 DIType
*DIBuilder::createArtificialType(DIType
*Ty
) {
641 // FIXME: Restrict this to the nodes where it's valid.
642 if (Ty
->isArtificial())
644 return createTypeWithFlags(Ty
, DINode::FlagArtificial
);
647 DIType
*DIBuilder::createObjectPointerType(DIType
*Ty
, bool Implicit
) {
648 // FIXME: Restrict this to the nodes where it's valid.
649 if (Ty
->isObjectPointer())
651 DINode::DIFlags Flags
= DINode::FlagObjectPointer
;
654 Flags
|= DINode::FlagArtificial
;
656 return createTypeWithFlags(Ty
, Flags
);
659 void DIBuilder::retainType(DIScope
*T
) {
660 assert(T
&& "Expected non-null type");
661 assert((isa
<DIType
>(T
) || (isa
<DISubprogram
>(T
) &&
662 cast
<DISubprogram
>(T
)->isDefinition() == false)) &&
663 "Expected type or subprogram declaration");
664 AllRetainTypes
.emplace_back(T
);
667 DIBasicType
*DIBuilder::createUnspecifiedParameter() { return nullptr; }
670 DIBuilder::createForwardDecl(unsigned Tag
, StringRef Name
, DIScope
*Scope
,
671 DIFile
*F
, unsigned Line
, unsigned RuntimeLang
,
672 uint64_t SizeInBits
, uint32_t AlignInBits
,
673 StringRef UniqueIdentifier
) {
674 // FIXME: Define in terms of createReplaceableForwardDecl() by calling
675 // replaceWithUniqued().
676 auto *RetTy
= DICompositeType::get(
677 VMContext
, Tag
, Name
, F
, Line
, getNonCompileUnitScope(Scope
), nullptr,
678 SizeInBits
, AlignInBits
, 0, DINode::FlagFwdDecl
, nullptr, RuntimeLang
,
679 nullptr, nullptr, UniqueIdentifier
);
680 trackIfUnresolved(RetTy
);
684 DICompositeType
*DIBuilder::createReplaceableCompositeType(
685 unsigned Tag
, StringRef Name
, DIScope
*Scope
, DIFile
*F
, unsigned Line
,
686 unsigned RuntimeLang
, uint64_t SizeInBits
, uint32_t AlignInBits
,
687 DINode::DIFlags Flags
, StringRef UniqueIdentifier
,
688 DINodeArray Annotations
) {
690 DICompositeType::getTemporary(
691 VMContext
, Tag
, Name
, F
, Line
, getNonCompileUnitScope(Scope
), nullptr,
692 SizeInBits
, AlignInBits
, 0, Flags
, nullptr, RuntimeLang
, nullptr,
693 nullptr, UniqueIdentifier
, nullptr, nullptr, nullptr, nullptr,
694 nullptr, Annotations
)
696 trackIfUnresolved(RetTy
);
700 DINodeArray
DIBuilder::getOrCreateArray(ArrayRef
<Metadata
*> Elements
) {
701 return MDTuple::get(VMContext
, Elements
);
705 DIBuilder::getOrCreateMacroArray(ArrayRef
<Metadata
*> Elements
) {
706 return MDTuple::get(VMContext
, Elements
);
709 DITypeRefArray
DIBuilder::getOrCreateTypeArray(ArrayRef
<Metadata
*> Elements
) {
710 SmallVector
<llvm::Metadata
*, 16> Elts
;
711 for (Metadata
*E
: Elements
) {
712 if (isa_and_nonnull
<MDNode
>(E
))
713 Elts
.push_back(cast
<DIType
>(E
));
717 return DITypeRefArray(MDNode::get(VMContext
, Elts
));
720 DISubrange
*DIBuilder::getOrCreateSubrange(int64_t Lo
, int64_t Count
) {
721 auto *LB
= ConstantAsMetadata::get(
722 ConstantInt::getSigned(Type::getInt64Ty(VMContext
), Lo
));
723 auto *CountNode
= ConstantAsMetadata::get(
724 ConstantInt::getSigned(Type::getInt64Ty(VMContext
), Count
));
725 return DISubrange::get(VMContext
, CountNode
, LB
, nullptr, nullptr);
728 DISubrange
*DIBuilder::getOrCreateSubrange(int64_t Lo
, Metadata
*CountNode
) {
729 auto *LB
= ConstantAsMetadata::get(
730 ConstantInt::getSigned(Type::getInt64Ty(VMContext
), Lo
));
731 return DISubrange::get(VMContext
, CountNode
, LB
, nullptr, nullptr);
734 DISubrange
*DIBuilder::getOrCreateSubrange(Metadata
*CountNode
, Metadata
*LB
,
735 Metadata
*UB
, Metadata
*Stride
) {
736 return DISubrange::get(VMContext
, CountNode
, LB
, UB
, Stride
);
739 DIGenericSubrange
*DIBuilder::getOrCreateGenericSubrange(
740 DIGenericSubrange::BoundType CountNode
, DIGenericSubrange::BoundType LB
,
741 DIGenericSubrange::BoundType UB
, DIGenericSubrange::BoundType Stride
) {
742 auto ConvToMetadata
= [&](DIGenericSubrange::BoundType Bound
) -> Metadata
* {
743 return isa
<DIExpression
*>(Bound
) ? (Metadata
*)cast
<DIExpression
*>(Bound
)
744 : (Metadata
*)cast
<DIVariable
*>(Bound
);
746 return DIGenericSubrange::get(VMContext
, ConvToMetadata(CountNode
),
747 ConvToMetadata(LB
), ConvToMetadata(UB
),
748 ConvToMetadata(Stride
));
751 static void checkGlobalVariableScope(DIScope
*Context
) {
754 dyn_cast_or_null
<DICompositeType
>(getNonCompileUnitScope(Context
)))
755 assert(CT
->getIdentifier().empty() &&
756 "Context of a global variable should not be a type with identifier");
760 DIGlobalVariableExpression
*DIBuilder::createGlobalVariableExpression(
761 DIScope
*Context
, StringRef Name
, StringRef LinkageName
, DIFile
*F
,
762 unsigned LineNumber
, DIType
*Ty
, bool IsLocalToUnit
, bool isDefined
,
763 DIExpression
*Expr
, MDNode
*Decl
, MDTuple
*TemplateParams
,
764 uint32_t AlignInBits
, DINodeArray Annotations
) {
765 checkGlobalVariableScope(Context
);
767 auto *GV
= DIGlobalVariable::getDistinct(
768 VMContext
, cast_or_null
<DIScope
>(Context
), Name
, LinkageName
, F
,
769 LineNumber
, Ty
, IsLocalToUnit
, isDefined
,
770 cast_or_null
<DIDerivedType
>(Decl
), TemplateParams
, AlignInBits
,
773 Expr
= createExpression();
774 auto *N
= DIGlobalVariableExpression::get(VMContext
, GV
, Expr
);
779 DIGlobalVariable
*DIBuilder::createTempGlobalVariableFwdDecl(
780 DIScope
*Context
, StringRef Name
, StringRef LinkageName
, DIFile
*F
,
781 unsigned LineNumber
, DIType
*Ty
, bool IsLocalToUnit
, MDNode
*Decl
,
782 MDTuple
*TemplateParams
, uint32_t AlignInBits
) {
783 checkGlobalVariableScope(Context
);
785 return DIGlobalVariable::getTemporary(
786 VMContext
, cast_or_null
<DIScope
>(Context
), Name
, LinkageName
, F
,
787 LineNumber
, Ty
, IsLocalToUnit
, false,
788 cast_or_null
<DIDerivedType
>(Decl
), TemplateParams
, AlignInBits
,
793 static DILocalVariable
*createLocalVariable(
794 LLVMContext
&VMContext
,
795 SmallVectorImpl
<TrackingMDNodeRef
> &PreservedNodes
,
796 DIScope
*Context
, StringRef Name
, unsigned ArgNo
, DIFile
*File
,
797 unsigned LineNo
, DIType
*Ty
, bool AlwaysPreserve
, DINode::DIFlags Flags
,
798 uint32_t AlignInBits
, DINodeArray Annotations
= nullptr) {
799 // FIXME: Why doesn't this check for a subprogram or lexical block (AFAICT
800 // the only valid scopes)?
801 auto *Scope
= cast
<DILocalScope
>(Context
);
802 auto *Node
= DILocalVariable::get(VMContext
, Scope
, Name
, File
, LineNo
, Ty
,
803 ArgNo
, Flags
, AlignInBits
, Annotations
);
804 if (AlwaysPreserve
) {
805 // The optimizer may remove local variables. If there is an interest
806 // to preserve variable info in such situation then stash it in a
808 PreservedNodes
.emplace_back(Node
);
813 DILocalVariable
*DIBuilder::createAutoVariable(DIScope
*Scope
, StringRef Name
,
814 DIFile
*File
, unsigned LineNo
,
815 DIType
*Ty
, bool AlwaysPreserve
,
816 DINode::DIFlags Flags
,
817 uint32_t AlignInBits
) {
818 assert(Scope
&& isa
<DILocalScope
>(Scope
) &&
819 "Unexpected scope for a local variable.");
820 return createLocalVariable(
821 VMContext
, getSubprogramNodesTrackingVector(Scope
), Scope
, Name
,
822 /* ArgNo */ 0, File
, LineNo
, Ty
, AlwaysPreserve
, Flags
, AlignInBits
);
825 DILocalVariable
*DIBuilder::createParameterVariable(
826 DIScope
*Scope
, StringRef Name
, unsigned ArgNo
, DIFile
*File
,
827 unsigned LineNo
, DIType
*Ty
, bool AlwaysPreserve
, DINode::DIFlags Flags
,
828 DINodeArray Annotations
) {
829 assert(ArgNo
&& "Expected non-zero argument number for parameter");
830 assert(Scope
&& isa
<DILocalScope
>(Scope
) &&
831 "Unexpected scope for a local variable.");
832 return createLocalVariable(
833 VMContext
, getSubprogramNodesTrackingVector(Scope
), Scope
, Name
, ArgNo
,
834 File
, LineNo
, Ty
, AlwaysPreserve
, Flags
, /*AlignInBits=*/0, Annotations
);
837 DILabel
*DIBuilder::createLabel(DIScope
*Context
, StringRef Name
, DIFile
*File
,
838 unsigned LineNo
, bool AlwaysPreserve
) {
839 auto *Scope
= cast
<DILocalScope
>(Context
);
840 auto *Node
= DILabel::get(VMContext
, Scope
, Name
, File
, LineNo
);
842 if (AlwaysPreserve
) {
843 /// The optimizer may remove labels. If there is an interest
844 /// to preserve label info in such situation then append it to
845 /// the list of retained nodes of the DISubprogram.
846 getSubprogramNodesTrackingVector(Scope
).emplace_back(Node
);
851 DIExpression
*DIBuilder::createExpression(ArrayRef
<uint64_t> Addr
) {
852 return DIExpression::get(VMContext
, Addr
);
855 template <class... Ts
>
856 static DISubprogram
*getSubprogram(bool IsDistinct
, Ts
&&...Args
) {
858 return DISubprogram::getDistinct(std::forward
<Ts
>(Args
)...);
859 return DISubprogram::get(std::forward
<Ts
>(Args
)...);
862 DISubprogram
*DIBuilder::createFunction(
863 DIScope
*Context
, StringRef Name
, StringRef LinkageName
, DIFile
*File
,
864 unsigned LineNo
, DISubroutineType
*Ty
, unsigned ScopeLine
,
865 DINode::DIFlags Flags
, DISubprogram::DISPFlags SPFlags
,
866 DITemplateParameterArray TParams
, DISubprogram
*Decl
,
867 DITypeArray ThrownTypes
, DINodeArray Annotations
,
868 StringRef TargetFuncName
) {
869 bool IsDefinition
= SPFlags
& DISubprogram::SPFlagDefinition
;
870 auto *Node
= getSubprogram(
871 /*IsDistinct=*/IsDefinition
, VMContext
, getNonCompileUnitScope(Context
),
872 Name
, LinkageName
, File
, LineNo
, Ty
, ScopeLine
, nullptr, 0, 0, Flags
,
873 SPFlags
, IsDefinition
? CUNode
: nullptr, TParams
, Decl
, nullptr,
874 ThrownTypes
, Annotations
, TargetFuncName
);
877 AllSubprograms
.push_back(Node
);
878 trackIfUnresolved(Node
);
882 DISubprogram
*DIBuilder::createTempFunctionFwdDecl(
883 DIScope
*Context
, StringRef Name
, StringRef LinkageName
, DIFile
*File
,
884 unsigned LineNo
, DISubroutineType
*Ty
, unsigned ScopeLine
,
885 DINode::DIFlags Flags
, DISubprogram::DISPFlags SPFlags
,
886 DITemplateParameterArray TParams
, DISubprogram
*Decl
,
887 DITypeArray ThrownTypes
) {
888 bool IsDefinition
= SPFlags
& DISubprogram::SPFlagDefinition
;
889 return DISubprogram::getTemporary(VMContext
, getNonCompileUnitScope(Context
),
890 Name
, LinkageName
, File
, LineNo
, Ty
,
891 ScopeLine
, nullptr, 0, 0, Flags
, SPFlags
,
892 IsDefinition
? CUNode
: nullptr, TParams
,
893 Decl
, nullptr, ThrownTypes
)
897 DISubprogram
*DIBuilder::createMethod(
898 DIScope
*Context
, StringRef Name
, StringRef LinkageName
, DIFile
*F
,
899 unsigned LineNo
, DISubroutineType
*Ty
, unsigned VIndex
, int ThisAdjustment
,
900 DIType
*VTableHolder
, DINode::DIFlags Flags
,
901 DISubprogram::DISPFlags SPFlags
, DITemplateParameterArray TParams
,
902 DITypeArray ThrownTypes
) {
903 assert(getNonCompileUnitScope(Context
) &&
904 "Methods should have both a Context and a context that isn't "
905 "the compile unit.");
906 // FIXME: Do we want to use different scope/lines?
907 bool IsDefinition
= SPFlags
& DISubprogram::SPFlagDefinition
;
908 auto *SP
= getSubprogram(
909 /*IsDistinct=*/IsDefinition
, VMContext
, cast
<DIScope
>(Context
), Name
,
910 LinkageName
, F
, LineNo
, Ty
, LineNo
, VTableHolder
, VIndex
, ThisAdjustment
,
911 Flags
, SPFlags
, IsDefinition
? CUNode
: nullptr, TParams
, nullptr,
912 nullptr, ThrownTypes
);
915 AllSubprograms
.push_back(SP
);
916 trackIfUnresolved(SP
);
920 DICommonBlock
*DIBuilder::createCommonBlock(DIScope
*Scope
,
921 DIGlobalVariable
*Decl
,
922 StringRef Name
, DIFile
*File
,
924 return DICommonBlock::get(VMContext
, Scope
, Decl
, Name
, File
, LineNo
);
927 DINamespace
*DIBuilder::createNameSpace(DIScope
*Scope
, StringRef Name
,
928 bool ExportSymbols
) {
930 // It is okay to *not* make anonymous top-level namespaces distinct, because
931 // all nodes that have an anonymous namespace as their parent scope are
932 // guaranteed to be unique and/or are linked to their containing
933 // DICompileUnit. This decision is an explicit tradeoff of link time versus
934 // memory usage versus code simplicity and may get revisited in the future.
935 return DINamespace::get(VMContext
, getNonCompileUnitScope(Scope
), Name
,
939 DIModule
*DIBuilder::createModule(DIScope
*Scope
, StringRef Name
,
940 StringRef ConfigurationMacros
,
941 StringRef IncludePath
, StringRef APINotesFile
,
942 DIFile
*File
, unsigned LineNo
, bool IsDecl
) {
943 return DIModule::get(VMContext
, File
, getNonCompileUnitScope(Scope
), Name
,
944 ConfigurationMacros
, IncludePath
, APINotesFile
, LineNo
,
948 DILexicalBlockFile
*DIBuilder::createLexicalBlockFile(DIScope
*Scope
,
950 unsigned Discriminator
) {
951 return DILexicalBlockFile::get(VMContext
, Scope
, File
, Discriminator
);
954 DILexicalBlock
*DIBuilder::createLexicalBlock(DIScope
*Scope
, DIFile
*File
,
955 unsigned Line
, unsigned Col
) {
956 // Make these distinct, to avoid merging two lexical blocks on the same
958 return DILexicalBlock::getDistinct(VMContext
, getNonCompileUnitScope(Scope
),
962 DbgInstPtr
DIBuilder::insertDeclare(Value
*Storage
, DILocalVariable
*VarInfo
,
963 DIExpression
*Expr
, const DILocation
*DL
,
964 Instruction
*InsertBefore
) {
965 return insertDeclare(Storage
, VarInfo
, Expr
, DL
, InsertBefore
->getParent(),
969 DbgInstPtr
DIBuilder::insertDeclare(Value
*Storage
, DILocalVariable
*VarInfo
,
970 DIExpression
*Expr
, const DILocation
*DL
,
971 BasicBlock
*InsertAtEnd
) {
972 // If this block already has a terminator then insert this intrinsic before
973 // the terminator. Otherwise, put it at the end of the block.
974 Instruction
*InsertBefore
= InsertAtEnd
->getTerminator();
975 return insertDeclare(Storage
, VarInfo
, Expr
, DL
, InsertAtEnd
, InsertBefore
);
978 DbgInstPtr
DIBuilder::insertDbgAssign(Instruction
*LinkedInstr
, Value
*Val
,
979 DILocalVariable
*SrcVar
,
980 DIExpression
*ValExpr
, Value
*Addr
,
981 DIExpression
*AddrExpr
,
982 const DILocation
*DL
) {
983 auto *Link
= cast_or_null
<DIAssignID
>(
984 LinkedInstr
->getMetadata(LLVMContext::MD_DIAssignID
));
985 assert(Link
&& "Linked instruction must have DIAssign metadata attached");
987 if (M
.IsNewDbgInfoFormat
) {
988 DbgVariableRecord
*DVR
= DbgVariableRecord::createDVRAssign(
989 Val
, SrcVar
, ValExpr
, Link
, Addr
, AddrExpr
, DL
);
990 BasicBlock
*InsertBB
= LinkedInstr
->getParent();
991 // Insert after LinkedInstr.
992 BasicBlock::iterator NextIt
= std::next(LinkedInstr
->getIterator());
993 Instruction
*InsertBefore
= NextIt
== InsertBB
->end() ? nullptr : &*NextIt
;
994 insertDbgVariableRecord(DVR
, InsertBB
, InsertBefore
, true);
998 LLVMContext
&Ctx
= LinkedInstr
->getContext();
999 Module
*M
= LinkedInstr
->getModule();
1001 AssignFn
= Intrinsic::getOrInsertDeclaration(M
, Intrinsic::dbg_assign
);
1003 std::array
<Value
*, 6> Args
= {
1004 MetadataAsValue::get(Ctx
, ValueAsMetadata::get(Val
)),
1005 MetadataAsValue::get(Ctx
, SrcVar
),
1006 MetadataAsValue::get(Ctx
, ValExpr
),
1007 MetadataAsValue::get(Ctx
, Link
),
1008 MetadataAsValue::get(Ctx
, ValueAsMetadata::get(Addr
)),
1009 MetadataAsValue::get(Ctx
, AddrExpr
),
1013 B
.SetCurrentDebugLocation(DL
);
1015 auto *DVI
= cast
<DbgAssignIntrinsic
>(B
.CreateCall(AssignFn
, Args
));
1016 DVI
->insertAfter(LinkedInstr
->getIterator());
1020 DbgInstPtr
DIBuilder::insertLabel(DILabel
*LabelInfo
, const DILocation
*DL
,
1021 Instruction
*InsertBefore
) {
1022 return insertLabel(LabelInfo
, DL
,
1023 InsertBefore
? InsertBefore
->getParent() : nullptr,
1027 DbgInstPtr
DIBuilder::insertLabel(DILabel
*LabelInfo
, const DILocation
*DL
,
1028 BasicBlock
*InsertAtEnd
) {
1029 return insertLabel(LabelInfo
, DL
, InsertAtEnd
, nullptr);
1032 DbgInstPtr
DIBuilder::insertDbgValueIntrinsic(Value
*V
,
1033 DILocalVariable
*VarInfo
,
1035 const DILocation
*DL
,
1036 Instruction
*InsertBefore
) {
1037 DbgInstPtr DVI
= insertDbgValueIntrinsic(
1038 V
, VarInfo
, Expr
, DL
, InsertBefore
? InsertBefore
->getParent() : nullptr,
1040 if (auto *Inst
= dyn_cast
<Instruction
*>(DVI
))
1041 cast
<CallInst
>(Inst
)->setTailCall();
1045 DbgInstPtr
DIBuilder::insertDbgValueIntrinsic(Value
*V
,
1046 DILocalVariable
*VarInfo
,
1048 const DILocation
*DL
,
1049 BasicBlock
*InsertAtEnd
) {
1050 return insertDbgValueIntrinsic(V
, VarInfo
, Expr
, DL
, InsertAtEnd
, nullptr);
1053 /// Initialize IRBuilder for inserting dbg.declare and dbg.value intrinsics.
1054 /// This abstracts over the various ways to specify an insert position.
1055 static void initIRBuilder(IRBuilder
<> &Builder
, const DILocation
*DL
,
1056 BasicBlock
*InsertBB
, Instruction
*InsertBefore
) {
1058 Builder
.SetInsertPoint(InsertBefore
);
1060 Builder
.SetInsertPoint(InsertBB
);
1061 Builder
.SetCurrentDebugLocation(DL
);
1064 static Value
*getDbgIntrinsicValueImpl(LLVMContext
&VMContext
, Value
*V
) {
1065 assert(V
&& "no value passed to dbg intrinsic");
1066 return MetadataAsValue::get(VMContext
, ValueAsMetadata::get(V
));
1069 static Function
*getDeclareIntrin(Module
&M
) {
1070 return Intrinsic::getOrInsertDeclaration(&M
, Intrinsic::dbg_declare
);
1073 DbgInstPtr
DIBuilder::insertDbgValueIntrinsic(
1074 llvm::Value
*Val
, DILocalVariable
*VarInfo
, DIExpression
*Expr
,
1075 const DILocation
*DL
, BasicBlock
*InsertBB
, Instruction
*InsertBefore
) {
1076 if (M
.IsNewDbgInfoFormat
) {
1077 DbgVariableRecord
*DVR
=
1078 DbgVariableRecord::createDbgVariableRecord(Val
, VarInfo
, Expr
, DL
);
1079 insertDbgVariableRecord(DVR
, InsertBB
, InsertBefore
);
1084 ValueFn
= Intrinsic::getOrInsertDeclaration(&M
, Intrinsic::dbg_value
);
1085 return insertDbgIntrinsic(ValueFn
, Val
, VarInfo
, Expr
, DL
, InsertBB
,
1089 DbgInstPtr
DIBuilder::insertDeclare(Value
*Storage
, DILocalVariable
*VarInfo
,
1090 DIExpression
*Expr
, const DILocation
*DL
,
1091 BasicBlock
*InsertBB
,
1092 Instruction
*InsertBefore
) {
1093 assert(VarInfo
&& "empty or invalid DILocalVariable* passed to dbg.declare");
1094 assert(DL
&& "Expected debug loc");
1095 assert(DL
->getScope()->getSubprogram() ==
1096 VarInfo
->getScope()->getSubprogram() &&
1097 "Expected matching subprograms");
1099 if (M
.IsNewDbgInfoFormat
) {
1100 DbgVariableRecord
*DVR
=
1101 DbgVariableRecord::createDVRDeclare(Storage
, VarInfo
, Expr
, DL
);
1102 insertDbgVariableRecord(DVR
, InsertBB
, InsertBefore
);
1107 DeclareFn
= getDeclareIntrin(M
);
1109 trackIfUnresolved(VarInfo
);
1110 trackIfUnresolved(Expr
);
1111 Value
*Args
[] = {getDbgIntrinsicValueImpl(VMContext
, Storage
),
1112 MetadataAsValue::get(VMContext
, VarInfo
),
1113 MetadataAsValue::get(VMContext
, Expr
)};
1115 IRBuilder
<> B(DL
->getContext());
1116 initIRBuilder(B
, DL
, InsertBB
, InsertBefore
);
1117 return B
.CreateCall(DeclareFn
, Args
);
1120 void DIBuilder::insertDbgVariableRecord(DbgVariableRecord
*DVR
,
1121 BasicBlock
*InsertBB
,
1122 Instruction
*InsertBefore
,
1123 bool InsertAtHead
) {
1124 assert(InsertBefore
|| InsertBB
);
1125 trackIfUnresolved(DVR
->getVariable());
1126 trackIfUnresolved(DVR
->getExpression());
1127 if (DVR
->isDbgAssign())
1128 trackIfUnresolved(DVR
->getAddressExpression());
1130 BasicBlock::iterator InsertPt
;
1131 if (InsertBB
&& InsertBefore
)
1132 InsertPt
= InsertBefore
->getIterator();
1134 InsertPt
= InsertBB
->end();
1135 InsertPt
.setHeadBit(InsertAtHead
);
1136 InsertBB
->insertDbgRecordBefore(DVR
, InsertPt
);
1139 Instruction
*DIBuilder::insertDbgIntrinsic(llvm::Function
*IntrinsicFn
,
1140 Value
*V
, DILocalVariable
*VarInfo
,
1142 const DILocation
*DL
,
1143 BasicBlock
*InsertBB
,
1144 Instruction
*InsertBefore
) {
1145 assert(IntrinsicFn
&& "must pass a non-null intrinsic function");
1146 assert(V
&& "must pass a value to a dbg intrinsic");
1148 "empty or invalid DILocalVariable* passed to debug intrinsic");
1149 assert(DL
&& "Expected debug loc");
1150 assert(DL
->getScope()->getSubprogram() ==
1151 VarInfo
->getScope()->getSubprogram() &&
1152 "Expected matching subprograms");
1154 trackIfUnresolved(VarInfo
);
1155 trackIfUnresolved(Expr
);
1156 Value
*Args
[] = {getDbgIntrinsicValueImpl(VMContext
, V
),
1157 MetadataAsValue::get(VMContext
, VarInfo
),
1158 MetadataAsValue::get(VMContext
, Expr
)};
1160 IRBuilder
<> B(DL
->getContext());
1161 initIRBuilder(B
, DL
, InsertBB
, InsertBefore
);
1162 return B
.CreateCall(IntrinsicFn
, Args
);
1165 DbgInstPtr
DIBuilder::insertLabel(DILabel
*LabelInfo
, const DILocation
*DL
,
1166 BasicBlock
*InsertBB
,
1167 Instruction
*InsertBefore
) {
1168 assert(LabelInfo
&& "empty or invalid DILabel* passed to dbg.label");
1169 assert(DL
&& "Expected debug loc");
1170 assert(DL
->getScope()->getSubprogram() ==
1171 LabelInfo
->getScope()->getSubprogram() &&
1172 "Expected matching subprograms");
1174 trackIfUnresolved(LabelInfo
);
1175 if (M
.IsNewDbgInfoFormat
) {
1176 DbgLabelRecord
*DLR
= new DbgLabelRecord(LabelInfo
, DL
);
1177 if (InsertBB
&& InsertBefore
)
1178 InsertBB
->insertDbgRecordBefore(DLR
, InsertBefore
->getIterator());
1180 InsertBB
->insertDbgRecordBefore(DLR
, InsertBB
->end());
1185 LabelFn
= Intrinsic::getOrInsertDeclaration(&M
, Intrinsic::dbg_label
);
1187 Value
*Args
[] = {MetadataAsValue::get(VMContext
, LabelInfo
)};
1189 IRBuilder
<> B(DL
->getContext());
1190 initIRBuilder(B
, DL
, InsertBB
, InsertBefore
);
1191 return B
.CreateCall(LabelFn
, Args
);
1194 void DIBuilder::replaceVTableHolder(DICompositeType
*&T
, DIType
*VTableHolder
) {
1196 TypedTrackingMDRef
<DICompositeType
> N(T
);
1197 N
->replaceVTableHolder(VTableHolder
);
1201 // If this didn't create a self-reference, just return.
1202 if (T
!= VTableHolder
)
1205 // Look for unresolved operands. T will drop RAUW support, orphaning any
1206 // cycles underneath it.
1207 if (T
->isResolved())
1208 for (const MDOperand
&O
: T
->operands())
1209 if (auto *N
= dyn_cast_or_null
<MDNode
>(O
))
1210 trackIfUnresolved(N
);
1213 void DIBuilder::replaceArrays(DICompositeType
*&T
, DINodeArray Elements
,
1214 DINodeArray TParams
) {
1216 TypedTrackingMDRef
<DICompositeType
> N(T
);
1218 N
->replaceElements(Elements
);
1220 N
->replaceTemplateParams(DITemplateParameterArray(TParams
));
1224 // If T isn't resolved, there's no problem.
1225 if (!T
->isResolved())
1228 // If T is resolved, it may be due to a self-reference cycle. Track the
1229 // arrays explicitly if they're unresolved, or else the cycles will be
1232 trackIfUnresolved(Elements
.get());
1234 trackIfUnresolved(TParams
.get());