LLVM  5.0.0
DebugInfoMetadata.cpp
Go to the documentation of this file.
1 //===- DebugInfoMetadata.cpp - Implement debug info metadata --------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the debug info Metadata classes.
11 //
12 //===----------------------------------------------------------------------===//
13 
15 #include "LLVMContextImpl.h"
16 #include "MetadataImpl.h"
17 #include "llvm/ADT/StringSwitch.h"
18 #include "llvm/IR/DIBuilder.h"
19 #include "llvm/IR/Function.h"
20 
21 using namespace llvm;
22 
23 DILocation::DILocation(LLVMContext &C, StorageType Storage, unsigned Line,
24  unsigned Column, ArrayRef<Metadata *> MDs)
25  : MDNode(C, DILocationKind, Storage, MDs) {
26  assert((MDs.size() == 1 || MDs.size() == 2) &&
27  "Expected a scope and optional inlined-at");
28 
29  // Set line and column.
30  assert(Column < (1u << 16) && "Expected 16-bit column");
31 
32  SubclassData32 = Line;
33  SubclassData16 = Column;
34 }
35 
36 static void adjustColumn(unsigned &Column) {
37  // Set to unknown on overflow. We only have 16 bits to play with here.
38  if (Column >= (1u << 16))
39  Column = 0;
40 }
41 
43  unsigned Column, Metadata *Scope,
44  Metadata *InlinedAt, StorageType Storage,
45  bool ShouldCreate) {
46  // Fixup column.
47  adjustColumn(Column);
48 
49  if (Storage == Uniqued) {
50  if (auto *N =
51  getUniqued(Context.pImpl->DILocations,
52  DILocationInfo::KeyTy(Line, Column, Scope, InlinedAt)))
53  return N;
54  if (!ShouldCreate)
55  return nullptr;
56  } else {
57  assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
58  }
59 
61  Ops.push_back(Scope);
62  if (InlinedAt)
63  Ops.push_back(InlinedAt);
64  return storeImpl(new (Ops.size())
65  DILocation(Context, Storage, Line, Column, Ops),
66  Storage, Context.pImpl->DILocations);
67 }
68 
71 #define HANDLE_DI_FLAG(ID, NAME) .Case("DIFlag" #NAME, Flag##NAME)
72 #include "llvm/IR/DebugInfoFlags.def"
73  .Default(DINode::FlagZero);
74 }
75 
77  switch (Flag) {
78 #define HANDLE_DI_FLAG(ID, NAME) \
79  case Flag##NAME: \
80  return "DIFlag" #NAME;
81 #include "llvm/IR/DebugInfoFlags.def"
82  }
83  return "";
84 }
85 
87  SmallVectorImpl<DIFlags> &SplitFlags) {
88  // Flags that are packed together need to be specially handled, so
89  // that, for example, we emit "DIFlagPublic" and not
90  // "DIFlagPrivate | DIFlagProtected".
91  if (DIFlags A = Flags & FlagAccessibility) {
92  if (A == FlagPrivate)
93  SplitFlags.push_back(FlagPrivate);
94  else if (A == FlagProtected)
95  SplitFlags.push_back(FlagProtected);
96  else
97  SplitFlags.push_back(FlagPublic);
98  Flags &= ~A;
99  }
100  if (DIFlags R = Flags & FlagPtrToMemberRep) {
101  if (R == FlagSingleInheritance)
102  SplitFlags.push_back(FlagSingleInheritance);
103  else if (R == FlagMultipleInheritance)
104  SplitFlags.push_back(FlagMultipleInheritance);
105  else
106  SplitFlags.push_back(FlagVirtualInheritance);
107  Flags &= ~R;
108  }
109  if ((Flags & FlagIndirectVirtualBase) == FlagIndirectVirtualBase) {
110  Flags &= ~FlagIndirectVirtualBase;
111  SplitFlags.push_back(FlagIndirectVirtualBase);
112  }
113 
114 #define HANDLE_DI_FLAG(ID, NAME) \
115  if (DIFlags Bit = Flags & Flag##NAME) { \
116  SplitFlags.push_back(Bit); \
117  Flags &= ~Bit; \
118  }
119 #include "llvm/IR/DebugInfoFlags.def"
120  return Flags;
121 }
122 
124  if (auto *T = dyn_cast<DIType>(this))
125  return T->getScope();
126 
127  if (auto *SP = dyn_cast<DISubprogram>(this))
128  return SP->getScope();
129 
130  if (auto *LB = dyn_cast<DILexicalBlockBase>(this))
131  return LB->getScope();
132 
133  if (auto *NS = dyn_cast<DINamespace>(this))
134  return NS->getScope();
135 
136  if (auto *M = dyn_cast<DIModule>(this))
137  return M->getScope();
138 
139  assert((isa<DIFile>(this) || isa<DICompileUnit>(this)) &&
140  "Unhandled type of scope.");
141  return nullptr;
142 }
143 
145  if (auto *T = dyn_cast<DIType>(this))
146  return T->getName();
147  if (auto *SP = dyn_cast<DISubprogram>(this))
148  return SP->getName();
149  if (auto *NS = dyn_cast<DINamespace>(this))
150  return NS->getName();
151  if (auto *M = dyn_cast<DIModule>(this))
152  return M->getName();
153  assert((isa<DILexicalBlockBase>(this) || isa<DIFile>(this) ||
154  isa<DICompileUnit>(this)) &&
155  "Unhandled type of scope.");
156  return "";
157 }
158 
159 #ifndef NDEBUG
160 static bool isCanonical(const MDString *S) {
161  return !S || !S->getString().empty();
162 }
163 #endif
164 
165 GenericDINode *GenericDINode::getImpl(LLVMContext &Context, unsigned Tag,
166  MDString *Header,
167  ArrayRef<Metadata *> DwarfOps,
168  StorageType Storage, bool ShouldCreate) {
169  unsigned Hash = 0;
170  if (Storage == Uniqued) {
171  GenericDINodeInfo::KeyTy Key(Tag, Header, DwarfOps);
172  if (auto *N = getUniqued(Context.pImpl->GenericDINodes, Key))
173  return N;
174  if (!ShouldCreate)
175  return nullptr;
176  Hash = Key.getHash();
177  } else {
178  assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
179  }
180 
181  // Use a nullptr for empty headers.
182  assert(isCanonical(Header) && "Expected canonical MDString");
183  Metadata *PreOps[] = {Header};
184  return storeImpl(new (DwarfOps.size() + 1) GenericDINode(
185  Context, Storage, Hash, Tag, PreOps, DwarfOps),
186  Storage, Context.pImpl->GenericDINodes);
187 }
188 
189 void GenericDINode::recalculateHash() {
190  setHash(GenericDINodeInfo::KeyTy::calculateHash(this));
191 }
192 
193 #define UNWRAP_ARGS_IMPL(...) __VA_ARGS__
194 #define UNWRAP_ARGS(ARGS) UNWRAP_ARGS_IMPL ARGS
195 #define DEFINE_GETIMPL_LOOKUP(CLASS, ARGS) \
196  do { \
197  if (Storage == Uniqued) { \
198  if (auto *N = getUniqued(Context.pImpl->CLASS##s, \
199  CLASS##Info::KeyTy(UNWRAP_ARGS(ARGS)))) \
200  return N; \
201  if (!ShouldCreate) \
202  return nullptr; \
203  } else { \
204  assert(ShouldCreate && \
205  "Expected non-uniqued nodes to always be created"); \
206  } \
207  } while (false)
208 #define DEFINE_GETIMPL_STORE(CLASS, ARGS, OPS) \
209  return storeImpl(new (array_lengthof(OPS)) \
210  CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS), \
211  Storage, Context.pImpl->CLASS##s)
212 #define DEFINE_GETIMPL_STORE_NO_OPS(CLASS, ARGS) \
213  return storeImpl(new (0u) CLASS(Context, Storage, UNWRAP_ARGS(ARGS)), \
214  Storage, Context.pImpl->CLASS##s)
215 #define DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(CLASS, OPS) \
216  return storeImpl(new (array_lengthof(OPS)) CLASS(Context, Storage, OPS), \
217  Storage, Context.pImpl->CLASS##s)
218 #define DEFINE_GETIMPL_STORE_N(CLASS, ARGS, OPS, NUM_OPS) \
219  return storeImpl(new (NUM_OPS) \
220  CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS), \
221  Storage, Context.pImpl->CLASS##s)
222 
223 DISubrange *DISubrange::getImpl(LLVMContext &Context, int64_t Count, int64_t Lo,
224  StorageType Storage, bool ShouldCreate) {
225  DEFINE_GETIMPL_LOOKUP(DISubrange, (Count, Lo));
227 }
228 
229 DIEnumerator *DIEnumerator::getImpl(LLVMContext &Context, int64_t Value,
230  MDString *Name, StorageType Storage,
231  bool ShouldCreate) {
232  assert(isCanonical(Name) && "Expected canonical MDString");
233  DEFINE_GETIMPL_LOOKUP(DIEnumerator, (Value, Name));
234  Metadata *Ops[] = {Name};
235  DEFINE_GETIMPL_STORE(DIEnumerator, (Value), Ops);
236 }
237 
238 DIBasicType *DIBasicType::getImpl(LLVMContext &Context, unsigned Tag,
239  MDString *Name, uint64_t SizeInBits,
240  uint32_t AlignInBits, unsigned Encoding,
241  StorageType Storage, bool ShouldCreate) {
242  assert(isCanonical(Name) && "Expected canonical MDString");
244  (Tag, Name, SizeInBits, AlignInBits, Encoding));
245  Metadata *Ops[] = {nullptr, nullptr, Name};
246  DEFINE_GETIMPL_STORE(DIBasicType, (Tag, SizeInBits, AlignInBits, Encoding),
247  Ops);
248 }
249 
250 DIDerivedType *DIDerivedType::getImpl(
251  LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
252  unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
253  uint32_t AlignInBits, uint64_t OffsetInBits,
254  Optional<unsigned> DWARFAddressSpace, DIFlags Flags, Metadata *ExtraData,
255  StorageType Storage, bool ShouldCreate) {
256  assert(isCanonical(Name) && "Expected canonical MDString");
258  (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
259  AlignInBits, OffsetInBits, DWARFAddressSpace, Flags,
260  ExtraData));
261  Metadata *Ops[] = {File, Scope, Name, BaseType, ExtraData};
263  DIDerivedType, (Tag, Line, SizeInBits, AlignInBits, OffsetInBits,
264  DWARFAddressSpace, Flags), Ops);
265 }
266 
267 DICompositeType *DICompositeType::getImpl(
268  LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
269  unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
270  uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags,
271  Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder,
272  Metadata *TemplateParams, MDString *Identifier, StorageType Storage,
273  bool ShouldCreate) {
274  assert(isCanonical(Name) && "Expected canonical MDString");
275 
276  // Keep this in sync with buildODRType.
278  DICompositeType, (Tag, Name, File, Line, Scope, BaseType, SizeInBits,
279  AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
280  VTableHolder, TemplateParams, Identifier));
281  Metadata *Ops[] = {File, Scope, Name, BaseType,
282  Elements, VTableHolder, TemplateParams, Identifier};
283  DEFINE_GETIMPL_STORE(DICompositeType, (Tag, Line, RuntimeLang, SizeInBits,
284  AlignInBits, OffsetInBits, Flags),
285  Ops);
286 }
287 
289  LLVMContext &Context, MDString &Identifier, unsigned Tag, MDString *Name,
290  Metadata *File, unsigned Line, Metadata *Scope, Metadata *BaseType,
291  uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
292  DIFlags Flags, Metadata *Elements, unsigned RuntimeLang,
293  Metadata *VTableHolder, Metadata *TemplateParams) {
294  assert(!Identifier.getString().empty() && "Expected valid identifier");
295  if (!Context.isODRUniquingDebugTypes())
296  return nullptr;
297  auto *&CT = (*Context.pImpl->DITypeMap)[&Identifier];
298  if (!CT)
299  return CT = DICompositeType::getDistinct(
300  Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
301  AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang,
302  VTableHolder, TemplateParams, &Identifier);
303 
304  // Only mutate CT if it's a forward declaration and the new operands aren't.
305  assert(CT->getRawIdentifier() == &Identifier && "Wrong ODR identifier?");
306  if (!CT->isForwardDecl() || (Flags & DINode::FlagFwdDecl))
307  return CT;
308 
309  // Mutate CT in place. Keep this in sync with getImpl.
310  CT->mutate(Tag, Line, RuntimeLang, SizeInBits, AlignInBits, OffsetInBits,
311  Flags);
312  Metadata *Ops[] = {File, Scope, Name, BaseType,
313  Elements, VTableHolder, TemplateParams, &Identifier};
314  assert((std::end(Ops) - std::begin(Ops)) == (int)CT->getNumOperands() &&
315  "Mismatched number of operands");
316  for (unsigned I = 0, E = CT->getNumOperands(); I != E; ++I)
317  if (Ops[I] != CT->getOperand(I))
318  CT->setOperand(I, Ops[I]);
319  return CT;
320 }
321 
322 DICompositeType *DICompositeType::getODRType(
323  LLVMContext &Context, MDString &Identifier, unsigned Tag, MDString *Name,
324  Metadata *File, unsigned Line, Metadata *Scope, Metadata *BaseType,
325  uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits,
326  DIFlags Flags, Metadata *Elements, unsigned RuntimeLang,
327  Metadata *VTableHolder, Metadata *TemplateParams) {
328  assert(!Identifier.getString().empty() && "Expected valid identifier");
329  if (!Context.isODRUniquingDebugTypes())
330  return nullptr;
331  auto *&CT = (*Context.pImpl->DITypeMap)[&Identifier];
332  if (!CT)
334  Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits,
335  AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang, VTableHolder,
336  TemplateParams, &Identifier);
337  return CT;
338 }
339 
341  MDString &Identifier) {
342  assert(!Identifier.getString().empty() && "Expected valid identifier");
343  if (!Context.isODRUniquingDebugTypes())
344  return nullptr;
345  return Context.pImpl->DITypeMap->lookup(&Identifier);
346 }
347 
348 DISubroutineType *DISubroutineType::getImpl(LLVMContext &Context, DIFlags Flags,
349  uint8_t CC, Metadata *TypeArray,
350  StorageType Storage,
351  bool ShouldCreate) {
352  DEFINE_GETIMPL_LOOKUP(DISubroutineType, (Flags, CC, TypeArray));
353  Metadata *Ops[] = {nullptr, nullptr, nullptr, TypeArray};
354  DEFINE_GETIMPL_STORE(DISubroutineType, (Flags, CC), Ops);
355 }
356 
357 static const char *ChecksumKindName[DIFile::CSK_Last + 1] = {
358  "CSK_None",
359  "CSK_MD5",
360  "CSK_SHA1"
361 };
362 
364  return StringSwitch<DIFile::ChecksumKind>(CSKindStr)
365  .Case("CSK_MD5", DIFile::CSK_MD5)
366  .Case("CSK_SHA1", DIFile::CSK_SHA1)
368 }
369 
370 StringRef DIFile::getChecksumKindAsString() const {
371  assert(CSKind <= DIFile::CSK_Last && "Invalid checksum kind");
372  return ChecksumKindName[CSKind];
373 }
374 
375 DIFile *DIFile::getImpl(LLVMContext &Context, MDString *Filename,
376  MDString *Directory, DIFile::ChecksumKind CSKind,
377  MDString *Checksum, StorageType Storage,
378  bool ShouldCreate) {
379  assert(isCanonical(Filename) && "Expected canonical MDString");
380  assert(isCanonical(Directory) && "Expected canonical MDString");
381  assert(isCanonical(Checksum) && "Expected canonical MDString");
382  DEFINE_GETIMPL_LOOKUP(DIFile, (Filename, Directory, CSKind, Checksum));
383  Metadata *Ops[] = {Filename, Directory, Checksum};
384  DEFINE_GETIMPL_STORE(DIFile, (CSKind), Ops);
385 }
386 
387 DICompileUnit *DICompileUnit::getImpl(
388  LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
389  MDString *Producer, bool IsOptimized, MDString *Flags,
390  unsigned RuntimeVersion, MDString *SplitDebugFilename,
391  unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
392  Metadata *GlobalVariables, Metadata *ImportedEntities, Metadata *Macros,
393  uint64_t DWOId, bool SplitDebugInlining, bool DebugInfoForProfiling,
394  StorageType Storage, bool ShouldCreate) {
395  assert(Storage != Uniqued && "Cannot unique DICompileUnit");
396  assert(isCanonical(Producer) && "Expected canonical MDString");
397  assert(isCanonical(Flags) && "Expected canonical MDString");
398  assert(isCanonical(SplitDebugFilename) && "Expected canonical MDString");
399 
400  Metadata *Ops[] = {
403  Macros};
404  return storeImpl(new (array_lengthof(Ops))
405  DICompileUnit(Context, Storage, SourceLanguage,
406  IsOptimized, RuntimeVersion, EmissionKind,
407  DWOId, SplitDebugInlining,
408  DebugInfoForProfiling, Ops),
409  Storage);
410 }
411 
415  .Case("NoDebug", NoDebug)
416  .Case("FullDebug", FullDebug)
417  .Case("LineTablesOnly", LineTablesOnly)
418  .Default(None);
419 }
420 
422  switch (EK) {
423  case NoDebug: return "NoDebug";
424  case FullDebug: return "FullDebug";
425  case LineTablesOnly: return "LineTablesOnly";
426  }
427  return nullptr;
428 }
429 
431  if (auto *Block = dyn_cast<DILexicalBlockBase>(this))
432  return Block->getScope()->getSubprogram();
433  return const_cast<DISubprogram *>(cast<DISubprogram>(this));
434 }
435 
437  if (auto *File = dyn_cast<DILexicalBlockFile>(this))
438  return File->getScope()->getNonLexicalBlockFileScope();
439  return const_cast<DILocalScope *>(this);
440 }
441 
442 DISubprogram *DISubprogram::getImpl(
443  LLVMContext &Context, Metadata *Scope, MDString *Name,
444  MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
445  bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
446  Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
447  int ThisAdjustment, DIFlags Flags, bool IsOptimized, Metadata *Unit,
448  Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
449  Metadata *ThrownTypes, StorageType Storage, bool ShouldCreate) {
450  assert(isCanonical(Name) && "Expected canonical MDString");
451  assert(isCanonical(LinkageName) && "Expected canonical MDString");
453  DISubprogram, (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit,
454  IsDefinition, ScopeLine, ContainingType, Virtuality,
455  VirtualIndex, ThisAdjustment, Flags, IsOptimized, Unit,
456  TemplateParams, Declaration, Variables, ThrownTypes));
460  if (!ThrownTypes) {
461  Ops.pop_back();
462  if (!TemplateParams) {
463  Ops.pop_back();
464  if (!ContainingType)
465  Ops.pop_back();
466  }
467  }
469  (Line, ScopeLine, Virtuality, VirtualIndex,
470  ThisAdjustment, Flags, IsLocalToUnit, IsDefinition,
471  IsOptimized),
472  Ops, Ops.size());
473 }
474 
475 bool DISubprogram::describes(const Function *F) const {
476  assert(F && "Invalid function");
477  if (F->getSubprogram() == this)
478  return true;
479  StringRef Name = getLinkageName();
480  if (Name.empty())
481  Name = getName();
482  return F->getName() == Name;
483 }
484 
485 DILexicalBlock *DILexicalBlock::getImpl(LLVMContext &Context, Metadata *Scope,
486  Metadata *File, unsigned Line,
487  unsigned Column, StorageType Storage,
488  bool ShouldCreate) {
489  // Fixup column.
490  adjustColumn(Column);
491 
492  assert(Scope && "Expected scope");
493  DEFINE_GETIMPL_LOOKUP(DILexicalBlock, (Scope, File, Line, Column));
494  Metadata *Ops[] = {File, Scope};
495  DEFINE_GETIMPL_STORE(DILexicalBlock, (Line, Column), Ops);
496 }
497 
498 DILexicalBlockFile *DILexicalBlockFile::getImpl(LLVMContext &Context,
499  Metadata *Scope, Metadata *File,
500  unsigned Discriminator,
501  StorageType Storage,
502  bool ShouldCreate) {
503  assert(Scope && "Expected scope");
504  DEFINE_GETIMPL_LOOKUP(DILexicalBlockFile, (Scope, File, Discriminator));
505  Metadata *Ops[] = {File, Scope};
506  DEFINE_GETIMPL_STORE(DILexicalBlockFile, (Discriminator), Ops);
507 }
508 
509 DINamespace *DINamespace::getImpl(LLVMContext &Context, Metadata *Scope,
510  MDString *Name, bool ExportSymbols,
511  StorageType Storage, bool ShouldCreate) {
512  assert(isCanonical(Name) && "Expected canonical MDString");
513  DEFINE_GETIMPL_LOOKUP(DINamespace, (Scope, Name, ExportSymbols));
514  // The nullptr is for DIScope's File operand. This should be refactored.
515  Metadata *Ops[] = {nullptr, Scope, Name};
516  DEFINE_GETIMPL_STORE(DINamespace, (ExportSymbols), Ops);
517 }
518 
519 DIModule *DIModule::getImpl(LLVMContext &Context, Metadata *Scope,
520  MDString *Name, MDString *ConfigurationMacros,
521  MDString *IncludePath, MDString *ISysRoot,
522  StorageType Storage, bool ShouldCreate) {
523  assert(isCanonical(Name) && "Expected canonical MDString");
525  DIModule, (Scope, Name, ConfigurationMacros, IncludePath, ISysRoot));
526  Metadata *Ops[] = {Scope, Name, ConfigurationMacros, IncludePath, ISysRoot};
528 }
529 
530 DITemplateTypeParameter *DITemplateTypeParameter::getImpl(LLVMContext &Context,
531  MDString *Name,
532  Metadata *Type,
533  StorageType Storage,
534  bool ShouldCreate) {
535  assert(isCanonical(Name) && "Expected canonical MDString");
537  Metadata *Ops[] = {Name, Type};
539 }
540 
541 DITemplateValueParameter *DITemplateValueParameter::getImpl(
542  LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *Type,
543  Metadata *Value, StorageType Storage, bool ShouldCreate) {
544  assert(isCanonical(Name) && "Expected canonical MDString");
545  DEFINE_GETIMPL_LOOKUP(DITemplateValueParameter, (Tag, Name, Type, Value));
546  Metadata *Ops[] = {Name, Type, Value};
548 }
549 
551 DIGlobalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
552  MDString *LinkageName, Metadata *File, unsigned Line,
553  Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
554  Metadata *StaticDataMemberDeclaration,
555  uint32_t AlignInBits, StorageType Storage,
556  bool ShouldCreate) {
557  assert(isCanonical(Name) && "Expected canonical MDString");
558  assert(isCanonical(LinkageName) && "Expected canonical MDString");
560  (Scope, Name, LinkageName, File, Line, Type,
561  IsLocalToUnit, IsDefinition,
562  StaticDataMemberDeclaration, AlignInBits));
563  Metadata *Ops[] = {
564  Scope, Name, File, Type, Name, LinkageName, StaticDataMemberDeclaration};
566  (Line, IsLocalToUnit, IsDefinition, AlignInBits),
567  Ops);
568 }
569 
570 DILocalVariable *DILocalVariable::getImpl(LLVMContext &Context, Metadata *Scope,
571  MDString *Name, Metadata *File,
572  unsigned Line, Metadata *Type,
573  unsigned Arg, DIFlags Flags,
574  uint32_t AlignInBits,
575  StorageType Storage,
576  bool ShouldCreate) {
577  // 64K ought to be enough for any frontend.
578  assert(Arg <= UINT16_MAX && "Expected argument number to fit in 16-bits");
579 
580  assert(Scope && "Expected scope");
581  assert(isCanonical(Name) && "Expected canonical MDString");
583  (Scope, Name, File, Line, Type, Arg, Flags,
584  AlignInBits));
585  Metadata *Ops[] = {Scope, Name, File, Type};
586  DEFINE_GETIMPL_STORE(DILocalVariable, (Line, Arg, Flags, AlignInBits), Ops);
587 }
588 
589 DIExpression *DIExpression::getImpl(LLVMContext &Context,
590  ArrayRef<uint64_t> Elements,
591  StorageType Storage, bool ShouldCreate) {
592  DEFINE_GETIMPL_LOOKUP(DIExpression, (Elements));
594 }
595 
597  switch (getOp()) {
599  return 3;
600  case dwarf::DW_OP_constu:
601  case dwarf::DW_OP_plus_uconst:
602  return 2;
603  default:
604  return 1;
605  }
606 }
607 
608 bool DIExpression::isValid() const {
609  for (auto I = expr_op_begin(), E = expr_op_end(); I != E; ++I) {
610  // Check that there's space for the operand.
611  if (I->get() + I->getSize() > E->get())
612  return false;
613 
614  // Check that the operand is valid.
615  switch (I->getOp()) {
616  default:
617  return false;
619  // A fragment operator must appear at the end.
620  return I->get() + I->getSize() == E->get();
621  case dwarf::DW_OP_stack_value: {
622  // Must be the last one or followed by a DW_OP_LLVM_fragment.
623  if (I->get() + I->getSize() == E->get())
624  break;
625  auto J = I;
626  if ((++J)->getOp() != dwarf::DW_OP_LLVM_fragment)
627  return false;
628  break;
629  }
630  case dwarf::DW_OP_swap: {
631  // Must be more than one implicit element on the stack.
632 
633  // FIXME: A better way to implement this would be to add a local variable
634  // that keeps track of the stack depth and introduce something like a
635  // DW_LLVM_OP_implicit_location as a placeholder for the location this
636  // DIExpression is attached to, or else pass the number of implicit stack
637  // elements into isValid.
638  if (getNumElements() == 1)
639  return false;
640  break;
641  }
642  case dwarf::DW_OP_constu:
643  case dwarf::DW_OP_plus_uconst:
644  case dwarf::DW_OP_plus:
645  case dwarf::DW_OP_minus:
646  case dwarf::DW_OP_deref:
647  case dwarf::DW_OP_xderef:
648  break;
649  }
650  }
651  return true;
652 }
653 
656  for (auto I = Start; I != End; ++I)
657  if (I->getOp() == dwarf::DW_OP_LLVM_fragment) {
658  DIExpression::FragmentInfo Info = {I->getArg(1), I->getArg(0)};
659  return Info;
660  }
661  return None;
662 }
663 
665  int64_t Offset) {
666  if (Offset > 0) {
667  Ops.push_back(dwarf::DW_OP_plus_uconst);
668  Ops.push_back(Offset);
669  } else if (Offset < 0) {
670  Ops.push_back(dwarf::DW_OP_constu);
671  Ops.push_back(-Offset);
672  Ops.push_back(dwarf::DW_OP_minus);
673  }
674 }
675 
676 bool DIExpression::extractIfOffset(int64_t &Offset) const {
677  if (getNumElements() == 0) {
678  Offset = 0;
679  return true;
680  }
681 
682  if (getNumElements() == 2 && Elements[0] == dwarf::DW_OP_plus_uconst) {
683  Offset = Elements[1];
684  return true;
685  }
686 
687  if (getNumElements() == 3 && Elements[0] == dwarf::DW_OP_constu) {
688  if (Elements[2] == dwarf::DW_OP_plus) {
689  Offset = Elements[1];
690  return true;
691  }
692  if (Elements[2] == dwarf::DW_OP_minus) {
693  Offset = -Elements[1];
694  return true;
695  }
696  }
697 
698  return false;
699 }
700 
702  int64_t Offset, bool StackValue) {
704  appendOffset(Ops, Offset);
705  if (Deref)
706  Ops.push_back(dwarf::DW_OP_deref);
707  if (Expr)
708  for (auto Op : Expr->expr_ops()) {
709  // A DW_OP_stack_value comes at the end, but before a DW_OP_LLVM_fragment.
710  if (StackValue) {
711  if (Op.getOp() == dwarf::DW_OP_stack_value)
712  StackValue = false;
713  else if (Op.getOp() == dwarf::DW_OP_LLVM_fragment) {
714  Ops.push_back(dwarf::DW_OP_stack_value);
715  StackValue = false;
716  }
717  }
718  Ops.push_back(Op.getOp());
719  for (unsigned I = 0; I < Op.getNumArgs(); ++I)
720  Ops.push_back(Op.getArg(I));
721  }
722  if (StackValue)
723  Ops.push_back(dwarf::DW_OP_stack_value);
724  return DIExpression::get(Expr->getContext(), Ops);
725 }
726 
728  // Recognize DW_OP_constu C DW_OP_stack_value (DW_OP_LLVM_fragment Len Ofs)?.
729  if (getNumElements() != 3 && getNumElements() != 6)
730  return false;
731  if (getElement(0) != dwarf::DW_OP_constu ||
732  getElement(2) != dwarf::DW_OP_stack_value)
733  return false;
735  return false;
736  return true;
737 }
738 
740 DIGlobalVariableExpression::getImpl(LLVMContext &Context, Metadata *Variable,
741  Metadata *Expression, StorageType Storage,
742  bool ShouldCreate) {
743  DEFINE_GETIMPL_LOOKUP(DIGlobalVariableExpression, (Variable, Expression));
744  Metadata *Ops[] = {Variable, Expression};
746 }
747 
748 DIObjCProperty *DIObjCProperty::getImpl(
749  LLVMContext &Context, MDString *Name, Metadata *File, unsigned Line,
750  MDString *GetterName, MDString *SetterName, unsigned Attributes,
751  Metadata *Type, StorageType Storage, bool ShouldCreate) {
752  assert(isCanonical(Name) && "Expected canonical MDString");
753  assert(isCanonical(GetterName) && "Expected canonical MDString");
754  assert(isCanonical(SetterName) && "Expected canonical MDString");
755  DEFINE_GETIMPL_LOOKUP(DIObjCProperty, (Name, File, Line, GetterName,
756  SetterName, Attributes, Type));
757  Metadata *Ops[] = {Name, File, GetterName, SetterName, Type};
758  DEFINE_GETIMPL_STORE(DIObjCProperty, (Line, Attributes), Ops);
759 }
760 
761 DIImportedEntity *DIImportedEntity::getImpl(LLVMContext &Context, unsigned Tag,
762  Metadata *Scope, Metadata *Entity,
763  Metadata *File, unsigned Line,
764  MDString *Name, StorageType Storage,
765  bool ShouldCreate) {
766  assert(isCanonical(Name) && "Expected canonical MDString");
768  (Tag, Scope, Entity, File, Line, Name));
769  Metadata *Ops[] = {Scope, Entity, Name, File};
770  DEFINE_GETIMPL_STORE(DIImportedEntity, (Tag, Line), Ops);
771 }
772 
773 DIMacro *DIMacro::getImpl(LLVMContext &Context, unsigned MIType,
774  unsigned Line, MDString *Name, MDString *Value,
775  StorageType Storage, bool ShouldCreate) {
776  assert(isCanonical(Name) && "Expected canonical MDString");
777  DEFINE_GETIMPL_LOOKUP(DIMacro, (MIType, Line, Name, Value));
778  Metadata *Ops[] = { Name, Value };
779  DEFINE_GETIMPL_STORE(DIMacro, (MIType, Line), Ops);
780 }
781 
782 DIMacroFile *DIMacroFile::getImpl(LLVMContext &Context, unsigned MIType,
783  unsigned Line, Metadata *File,
784  Metadata *Elements, StorageType Storage,
785  bool ShouldCreate) {
787  (MIType, Line, File, Elements));
788  Metadata *Ops[] = { File, Elements };
789  DEFINE_GETIMPL_STORE(DIMacroFile, (MIType, Line), Ops);
790 }
791 
static void appendOffset(SmallVectorImpl< uint64_t > &Ops, int64_t Offset)
Append Ops with operations to apply the Offset.
Metadata MDString MDString * ConfigurationMacros
const_iterator end(StringRef path)
Get end iterator over path.
Definition: Path.cpp:243
unsigned char Storage
Storage flag for non-uniqued, otherwise unowned, metadata.
Definition: Metadata.h:69
LLVMContext & Context
const_iterator begin(StringRef path, Style style=Style::native)
Get begin iterator over path.
Definition: Path.cpp:234
MDString MDString * Directory
static DICompositeType * getODRTypeIfExists(LLVMContext &Context, MDString &Identifier)
unsigned MDString Metadata unsigned Metadata Metadata * BaseType
static MDTuple * getDistinct(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1172
uint64_t getElement(unsigned I) const
bool extractIfOffset(int64_t &Offset) const
If this is a constant offset, extract it.
#define DEFINE_GETIMPL_STORE_N(CLASS, ARGS, OPS, NUM_OPS)
static bool isCanonical(const MDString *S)
Metadata node.
Definition: Metadata.h:862
iterator_range< expr_op_iterator > expr_ops() const
static T * storeImpl(T *N, StorageType Storage, StoreT &Store)
Definition: MetadataImpl.h:43
unsigned MDString Metadata unsigned Metadata Metadata uint64_t uint32_t uint64_t DIFlags Metadata unsigned Metadata Metadata * TemplateParams
unsigned Metadata MDString bool MDString * Flags
unsigned MDString Metadata * Type
A scope for locals.
#define DEFINE_GETIMPL_LOOKUP(CLASS, ARGS)
Metadata MDString MDString Metadata unsigned Metadata * Type
DebugEmissionKind getEmissionKind() const
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:218
bool isODRUniquingDebugTypes() const
Whether there is a string map for uniquing debug info identifiers across the context.
DILocalScope * getNonLexicalBlockFileScope() const
Get the first non DILexicalBlockFile scope of this scope.
Array subrange.
unsigned Metadata MDString * Producer
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
LLVM_ATTRIBUTE_ALWAYS_INLINE R Default(const T &Value) const
Definition: StringSwitch.h:244
MDString * Filename
unsigned Metadata MDString bool MDString unsigned MDString * SplitDebugFilename
static const char * EmissionKindString(DebugEmissionKind EK)
Only used in LLVM metadata.
Definition: Dwarf.h:123
unsigned Metadata MDString bool MDString unsigned MDString unsigned Metadata Metadata Metadata * GlobalVariables
Holds a subclass of DINode.
Subprogram description.
LLVM_ATTRIBUTE_ALWAYS_INLINE StringSwitch & Case(const char(&S)[N], const T &Value)
Definition: StringSwitch.h:74
#define F(x, y, z)
Definition: MD5.cpp:55
unsigned StringRef DIFile unsigned DIScopeRef DITypeRef BaseType
Holds the characteristics of one fragment of a larger variable.
unsigned StringRef DIFile * File
expr_op_iterator expr_op_begin() const
Visit the elements via ExprOperand wrappers.
Enumeration value.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
Metadata MDString * Name
DIScopeRef getScope() const
Debug location.
unsigned MDString Metadata unsigned Metadata Metadata uint64_t uint32_t uint64_t DIFlags Metadata * Elements
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:149
const char * Name
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:121
static ChecksumKind getChecksumKind(StringRef CSKindStr)
static StringRef getFlagString(DIFlags Flag)
Metadata MDString MDString Metadata unsigned Metadata bool bool unsigned Metadata unsigned unsigned int DIFlags bool Metadata Metadata * TemplateParams
Metadata MDString MDString Metadata * File
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:43
static T * getUniqued(DenseSet< T *, InfoT > &Store, const typename InfoT::KeyTy &Key)
Definition: MetadataImpl.h:23
unsigned getNumElements() const
unsigned StringRef Name
StringRef getName() const
unsigned MDString * Name
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
static DICompositeType * buildODRType(LLVMContext &Context, MDString &Identifier, unsigned Tag, MDString *Name, Metadata *File, unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags, Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder, Metadata *TemplateParams)
Build a DICompositeType with the given ODR identifier.
Metadata Metadata * File
#define DEFINE_GETIMPL_STORE(CLASS, ARGS, OPS)
static void adjustColumn(unsigned &Column)
SourceLanguage
Definition: Dwarf.h:177
unsigned getSize() const
Return the size of the operand.
unsigned StringRef DIFile unsigned DIScopeRef Scope
#define A
Definition: LargeTest.cpp:12
Metadata Metadata * File
unsigned Metadata * File
StorageType
Active type of storage.
Definition: Metadata.h:66
A pair of DIGlobalVariable and DIExpression.
static const unsigned End
Metadata MDString MDString * LinkageName
Metadata MDString * Name
unsigned Metadata MDString bool MDString unsigned MDString unsigned Metadata Metadata * RetainedTypes
LLVMContextImpl *const pImpl
Definition: LLVMContext.h:70
Metadata MDString MDString Metadata unsigned Metadata bool bool unsigned Metadata unsigned unsigned int DIFlags bool Metadata Metadata Metadata * Declaration
An imported module (C++ using directive or similar).
BaseType
A given derived pointer can have multiple base pointers through phi/selects.
#define DEFINE_GETIMPL_STORE_NO_OPS(CLASS, ARGS)
StringRef getString() const
Definition: Metadata.cpp:456
#define E
Definition: LargeTest.cpp:27
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
Metadata MDString MDString * LinkageName
constexpr size_t array_lengthof(T(&)[N])
Find the length of an array.
Definition: STLExtras.h:731
static DIFlags splitFlags(DIFlags Flags, SmallVectorImpl< DIFlags > &SplitFlags)
Split up a flags bitfield.
const size_t N
Optional< FragmentInfo > getFragmentInfo() const
Retrieve the details of this fragment expression.
DWARF expression.
Metadata MDString MDString Metadata unsigned Metadata bool bool unsigned Metadata * ContainingType
static DIFlags getFlag(StringRef Flag)
A (clang) module that has been imported by the compile unit.
DISubprogram * getSubprogram() const
Get the attached subprogram.
Definition: Metadata.cpp:1498
Metadata MDString MDString Metadata unsigned Metadata bool bool unsigned Metadata unsigned unsigned int DIFlags bool Metadata * Unit
unsigned Metadata MDString bool MDString unsigned MDString unsigned Metadata * EnumTypes
Generic tagged DWARF-like metadata node.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:133
Metadata MDString Metadata * File
#define HANDLE_DI_FLAG(ID, NAME)
Type array for a subprogram.
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1164
static std::vector< std::string > Flags
Definition: FlagsTest.cpp:8
DIFlags
Debug info flags.
static const char * ChecksumKindName[DIFile::CSK_Last+1]
Metadata MDString MDString Metadata * File
#define C
Definition: LargeTest.cpp:25
#define I(x, y, z)
Definition: MD5.cpp:58
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:135
Metadata MDString * Name
Metadata MDString MDString MDString * IncludePath
LLVMContext & getContext() const
Definition: Metadata.h:922
Metadata MDString MDString Metadata unsigned Metadata bool bool unsigned Metadata unsigned unsigned int DIFlags bool Metadata Metadata Metadata Metadata * Variables
unsigned MDString Metadata unsigned Metadata * Scope
bool isConstant() const
Determine whether this represents a standalone constant value.
std::vector< uint8_t > Unit
Definition: FuzzerDefs.h:105
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
unsigned MDString Metadata unsigned Metadata Metadata uint64_t uint32_t uint64_t DIFlags Metadata unsigned Metadata * VTableHolder
LLVM Value Representation.
Definition: Value.h:73
unsigned Metadata MDString bool MDString unsigned MDString unsigned Metadata Metadata Metadata Metadata * ImportedEntities
#define DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(CLASS, OPS)
Metadata MDString MDString Metadata unsigned Metadata * Type
static DIExpression * prepend(const DIExpression *DIExpr, bool Deref, int64_t Offset=0, bool StackValue=false)
Prepend DIExpr with a deref and offset operation and optionally turn it into a stack value...
An iterator for expression operands.
uint64_t getOp() const
Get the operand code.
std::string Hash(const Unit &U)
Definition: FuzzerSHA1.cpp:216
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
A single uniqued string.
Definition: Metadata.h:602
static LazyValueInfoImpl & getImpl(void *&PImpl, AssumptionCache *AC, const DataLayout *DL, DominatorTree *DT=nullptr)
This lazily constructs the LazyValueInfoImpl.
DISubprogram * getSubprogram() const
Get the subprogram for this scope.
expr_op_iterator expr_op_end() const
Optional< DenseMap< const MDString *, DICompositeType * > > DITypeMap
Root of the metadata hierarchy.
Definition: Metadata.h:58
Metadata MDString * Name
unsigned MDString Metadata * File
Basic type, like 'int' or 'float'.