1 //===- DebugInfoMetadata.cpp - Implement debug info metadata --------------===//
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 debug info Metadata classes.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/IR/DebugInfoMetadata.h"
14 #include "LLVMContextImpl.h"
15 #include "MetadataImpl.h"
16 #include "llvm/ADT/SmallSet.h"
17 #include "llvm/ADT/StringSwitch.h"
18 #include "llvm/IR/DIBuilder.h"
19 #include "llvm/IR/Function.h"
20 #include "llvm/IR/Instructions.h"
27 // Use FS-AFDO discriminator.
28 cl::opt
<bool> EnableFSDiscriminator(
29 "enable-fs-discriminator", cl::Hidden
, cl::init(false),
30 cl::desc("Enable adding flow sensitive discriminators"));
33 const DIExpression::FragmentInfo
DebugVariable::DefaultFragment
= {
34 std::numeric_limits
<uint64_t>::max(), std::numeric_limits
<uint64_t>::min()};
36 DILocation::DILocation(LLVMContext
&C
, StorageType Storage
, unsigned Line
,
37 unsigned Column
, ArrayRef
<Metadata
*> MDs
,
39 : MDNode(C
, DILocationKind
, Storage
, MDs
) {
40 assert((MDs
.size() == 1 || MDs
.size() == 2) &&
41 "Expected a scope and optional inlined-at");
43 // Set line and column.
44 assert(Column
< (1u << 16) && "Expected 16-bit column");
46 SubclassData32
= Line
;
47 SubclassData16
= Column
;
49 setImplicitCode(ImplicitCode
);
52 static void adjustColumn(unsigned &Column
) {
53 // Set to unknown on overflow. We only have 16 bits to play with here.
54 if (Column
>= (1u << 16))
58 DILocation
*DILocation::getImpl(LLVMContext
&Context
, unsigned Line
,
59 unsigned Column
, Metadata
*Scope
,
60 Metadata
*InlinedAt
, bool ImplicitCode
,
61 StorageType Storage
, bool ShouldCreate
) {
65 if (Storage
== Uniqued
) {
66 if (auto *N
= getUniqued(Context
.pImpl
->DILocations
,
67 DILocationInfo::KeyTy(Line
, Column
, Scope
,
68 InlinedAt
, ImplicitCode
)))
73 assert(ShouldCreate
&& "Expected non-uniqued nodes to always be created");
76 SmallVector
<Metadata
*, 2> Ops
;
79 Ops
.push_back(InlinedAt
);
80 return storeImpl(new (Ops
.size()) DILocation(Context
, Storage
, Line
, Column
,
82 Storage
, Context
.pImpl
->DILocations
);
86 DILocation
*DILocation::getMergedLocations(ArrayRef
<const DILocation
*> Locs
) {
91 auto *Merged
= Locs
[0];
92 for (const DILocation
*L
: llvm::drop_begin(Locs
)) {
93 Merged
= getMergedLocation(Merged
, L
);
94 if (Merged
== nullptr)
100 const DILocation
*DILocation::getMergedLocation(const DILocation
*LocA
,
101 const DILocation
*LocB
) {
108 SmallPtrSet
<DILocation
*, 5> InlinedLocationsA
;
109 for (DILocation
*L
= LocA
->getInlinedAt(); L
; L
= L
->getInlinedAt())
110 InlinedLocationsA
.insert(L
);
111 SmallSet
<std::pair
<DIScope
*, DILocation
*>, 5> Locations
;
112 DIScope
*S
= LocA
->getScope();
113 DILocation
*L
= LocA
->getInlinedAt();
115 Locations
.insert(std::make_pair(S
, L
));
119 L
= L
->getInlinedAt();
122 const DILocation
*Result
= LocB
;
123 S
= LocB
->getScope();
124 L
= LocB
->getInlinedAt();
126 if (Locations
.count(std::make_pair(S
, L
)))
131 L
= L
->getInlinedAt();
135 // If the two locations are irreconsilable, just pick one. This is misleading,
136 // but on the other hand, it's a "line 0" location.
137 if (!S
|| !isa
<DILocalScope
>(S
))
138 S
= LocA
->getScope();
139 return DILocation::get(Result
->getContext(), 0, 0, S
, L
);
142 Optional
<unsigned> DILocation::encodeDiscriminator(unsigned BD
, unsigned DF
, unsigned CI
) {
143 std::array
<unsigned, 3> Components
= {BD
, DF
, CI
};
144 uint64_t RemainingWork
= 0U;
145 // We use RemainingWork to figure out if we have no remaining components to
146 // encode. For example: if BD != 0 but DF == 0 && CI == 0, we don't need to
147 // encode anything for the latter 2.
148 // Since any of the input components is at most 32 bits, their sum will be
149 // less than 34 bits, and thus RemainingWork won't overflow.
150 RemainingWork
= std::accumulate(Components
.begin(), Components
.end(), RemainingWork
);
154 unsigned NextBitInsertionIndex
= 0;
155 while (RemainingWork
> 0) {
156 unsigned C
= Components
[I
++];
158 unsigned EC
= encodeComponent(C
);
159 Ret
|= (EC
<< NextBitInsertionIndex
);
160 NextBitInsertionIndex
+= encodingBits(C
);
163 // Encoding may be unsuccessful because of overflow. We determine success by
164 // checking equivalence of components before & after encoding. Alternatively,
165 // we could determine Success during encoding, but the current alternative is
167 unsigned TBD
, TDF
, TCI
= 0;
168 decodeDiscriminator(Ret
, TBD
, TDF
, TCI
);
169 if (TBD
== BD
&& TDF
== DF
&& TCI
== CI
)
174 void DILocation::decodeDiscriminator(unsigned D
, unsigned &BD
, unsigned &DF
,
176 BD
= getUnsignedFromPrefixEncoding(D
);
177 DF
= getUnsignedFromPrefixEncoding(getNextComponentInDiscriminator(D
));
178 CI
= getUnsignedFromPrefixEncoding(
179 getNextComponentInDiscriminator(getNextComponentInDiscriminator(D
)));
183 DINode::DIFlags
DINode::getFlag(StringRef Flag
) {
184 return StringSwitch
<DIFlags
>(Flag
)
185 #define HANDLE_DI_FLAG(ID, NAME) .Case("DIFlag" #NAME, Flag##NAME)
186 #include "llvm/IR/DebugInfoFlags.def"
187 .Default(DINode::FlagZero
);
190 StringRef
DINode::getFlagString(DIFlags Flag
) {
192 #define HANDLE_DI_FLAG(ID, NAME) \
194 return "DIFlag" #NAME;
195 #include "llvm/IR/DebugInfoFlags.def"
200 DINode::DIFlags
DINode::splitFlags(DIFlags Flags
,
201 SmallVectorImpl
<DIFlags
> &SplitFlags
) {
202 // Flags that are packed together need to be specially handled, so
203 // that, for example, we emit "DIFlagPublic" and not
204 // "DIFlagPrivate | DIFlagProtected".
205 if (DIFlags A
= Flags
& FlagAccessibility
) {
206 if (A
== FlagPrivate
)
207 SplitFlags
.push_back(FlagPrivate
);
208 else if (A
== FlagProtected
)
209 SplitFlags
.push_back(FlagProtected
);
211 SplitFlags
.push_back(FlagPublic
);
214 if (DIFlags R
= Flags
& FlagPtrToMemberRep
) {
215 if (R
== FlagSingleInheritance
)
216 SplitFlags
.push_back(FlagSingleInheritance
);
217 else if (R
== FlagMultipleInheritance
)
218 SplitFlags
.push_back(FlagMultipleInheritance
);
220 SplitFlags
.push_back(FlagVirtualInheritance
);
223 if ((Flags
& FlagIndirectVirtualBase
) == FlagIndirectVirtualBase
) {
224 Flags
&= ~FlagIndirectVirtualBase
;
225 SplitFlags
.push_back(FlagIndirectVirtualBase
);
228 #define HANDLE_DI_FLAG(ID, NAME) \
229 if (DIFlags Bit = Flags & Flag##NAME) { \
230 SplitFlags.push_back(Bit); \
233 #include "llvm/IR/DebugInfoFlags.def"
237 DIScope
*DIScope::getScope() const {
238 if (auto *T
= dyn_cast
<DIType
>(this))
239 return T
->getScope();
241 if (auto *SP
= dyn_cast
<DISubprogram
>(this))
242 return SP
->getScope();
244 if (auto *LB
= dyn_cast
<DILexicalBlockBase
>(this))
245 return LB
->getScope();
247 if (auto *NS
= dyn_cast
<DINamespace
>(this))
248 return NS
->getScope();
250 if (auto *CB
= dyn_cast
<DICommonBlock
>(this))
251 return CB
->getScope();
253 if (auto *M
= dyn_cast
<DIModule
>(this))
254 return M
->getScope();
256 assert((isa
<DIFile
>(this) || isa
<DICompileUnit
>(this)) &&
257 "Unhandled type of scope.");
261 StringRef
DIScope::getName() const {
262 if (auto *T
= dyn_cast
<DIType
>(this))
264 if (auto *SP
= dyn_cast
<DISubprogram
>(this))
265 return SP
->getName();
266 if (auto *NS
= dyn_cast
<DINamespace
>(this))
267 return NS
->getName();
268 if (auto *CB
= dyn_cast
<DICommonBlock
>(this))
269 return CB
->getName();
270 if (auto *M
= dyn_cast
<DIModule
>(this))
272 assert((isa
<DILexicalBlockBase
>(this) || isa
<DIFile
>(this) ||
273 isa
<DICompileUnit
>(this)) &&
274 "Unhandled type of scope.");
279 static bool isCanonical(const MDString
*S
) {
280 return !S
|| !S
->getString().empty();
284 GenericDINode
*GenericDINode::getImpl(LLVMContext
&Context
, unsigned Tag
,
286 ArrayRef
<Metadata
*> DwarfOps
,
287 StorageType Storage
, bool ShouldCreate
) {
289 if (Storage
== Uniqued
) {
290 GenericDINodeInfo::KeyTy
Key(Tag
, Header
, DwarfOps
);
291 if (auto *N
= getUniqued(Context
.pImpl
->GenericDINodes
, Key
))
295 Hash
= Key
.getHash();
297 assert(ShouldCreate
&& "Expected non-uniqued nodes to always be created");
300 // Use a nullptr for empty headers.
301 assert(isCanonical(Header
) && "Expected canonical MDString");
302 Metadata
*PreOps
[] = {Header
};
303 return storeImpl(new (DwarfOps
.size() + 1) GenericDINode(
304 Context
, Storage
, Hash
, Tag
, PreOps
, DwarfOps
),
305 Storage
, Context
.pImpl
->GenericDINodes
);
308 void GenericDINode::recalculateHash() {
309 setHash(GenericDINodeInfo::KeyTy::calculateHash(this));
312 #define UNWRAP_ARGS_IMPL(...) __VA_ARGS__
313 #define UNWRAP_ARGS(ARGS) UNWRAP_ARGS_IMPL ARGS
314 #define DEFINE_GETIMPL_LOOKUP(CLASS, ARGS) \
316 if (Storage == Uniqued) { \
317 if (auto *N = getUniqued(Context.pImpl->CLASS##s, \
318 CLASS##Info::KeyTy(UNWRAP_ARGS(ARGS)))) \
323 assert(ShouldCreate && \
324 "Expected non-uniqued nodes to always be created"); \
327 #define DEFINE_GETIMPL_STORE(CLASS, ARGS, OPS) \
328 return storeImpl(new (array_lengthof(OPS)) \
329 CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS), \
330 Storage, Context.pImpl->CLASS##s)
331 #define DEFINE_GETIMPL_STORE_NO_OPS(CLASS, ARGS) \
332 return storeImpl(new (0u) CLASS(Context, Storage, UNWRAP_ARGS(ARGS)), \
333 Storage, Context.pImpl->CLASS##s)
334 #define DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(CLASS, OPS) \
335 return storeImpl(new (array_lengthof(OPS)) CLASS(Context, Storage, OPS), \
336 Storage, Context.pImpl->CLASS##s)
337 #define DEFINE_GETIMPL_STORE_N(CLASS, ARGS, OPS, NUM_OPS) \
338 return storeImpl(new (NUM_OPS) \
339 CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS), \
340 Storage, Context.pImpl->CLASS##s)
342 DISubrange
*DISubrange::getImpl(LLVMContext
&Context
, int64_t Count
, int64_t Lo
,
343 StorageType Storage
, bool ShouldCreate
) {
344 auto *CountNode
= ConstantAsMetadata::get(
345 ConstantInt::getSigned(Type::getInt64Ty(Context
), Count
));
346 auto *LB
= ConstantAsMetadata::get(
347 ConstantInt::getSigned(Type::getInt64Ty(Context
), Lo
));
348 return getImpl(Context
, CountNode
, LB
, nullptr, nullptr, Storage
,
352 DISubrange
*DISubrange::getImpl(LLVMContext
&Context
, Metadata
*CountNode
,
353 int64_t Lo
, StorageType Storage
,
355 auto *LB
= ConstantAsMetadata::get(
356 ConstantInt::getSigned(Type::getInt64Ty(Context
), Lo
));
357 return getImpl(Context
, CountNode
, LB
, nullptr, nullptr, Storage
,
361 DISubrange
*DISubrange::getImpl(LLVMContext
&Context
, Metadata
*CountNode
,
362 Metadata
*LB
, Metadata
*UB
, Metadata
*Stride
,
363 StorageType Storage
, bool ShouldCreate
) {
364 DEFINE_GETIMPL_LOOKUP(DISubrange
, (CountNode
, LB
, UB
, Stride
));
365 Metadata
*Ops
[] = {CountNode
, LB
, UB
, Stride
};
366 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DISubrange
, Ops
);
369 DISubrange::BoundType
DISubrange::getCount() const {
370 Metadata
*CB
= getRawCountNode();
374 assert((isa
<ConstantAsMetadata
>(CB
) || isa
<DIVariable
>(CB
) ||
375 isa
<DIExpression
>(CB
)) &&
376 "Count must be signed constant or DIVariable or DIExpression");
378 if (auto *MD
= dyn_cast
<ConstantAsMetadata
>(CB
))
379 return BoundType(cast
<ConstantInt
>(MD
->getValue()));
381 if (auto *MD
= dyn_cast
<DIVariable
>(CB
))
382 return BoundType(MD
);
384 if (auto *MD
= dyn_cast
<DIExpression
>(CB
))
385 return BoundType(MD
);
390 DISubrange::BoundType
DISubrange::getLowerBound() const {
391 Metadata
*LB
= getRawLowerBound();
395 assert((isa
<ConstantAsMetadata
>(LB
) || isa
<DIVariable
>(LB
) ||
396 isa
<DIExpression
>(LB
)) &&
397 "LowerBound must be signed constant or DIVariable or DIExpression");
399 if (auto *MD
= dyn_cast
<ConstantAsMetadata
>(LB
))
400 return BoundType(cast
<ConstantInt
>(MD
->getValue()));
402 if (auto *MD
= dyn_cast
<DIVariable
>(LB
))
403 return BoundType(MD
);
405 if (auto *MD
= dyn_cast
<DIExpression
>(LB
))
406 return BoundType(MD
);
411 DISubrange::BoundType
DISubrange::getUpperBound() const {
412 Metadata
*UB
= getRawUpperBound();
416 assert((isa
<ConstantAsMetadata
>(UB
) || isa
<DIVariable
>(UB
) ||
417 isa
<DIExpression
>(UB
)) &&
418 "UpperBound must be signed constant or DIVariable or DIExpression");
420 if (auto *MD
= dyn_cast
<ConstantAsMetadata
>(UB
))
421 return BoundType(cast
<ConstantInt
>(MD
->getValue()));
423 if (auto *MD
= dyn_cast
<DIVariable
>(UB
))
424 return BoundType(MD
);
426 if (auto *MD
= dyn_cast
<DIExpression
>(UB
))
427 return BoundType(MD
);
432 DISubrange::BoundType
DISubrange::getStride() const {
433 Metadata
*ST
= getRawStride();
437 assert((isa
<ConstantAsMetadata
>(ST
) || isa
<DIVariable
>(ST
) ||
438 isa
<DIExpression
>(ST
)) &&
439 "Stride must be signed constant or DIVariable or DIExpression");
441 if (auto *MD
= dyn_cast
<ConstantAsMetadata
>(ST
))
442 return BoundType(cast
<ConstantInt
>(MD
->getValue()));
444 if (auto *MD
= dyn_cast
<DIVariable
>(ST
))
445 return BoundType(MD
);
447 if (auto *MD
= dyn_cast
<DIExpression
>(ST
))
448 return BoundType(MD
);
453 DIGenericSubrange
*DIGenericSubrange::getImpl(LLVMContext
&Context
,
454 Metadata
*CountNode
, Metadata
*LB
,
455 Metadata
*UB
, Metadata
*Stride
,
458 DEFINE_GETIMPL_LOOKUP(DIGenericSubrange
, (CountNode
, LB
, UB
, Stride
));
459 Metadata
*Ops
[] = {CountNode
, LB
, UB
, Stride
};
460 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DIGenericSubrange
, Ops
);
463 DIGenericSubrange::BoundType
DIGenericSubrange::getCount() const {
464 Metadata
*CB
= getRawCountNode();
468 assert((isa
<DIVariable
>(CB
) || isa
<DIExpression
>(CB
)) &&
469 "Count must be signed constant or DIVariable or DIExpression");
471 if (auto *MD
= dyn_cast
<DIVariable
>(CB
))
472 return BoundType(MD
);
474 if (auto *MD
= dyn_cast
<DIExpression
>(CB
))
475 return BoundType(MD
);
480 DIGenericSubrange::BoundType
DIGenericSubrange::getLowerBound() const {
481 Metadata
*LB
= getRawLowerBound();
485 assert((isa
<DIVariable
>(LB
) || isa
<DIExpression
>(LB
)) &&
486 "LowerBound must be signed constant or DIVariable or DIExpression");
488 if (auto *MD
= dyn_cast
<DIVariable
>(LB
))
489 return BoundType(MD
);
491 if (auto *MD
= dyn_cast
<DIExpression
>(LB
))
492 return BoundType(MD
);
497 DIGenericSubrange::BoundType
DIGenericSubrange::getUpperBound() const {
498 Metadata
*UB
= getRawUpperBound();
502 assert((isa
<DIVariable
>(UB
) || isa
<DIExpression
>(UB
)) &&
503 "UpperBound must be signed constant or DIVariable or DIExpression");
505 if (auto *MD
= dyn_cast
<DIVariable
>(UB
))
506 return BoundType(MD
);
508 if (auto *MD
= dyn_cast
<DIExpression
>(UB
))
509 return BoundType(MD
);
514 DIGenericSubrange::BoundType
DIGenericSubrange::getStride() const {
515 Metadata
*ST
= getRawStride();
519 assert((isa
<DIVariable
>(ST
) || isa
<DIExpression
>(ST
)) &&
520 "Stride must be signed constant or DIVariable or DIExpression");
522 if (auto *MD
= dyn_cast
<DIVariable
>(ST
))
523 return BoundType(MD
);
525 if (auto *MD
= dyn_cast
<DIExpression
>(ST
))
526 return BoundType(MD
);
531 DIEnumerator
*DIEnumerator::getImpl(LLVMContext
&Context
, const APInt
&Value
,
532 bool IsUnsigned
, MDString
*Name
,
533 StorageType Storage
, bool ShouldCreate
) {
534 assert(isCanonical(Name
) && "Expected canonical MDString");
535 DEFINE_GETIMPL_LOOKUP(DIEnumerator
, (Value
, IsUnsigned
, Name
));
536 Metadata
*Ops
[] = {Name
};
537 DEFINE_GETIMPL_STORE(DIEnumerator
, (Value
, IsUnsigned
), Ops
);
540 DIBasicType
*DIBasicType::getImpl(LLVMContext
&Context
, unsigned Tag
,
541 MDString
*Name
, uint64_t SizeInBits
,
542 uint32_t AlignInBits
, unsigned Encoding
,
543 DIFlags Flags
, StorageType Storage
,
545 assert(isCanonical(Name
) && "Expected canonical MDString");
546 DEFINE_GETIMPL_LOOKUP(DIBasicType
,
547 (Tag
, Name
, SizeInBits
, AlignInBits
, Encoding
, Flags
));
548 Metadata
*Ops
[] = {nullptr, nullptr, Name
};
549 DEFINE_GETIMPL_STORE(DIBasicType
, (Tag
, SizeInBits
, AlignInBits
, Encoding
,
553 Optional
<DIBasicType::Signedness
> DIBasicType::getSignedness() const {
554 switch (getEncoding()) {
555 case dwarf::DW_ATE_signed
:
556 case dwarf::DW_ATE_signed_char
:
557 return Signedness::Signed
;
558 case dwarf::DW_ATE_unsigned
:
559 case dwarf::DW_ATE_unsigned_char
:
560 return Signedness::Unsigned
;
566 DIStringType
*DIStringType::getImpl(LLVMContext
&Context
, unsigned Tag
,
567 MDString
*Name
, Metadata
*StringLength
,
568 Metadata
*StringLengthExp
,
569 uint64_t SizeInBits
, uint32_t AlignInBits
,
570 unsigned Encoding
, StorageType Storage
,
572 assert(isCanonical(Name
) && "Expected canonical MDString");
573 DEFINE_GETIMPL_LOOKUP(DIStringType
, (Tag
, Name
, StringLength
, StringLengthExp
,
574 SizeInBits
, AlignInBits
, Encoding
));
575 Metadata
*Ops
[] = {nullptr, nullptr, Name
, StringLength
, StringLengthExp
};
576 DEFINE_GETIMPL_STORE(DIStringType
, (Tag
, SizeInBits
, AlignInBits
, Encoding
),
580 DIDerivedType
*DIDerivedType::getImpl(
581 LLVMContext
&Context
, unsigned Tag
, MDString
*Name
, Metadata
*File
,
582 unsigned Line
, Metadata
*Scope
, Metadata
*BaseType
, uint64_t SizeInBits
,
583 uint32_t AlignInBits
, uint64_t OffsetInBits
,
584 Optional
<unsigned> DWARFAddressSpace
, DIFlags Flags
, Metadata
*ExtraData
,
585 Metadata
*Annotations
, StorageType Storage
, bool ShouldCreate
) {
586 assert(isCanonical(Name
) && "Expected canonical MDString");
587 DEFINE_GETIMPL_LOOKUP(DIDerivedType
,
588 (Tag
, Name
, File
, Line
, Scope
, BaseType
, SizeInBits
,
589 AlignInBits
, OffsetInBits
, DWARFAddressSpace
, Flags
,
590 ExtraData
, Annotations
));
591 Metadata
*Ops
[] = {File
, Scope
, Name
, BaseType
, ExtraData
, Annotations
};
592 DEFINE_GETIMPL_STORE(
593 DIDerivedType
, (Tag
, Line
, SizeInBits
, AlignInBits
, OffsetInBits
,
594 DWARFAddressSpace
, Flags
), Ops
);
597 DICompositeType
*DICompositeType::getImpl(
598 LLVMContext
&Context
, unsigned Tag
, MDString
*Name
, Metadata
*File
,
599 unsigned Line
, Metadata
*Scope
, Metadata
*BaseType
, uint64_t SizeInBits
,
600 uint32_t AlignInBits
, uint64_t OffsetInBits
, DIFlags Flags
,
601 Metadata
*Elements
, unsigned RuntimeLang
, Metadata
*VTableHolder
,
602 Metadata
*TemplateParams
, MDString
*Identifier
, Metadata
*Discriminator
,
603 Metadata
*DataLocation
, Metadata
*Associated
, Metadata
*Allocated
,
604 Metadata
*Rank
, Metadata
*Annotations
, StorageType Storage
,
606 assert(isCanonical(Name
) && "Expected canonical MDString");
608 // Keep this in sync with buildODRType.
609 DEFINE_GETIMPL_LOOKUP(DICompositeType
,
610 (Tag
, Name
, File
, Line
, Scope
, BaseType
, SizeInBits
,
611 AlignInBits
, OffsetInBits
, Flags
, Elements
,
612 RuntimeLang
, VTableHolder
, TemplateParams
, Identifier
,
613 Discriminator
, DataLocation
, Associated
, Allocated
,
615 Metadata
*Ops
[] = {File
, Scope
, Name
, BaseType
,
616 Elements
, VTableHolder
, TemplateParams
, Identifier
,
617 Discriminator
, DataLocation
, Associated
, Allocated
,
619 DEFINE_GETIMPL_STORE(DICompositeType
, (Tag
, Line
, RuntimeLang
, SizeInBits
,
620 AlignInBits
, OffsetInBits
, Flags
),
624 DICompositeType
*DICompositeType::buildODRType(
625 LLVMContext
&Context
, MDString
&Identifier
, unsigned Tag
, MDString
*Name
,
626 Metadata
*File
, unsigned Line
, Metadata
*Scope
, Metadata
*BaseType
,
627 uint64_t SizeInBits
, uint32_t AlignInBits
, uint64_t OffsetInBits
,
628 DIFlags Flags
, Metadata
*Elements
, unsigned RuntimeLang
,
629 Metadata
*VTableHolder
, Metadata
*TemplateParams
, Metadata
*Discriminator
,
630 Metadata
*DataLocation
, Metadata
*Associated
, Metadata
*Allocated
,
631 Metadata
*Rank
, Metadata
*Annotations
) {
632 assert(!Identifier
.getString().empty() && "Expected valid identifier");
633 if (!Context
.isODRUniquingDebugTypes())
635 auto *&CT
= (*Context
.pImpl
->DITypeMap
)[&Identifier
];
637 return CT
= DICompositeType::getDistinct(
638 Context
, Tag
, Name
, File
, Line
, Scope
, BaseType
, SizeInBits
,
639 AlignInBits
, OffsetInBits
, Flags
, Elements
, RuntimeLang
,
640 VTableHolder
, TemplateParams
, &Identifier
, Discriminator
,
641 DataLocation
, Associated
, Allocated
, Rank
, Annotations
);
643 // Only mutate CT if it's a forward declaration and the new operands aren't.
644 assert(CT
->getRawIdentifier() == &Identifier
&& "Wrong ODR identifier?");
645 if (!CT
->isForwardDecl() || (Flags
& DINode::FlagFwdDecl
))
648 // Mutate CT in place. Keep this in sync with getImpl.
649 CT
->mutate(Tag
, Line
, RuntimeLang
, SizeInBits
, AlignInBits
, OffsetInBits
,
651 Metadata
*Ops
[] = {File
, Scope
, Name
, BaseType
,
652 Elements
, VTableHolder
, TemplateParams
, &Identifier
,
653 Discriminator
, DataLocation
, Associated
, Allocated
,
655 assert((std::end(Ops
) - std::begin(Ops
)) == (int)CT
->getNumOperands() &&
656 "Mismatched number of operands");
657 for (unsigned I
= 0, E
= CT
->getNumOperands(); I
!= E
; ++I
)
658 if (Ops
[I
] != CT
->getOperand(I
))
659 CT
->setOperand(I
, Ops
[I
]);
663 DICompositeType
*DICompositeType::getODRType(
664 LLVMContext
&Context
, MDString
&Identifier
, unsigned Tag
, MDString
*Name
,
665 Metadata
*File
, unsigned Line
, Metadata
*Scope
, Metadata
*BaseType
,
666 uint64_t SizeInBits
, uint32_t AlignInBits
, uint64_t OffsetInBits
,
667 DIFlags Flags
, Metadata
*Elements
, unsigned RuntimeLang
,
668 Metadata
*VTableHolder
, Metadata
*TemplateParams
, Metadata
*Discriminator
,
669 Metadata
*DataLocation
, Metadata
*Associated
, Metadata
*Allocated
,
670 Metadata
*Rank
, Metadata
*Annotations
) {
671 assert(!Identifier
.getString().empty() && "Expected valid identifier");
672 if (!Context
.isODRUniquingDebugTypes())
674 auto *&CT
= (*Context
.pImpl
->DITypeMap
)[&Identifier
];
676 CT
= DICompositeType::getDistinct(
677 Context
, Tag
, Name
, File
, Line
, Scope
, BaseType
, SizeInBits
,
678 AlignInBits
, OffsetInBits
, Flags
, Elements
, RuntimeLang
, VTableHolder
,
679 TemplateParams
, &Identifier
, Discriminator
, DataLocation
, Associated
,
680 Allocated
, Rank
, Annotations
);
684 DICompositeType
*DICompositeType::getODRTypeIfExists(LLVMContext
&Context
,
685 MDString
&Identifier
) {
686 assert(!Identifier
.getString().empty() && "Expected valid identifier");
687 if (!Context
.isODRUniquingDebugTypes())
689 return Context
.pImpl
->DITypeMap
->lookup(&Identifier
);
692 DISubroutineType
*DISubroutineType::getImpl(LLVMContext
&Context
, DIFlags Flags
,
693 uint8_t CC
, Metadata
*TypeArray
,
696 DEFINE_GETIMPL_LOOKUP(DISubroutineType
, (Flags
, CC
, TypeArray
));
697 Metadata
*Ops
[] = {nullptr, nullptr, nullptr, TypeArray
};
698 DEFINE_GETIMPL_STORE(DISubroutineType
, (Flags
, CC
), Ops
);
701 // FIXME: Implement this string-enum correspondence with a .def file and macros,
702 // so that the association is explicit rather than implied.
703 static const char *ChecksumKindName
[DIFile::CSK_Last
] = {
709 StringRef
DIFile::getChecksumKindAsString(ChecksumKind CSKind
) {
710 assert(CSKind
<= DIFile::CSK_Last
&& "Invalid checksum kind");
711 // The first space was originally the CSK_None variant, which is now
712 // obsolete, but the space is still reserved in ChecksumKind, so we account
714 return ChecksumKindName
[CSKind
- 1];
717 Optional
<DIFile::ChecksumKind
> DIFile::getChecksumKind(StringRef CSKindStr
) {
718 return StringSwitch
<Optional
<DIFile::ChecksumKind
>>(CSKindStr
)
719 .Case("CSK_MD5", DIFile::CSK_MD5
)
720 .Case("CSK_SHA1", DIFile::CSK_SHA1
)
721 .Case("CSK_SHA256", DIFile::CSK_SHA256
)
725 DIFile
*DIFile::getImpl(LLVMContext
&Context
, MDString
*Filename
,
727 Optional
<DIFile::ChecksumInfo
<MDString
*>> CS
,
728 Optional
<MDString
*> Source
, StorageType Storage
,
730 assert(isCanonical(Filename
) && "Expected canonical MDString");
731 assert(isCanonical(Directory
) && "Expected canonical MDString");
732 assert((!CS
|| isCanonical(CS
->Value
)) && "Expected canonical MDString");
733 assert((!Source
|| isCanonical(*Source
)) && "Expected canonical MDString");
734 DEFINE_GETIMPL_LOOKUP(DIFile
, (Filename
, Directory
, CS
, Source
));
735 Metadata
*Ops
[] = {Filename
, Directory
, CS
? CS
->Value
: nullptr,
736 Source
.getValueOr(nullptr)};
737 DEFINE_GETIMPL_STORE(DIFile
, (CS
, Source
), Ops
);
740 DICompileUnit
*DICompileUnit::getImpl(
741 LLVMContext
&Context
, unsigned SourceLanguage
, Metadata
*File
,
742 MDString
*Producer
, bool IsOptimized
, MDString
*Flags
,
743 unsigned RuntimeVersion
, MDString
*SplitDebugFilename
,
744 unsigned EmissionKind
, Metadata
*EnumTypes
, Metadata
*RetainedTypes
,
745 Metadata
*GlobalVariables
, Metadata
*ImportedEntities
, Metadata
*Macros
,
746 uint64_t DWOId
, bool SplitDebugInlining
, bool DebugInfoForProfiling
,
747 unsigned NameTableKind
, bool RangesBaseAddress
, MDString
*SysRoot
,
748 MDString
*SDK
, StorageType Storage
, bool ShouldCreate
) {
749 assert(Storage
!= Uniqued
&& "Cannot unique DICompileUnit");
750 assert(isCanonical(Producer
) && "Expected canonical MDString");
751 assert(isCanonical(Flags
) && "Expected canonical MDString");
752 assert(isCanonical(SplitDebugFilename
) && "Expected canonical MDString");
754 Metadata
*Ops
[] = {File
,
765 return storeImpl(new (array_lengthof(Ops
)) DICompileUnit(
766 Context
, Storage
, SourceLanguage
, IsOptimized
,
767 RuntimeVersion
, EmissionKind
, DWOId
, SplitDebugInlining
,
768 DebugInfoForProfiling
, NameTableKind
, RangesBaseAddress
,
773 Optional
<DICompileUnit::DebugEmissionKind
>
774 DICompileUnit::getEmissionKind(StringRef Str
) {
775 return StringSwitch
<Optional
<DebugEmissionKind
>>(Str
)
776 .Case("NoDebug", NoDebug
)
777 .Case("FullDebug", FullDebug
)
778 .Case("LineTablesOnly", LineTablesOnly
)
779 .Case("DebugDirectivesOnly", DebugDirectivesOnly
)
783 Optional
<DICompileUnit::DebugNameTableKind
>
784 DICompileUnit::getNameTableKind(StringRef Str
) {
785 return StringSwitch
<Optional
<DebugNameTableKind
>>(Str
)
786 .Case("Default", DebugNameTableKind::Default
)
787 .Case("GNU", DebugNameTableKind::GNU
)
788 .Case("None", DebugNameTableKind::None
)
792 const char *DICompileUnit::emissionKindString(DebugEmissionKind EK
) {
794 case NoDebug
: return "NoDebug";
795 case FullDebug
: return "FullDebug";
796 case LineTablesOnly
: return "LineTablesOnly";
797 case DebugDirectivesOnly
: return "DebugDirectivesOnly";
802 const char *DICompileUnit::nameTableKindString(DebugNameTableKind NTK
) {
804 case DebugNameTableKind::Default
:
806 case DebugNameTableKind::GNU
:
808 case DebugNameTableKind::None
:
814 DISubprogram
*DILocalScope::getSubprogram() const {
815 if (auto *Block
= dyn_cast
<DILexicalBlockBase
>(this))
816 return Block
->getScope()->getSubprogram();
817 return const_cast<DISubprogram
*>(cast
<DISubprogram
>(this));
820 DILocalScope
*DILocalScope::getNonLexicalBlockFileScope() const {
821 if (auto *File
= dyn_cast
<DILexicalBlockFile
>(this))
822 return File
->getScope()->getNonLexicalBlockFileScope();
823 return const_cast<DILocalScope
*>(this);
826 DISubprogram::DISPFlags
DISubprogram::getFlag(StringRef Flag
) {
827 return StringSwitch
<DISPFlags
>(Flag
)
828 #define HANDLE_DISP_FLAG(ID, NAME) .Case("DISPFlag" #NAME, SPFlag##NAME)
829 #include "llvm/IR/DebugInfoFlags.def"
830 .Default(SPFlagZero
);
833 StringRef
DISubprogram::getFlagString(DISPFlags Flag
) {
835 // Appease a warning.
836 case SPFlagVirtuality
:
838 #define HANDLE_DISP_FLAG(ID, NAME) \
840 return "DISPFlag" #NAME;
841 #include "llvm/IR/DebugInfoFlags.def"
846 DISubprogram::DISPFlags
847 DISubprogram::splitFlags(DISPFlags Flags
,
848 SmallVectorImpl
<DISPFlags
> &SplitFlags
) {
849 // Multi-bit fields can require special handling. In our case, however, the
850 // only multi-bit field is virtuality, and all its values happen to be
851 // single-bit values, so the right behavior just falls out.
852 #define HANDLE_DISP_FLAG(ID, NAME) \
853 if (DISPFlags Bit = Flags & SPFlag##NAME) { \
854 SplitFlags.push_back(Bit); \
857 #include "llvm/IR/DebugInfoFlags.def"
861 DISubprogram
*DISubprogram::getImpl(
862 LLVMContext
&Context
, Metadata
*Scope
, MDString
*Name
,
863 MDString
*LinkageName
, Metadata
*File
, unsigned Line
, Metadata
*Type
,
864 unsigned ScopeLine
, Metadata
*ContainingType
, unsigned VirtualIndex
,
865 int ThisAdjustment
, DIFlags Flags
, DISPFlags SPFlags
, Metadata
*Unit
,
866 Metadata
*TemplateParams
, Metadata
*Declaration
, Metadata
*RetainedNodes
,
867 Metadata
*ThrownTypes
, StorageType Storage
, bool ShouldCreate
) {
868 assert(isCanonical(Name
) && "Expected canonical MDString");
869 assert(isCanonical(LinkageName
) && "Expected canonical MDString");
870 DEFINE_GETIMPL_LOOKUP(DISubprogram
,
871 (Scope
, Name
, LinkageName
, File
, Line
, Type
, ScopeLine
,
872 ContainingType
, VirtualIndex
, ThisAdjustment
, Flags
,
873 SPFlags
, Unit
, TemplateParams
, Declaration
,
874 RetainedNodes
, ThrownTypes
));
875 SmallVector
<Metadata
*, 11> Ops
= {
876 File
, Scope
, Name
, LinkageName
, Type
, Unit
,
877 Declaration
, RetainedNodes
, ContainingType
, TemplateParams
, ThrownTypes
};
880 if (!TemplateParams
) {
886 DEFINE_GETIMPL_STORE_N(
888 (Line
, ScopeLine
, VirtualIndex
, ThisAdjustment
, Flags
, SPFlags
), Ops
,
892 bool DISubprogram::describes(const Function
*F
) const {
893 assert(F
&& "Invalid function");
894 return F
->getSubprogram() == this;
897 DILexicalBlock
*DILexicalBlock::getImpl(LLVMContext
&Context
, Metadata
*Scope
,
898 Metadata
*File
, unsigned Line
,
899 unsigned Column
, StorageType Storage
,
902 adjustColumn(Column
);
904 assert(Scope
&& "Expected scope");
905 DEFINE_GETIMPL_LOOKUP(DILexicalBlock
, (Scope
, File
, Line
, Column
));
906 Metadata
*Ops
[] = {File
, Scope
};
907 DEFINE_GETIMPL_STORE(DILexicalBlock
, (Line
, Column
), Ops
);
910 DILexicalBlockFile
*DILexicalBlockFile::getImpl(LLVMContext
&Context
,
911 Metadata
*Scope
, Metadata
*File
,
912 unsigned Discriminator
,
915 assert(Scope
&& "Expected scope");
916 DEFINE_GETIMPL_LOOKUP(DILexicalBlockFile
, (Scope
, File
, Discriminator
));
917 Metadata
*Ops
[] = {File
, Scope
};
918 DEFINE_GETIMPL_STORE(DILexicalBlockFile
, (Discriminator
), Ops
);
921 DINamespace
*DINamespace::getImpl(LLVMContext
&Context
, Metadata
*Scope
,
922 MDString
*Name
, bool ExportSymbols
,
923 StorageType Storage
, bool ShouldCreate
) {
924 assert(isCanonical(Name
) && "Expected canonical MDString");
925 DEFINE_GETIMPL_LOOKUP(DINamespace
, (Scope
, Name
, ExportSymbols
));
926 // The nullptr is for DIScope's File operand. This should be refactored.
927 Metadata
*Ops
[] = {nullptr, Scope
, Name
};
928 DEFINE_GETIMPL_STORE(DINamespace
, (ExportSymbols
), Ops
);
931 DICommonBlock
*DICommonBlock::getImpl(LLVMContext
&Context
, Metadata
*Scope
,
932 Metadata
*Decl
, MDString
*Name
,
933 Metadata
*File
, unsigned LineNo
,
934 StorageType Storage
, bool ShouldCreate
) {
935 assert(isCanonical(Name
) && "Expected canonical MDString");
936 DEFINE_GETIMPL_LOOKUP(DICommonBlock
, (Scope
, Decl
, Name
, File
, LineNo
));
937 // The nullptr is for DIScope's File operand. This should be refactored.
938 Metadata
*Ops
[] = {Scope
, Decl
, Name
, File
};
939 DEFINE_GETIMPL_STORE(DICommonBlock
, (LineNo
), Ops
);
942 DIModule
*DIModule::getImpl(LLVMContext
&Context
, Metadata
*File
,
943 Metadata
*Scope
, MDString
*Name
,
944 MDString
*ConfigurationMacros
,
945 MDString
*IncludePath
, MDString
*APINotesFile
,
946 unsigned LineNo
, bool IsDecl
, StorageType Storage
,
948 assert(isCanonical(Name
) && "Expected canonical MDString");
949 DEFINE_GETIMPL_LOOKUP(DIModule
, (File
, Scope
, Name
, ConfigurationMacros
,
950 IncludePath
, APINotesFile
, LineNo
, IsDecl
));
951 Metadata
*Ops
[] = {File
, Scope
, Name
, ConfigurationMacros
,
952 IncludePath
, APINotesFile
};
953 DEFINE_GETIMPL_STORE(DIModule
, (LineNo
, IsDecl
), Ops
);
956 DITemplateTypeParameter
*
957 DITemplateTypeParameter::getImpl(LLVMContext
&Context
, MDString
*Name
,
958 Metadata
*Type
, bool isDefault
,
959 StorageType Storage
, bool ShouldCreate
) {
960 assert(isCanonical(Name
) && "Expected canonical MDString");
961 DEFINE_GETIMPL_LOOKUP(DITemplateTypeParameter
, (Name
, Type
, isDefault
));
962 Metadata
*Ops
[] = {Name
, Type
};
963 DEFINE_GETIMPL_STORE(DITemplateTypeParameter
, (isDefault
), Ops
);
966 DITemplateValueParameter
*DITemplateValueParameter::getImpl(
967 LLVMContext
&Context
, unsigned Tag
, MDString
*Name
, Metadata
*Type
,
968 bool isDefault
, Metadata
*Value
, StorageType Storage
, bool ShouldCreate
) {
969 assert(isCanonical(Name
) && "Expected canonical MDString");
970 DEFINE_GETIMPL_LOOKUP(DITemplateValueParameter
,
971 (Tag
, Name
, Type
, isDefault
, Value
));
972 Metadata
*Ops
[] = {Name
, Type
, Value
};
973 DEFINE_GETIMPL_STORE(DITemplateValueParameter
, (Tag
, isDefault
), Ops
);
977 DIGlobalVariable::getImpl(LLVMContext
&Context
, Metadata
*Scope
, MDString
*Name
,
978 MDString
*LinkageName
, Metadata
*File
, unsigned Line
,
979 Metadata
*Type
, bool IsLocalToUnit
, bool IsDefinition
,
980 Metadata
*StaticDataMemberDeclaration
,
981 Metadata
*TemplateParams
, uint32_t AlignInBits
,
982 StorageType Storage
, bool ShouldCreate
) {
983 assert(isCanonical(Name
) && "Expected canonical MDString");
984 assert(isCanonical(LinkageName
) && "Expected canonical MDString");
985 DEFINE_GETIMPL_LOOKUP(DIGlobalVariable
, (Scope
, Name
, LinkageName
, File
, Line
,
986 Type
, IsLocalToUnit
, IsDefinition
,
987 StaticDataMemberDeclaration
,
988 TemplateParams
, AlignInBits
));
989 Metadata
*Ops
[] = {Scope
,
995 StaticDataMemberDeclaration
,
997 DEFINE_GETIMPL_STORE(DIGlobalVariable
,
998 (Line
, IsLocalToUnit
, IsDefinition
, AlignInBits
), Ops
);
1001 DILocalVariable
*DILocalVariable::getImpl(LLVMContext
&Context
, Metadata
*Scope
,
1002 MDString
*Name
, Metadata
*File
,
1003 unsigned Line
, Metadata
*Type
,
1004 unsigned Arg
, DIFlags Flags
,
1005 uint32_t AlignInBits
,
1006 StorageType Storage
,
1007 bool ShouldCreate
) {
1008 // 64K ought to be enough for any frontend.
1009 assert(Arg
<= UINT16_MAX
&& "Expected argument number to fit in 16-bits");
1011 assert(Scope
&& "Expected scope");
1012 assert(isCanonical(Name
) && "Expected canonical MDString");
1013 DEFINE_GETIMPL_LOOKUP(DILocalVariable
,
1014 (Scope
, Name
, File
, Line
, Type
, Arg
, Flags
,
1016 Metadata
*Ops
[] = {Scope
, Name
, File
, Type
};
1017 DEFINE_GETIMPL_STORE(DILocalVariable
, (Line
, Arg
, Flags
, AlignInBits
), Ops
);
1020 Optional
<uint64_t> DIVariable::getSizeInBits() const {
1021 // This is used by the Verifier so be mindful of broken types.
1022 const Metadata
*RawType
= getRawType();
1024 // Try to get the size directly.
1025 if (auto *T
= dyn_cast
<DIType
>(RawType
))
1026 if (uint64_t Size
= T
->getSizeInBits())
1029 if (auto *DT
= dyn_cast
<DIDerivedType
>(RawType
)) {
1030 // Look at the base type.
1031 RawType
= DT
->getRawBaseType();
1035 // Missing type or size.
1043 DILabel
*DILabel::getImpl(LLVMContext
&Context
, Metadata
*Scope
,
1044 MDString
*Name
, Metadata
*File
, unsigned Line
,
1045 StorageType Storage
,
1046 bool ShouldCreate
) {
1047 assert(Scope
&& "Expected scope");
1048 assert(isCanonical(Name
) && "Expected canonical MDString");
1049 DEFINE_GETIMPL_LOOKUP(DILabel
,
1050 (Scope
, Name
, File
, Line
));
1051 Metadata
*Ops
[] = {Scope
, Name
, File
};
1052 DEFINE_GETIMPL_STORE(DILabel
, (Line
), Ops
);
1055 DIExpression
*DIExpression::getImpl(LLVMContext
&Context
,
1056 ArrayRef
<uint64_t> Elements
,
1057 StorageType Storage
, bool ShouldCreate
) {
1058 DEFINE_GETIMPL_LOOKUP(DIExpression
, (Elements
));
1059 DEFINE_GETIMPL_STORE_NO_OPS(DIExpression
, (Elements
));
1062 unsigned DIExpression::ExprOperand::getSize() const {
1063 uint64_t Op
= getOp();
1065 if (Op
>= dwarf::DW_OP_breg0
&& Op
<= dwarf::DW_OP_breg31
)
1069 case dwarf::DW_OP_LLVM_convert
:
1070 case dwarf::DW_OP_LLVM_fragment
:
1071 case dwarf::DW_OP_bregx
:
1073 case dwarf::DW_OP_constu
:
1074 case dwarf::DW_OP_consts
:
1075 case dwarf::DW_OP_deref_size
:
1076 case dwarf::DW_OP_plus_uconst
:
1077 case dwarf::DW_OP_LLVM_tag_offset
:
1078 case dwarf::DW_OP_LLVM_entry_value
:
1079 case dwarf::DW_OP_LLVM_arg
:
1080 case dwarf::DW_OP_regx
:
1087 bool DIExpression::isValid() const {
1088 for (auto I
= expr_op_begin(), E
= expr_op_end(); I
!= E
; ++I
) {
1089 // Check that there's space for the operand.
1090 if (I
->get() + I
->getSize() > E
->get())
1093 uint64_t Op
= I
->getOp();
1094 if ((Op
>= dwarf::DW_OP_reg0
&& Op
<= dwarf::DW_OP_reg31
) ||
1095 (Op
>= dwarf::DW_OP_breg0
&& Op
<= dwarf::DW_OP_breg31
))
1098 // Check that the operand is valid.
1102 case dwarf::DW_OP_LLVM_fragment
:
1103 // A fragment operator must appear at the end.
1104 return I
->get() + I
->getSize() == E
->get();
1105 case dwarf::DW_OP_stack_value
: {
1106 // Must be the last one or followed by a DW_OP_LLVM_fragment.
1107 if (I
->get() + I
->getSize() == E
->get())
1110 if ((++J
)->getOp() != dwarf::DW_OP_LLVM_fragment
)
1114 case dwarf::DW_OP_swap
: {
1115 // Must be more than one implicit element on the stack.
1117 // FIXME: A better way to implement this would be to add a local variable
1118 // that keeps track of the stack depth and introduce something like a
1119 // DW_LLVM_OP_implicit_location as a placeholder for the location this
1120 // DIExpression is attached to, or else pass the number of implicit stack
1121 // elements into isValid.
1122 if (getNumElements() == 1)
1126 case dwarf::DW_OP_LLVM_entry_value
: {
1127 // An entry value operator must appear at the beginning and the number of
1128 // operations it cover can currently only be 1, because we support only
1129 // entry values of a simple register location. One reason for this is that
1130 // we currently can't calculate the size of the resulting DWARF block for
1131 // other expressions.
1132 return I
->get() == expr_op_begin()->get() && I
->getArg(0) == 1;
1134 case dwarf::DW_OP_LLVM_implicit_pointer
:
1135 case dwarf::DW_OP_LLVM_convert
:
1136 case dwarf::DW_OP_LLVM_arg
:
1137 case dwarf::DW_OP_LLVM_tag_offset
:
1138 case dwarf::DW_OP_constu
:
1139 case dwarf::DW_OP_plus_uconst
:
1140 case dwarf::DW_OP_plus
:
1141 case dwarf::DW_OP_minus
:
1142 case dwarf::DW_OP_mul
:
1143 case dwarf::DW_OP_div
:
1144 case dwarf::DW_OP_mod
:
1145 case dwarf::DW_OP_or
:
1146 case dwarf::DW_OP_and
:
1147 case dwarf::DW_OP_xor
:
1148 case dwarf::DW_OP_shl
:
1149 case dwarf::DW_OP_shr
:
1150 case dwarf::DW_OP_shra
:
1151 case dwarf::DW_OP_deref
:
1152 case dwarf::DW_OP_deref_size
:
1153 case dwarf::DW_OP_xderef
:
1154 case dwarf::DW_OP_lit0
:
1155 case dwarf::DW_OP_not
:
1156 case dwarf::DW_OP_dup
:
1157 case dwarf::DW_OP_regx
:
1158 case dwarf::DW_OP_bregx
:
1159 case dwarf::DW_OP_push_object_address
:
1160 case dwarf::DW_OP_over
:
1161 case dwarf::DW_OP_consts
:
1168 bool DIExpression::isImplicit() const {
1172 if (getNumElements() == 0)
1175 for (const auto &It
: expr_ops()) {
1176 switch (It
.getOp()) {
1179 case dwarf::DW_OP_stack_value
:
1180 case dwarf::DW_OP_LLVM_tag_offset
:
1188 bool DIExpression::isComplex() const {
1192 if (getNumElements() == 0)
1195 // If there are any elements other than fragment or tag_offset, then some
1196 // kind of complex computation occurs.
1197 for (const auto &It
: expr_ops()) {
1198 switch (It
.getOp()) {
1199 case dwarf::DW_OP_LLVM_tag_offset
:
1200 case dwarf::DW_OP_LLVM_fragment
:
1202 default: return true;
1209 Optional
<DIExpression::FragmentInfo
>
1210 DIExpression::getFragmentInfo(expr_op_iterator Start
, expr_op_iterator End
) {
1211 for (auto I
= Start
; I
!= End
; ++I
)
1212 if (I
->getOp() == dwarf::DW_OP_LLVM_fragment
) {
1213 DIExpression::FragmentInfo Info
= {I
->getArg(1), I
->getArg(0)};
1219 void DIExpression::appendOffset(SmallVectorImpl
<uint64_t> &Ops
,
1222 Ops
.push_back(dwarf::DW_OP_plus_uconst
);
1223 Ops
.push_back(Offset
);
1224 } else if (Offset
< 0) {
1225 Ops
.push_back(dwarf::DW_OP_constu
);
1226 Ops
.push_back(-Offset
);
1227 Ops
.push_back(dwarf::DW_OP_minus
);
1231 bool DIExpression::extractIfOffset(int64_t &Offset
) const {
1232 if (getNumElements() == 0) {
1237 if (getNumElements() == 2 && Elements
[0] == dwarf::DW_OP_plus_uconst
) {
1238 Offset
= Elements
[1];
1242 if (getNumElements() == 3 && Elements
[0] == dwarf::DW_OP_constu
) {
1243 if (Elements
[2] == dwarf::DW_OP_plus
) {
1244 Offset
= Elements
[1];
1247 if (Elements
[2] == dwarf::DW_OP_minus
) {
1248 Offset
= -Elements
[1];
1256 bool DIExpression::hasAllLocationOps(unsigned N
) const {
1257 SmallDenseSet
<uint64_t, 4> SeenOps
;
1258 for (auto ExprOp
: expr_ops())
1259 if (ExprOp
.getOp() == dwarf::DW_OP_LLVM_arg
)
1260 SeenOps
.insert(ExprOp
.getArg(0));
1261 for (uint64_t Idx
= 0; Idx
< N
; ++Idx
)
1262 if (!is_contained(SeenOps
, Idx
))
1267 const DIExpression
*DIExpression::extractAddressClass(const DIExpression
*Expr
,
1268 unsigned &AddrClass
) {
1269 // FIXME: This seems fragile. Nothing that verifies that these elements
1270 // actually map to ops and not operands.
1271 const unsigned PatternSize
= 4;
1272 if (Expr
->Elements
.size() >= PatternSize
&&
1273 Expr
->Elements
[PatternSize
- 4] == dwarf::DW_OP_constu
&&
1274 Expr
->Elements
[PatternSize
- 2] == dwarf::DW_OP_swap
&&
1275 Expr
->Elements
[PatternSize
- 1] == dwarf::DW_OP_xderef
) {
1276 AddrClass
= Expr
->Elements
[PatternSize
- 3];
1278 if (Expr
->Elements
.size() == PatternSize
)
1280 return DIExpression::get(Expr
->getContext(),
1281 makeArrayRef(&*Expr
->Elements
.begin(),
1282 Expr
->Elements
.size() - PatternSize
));
1287 DIExpression
*DIExpression::prepend(const DIExpression
*Expr
, uint8_t Flags
,
1289 SmallVector
<uint64_t, 8> Ops
;
1290 if (Flags
& DIExpression::DerefBefore
)
1291 Ops
.push_back(dwarf::DW_OP_deref
);
1293 appendOffset(Ops
, Offset
);
1294 if (Flags
& DIExpression::DerefAfter
)
1295 Ops
.push_back(dwarf::DW_OP_deref
);
1297 bool StackValue
= Flags
& DIExpression::StackValue
;
1298 bool EntryValue
= Flags
& DIExpression::EntryValue
;
1300 return prependOpcodes(Expr
, Ops
, StackValue
, EntryValue
);
1303 DIExpression
*DIExpression::appendOpsToArg(const DIExpression
*Expr
,
1304 ArrayRef
<uint64_t> Ops
,
1305 unsigned ArgNo
, bool StackValue
) {
1306 assert(Expr
&& "Can't add ops to this expression");
1308 // Handle non-variadic intrinsics by prepending the opcodes.
1309 if (!any_of(Expr
->expr_ops(),
1310 [](auto Op
) { return Op
.getOp() == dwarf::DW_OP_LLVM_arg
; })) {
1311 assert(ArgNo
== 0 &&
1312 "Location Index must be 0 for a non-variadic expression.");
1313 SmallVector
<uint64_t, 8> NewOps(Ops
.begin(), Ops
.end());
1314 return DIExpression::prependOpcodes(Expr
, NewOps
, StackValue
);
1317 SmallVector
<uint64_t, 8> NewOps
;
1318 for (auto Op
: Expr
->expr_ops()) {
1319 Op
.appendToVector(NewOps
);
1320 if (Op
.getOp() == dwarf::DW_OP_LLVM_arg
&& Op
.getArg(0) == ArgNo
)
1321 NewOps
.insert(NewOps
.end(), Ops
.begin(), Ops
.end());
1324 return DIExpression::get(Expr
->getContext(), NewOps
);
1327 DIExpression
*DIExpression::replaceArg(const DIExpression
*Expr
,
1328 uint64_t OldArg
, uint64_t NewArg
) {
1329 assert(Expr
&& "Can't replace args in this expression");
1331 SmallVector
<uint64_t, 8> NewOps
;
1333 for (auto Op
: Expr
->expr_ops()) {
1334 if (Op
.getOp() != dwarf::DW_OP_LLVM_arg
|| Op
.getArg(0) < OldArg
) {
1335 Op
.appendToVector(NewOps
);
1338 NewOps
.push_back(dwarf::DW_OP_LLVM_arg
);
1339 uint64_t Arg
= Op
.getArg(0) == OldArg
? NewArg
: Op
.getArg(0);
1340 // OldArg has been deleted from the Op list, so decrement all indices
1344 NewOps
.push_back(Arg
);
1346 return DIExpression::get(Expr
->getContext(), NewOps
);
1349 DIExpression
*DIExpression::prependOpcodes(const DIExpression
*Expr
,
1350 SmallVectorImpl
<uint64_t> &Ops
,
1353 assert(Expr
&& "Can't prepend ops to this expression");
1356 Ops
.push_back(dwarf::DW_OP_LLVM_entry_value
);
1357 // Use a block size of 1 for the target register operand. The
1358 // DWARF backend currently cannot emit entry values with a block
1363 // If there are no ops to prepend, do not even add the DW_OP_stack_value.
1366 for (auto Op
: Expr
->expr_ops()) {
1367 // A DW_OP_stack_value comes at the end, but before a DW_OP_LLVM_fragment.
1369 if (Op
.getOp() == dwarf::DW_OP_stack_value
)
1371 else if (Op
.getOp() == dwarf::DW_OP_LLVM_fragment
) {
1372 Ops
.push_back(dwarf::DW_OP_stack_value
);
1376 Op
.appendToVector(Ops
);
1379 Ops
.push_back(dwarf::DW_OP_stack_value
);
1380 return DIExpression::get(Expr
->getContext(), Ops
);
1383 DIExpression
*DIExpression::append(const DIExpression
*Expr
,
1384 ArrayRef
<uint64_t> Ops
) {
1385 assert(Expr
&& !Ops
.empty() && "Can't append ops to this expression");
1387 // Copy Expr's current op list.
1388 SmallVector
<uint64_t, 16> NewOps
;
1389 for (auto Op
: Expr
->expr_ops()) {
1390 // Append new opcodes before DW_OP_{stack_value, LLVM_fragment}.
1391 if (Op
.getOp() == dwarf::DW_OP_stack_value
||
1392 Op
.getOp() == dwarf::DW_OP_LLVM_fragment
) {
1393 NewOps
.append(Ops
.begin(), Ops
.end());
1395 // Ensure that the new opcodes are only appended once.
1398 Op
.appendToVector(NewOps
);
1401 NewOps
.append(Ops
.begin(), Ops
.end());
1402 auto *result
= DIExpression::get(Expr
->getContext(), NewOps
);
1403 assert(result
->isValid() && "concatenated expression is not valid");
1407 DIExpression
*DIExpression::appendToStack(const DIExpression
*Expr
,
1408 ArrayRef
<uint64_t> Ops
) {
1409 assert(Expr
&& !Ops
.empty() && "Can't append ops to this expression");
1412 return Op
== dwarf::DW_OP_stack_value
||
1413 Op
== dwarf::DW_OP_LLVM_fragment
;
1415 "Can't append this op");
1417 // Append a DW_OP_deref after Expr's current op list if it's non-empty and
1418 // has no DW_OP_stack_value.
1420 // Match .* DW_OP_stack_value (DW_OP_LLVM_fragment A B)?.
1421 Optional
<FragmentInfo
> FI
= Expr
->getFragmentInfo();
1422 unsigned DropUntilStackValue
= FI
.hasValue() ? 3 : 0;
1423 ArrayRef
<uint64_t> ExprOpsBeforeFragment
=
1424 Expr
->getElements().drop_back(DropUntilStackValue
);
1425 bool NeedsDeref
= (Expr
->getNumElements() > DropUntilStackValue
) &&
1426 (ExprOpsBeforeFragment
.back() != dwarf::DW_OP_stack_value
);
1427 bool NeedsStackValue
= NeedsDeref
|| ExprOpsBeforeFragment
.empty();
1429 // Append a DW_OP_deref after Expr's current op list if needed, then append
1430 // the new ops, and finally ensure that a single DW_OP_stack_value is present.
1431 SmallVector
<uint64_t, 16> NewOps
;
1433 NewOps
.push_back(dwarf::DW_OP_deref
);
1434 NewOps
.append(Ops
.begin(), Ops
.end());
1435 if (NeedsStackValue
)
1436 NewOps
.push_back(dwarf::DW_OP_stack_value
);
1437 return DIExpression::append(Expr
, NewOps
);
1440 Optional
<DIExpression
*> DIExpression::createFragmentExpression(
1441 const DIExpression
*Expr
, unsigned OffsetInBits
, unsigned SizeInBits
) {
1442 SmallVector
<uint64_t, 8> Ops
;
1443 // Copy over the expression, but leave off any trailing DW_OP_LLVM_fragment.
1445 for (auto Op
: Expr
->expr_ops()) {
1446 switch (Op
.getOp()) {
1448 case dwarf::DW_OP_shr
:
1449 case dwarf::DW_OP_shra
:
1450 case dwarf::DW_OP_shl
:
1451 case dwarf::DW_OP_plus
:
1452 case dwarf::DW_OP_plus_uconst
:
1453 case dwarf::DW_OP_minus
:
1454 // We can't safely split arithmetic or shift operations into multiple
1455 // fragments because we can't express carry-over between fragments.
1457 // FIXME: We *could* preserve the lowest fragment of a constant offset
1458 // operation if the offset fits into SizeInBits.
1460 case dwarf::DW_OP_LLVM_fragment
: {
1461 // Make the new offset point into the existing fragment.
1462 uint64_t FragmentOffsetInBits
= Op
.getArg(0);
1463 uint64_t FragmentSizeInBits
= Op
.getArg(1);
1464 (void)FragmentSizeInBits
;
1465 assert((OffsetInBits
+ SizeInBits
<= FragmentSizeInBits
) &&
1466 "new fragment outside of original fragment");
1467 OffsetInBits
+= FragmentOffsetInBits
;
1471 Op
.appendToVector(Ops
);
1474 assert(Expr
&& "Unknown DIExpression");
1475 Ops
.push_back(dwarf::DW_OP_LLVM_fragment
);
1476 Ops
.push_back(OffsetInBits
);
1477 Ops
.push_back(SizeInBits
);
1478 return DIExpression::get(Expr
->getContext(), Ops
);
1481 std::pair
<DIExpression
*, const ConstantInt
*>
1482 DIExpression::constantFold(const ConstantInt
*CI
) {
1483 // Copy the APInt so we can modify it.
1484 APInt NewInt
= CI
->getValue();
1485 SmallVector
<uint64_t, 8> Ops
;
1487 // Fold operators only at the beginning of the expression.
1489 bool Changed
= false;
1490 for (auto Op
: expr_ops()) {
1491 switch (Op
.getOp()) {
1493 // We fold only the leading part of the expression; if we get to a part
1494 // that we're going to copy unchanged, and haven't done any folding,
1495 // then the entire expression is unchanged and we can return early.
1500 case dwarf::DW_OP_LLVM_convert
:
1504 if (Op
.getArg(1) == dwarf::DW_ATE_signed
)
1505 NewInt
= NewInt
.sextOrTrunc(Op
.getArg(0));
1507 assert(Op
.getArg(1) == dwarf::DW_ATE_unsigned
&& "Unexpected operand");
1508 NewInt
= NewInt
.zextOrTrunc(Op
.getArg(0));
1512 Op
.appendToVector(Ops
);
1516 return {DIExpression::get(getContext(), Ops
),
1517 ConstantInt::get(getContext(), NewInt
)};
1520 uint64_t DIExpression::getNumLocationOperands() const {
1521 uint64_t Result
= 0;
1522 for (auto ExprOp
: expr_ops())
1523 if (ExprOp
.getOp() == dwarf::DW_OP_LLVM_arg
)
1524 Result
= std::max(Result
, ExprOp
.getArg(0) + 1);
1525 assert(hasAllLocationOps(Result
) &&
1526 "Expression is missing one or more location operands.");
1530 llvm::Optional
<DIExpression::SignedOrUnsignedConstant
>
1531 DIExpression::isConstant() const {
1533 // Recognize signed and unsigned constants.
1534 // An signed constants can be represented as DW_OP_consts C DW_OP_stack_value
1535 // (DW_OP_LLVM_fragment of Len).
1536 // An unsigned constant can be represented as
1537 // DW_OP_constu C DW_OP_stack_value (DW_OP_LLVM_fragment of Len).
1539 if ((getNumElements() != 2 && getNumElements() != 3 &&
1540 getNumElements() != 6) ||
1541 (getElement(0) != dwarf::DW_OP_consts
&&
1542 getElement(0) != dwarf::DW_OP_constu
))
1545 if (getNumElements() == 2 && getElement(0) == dwarf::DW_OP_consts
)
1546 return SignedOrUnsignedConstant::SignedConstant
;
1548 if ((getNumElements() == 3 && getElement(2) != dwarf::DW_OP_stack_value
) ||
1549 (getNumElements() == 6 && (getElement(2) != dwarf::DW_OP_stack_value
||
1550 getElement(3) != dwarf::DW_OP_LLVM_fragment
)))
1552 return getElement(0) == dwarf::DW_OP_constu
1553 ? SignedOrUnsignedConstant::UnsignedConstant
1554 : SignedOrUnsignedConstant::SignedConstant
;
1557 DIExpression::ExtOps
DIExpression::getExtOps(unsigned FromSize
, unsigned ToSize
,
1559 dwarf::TypeKind TK
= Signed
? dwarf::DW_ATE_signed
: dwarf::DW_ATE_unsigned
;
1560 DIExpression::ExtOps Ops
{{dwarf::DW_OP_LLVM_convert
, FromSize
, TK
,
1561 dwarf::DW_OP_LLVM_convert
, ToSize
, TK
}};
1565 DIExpression
*DIExpression::appendExt(const DIExpression
*Expr
,
1566 unsigned FromSize
, unsigned ToSize
,
1568 return appendToStack(Expr
, getExtOps(FromSize
, ToSize
, Signed
));
1571 DIGlobalVariableExpression
*
1572 DIGlobalVariableExpression::getImpl(LLVMContext
&Context
, Metadata
*Variable
,
1573 Metadata
*Expression
, StorageType Storage
,
1574 bool ShouldCreate
) {
1575 DEFINE_GETIMPL_LOOKUP(DIGlobalVariableExpression
, (Variable
, Expression
));
1576 Metadata
*Ops
[] = {Variable
, Expression
};
1577 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DIGlobalVariableExpression
, Ops
);
1580 DIObjCProperty
*DIObjCProperty::getImpl(
1581 LLVMContext
&Context
, MDString
*Name
, Metadata
*File
, unsigned Line
,
1582 MDString
*GetterName
, MDString
*SetterName
, unsigned Attributes
,
1583 Metadata
*Type
, StorageType Storage
, bool ShouldCreate
) {
1584 assert(isCanonical(Name
) && "Expected canonical MDString");
1585 assert(isCanonical(GetterName
) && "Expected canonical MDString");
1586 assert(isCanonical(SetterName
) && "Expected canonical MDString");
1587 DEFINE_GETIMPL_LOOKUP(DIObjCProperty
, (Name
, File
, Line
, GetterName
,
1588 SetterName
, Attributes
, Type
));
1589 Metadata
*Ops
[] = {Name
, File
, GetterName
, SetterName
, Type
};
1590 DEFINE_GETIMPL_STORE(DIObjCProperty
, (Line
, Attributes
), Ops
);
1593 DIImportedEntity
*DIImportedEntity::getImpl(LLVMContext
&Context
, unsigned Tag
,
1594 Metadata
*Scope
, Metadata
*Entity
,
1595 Metadata
*File
, unsigned Line
,
1596 MDString
*Name
, StorageType Storage
,
1597 bool ShouldCreate
) {
1598 assert(isCanonical(Name
) && "Expected canonical MDString");
1599 DEFINE_GETIMPL_LOOKUP(DIImportedEntity
,
1600 (Tag
, Scope
, Entity
, File
, Line
, Name
));
1601 Metadata
*Ops
[] = {Scope
, Entity
, Name
, File
};
1602 DEFINE_GETIMPL_STORE(DIImportedEntity
, (Tag
, Line
), Ops
);
1605 DIMacro
*DIMacro::getImpl(LLVMContext
&Context
, unsigned MIType
,
1606 unsigned Line
, MDString
*Name
, MDString
*Value
,
1607 StorageType Storage
, bool ShouldCreate
) {
1608 assert(isCanonical(Name
) && "Expected canonical MDString");
1609 DEFINE_GETIMPL_LOOKUP(DIMacro
, (MIType
, Line
, Name
, Value
));
1610 Metadata
*Ops
[] = { Name
, Value
};
1611 DEFINE_GETIMPL_STORE(DIMacro
, (MIType
, Line
), Ops
);
1614 DIMacroFile
*DIMacroFile::getImpl(LLVMContext
&Context
, unsigned MIType
,
1615 unsigned Line
, Metadata
*File
,
1616 Metadata
*Elements
, StorageType Storage
,
1617 bool ShouldCreate
) {
1618 DEFINE_GETIMPL_LOOKUP(DIMacroFile
,
1619 (MIType
, Line
, File
, Elements
));
1620 Metadata
*Ops
[] = { File
, Elements
};
1621 DEFINE_GETIMPL_STORE(DIMacroFile
, (MIType
, Line
), Ops
);
1624 DIArgList
*DIArgList::getImpl(LLVMContext
&Context
,
1625 ArrayRef
<ValueAsMetadata
*> Args
,
1626 StorageType Storage
, bool ShouldCreate
) {
1627 DEFINE_GETIMPL_LOOKUP(DIArgList
, (Args
));
1628 DEFINE_GETIMPL_STORE_NO_OPS(DIArgList
, (Args
));
1631 void DIArgList::handleChangedOperand(void *Ref
, Metadata
*New
) {
1632 ValueAsMetadata
**OldVMPtr
= static_cast<ValueAsMetadata
**>(Ref
);
1633 assert((!New
|| isa
<ValueAsMetadata
>(New
)) &&
1634 "DIArgList must be passed a ValueAsMetadata");
1636 ValueAsMetadata
*NewVM
= cast_or_null
<ValueAsMetadata
>(New
);
1637 for (ValueAsMetadata
*&VM
: Args
) {
1638 if (&VM
== OldVMPtr
) {
1642 VM
= ValueAsMetadata::get(UndefValue::get(VM
->getValue()->getType()));
1647 void DIArgList::track() {
1648 for (ValueAsMetadata
*&VAM
: Args
)
1650 MetadataTracking::track(&VAM
, *VAM
, *this);
1652 void DIArgList::untrack() {
1653 for (ValueAsMetadata
*&VAM
: Args
)
1655 MetadataTracking::untrack(&VAM
, *VAM
);
1657 void DIArgList::dropAllReferences() {
1660 MDNode::dropAllReferences();