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/IR/Function.h"
21 MDLocation::MDLocation(LLVMContext &C, StorageType Storage, unsigned Line,
22 unsigned Column, ArrayRef<Metadata *> MDs)
23 : MDNode(C, MDLocationKind, Storage, MDs) {
24 assert((MDs.size() == 1 || MDs.size() == 2) &&
25 "Expected a scope and optional inlined-at");
27 // Set line and column.
28 assert(Column < (1u << 16) && "Expected 16-bit column");
30 SubclassData32 = Line;
31 SubclassData16 = Column;
34 static void adjustColumn(unsigned &Column) {
35 // Set to unknown on overflow. We only have 16 bits to play with here.
36 if (Column >= (1u << 16))
40 MDLocation *MDLocation::getImpl(LLVMContext &Context, unsigned Line,
41 unsigned Column, Metadata *Scope,
42 Metadata *InlinedAt, StorageType Storage,
47 if (Storage == Uniqued) {
49 getUniqued(Context.pImpl->MDLocations,
50 MDLocationInfo::KeyTy(Line, Column, Scope, InlinedAt)))
55 assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
58 SmallVector<Metadata *, 2> Ops;
61 Ops.push_back(InlinedAt);
62 return storeImpl(new (Ops.size())
63 MDLocation(Context, Storage, Line, Column, Ops),
64 Storage, Context.pImpl->MDLocations);
67 static StringRef getString(const MDString *S) {
69 return S->getString();
74 static bool isCanonical(const MDString *S) {
75 return !S || !S->getString().empty();
79 GenericDebugNode *GenericDebugNode::getImpl(LLVMContext &Context, unsigned Tag,
81 ArrayRef<Metadata *> DwarfOps,
85 if (Storage == Uniqued) {
86 GenericDebugNodeInfo::KeyTy Key(Tag, getString(Header), DwarfOps);
87 if (auto *N = getUniqued(Context.pImpl->GenericDebugNodes, Key))
93 assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
96 // Use a nullptr for empty headers.
97 assert(isCanonical(Header) && "Expected canonical MDString");
98 Metadata *PreOps[] = {Header};
99 return storeImpl(new (DwarfOps.size() + 1) GenericDebugNode(
100 Context, Storage, Hash, Tag, PreOps, DwarfOps),
101 Storage, Context.pImpl->GenericDebugNodes);
104 void GenericDebugNode::recalculateHash() {
105 setHash(GenericDebugNodeInfo::KeyTy::calculateHash(this));
108 #define UNWRAP_ARGS_IMPL(...) __VA_ARGS__
109 #define UNWRAP_ARGS(ARGS) UNWRAP_ARGS_IMPL ARGS
110 #define DEFINE_GETIMPL_LOOKUP(CLASS, ARGS) \
112 if (Storage == Uniqued) { \
113 if (auto *N = getUniqued(Context.pImpl->CLASS##s, \
114 CLASS##Info::KeyTy(UNWRAP_ARGS(ARGS)))) \
119 assert(ShouldCreate && \
120 "Expected non-uniqued nodes to always be created"); \
123 #define DEFINE_GETIMPL_STORE(CLASS, ARGS, OPS) \
124 return storeImpl(new (ArrayRef<Metadata *>(OPS).size()) \
125 CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS), \
126 Storage, Context.pImpl->CLASS##s)
127 #define DEFINE_GETIMPL_STORE_NO_OPS(CLASS, ARGS) \
128 return storeImpl(new (0u) CLASS(Context, Storage, UNWRAP_ARGS(ARGS)), \
129 Storage, Context.pImpl->CLASS##s)
130 #define DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(CLASS, OPS) \
131 return storeImpl(new (ArrayRef<Metadata *>(OPS).size()) \
132 CLASS(Context, Storage, OPS), \
133 Storage, Context.pImpl->CLASS##s)
135 MDSubrange *MDSubrange::getImpl(LLVMContext &Context, int64_t Count, int64_t Lo,
136 StorageType Storage, bool ShouldCreate) {
137 DEFINE_GETIMPL_LOOKUP(MDSubrange, (Count, Lo));
138 DEFINE_GETIMPL_STORE_NO_OPS(MDSubrange, (Count, Lo));
141 MDEnumerator *MDEnumerator::getImpl(LLVMContext &Context, int64_t Value,
142 MDString *Name, StorageType Storage,
144 assert(isCanonical(Name) && "Expected canonical MDString");
145 DEFINE_GETIMPL_LOOKUP(MDEnumerator, (Value, getString(Name)));
146 Metadata *Ops[] = {Name};
147 DEFINE_GETIMPL_STORE(MDEnumerator, (Value), Ops);
150 MDBasicType *MDBasicType::getImpl(LLVMContext &Context, unsigned Tag,
151 MDString *Name, uint64_t SizeInBits,
152 uint64_t AlignInBits, unsigned Encoding,
153 StorageType Storage, bool ShouldCreate) {
154 assert(isCanonical(Name) && "Expected canonical MDString");
155 DEFINE_GETIMPL_LOOKUP(
156 MDBasicType, (Tag, getString(Name), SizeInBits, AlignInBits, Encoding));
157 Metadata *Ops[] = {nullptr, nullptr, Name};
158 DEFINE_GETIMPL_STORE(MDBasicType, (Tag, SizeInBits, AlignInBits, Encoding),
162 MDDerivedType *MDDerivedType::getImpl(
163 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
164 unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
165 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
166 Metadata *ExtraData, StorageType Storage, bool ShouldCreate) {
167 assert(isCanonical(Name) && "Expected canonical MDString");
168 DEFINE_GETIMPL_LOOKUP(MDDerivedType, (Tag, getString(Name), File, Line, Scope,
169 BaseType, SizeInBits, AlignInBits,
170 OffsetInBits, Flags, ExtraData));
171 Metadata *Ops[] = {File, Scope, Name, BaseType, ExtraData};
172 DEFINE_GETIMPL_STORE(
173 MDDerivedType, (Tag, Line, SizeInBits, AlignInBits, OffsetInBits, Flags),
177 MDCompositeType *MDCompositeType::getImpl(
178 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File,
179 unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits,
180 uint64_t AlignInBits, uint64_t OffsetInBits, unsigned Flags,
181 Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder,
182 Metadata *TemplateParams, MDString *Identifier, StorageType Storage,
184 assert(isCanonical(Name) && "Expected canonical MDString");
185 DEFINE_GETIMPL_LOOKUP(MDCompositeType,
186 (Tag, getString(Name), File, Line, Scope, BaseType,
187 SizeInBits, AlignInBits, OffsetInBits, Flags, Elements,
188 RuntimeLang, VTableHolder, TemplateParams,
189 getString(Identifier)));
190 Metadata *Ops[] = {File, Scope, Name, BaseType,
191 Elements, VTableHolder, TemplateParams, Identifier};
192 DEFINE_GETIMPL_STORE(MDCompositeType, (Tag, Line, RuntimeLang, SizeInBits,
193 AlignInBits, OffsetInBits, Flags),
197 MDSubroutineType *MDSubroutineType::getImpl(LLVMContext &Context,
198 unsigned Flags, Metadata *TypeArray,
201 DEFINE_GETIMPL_LOOKUP(MDSubroutineType, (Flags, TypeArray));
202 Metadata *Ops[] = {nullptr, nullptr, nullptr, nullptr,
203 TypeArray, nullptr, nullptr, nullptr};
204 DEFINE_GETIMPL_STORE(MDSubroutineType, (Flags), Ops);
207 MDFile *MDFile::getImpl(LLVMContext &Context, MDString *Filename,
208 MDString *Directory, StorageType Storage,
210 assert(isCanonical(Filename) && "Expected canonical MDString");
211 assert(isCanonical(Directory) && "Expected canonical MDString");
212 DEFINE_GETIMPL_LOOKUP(MDFile, (getString(Filename), getString(Directory)));
213 Metadata *NodeOps[] = {Filename, Directory};
214 Metadata *Ops[] = {MDTuple::get(Context, NodeOps)};
215 return storeImpl(new (ArrayRef<Metadata *>(Ops).size())
216 MDFile(Context, Storage, Ops),
217 Storage, Context.pImpl->MDFiles);
220 MDCompileUnit *MDCompileUnit::getImpl(
221 LLVMContext &Context, unsigned SourceLanguage, Metadata *File,
222 MDString *Producer, bool IsOptimized, MDString *Flags,
223 unsigned RuntimeVersion, MDString *SplitDebugFilename,
224 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes,
225 Metadata *Subprograms, Metadata *GlobalVariables,
226 Metadata *ImportedEntities, StorageType Storage, bool ShouldCreate) {
227 assert(isCanonical(Producer) && "Expected canonical MDString");
228 assert(isCanonical(Flags) && "Expected canonical MDString");
229 assert(isCanonical(SplitDebugFilename) && "Expected canonical MDString");
230 DEFINE_GETIMPL_LOOKUP(
232 (SourceLanguage, File, getString(Producer), IsOptimized, getString(Flags),
233 RuntimeVersion, getString(SplitDebugFilename), EmissionKind, EnumTypes,
234 RetainedTypes, Subprograms, GlobalVariables, ImportedEntities));
235 Metadata *Ops[] = {File, Producer, Flags, SplitDebugFilename, EnumTypes,
236 RetainedTypes, Subprograms, GlobalVariables,
238 DEFINE_GETIMPL_STORE(
240 (SourceLanguage, IsOptimized, RuntimeVersion, EmissionKind), Ops);
243 MDSubprogram *MDSubprogram::getImpl(
244 LLVMContext &Context, Metadata *Scope, MDString *Name,
245 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type,
246 bool IsLocalToUnit, bool IsDefinition, unsigned ScopeLine,
247 Metadata *ContainingType, unsigned Virtuality, unsigned VirtualIndex,
248 unsigned Flags, bool IsOptimized, Metadata *Function,
249 Metadata *TemplateParams, Metadata *Declaration, Metadata *Variables,
250 StorageType Storage, bool ShouldCreate) {
251 assert(isCanonical(Name) && "Expected canonical MDString");
252 assert(isCanonical(LinkageName) && "Expected canonical MDString");
253 DEFINE_GETIMPL_LOOKUP(MDSubprogram,
254 (Scope, getString(Name), getString(LinkageName), File,
255 Line, Type, IsLocalToUnit, IsDefinition, ScopeLine,
256 ContainingType, Virtuality, VirtualIndex, Flags,
257 IsOptimized, Function, TemplateParams, Declaration,
259 Metadata *Ops[] = {File, Scope, Name, Name,
260 LinkageName, Type, ContainingType, Function,
261 TemplateParams, Declaration, Variables};
262 DEFINE_GETIMPL_STORE(MDSubprogram,
263 (Line, ScopeLine, Virtuality, VirtualIndex, Flags,
264 IsLocalToUnit, IsDefinition, IsOptimized),
268 void MDSubprogram::replaceFunction(Function *F) {
269 replaceFunction(F ? ConstantAsMetadata::get(F)
270 : static_cast<ConstantAsMetadata *>(nullptr));
273 MDLexicalBlock *MDLexicalBlock::getImpl(LLVMContext &Context, Metadata *Scope,
274 Metadata *File, unsigned Line,
275 unsigned Column, StorageType Storage,
277 DEFINE_GETIMPL_LOOKUP(MDLexicalBlock, (Scope, File, Line, Column));
278 Metadata *Ops[] = {File, Scope};
279 DEFINE_GETIMPL_STORE(MDLexicalBlock, (Line, Column), Ops);
282 MDLexicalBlockFile *MDLexicalBlockFile::getImpl(LLVMContext &Context,
283 Metadata *Scope, Metadata *File,
284 unsigned Discriminator,
287 DEFINE_GETIMPL_LOOKUP(MDLexicalBlockFile, (Scope, File, Discriminator));
288 Metadata *Ops[] = {File, Scope};
289 DEFINE_GETIMPL_STORE(MDLexicalBlockFile, (Discriminator), Ops);
292 MDNamespace *MDNamespace::getImpl(LLVMContext &Context, Metadata *Scope,
293 Metadata *File, MDString *Name, unsigned Line,
294 StorageType Storage, bool ShouldCreate) {
295 assert(isCanonical(Name) && "Expected canonical MDString");
296 DEFINE_GETIMPL_LOOKUP(MDNamespace, (Scope, File, getString(Name), Line));
297 Metadata *Ops[] = {File, Scope, Name};
298 DEFINE_GETIMPL_STORE(MDNamespace, (Line), Ops);
301 MDTemplateTypeParameter *MDTemplateTypeParameter::getImpl(LLVMContext &Context,
306 assert(isCanonical(Name) && "Expected canonical MDString");
307 DEFINE_GETIMPL_LOOKUP(MDTemplateTypeParameter, (getString(Name), Type));
308 Metadata *Ops[] = {Name, Type};
309 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(MDTemplateTypeParameter, Ops);
312 MDTemplateValueParameter *MDTemplateValueParameter::getImpl(
313 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *Type,
314 Metadata *Value, StorageType Storage, bool ShouldCreate) {
315 assert(isCanonical(Name) && "Expected canonical MDString");
316 DEFINE_GETIMPL_LOOKUP(MDTemplateValueParameter,
317 (Tag, getString(Name), Type, Value));
318 Metadata *Ops[] = {Name, Type, Value};
319 DEFINE_GETIMPL_STORE(MDTemplateValueParameter, (Tag), Ops);
323 MDGlobalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name,
324 MDString *LinkageName, Metadata *File, unsigned Line,
325 Metadata *Type, bool IsLocalToUnit, bool IsDefinition,
327 Metadata *StaticDataMemberDeclaration,
328 StorageType Storage, bool ShouldCreate) {
329 assert(isCanonical(Name) && "Expected canonical MDString");
330 assert(isCanonical(LinkageName) && "Expected canonical MDString");
331 DEFINE_GETIMPL_LOOKUP(MDGlobalVariable,
332 (Scope, getString(Name), getString(LinkageName), File,
333 Line, Type, IsLocalToUnit, IsDefinition, Variable,
334 StaticDataMemberDeclaration));
335 Metadata *Ops[] = {Scope, Name, File, Type,
336 Name, LinkageName, Variable, StaticDataMemberDeclaration};
337 DEFINE_GETIMPL_STORE(MDGlobalVariable, (Line, IsLocalToUnit, IsDefinition),
341 MDLocalVariable *MDLocalVariable::getImpl(
342 LLVMContext &Context, unsigned Tag, Metadata *Scope, MDString *Name,
343 Metadata *File, unsigned Line, Metadata *Type, unsigned Arg, unsigned Flags,
344 Metadata *InlinedAt, StorageType Storage, bool ShouldCreate) {
345 // Truncate Arg to 8 bits.
347 // FIXME: This is gross (and should be changed to an assert or removed), but
348 // it matches historical behaviour for now.
349 Arg &= (1u << 8) - 1;
351 assert(isCanonical(Name) && "Expected canonical MDString");
352 DEFINE_GETIMPL_LOOKUP(MDLocalVariable, (Tag, Scope, getString(Name), File,
353 Line, Type, Arg, Flags, InlinedAt));
354 Metadata *Ops[] = {Scope, Name, File, Type, InlinedAt};
355 DEFINE_GETIMPL_STORE(MDLocalVariable, (Tag, Line, Arg, Flags), Ops);
358 MDExpression *MDExpression::getImpl(LLVMContext &Context,
359 ArrayRef<uint64_t> Elements,
360 StorageType Storage, bool ShouldCreate) {
361 DEFINE_GETIMPL_LOOKUP(MDExpression, (Elements));
362 DEFINE_GETIMPL_STORE_NO_OPS(MDExpression, (Elements));
365 unsigned MDExpression::ExprOperand::getSize() const {
367 case dwarf::DW_OP_bit_piece:
369 case dwarf::DW_OP_plus:
376 bool MDExpression::isValid() const {
377 for (auto I = expr_op_begin(), E = expr_op_end(); I != E; ++I) {
378 // Check that there's space for the operand.
379 if (I->get() + I->getSize() > E->get())
382 // Check that the operand is valid.
383 switch (I->getOp()) {
386 case dwarf::DW_OP_bit_piece:
387 // Piece expressions must be at the end.
388 return I->get() + I->getSize() == E->get();
389 case dwarf::DW_OP_plus:
390 case dwarf::DW_OP_deref:
397 MDObjCProperty *MDObjCProperty::getImpl(
398 LLVMContext &Context, MDString *Name, Metadata *File, unsigned Line,
399 MDString *GetterName, MDString *SetterName, unsigned Attributes,
400 Metadata *Type, StorageType Storage, bool ShouldCreate) {
401 assert(isCanonical(Name) && "Expected canonical MDString");
402 assert(isCanonical(GetterName) && "Expected canonical MDString");
403 assert(isCanonical(SetterName) && "Expected canonical MDString");
404 DEFINE_GETIMPL_LOOKUP(MDObjCProperty,
405 (getString(Name), File, Line, getString(GetterName),
406 getString(SetterName), Attributes, Type));
407 Metadata *Ops[] = {Name, File, GetterName, SetterName, Type};
408 DEFINE_GETIMPL_STORE(MDObjCProperty, (Line, Attributes), Ops);
411 MDImportedEntity *MDImportedEntity::getImpl(LLVMContext &Context, unsigned Tag,
412 Metadata *Scope, Metadata *Entity,
413 unsigned Line, MDString *Name,
416 assert(isCanonical(Name) && "Expected canonical MDString");
417 DEFINE_GETIMPL_LOOKUP(MDImportedEntity,
418 (Tag, Scope, Entity, Line, getString(Name)));
419 Metadata *Ops[] = {Scope, Entity, Name};
420 DEFINE_GETIMPL_STORE(MDImportedEntity, (Tag, Line), Ops);