1 //===- DebugInfoMetadata.cpp - Implement debug info metadata --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the debug info Metadata classes.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/IR/DebugInfoMetadata.h"
15 #include "LLVMContextImpl.h"
16 #include "MetadataImpl.h"
17 #include "llvm/ADT/SmallPtrSet.h"
18 #include "llvm/ADT/StringSwitch.h"
19 #include "llvm/IR/DIBuilder.h"
20 #include "llvm/IR/Function.h"
21 #include "llvm/IR/Instructions.h"
25 DILocation::DILocation(LLVMContext &C, StorageType Storage, unsigned Line,
26 unsigned Column, ArrayRef<Metadata *> MDs)
27 : MDNode(C, DILocationKind, Storage, MDs) {
28 assert((MDs.size() == 1 || MDs.size() == 2) &&
29 "Expected a scope and optional inlined-at");
31 // Set line and column.
32 assert(Column < (1u << 16) && "Expected 16-bit column");
34 SubclassData32 = Line;
35 SubclassData16 = Column;
38 static void adjustColumn(unsigned &Column) {
39 // Set to unknown on overflow. We only have 16 bits to play with here.
40 if (Column >= (1u << 16))
44 DILocation *DILocation::getImpl(LLVMContext &Context, unsigned Line,
45 unsigned Column, Metadata *Scope,
46 Metadata *InlinedAt, StorageType Storage,
51 if (Storage == Uniqued) {
53 getUniqued(Context.pImpl->DILocations,
54 DILocationInfo::KeyTy(Line, Column, Scope, InlinedAt)))
59 assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
62 SmallVector<Metadata *, 2> Ops;
65 Ops.push_back(InlinedAt);
66 return storeImpl(new (Ops.size())
67 DILocation(Context, Storage, Line, Column, Ops),
68 Storage, Context.pImpl->DILocations);
72 DILocation::getMergedLocation(const DILocation *LocA, const DILocation *LocB,
73 const Instruction *ForInst) {
77 if (LocA == LocB || !LocA->canDiscriminate(*LocB))
80 if (!dyn_cast_or_null<CallInst>(ForInst))
83 SmallPtrSet<DILocation *, 5> InlinedLocationsA;
84 for (DILocation *L = LocA->getInlinedAt(); L; L = L->getInlinedAt())
85 InlinedLocationsA.insert(L);
86 const DILocation *Result = LocB;
87 for (DILocation *L = LocB->getInlinedAt(); L; L = L->getInlinedAt()) {
89 if (InlinedLocationsA.count(L))
92 return DILocation::get(Result->getContext(), 0, 0, Result->getScope(),
93 Result->getInlinedAt());
96 DINode::DIFlags DINode::getFlag(StringRef Flag) {
97 return StringSwitch<DIFlags>(Flag)
98 #define HANDLE_DI_FLAG(ID, NAME) .Case("DIFlag" #NAME, Flag##NAME)
99 #include "llvm/IR/DebugInfoFlags.def"
100 .Default(DINode::FlagZero);
103 StringRef DINode::getFlagString(DIFlags Flag) {
105 #define HANDLE_DI_FLAG(ID, NAME) \
107 return "DIFlag" #NAME;
108 #include "llvm/IR/DebugInfoFlags.def"
113 DINode::DIFlags DINode::splitFlags(DIFlags Flags,
114 SmallVectorImpl<DIFlags> &SplitFlags) {
115 // Flags that are packed together need to be specially handled, so
116 // that, for example, we emit "DIFlagPublic" and not
117 // "DIFlagPrivate | DIFlagProtected".
118 if (DIFlags A = Flags & FlagAccessibility) {
119 if (A == FlagPrivate)
120 SplitFlags.push_back(FlagPrivate);
121 else if (A == FlagProtected)
122 SplitFlags.push_back(FlagProtected);
124 SplitFlags.push_back(FlagPublic);
127 if (DIFlags R = Flags & FlagPtrToMemberRep) {
128 if (R == FlagSingleInheritance)
129 SplitFlags.push_back(FlagSingleInheritance);
130 else if (R == FlagMultipleInheritance)
131 SplitFlags.push_back(FlagMultipleInheritance);
133 SplitFlags.push_back(FlagVirtualInheritance);
136 if ((Flags & FlagIndirectVirtualBase) == FlagIndirectVirtualBase) {
137 Flags &= ~FlagIndirectVirtualBase;
138 SplitFlags.push_back(FlagIndirectVirtualBase);
141 #define HANDLE_DI_FLAG(ID, NAME) \
142 if (DIFlags Bit = Flags & Flag##NAME) { \
143 SplitFlags.push_back(Bit); \
146 #include "llvm/IR/DebugInfoFlags.def"
150 DIScopeRef DIScope::getScope() const {
151 if (auto *T = dyn_cast<DIType>(this))
152 return T->getScope();
154 if (auto *SP = dyn_cast<DISubprogram>(this))
155 return SP->getScope();
157 if (auto *LB = dyn_cast<DILexicalBlockBase>(this))
158 return LB->getScope();
160 if (auto *NS = dyn_cast<DINamespace>(this))
161 return NS->getScope();
163 if (auto *M = dyn_cast<DIModule>(this))
164 return M->getScope();
166 assert((isa<DIFile>(this) || isa<DICompileUnit>(this)) &&
167 "Unhandled type of scope.");
171 StringRef DIScope::getName() const {
172 if (auto *T = dyn_cast<DIType>(this))
174 if (auto *SP = dyn_cast<DISubprogram>(this))
175 return SP->getName();
176 if (auto *NS = dyn_cast<DINamespace>(this))
177 return NS->getName();
178 if (auto *M = dyn_cast<DIModule>(this))
180 assert((isa<DILexicalBlockBase>(this) || isa<DIFile>(this) ||
181 isa<DICompileUnit>(this)) &&
182 "Unhandled type of scope.");
187 static bool isCanonical(const MDString *S) {
188 return !S || !S->getString().empty();
192 GenericDINode *GenericDINode::getImpl(LLVMContext &Context, unsigned Tag,
194 ArrayRef<Metadata *> DwarfOps,
195 StorageType Storage, bool ShouldCreate) {
197 if (Storage == Uniqued) {
198 GenericDINodeInfo::KeyTy Key(Tag, Header, DwarfOps);
199 if (auto *N = getUniqued(Context.pImpl->GenericDINodes, Key))
203 Hash = Key.getHash();
205 assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
208 // Use a nullptr for empty headers.
209 assert(isCanonical(Header) && "Expected canonical MDString");
210 Metadata *PreOps[] = {Header};
211 return storeImpl(new (DwarfOps.size() + 1) GenericDINode(
212 Context, Storage, Hash, Tag, PreOps, DwarfOps),
213 Storage, Context.pImpl->GenericDINodes);
216 void GenericDINode::recalculateHash() {
217 setHash(GenericDINodeInfo::KeyTy::calculateHash(this));
220 #define UNWRAP_ARGS_IMPL(...) __VA_ARGS__
221 #define UNWRAP_ARGS(ARGS) UNWRAP_ARGS_IMPL ARGS
222 #define DEFINE_GETIMPL_LOOKUP(CLASS, ARGS) \
224 if (Storage == Uniqued) { \
225 if (auto *N = getUniqued(Context.pImpl->CLASS##s, \
226 CLASS##Info::KeyTy(UNWRAP_ARGS(ARGS)))) \
231 assert(ShouldCreate && \
232 "Expected non-uniqued nodes to always be created"); \
235 #define DEFINE_GETIMPL_STORE(CLASS, ARGS, OPS) \
236 return storeImpl(new (array_lengthof(OPS)) \
237 CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS), \
238 Storage, Context.pImpl->CLASS##s)
239 #define DEFINE_GETIMPL_STORE_NO_OPS(CLASS, ARGS) \
240 return storeImpl(new (0u) CLASS(Context, Storage, UNWRAP_ARGS(ARGS)), \
241 Storage, Context.pImpl->CLASS##s)
242 #define DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(CLASS, OPS) \
243 return storeImpl(new (array_lengthof(OPS)) CLASS(Context, Storage, OPS), \
244 Storage, Context.pImpl->CLASS##s)
245 #define DEFINE_GETIMPL_STORE_N(CLASS, ARGS, OPS, NUM_OPS) \
246 return storeImpl(new (NUM_OPS) \
247 CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS), \
248 Storage, Context.pImpl->CLASS##s)
250 DISubrange *DISubrange::getImpl(LLVMContext &Context, int64_t Count, int64_t Lo,
251 StorageType Storage, bool ShouldCreate) {
252 DEFINE_GETIMPL_LOOKUP(DISubrange, (Count, Lo));
253 DEFINE_GETIMPL_STORE_NO_OPS(DISubrange, (Count, Lo));
256 DIEnumerator *DIEnumerator::getImpl(LLVMContext &Context, int64_t Value,
257 MDString *Name, StorageType Storage,
259 assert(isCanonical(Name) && "Expected canonical MDString");
260 DEFINE_GETIMPL_LOOKUP(DIEnumerator, (Value, Name));
261 Metadata *Ops[] = {Name};
262 DEFINE_GETIMPL_STORE(DIEnumerator, (Value), Ops);
265 DIBasicType *DIBasicType::getImpl(LLVMContext &Context, unsigned Tag,
266 MDString *Name, uint64_t SizeInBits,
267 uint32_t AlignInBits, unsigned Encoding,
268 StorageType Storage, bool ShouldCreate) {
269 assert(isCanonical(Name) && "Expected canonical MDString");
270 DEFINE_GETIMPL_LOOKUP(DIBasicType,
271 (Tag, Name, SizeInBits, AlignInBits, Encoding));
272 Metadata *Ops[] = {nullptr, nullptr, Name};
273 DEFINE_GETIMPL_STORE(DIBasicType, (Tag, SizeInBits, AlignInBits, Encoding),
277 DIDerivedType *DIDerivedType::getImpl(
278 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
279 unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
280 uint32_t AlignInBits, uint64_t OffsetInBits,
281 Optional<unsigned> DWARFAddressSpace, DIFlags Flags, Metadata *ExtraData,
282 StorageType Storage, bool ShouldCreate) {
283 assert(isCanonical(Name) && "Expected canonical MDString");
284 DEFINE_GETIMPL_LOOKUP(DIDerivedType,
285 (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
286 AlignInBits, OffsetInBits, DWARFAddressSpace, Flags,
288 Metadata *Ops[] = {File, Scope, Name, BaseType, ExtraData};
289 DEFINE_GETIMPL_STORE(
290 DIDerivedType, (Tag, Line, SizeInBits, AlignInBits, OffsetInBits,
291 DWARFAddressSpace, Flags), Ops);
294 DICompositeType *DICompositeType::getImpl(
295 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
296 unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
297 uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags,
298 Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder,
299 Metadata *TemplateParams, MDString *Identifier, StorageType Storage,
301 assert(isCanonical(Name) && "Expected canonical MDString");
303 // Keep this in sync with buildODRType.
304 DEFINE_GETIMPL_LOOKUP(
305 DICompositeType, (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
306 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
307 VTableHolder, TemplateParams, Identifier));
308 Metadata *Ops[] = {File, Scope, Name, BaseType,
309 Elements, VTableHolder, TemplateParams, Identifier};
310 DEFINE_GETIMPL_STORE(DICompositeType, (Tag, Line, RuntimeLang, SizeInBits,
311 AlignInBits, OffsetInBits, Flags),
315 DICompositeType *DICompositeType::buildODRType(
316 LLVMContext &Context, MDString &Identifier, unsigned Tag, MDString *Name,
317 Metadata *File, unsigned Line, Metadata *Scope, Metadata *BaseType,
318 uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
319 DIFlags Flags, Metadata *Elements, unsigned RuntimeLang,
320 Metadata *VTableHolder, Metadata *TemplateParams) {
321 assert(!Identifier.getString().empty() && "Expected valid identifier");
322 if (!Context.isODRUniquingDebugTypes())
324 auto *&CT = (*Context.pImpl->DITypeMap)[&Identifier];
326 return CT = DICompositeType::getDistinct(
327 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
328 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
329 VTableHolder, TemplateParams, &Identifier);
331 // Only mutate CT if it's a forward declaration and the new operands aren't.
332 assert(CT->getRawIdentifier() == &Identifier && "Wrong ODR identifier?");
333 if (!CT->isForwardDecl() || (Flags & DINode::FlagFwdDecl))
336 // Mutate CT in place. Keep this in sync with getImpl.
337 CT->mutate(Tag, Line, RuntimeLang, SizeInBits, AlignInBits, OffsetInBits,
339 Metadata *Ops[] = {File, Scope, Name, BaseType,
340 Elements, VTableHolder, TemplateParams, &Identifier};
341 assert((std::end(Ops) - std::begin(Ops)) == (int)CT->getNumOperands() &&
342 "Mismatched number of operands");
343 for (unsigned I = 0, E = CT->getNumOperands(); I != E; ++I)
344 if (Ops[I] != CT->getOperand(I))
345 CT->setOperand(I, Ops[I]);
349 DICompositeType *DICompositeType::getODRType(
350 LLVMContext &Context, MDString &Identifier, unsigned Tag, MDString *Name,
351 Metadata *File, unsigned Line, Metadata *Scope, Metadata *BaseType,
352 uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
353 DIFlags Flags, Metadata *Elements, unsigned RuntimeLang,
354 Metadata *VTableHolder, Metadata *TemplateParams) {
355 assert(!Identifier.getString().empty() && "Expected valid identifier");
356 if (!Context.isODRUniquingDebugTypes())
358 auto *&CT = (*Context.pImpl->DITypeMap)[&Identifier];
360 CT = DICompositeType::getDistinct(
361 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
362 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang, VTableHolder,
363 TemplateParams, &Identifier);
367 DICompositeType *DICompositeType::getODRTypeIfExists(LLVMContext &Context,
368 MDString &Identifier) {
369 assert(!Identifier.getString().empty() && "Expected valid identifier");
370 if (!Context.isODRUniquingDebugTypes())
372 return Context.pImpl->DITypeMap->lookup(&Identifier);
375 DISubroutineType *DISubroutineType::getImpl(LLVMContext &Context, DIFlags Flags,
376 uint8_t CC, Metadata *TypeArray,
379 DEFINE_GETIMPL_LOOKUP(DISubroutineType, (Flags, CC, TypeArray));
380 Metadata *Ops[] = {nullptr, nullptr, nullptr, TypeArray};
381 DEFINE_GETIMPL_STORE(DISubroutineType, (Flags, CC), Ops);
384 // FIXME: Implement this string-enum correspondence with a .def file and macros,
385 // so that the association is explicit rather than implied.
386 static const char *ChecksumKindName[DIFile::CSK_Last + 1] = {
392 DIFile::ChecksumKind DIFile::getChecksumKind(StringRef CSKindStr) {
393 return StringSwitch<DIFile::ChecksumKind>(CSKindStr)
394 .Case("CSK_MD5", DIFile::CSK_MD5)
395 .Case("CSK_SHA1", DIFile::CSK_SHA1)
396 .Default(DIFile::CSK_None);
399 StringRef DIFile::getChecksumKindAsString() const {
400 assert(CSKind <= DIFile::CSK_Last && "Invalid checksum kind");
401 return ChecksumKindName[CSKind];
404 DIFile *DIFile::getImpl(LLVMContext &Context, MDString *Filename,
405 MDString *Directory, DIFile::ChecksumKind CSKind,
406 MDString *Checksum, StorageType Storage,
408 assert(isCanonical(Filename) && "Expected canonical MDString");
409 assert(isCanonical(Directory) && "Expected canonical MDString");
410 assert(isCanonical(Checksum) && "Expected canonical MDString");
411 DEFINE_GETIMPL_LOOKUP(DIFile, (Filename, Directory, CSKind, Checksum));
412 Metadata *Ops[] = {Filename, Directory, Checksum};
413 DEFINE_GETIMPL_STORE(DIFile, (CSKind), Ops);
416 DICompileUnit *DICompileUnit::getImpl(
417 LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
418 MDString *Producer, bool IsOptimized, MDString *Flags,
419 unsigned RuntimeVersion, MDString *SplitDebugFilename,
420 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
421 Metadata *GlobalVariables, Metadata *ImportedEntities, Metadata *Macros,
422 uint64_t DWOId, bool SplitDebugInlining, bool DebugInfoForProfiling,
423 bool GnuPubnames, StorageType Storage, bool ShouldCreate) {
424 assert(Storage != Uniqued && "Cannot unique DICompileUnit");
425 assert(isCanonical(Producer) && "Expected canonical MDString");
426 assert(isCanonical(Flags) && "Expected canonical MDString");
427 assert(isCanonical(SplitDebugFilename) && "Expected canonical MDString");
430 File, Producer, Flags, SplitDebugFilename,
431 EnumTypes, RetainedTypes, GlobalVariables, ImportedEntities,
433 return storeImpl(new (array_lengthof(Ops)) DICompileUnit(
434 Context, Storage, SourceLanguage, IsOptimized,
435 RuntimeVersion, EmissionKind, DWOId, SplitDebugInlining,
436 DebugInfoForProfiling, GnuPubnames, Ops),
440 Optional<DICompileUnit::DebugEmissionKind>
441 DICompileUnit::getEmissionKind(StringRef Str) {
442 return StringSwitch<Optional<DebugEmissionKind>>(Str)
443 .Case("NoDebug", NoDebug)
444 .Case("FullDebug", FullDebug)
445 .Case("LineTablesOnly", LineTablesOnly)
449 const char *DICompileUnit::EmissionKindString(DebugEmissionKind EK) {
451 case NoDebug: return "NoDebug";
452 case FullDebug: return "FullDebug";
453 case LineTablesOnly: return "LineTablesOnly";
458 DISubprogram *DILocalScope::getSubprogram() const {
459 if (auto *Block = dyn_cast<DILexicalBlockBase>(this))
460 return Block->getScope()->getSubprogram();
461 return const_cast<DISubprogram *>(cast<DISubprogram>(this));
464 DILocalScope *DILocalScope::getNonLexicalBlockFileScope() const {
465 if (auto *File = dyn_cast<DILexicalBlockFile>(this))
466 return File->getScope()->getNonLexicalBlockFileScope();
467 return const_cast<DILocalScope *>(this);
470 DISubprogram *DISubprogram::getImpl(
471 LLVMContext &Context, Metadata *Scope, MDString *Name,
472 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
473 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
474 Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
475 int ThisAdjustment, DIFlags Flags, bool IsOptimized, Metadata *Unit,
476 Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
477 Metadata *ThrownTypes, StorageType Storage, bool ShouldCreate) {
478 assert(isCanonical(Name) && "Expected canonical MDString");
479 assert(isCanonical(LinkageName) && "Expected canonical MDString");
480 DEFINE_GETIMPL_LOOKUP(
481 DISubprogram, (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
482 IsDefinition, ScopeLine, ContainingType, Virtuality,
483 VirtualIndex, ThisAdjustment, Flags, IsOptimized, Unit,
484 TemplateParams, Declaration, Variables, ThrownTypes));
485 SmallVector<Metadata *, 11> Ops = {
486 File, Scope, Name, LinkageName, Type, Unit,
487 Declaration, Variables, ContainingType, TemplateParams, ThrownTypes};
490 if (!TemplateParams) {
496 DEFINE_GETIMPL_STORE_N(DISubprogram,
497 (Line, ScopeLine, Virtuality, VirtualIndex,
498 ThisAdjustment, Flags, IsLocalToUnit, IsDefinition,
503 bool DISubprogram::describes(const Function *F) const {
504 assert(F && "Invalid function");
505 if (F->getSubprogram() == this)
507 StringRef Name = getLinkageName();
510 return F->getName() == Name;
513 DILexicalBlock *DILexicalBlock::getImpl(LLVMContext &Context, Metadata *Scope,
514 Metadata *File, unsigned Line,
515 unsigned Column, StorageType Storage,
518 adjustColumn(Column);
520 assert(Scope && "Expected scope");
521 DEFINE_GETIMPL_LOOKUP(DILexicalBlock, (Scope, File, Line, Column));
522 Metadata *Ops[] = {File, Scope};
523 DEFINE_GETIMPL_STORE(DILexicalBlock, (Line, Column), Ops);
526 DILexicalBlockFile *DILexicalBlockFile::getImpl(LLVMContext &Context,
527 Metadata *Scope, Metadata *File,
528 unsigned Discriminator,
531 assert(Scope && "Expected scope");
532 DEFINE_GETIMPL_LOOKUP(DILexicalBlockFile, (Scope, File, Discriminator));
533 Metadata *Ops[] = {File, Scope};
534 DEFINE_GETIMPL_STORE(DILexicalBlockFile, (Discriminator), Ops);
537 DINamespace *DINamespace::getImpl(LLVMContext &Context, Metadata *Scope,
538 MDString *Name, bool ExportSymbols,
539 StorageType Storage, bool ShouldCreate) {
540 assert(isCanonical(Name) && "Expected canonical MDString");
541 DEFINE_GETIMPL_LOOKUP(DINamespace, (Scope, Name, ExportSymbols));
542 // The nullptr is for DIScope's File operand. This should be refactored.
543 Metadata *Ops[] = {nullptr, Scope, Name};
544 DEFINE_GETIMPL_STORE(DINamespace, (ExportSymbols), Ops);
547 DIModule *DIModule::getImpl(LLVMContext &Context, Metadata *Scope,
548 MDString *Name, MDString *ConfigurationMacros,
549 MDString *IncludePath, MDString *ISysRoot,
550 StorageType Storage, bool ShouldCreate) {
551 assert(isCanonical(Name) && "Expected canonical MDString");
552 DEFINE_GETIMPL_LOOKUP(
553 DIModule, (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot));
554 Metadata *Ops[] = {Scope, Name, ConfigurationMacros, IncludePath, ISysRoot};
555 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DIModule, Ops);
558 DITemplateTypeParameter *DITemplateTypeParameter::getImpl(LLVMContext &Context,
563 assert(isCanonical(Name) && "Expected canonical MDString");
564 DEFINE_GETIMPL_LOOKUP(DITemplateTypeParameter, (Name, Type));
565 Metadata *Ops[] = {Name, Type};
566 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DITemplateTypeParameter, Ops);
569 DITemplateValueParameter *DITemplateValueParameter::getImpl(
570 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *Type,
571 Metadata *Value, StorageType Storage, bool ShouldCreate) {
572 assert(isCanonical(Name) && "Expected canonical MDString");
573 DEFINE_GETIMPL_LOOKUP(DITemplateValueParameter, (Tag, Name, Type, Value));
574 Metadata *Ops[] = {Name, Type, Value};
575 DEFINE_GETIMPL_STORE(DITemplateValueParameter, (Tag), Ops);
579 DIGlobalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
580 MDString *LinkageName, Metadata *File, unsigned Line,
581 Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
582 Metadata *StaticDataMemberDeclaration,
583 uint32_t AlignInBits, StorageType Storage,
585 assert(isCanonical(Name) && "Expected canonical MDString");
586 assert(isCanonical(LinkageName) && "Expected canonical MDString");
587 DEFINE_GETIMPL_LOOKUP(DIGlobalVariable,
588 (Scope, Name, LinkageName, File, Line, Type,
589 IsLocalToUnit, IsDefinition,
590 StaticDataMemberDeclaration, AlignInBits));
592 Scope, Name, File, Type, Name, LinkageName, StaticDataMemberDeclaration};
593 DEFINE_GETIMPL_STORE(DIGlobalVariable,
594 (Line, IsLocalToUnit, IsDefinition, AlignInBits),
598 DILocalVariable *DILocalVariable::getImpl(LLVMContext &Context, Metadata *Scope,
599 MDString *Name, Metadata *File,
600 unsigned Line, Metadata *Type,
601 unsigned Arg, DIFlags Flags,
602 uint32_t AlignInBits,
605 // 64K ought to be enough for any frontend.
606 assert(Arg <= UINT16_MAX && "Expected argument number to fit in 16-bits");
608 assert(Scope && "Expected scope");
609 assert(isCanonical(Name) && "Expected canonical MDString");
610 DEFINE_GETIMPL_LOOKUP(DILocalVariable,
611 (Scope, Name, File, Line, Type, Arg, Flags,
613 Metadata *Ops[] = {Scope, Name, File, Type};
614 DEFINE_GETIMPL_STORE(DILocalVariable, (Line, Arg, Flags, AlignInBits), Ops);
617 Optional<uint64_t> DIVariable::getSizeInBits() const {
618 // This is used by the Verifier so be mindful of broken types.
619 const Metadata *RawType = getRawType();
621 // Try to get the size directly.
622 if (auto *T = dyn_cast<DIType>(RawType))
623 if (uint64_t Size = T->getSizeInBits())
626 if (auto *DT = dyn_cast<DIDerivedType>(RawType)) {
627 // Look at the base type.
628 RawType = DT->getRawBaseType();
632 // Missing type or size.
640 DIExpression *DIExpression::getImpl(LLVMContext &Context,
641 ArrayRef<uint64_t> Elements,
642 StorageType Storage, bool ShouldCreate) {
643 DEFINE_GETIMPL_LOOKUP(DIExpression, (Elements));
644 DEFINE_GETIMPL_STORE_NO_OPS(DIExpression, (Elements));
647 unsigned DIExpression::ExprOperand::getSize() const {
649 case dwarf::DW_OP_LLVM_fragment:
651 case dwarf::DW_OP_constu:
652 case dwarf::DW_OP_plus_uconst:
659 bool DIExpression::isValid() const {
660 for (auto I = expr_op_begin(), E = expr_op_end(); I != E; ++I) {
661 // Check that there's space for the operand.
662 if (I->get() + I->getSize() > E->get())
665 // Check that the operand is valid.
666 switch (I->getOp()) {
669 case dwarf::DW_OP_LLVM_fragment:
670 // A fragment operator must appear at the end.
671 return I->get() + I->getSize() == E->get();
672 case dwarf::DW_OP_stack_value: {
673 // Must be the last one or followed by a DW_OP_LLVM_fragment.
674 if (I->get() + I->getSize() == E->get())
677 if ((++J)->getOp() != dwarf::DW_OP_LLVM_fragment)
681 case dwarf::DW_OP_swap: {
682 // Must be more than one implicit element on the stack.
684 // FIXME: A better way to implement this would be to add a local variable
685 // that keeps track of the stack depth and introduce something like a
686 // DW_LLVM_OP_implicit_location as a placeholder for the location this
687 // DIExpression is attached to, or else pass the number of implicit stack
688 // elements into isValid.
689 if (getNumElements() == 1)
693 case dwarf::DW_OP_constu:
694 case dwarf::DW_OP_plus_uconst:
695 case dwarf::DW_OP_plus:
696 case dwarf::DW_OP_minus:
697 case dwarf::DW_OP_mul:
698 case dwarf::DW_OP_deref:
699 case dwarf::DW_OP_xderef:
706 Optional<DIExpression::FragmentInfo>
707 DIExpression::getFragmentInfo(expr_op_iterator Start, expr_op_iterator End) {
708 for (auto I = Start; I != End; ++I)
709 if (I->getOp() == dwarf::DW_OP_LLVM_fragment) {
710 DIExpression::FragmentInfo Info = {I->getArg(1), I->getArg(0)};
716 void DIExpression::appendOffset(SmallVectorImpl<uint64_t> &Ops,
719 Ops.push_back(dwarf::DW_OP_plus_uconst);
720 Ops.push_back(Offset);
721 } else if (Offset < 0) {
722 Ops.push_back(dwarf::DW_OP_constu);
723 Ops.push_back(-Offset);
724 Ops.push_back(dwarf::DW_OP_minus);
728 bool DIExpression::extractIfOffset(int64_t &Offset) const {
729 if (getNumElements() == 0) {
734 if (getNumElements() == 2 && Elements[0] == dwarf::DW_OP_plus_uconst) {
735 Offset = Elements[1];
739 if (getNumElements() == 3 && Elements[0] == dwarf::DW_OP_constu) {
740 if (Elements[2] == dwarf::DW_OP_plus) {
741 Offset = Elements[1];
744 if (Elements[2] == dwarf::DW_OP_minus) {
745 Offset = -Elements[1];
753 DIExpression *DIExpression::prepend(const DIExpression *Expr, bool DerefBefore,
754 int64_t Offset, bool DerefAfter,
756 SmallVector<uint64_t, 8> Ops;
758 Ops.push_back(dwarf::DW_OP_deref);
760 appendOffset(Ops, Offset);
762 Ops.push_back(dwarf::DW_OP_deref);
765 for (auto Op : Expr->expr_ops()) {
766 // A DW_OP_stack_value comes at the end, but before a DW_OP_LLVM_fragment.
768 if (Op.getOp() == dwarf::DW_OP_stack_value)
770 else if (Op.getOp() == dwarf::DW_OP_LLVM_fragment) {
771 Ops.push_back(dwarf::DW_OP_stack_value);
775 Ops.push_back(Op.getOp());
776 for (unsigned I = 0; I < Op.getNumArgs(); ++I)
777 Ops.push_back(Op.getArg(I));
780 Ops.push_back(dwarf::DW_OP_stack_value);
781 return DIExpression::get(Expr->getContext(), Ops);
784 Optional<DIExpression *> DIExpression::createFragmentExpression(
785 const DIExpression *Expr, unsigned OffsetInBits, unsigned SizeInBits) {
786 SmallVector<uint64_t, 8> Ops;
787 // Copy over the expression, but leave off any trailing DW_OP_LLVM_fragment.
789 for (auto Op : Expr->expr_ops()) {
790 switch (Op.getOp()) {
792 case dwarf::DW_OP_plus:
793 case dwarf::DW_OP_minus:
794 // We can't safely split arithmetic into multiple fragments because we
795 // can't express carry-over between fragments.
797 // FIXME: We *could* preserve the lowest fragment of a constant offset
798 // operation if the offset fits into SizeInBits.
800 case dwarf::DW_OP_LLVM_fragment: {
801 // Make the new offset point into the existing fragment.
802 uint64_t FragmentOffsetInBits = Op.getArg(0);
803 // Op.getArg(0) is FragmentOffsetInBits.
804 // Op.getArg(1) is FragmentSizeInBits.
805 assert((OffsetInBits + SizeInBits <= Op.getArg(0) + Op.getArg(1)) &&
806 "new fragment outside of original fragment");
807 OffsetInBits += FragmentOffsetInBits;
811 Ops.push_back(Op.getOp());
812 for (unsigned I = 0; I < Op.getNumArgs(); ++I)
813 Ops.push_back(Op.getArg(I));
816 Ops.push_back(dwarf::DW_OP_LLVM_fragment);
817 Ops.push_back(OffsetInBits);
818 Ops.push_back(SizeInBits);
819 return DIExpression::get(Expr->getContext(), Ops);
822 bool DIExpression::isConstant() const {
823 // Recognize DW_OP_constu C DW_OP_stack_value (DW_OP_LLVM_fragment Len Ofs)?.
824 if (getNumElements() != 3 && getNumElements() != 6)
826 if (getElement(0) != dwarf::DW_OP_constu ||
827 getElement(2) != dwarf::DW_OP_stack_value)
829 if (getNumElements() == 6 && getElement(3) != dwarf::DW_OP_LLVM_fragment)
834 DIGlobalVariableExpression *
835 DIGlobalVariableExpression::getImpl(LLVMContext &Context, Metadata *Variable,
836 Metadata *Expression, StorageType Storage,
838 DEFINE_GETIMPL_LOOKUP(DIGlobalVariableExpression, (Variable, Expression));
839 Metadata *Ops[] = {Variable, Expression};
840 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DIGlobalVariableExpression, Ops);
843 DIObjCProperty *DIObjCProperty::getImpl(
844 LLVMContext &Context, MDString *Name, Metadata *File, unsigned Line,
845 MDString *GetterName, MDString *SetterName, unsigned Attributes,
846 Metadata *Type, StorageType Storage, bool ShouldCreate) {
847 assert(isCanonical(Name) && "Expected canonical MDString");
848 assert(isCanonical(GetterName) && "Expected canonical MDString");
849 assert(isCanonical(SetterName) && "Expected canonical MDString");
850 DEFINE_GETIMPL_LOOKUP(DIObjCProperty, (Name, File, Line, GetterName,
851 SetterName, Attributes, Type));
852 Metadata *Ops[] = {Name, File, GetterName, SetterName, Type};
853 DEFINE_GETIMPL_STORE(DIObjCProperty, (Line, Attributes), Ops);
856 DIImportedEntity *DIImportedEntity::getImpl(LLVMContext &Context, unsigned Tag,
857 Metadata *Scope, Metadata *Entity,
858 Metadata *File, unsigned Line,
859 MDString *Name, StorageType Storage,
861 assert(isCanonical(Name) && "Expected canonical MDString");
862 DEFINE_GETIMPL_LOOKUP(DIImportedEntity,
863 (Tag, Scope, Entity, File, Line, Name));
864 Metadata *Ops[] = {Scope, Entity, Name, File};
865 DEFINE_GETIMPL_STORE(DIImportedEntity, (Tag, Line), Ops);
868 DIMacro *DIMacro::getImpl(LLVMContext &Context, unsigned MIType,
869 unsigned Line, MDString *Name, MDString *Value,
870 StorageType Storage, bool ShouldCreate) {
871 assert(isCanonical(Name) && "Expected canonical MDString");
872 DEFINE_GETIMPL_LOOKUP(DIMacro, (MIType, Line, Name, Value));
873 Metadata *Ops[] = { Name, Value };
874 DEFINE_GETIMPL_STORE(DIMacro, (MIType, Line), Ops);
877 DIMacroFile *DIMacroFile::getImpl(LLVMContext &Context, unsigned MIType,
878 unsigned Line, Metadata *File,
879 Metadata *Elements, StorageType Storage,
881 DEFINE_GETIMPL_LOOKUP(DIMacroFile,
882 (MIType, Line, File, Elements));
883 Metadata *Ops[] = { File, Elements };
884 DEFINE_GETIMPL_STORE(DIMacroFile, (MIType, Line), Ops);