1 //===- DebugInfoMetadata.cpp - Implement debug info metadata --------------===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the debug info Metadata classes. 10 // 11 //===----------------------------------------------------------------------===// 12 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" 21 22 #include <numeric> 23 24 using namespace llvm; 25 26 namespace llvm { 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")); 31 } // namespace llvm 32 33 const DIExpression::FragmentInfo DebugVariable::DefaultFragment = { 34 std::numeric_limits<uint64_t>::max(), std::numeric_limits<uint64_t>::min()}; 35 36 DILocation::DILocation(LLVMContext &C, StorageType Storage, unsigned Line, 37 unsigned Column, ArrayRef<Metadata *> MDs, 38 bool ImplicitCode) 39 : MDNode(C, DILocationKind, Storage, MDs) { 40 assert((MDs.size() == 1 || MDs.size() == 2) && 41 "Expected a scope and optional inlined-at"); 42 43 // Set line and column. 44 assert(Column < (1u << 16) && "Expected 16-bit column"); 45 46 SubclassData32 = Line; 47 SubclassData16 = Column; 48 49 setImplicitCode(ImplicitCode); 50 } 51 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)) 55 Column = 0; 56 } 57 58 DILocation *DILocation::getImpl(LLVMContext &Context, unsigned Line, 59 unsigned Column, Metadata *Scope, 60 Metadata *InlinedAt, bool ImplicitCode, 61 StorageType Storage, bool ShouldCreate) { 62 // Fixup column. 63 adjustColumn(Column); 64 65 if (Storage == Uniqued) { 66 if (auto *N = getUniqued(Context.pImpl->DILocations, 67 DILocationInfo::KeyTy(Line, Column, Scope, 68 InlinedAt, ImplicitCode))) 69 return N; 70 if (!ShouldCreate) 71 return nullptr; 72 } else { 73 assert(ShouldCreate && "Expected non-uniqued nodes to always be created"); 74 } 75 76 SmallVector<Metadata *, 2> Ops; 77 Ops.push_back(Scope); 78 if (InlinedAt) 79 Ops.push_back(InlinedAt); 80 return storeImpl(new (Ops.size()) DILocation(Context, Storage, Line, Column, 81 Ops, ImplicitCode), 82 Storage, Context.pImpl->DILocations); 83 } 84 85 const 86 DILocation *DILocation::getMergedLocations(ArrayRef<const DILocation *> Locs) { 87 if (Locs.empty()) 88 return nullptr; 89 if (Locs.size() == 1) 90 return Locs[0]; 91 auto *Merged = Locs[0]; 92 for (const DILocation *L : llvm::drop_begin(Locs)) { 93 Merged = getMergedLocation(Merged, L); 94 if (Merged == nullptr) 95 break; 96 } 97 return Merged; 98 } 99 100 const DILocation *DILocation::getMergedLocation(const DILocation *LocA, 101 const DILocation *LocB) { 102 if (!LocA || !LocB) 103 return nullptr; 104 105 if (LocA == LocB) 106 return LocA; 107 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(); 114 while (S) { 115 Locations.insert(std::make_pair(S, L)); 116 S = S->getScope(); 117 if (!S && L) { 118 S = L->getScope(); 119 L = L->getInlinedAt(); 120 } 121 } 122 const DILocation *Result = LocB; 123 S = LocB->getScope(); 124 L = LocB->getInlinedAt(); 125 while (S) { 126 if (Locations.count(std::make_pair(S, L))) 127 break; 128 S = S->getScope(); 129 if (!S && L) { 130 S = L->getScope(); 131 L = L->getInlinedAt(); 132 } 133 } 134 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); 140 } 141 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); 151 152 int I = 0; 153 unsigned Ret = 0; 154 unsigned NextBitInsertionIndex = 0; 155 while (RemainingWork > 0) { 156 unsigned C = Components[I++]; 157 RemainingWork -= C; 158 unsigned EC = encodeComponent(C); 159 Ret |= (EC << NextBitInsertionIndex); 160 NextBitInsertionIndex += encodingBits(C); 161 } 162 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 166 // simpler. 167 unsigned TBD, TDF, TCI = 0; 168 decodeDiscriminator(Ret, TBD, TDF, TCI); 169 if (TBD == BD && TDF == DF && TCI == CI) 170 return Ret; 171 return None; 172 } 173 174 void DILocation::decodeDiscriminator(unsigned D, unsigned &BD, unsigned &DF, 175 unsigned &CI) { 176 BD = getUnsignedFromPrefixEncoding(D); 177 DF = getUnsignedFromPrefixEncoding(getNextComponentInDiscriminator(D)); 178 CI = getUnsignedFromPrefixEncoding( 179 getNextComponentInDiscriminator(getNextComponentInDiscriminator(D))); 180 } 181 182 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); 188 } 189 190 StringRef DINode::getFlagString(DIFlags Flag) { 191 switch (Flag) { 192 #define HANDLE_DI_FLAG(ID, NAME) \ 193 case Flag##NAME: \ 194 return "DIFlag" #NAME; 195 #include "llvm/IR/DebugInfoFlags.def" 196 } 197 return ""; 198 } 199 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); 210 else 211 SplitFlags.push_back(FlagPublic); 212 Flags &= ~A; 213 } 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); 219 else 220 SplitFlags.push_back(FlagVirtualInheritance); 221 Flags &= ~R; 222 } 223 if ((Flags & FlagIndirectVirtualBase) == FlagIndirectVirtualBase) { 224 Flags &= ~FlagIndirectVirtualBase; 225 SplitFlags.push_back(FlagIndirectVirtualBase); 226 } 227 228 #define HANDLE_DI_FLAG(ID, NAME) \ 229 if (DIFlags Bit = Flags & Flag##NAME) { \ 230 SplitFlags.push_back(Bit); \ 231 Flags &= ~Bit; \ 232 } 233 #include "llvm/IR/DebugInfoFlags.def" 234 return Flags; 235 } 236 237 DIScope *DIScope::getScope() const { 238 if (auto *T = dyn_cast<DIType>(this)) 239 return T->getScope(); 240 241 if (auto *SP = dyn_cast<DISubprogram>(this)) 242 return SP->getScope(); 243 244 if (auto *LB = dyn_cast<DILexicalBlockBase>(this)) 245 return LB->getScope(); 246 247 if (auto *NS = dyn_cast<DINamespace>(this)) 248 return NS->getScope(); 249 250 if (auto *CB = dyn_cast<DICommonBlock>(this)) 251 return CB->getScope(); 252 253 if (auto *M = dyn_cast<DIModule>(this)) 254 return M->getScope(); 255 256 assert((isa<DIFile>(this) || isa<DICompileUnit>(this)) && 257 "Unhandled type of scope."); 258 return nullptr; 259 } 260 261 StringRef DIScope::getName() const { 262 if (auto *T = dyn_cast<DIType>(this)) 263 return T->getName(); 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)) 271 return M->getName(); 272 assert((isa<DILexicalBlockBase>(this) || isa<DIFile>(this) || 273 isa<DICompileUnit>(this)) && 274 "Unhandled type of scope."); 275 return ""; 276 } 277 278 #ifndef NDEBUG 279 static bool isCanonical(const MDString *S) { 280 return !S || !S->getString().empty(); 281 } 282 #endif 283 284 GenericDINode *GenericDINode::getImpl(LLVMContext &Context, unsigned Tag, 285 MDString *Header, 286 ArrayRef<Metadata *> DwarfOps, 287 StorageType Storage, bool ShouldCreate) { 288 unsigned Hash = 0; 289 if (Storage == Uniqued) { 290 GenericDINodeInfo::KeyTy Key(Tag, Header, DwarfOps); 291 if (auto *N = getUniqued(Context.pImpl->GenericDINodes, Key)) 292 return N; 293 if (!ShouldCreate) 294 return nullptr; 295 Hash = Key.getHash(); 296 } else { 297 assert(ShouldCreate && "Expected non-uniqued nodes to always be created"); 298 } 299 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); 306 } 307 308 void GenericDINode::recalculateHash() { 309 setHash(GenericDINodeInfo::KeyTy::calculateHash(this)); 310 } 311 312 #define UNWRAP_ARGS_IMPL(...) __VA_ARGS__ 313 #define UNWRAP_ARGS(ARGS) UNWRAP_ARGS_IMPL ARGS 314 #define DEFINE_GETIMPL_LOOKUP(CLASS, ARGS) \ 315 do { \ 316 if (Storage == Uniqued) { \ 317 if (auto *N = getUniqued(Context.pImpl->CLASS##s, \ 318 CLASS##Info::KeyTy(UNWRAP_ARGS(ARGS)))) \ 319 return N; \ 320 if (!ShouldCreate) \ 321 return nullptr; \ 322 } else { \ 323 assert(ShouldCreate && \ 324 "Expected non-uniqued nodes to always be created"); \ 325 } \ 326 } while (false) 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) 341 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, 349 ShouldCreate); 350 } 351 352 DISubrange *DISubrange::getImpl(LLVMContext &Context, Metadata *CountNode, 353 int64_t Lo, StorageType Storage, 354 bool ShouldCreate) { 355 auto *LB = ConstantAsMetadata::get( 356 ConstantInt::getSigned(Type::getInt64Ty(Context), Lo)); 357 return getImpl(Context, CountNode, LB, nullptr, nullptr, Storage, 358 ShouldCreate); 359 } 360 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); 367 } 368 369 DISubrange::BoundType DISubrange::getCount() const { 370 Metadata *CB = getRawCountNode(); 371 if (!CB) 372 return BoundType(); 373 374 assert((isa<ConstantAsMetadata>(CB) || isa<DIVariable>(CB) || 375 isa<DIExpression>(CB)) && 376 "Count must be signed constant or DIVariable or DIExpression"); 377 378 if (auto *MD = dyn_cast<ConstantAsMetadata>(CB)) 379 return BoundType(cast<ConstantInt>(MD->getValue())); 380 381 if (auto *MD = dyn_cast<DIVariable>(CB)) 382 return BoundType(MD); 383 384 if (auto *MD = dyn_cast<DIExpression>(CB)) 385 return BoundType(MD); 386 387 return BoundType(); 388 } 389 390 DISubrange::BoundType DISubrange::getLowerBound() const { 391 Metadata *LB = getRawLowerBound(); 392 if (!LB) 393 return BoundType(); 394 395 assert((isa<ConstantAsMetadata>(LB) || isa<DIVariable>(LB) || 396 isa<DIExpression>(LB)) && 397 "LowerBound must be signed constant or DIVariable or DIExpression"); 398 399 if (auto *MD = dyn_cast<ConstantAsMetadata>(LB)) 400 return BoundType(cast<ConstantInt>(MD->getValue())); 401 402 if (auto *MD = dyn_cast<DIVariable>(LB)) 403 return BoundType(MD); 404 405 if (auto *MD = dyn_cast<DIExpression>(LB)) 406 return BoundType(MD); 407 408 return BoundType(); 409 } 410 411 DISubrange::BoundType DISubrange::getUpperBound() const { 412 Metadata *UB = getRawUpperBound(); 413 if (!UB) 414 return BoundType(); 415 416 assert((isa<ConstantAsMetadata>(UB) || isa<DIVariable>(UB) || 417 isa<DIExpression>(UB)) && 418 "UpperBound must be signed constant or DIVariable or DIExpression"); 419 420 if (auto *MD = dyn_cast<ConstantAsMetadata>(UB)) 421 return BoundType(cast<ConstantInt>(MD->getValue())); 422 423 if (auto *MD = dyn_cast<DIVariable>(UB)) 424 return BoundType(MD); 425 426 if (auto *MD = dyn_cast<DIExpression>(UB)) 427 return BoundType(MD); 428 429 return BoundType(); 430 } 431 432 DISubrange::BoundType DISubrange::getStride() const { 433 Metadata *ST = getRawStride(); 434 if (!ST) 435 return BoundType(); 436 437 assert((isa<ConstantAsMetadata>(ST) || isa<DIVariable>(ST) || 438 isa<DIExpression>(ST)) && 439 "Stride must be signed constant or DIVariable or DIExpression"); 440 441 if (auto *MD = dyn_cast<ConstantAsMetadata>(ST)) 442 return BoundType(cast<ConstantInt>(MD->getValue())); 443 444 if (auto *MD = dyn_cast<DIVariable>(ST)) 445 return BoundType(MD); 446 447 if (auto *MD = dyn_cast<DIExpression>(ST)) 448 return BoundType(MD); 449 450 return BoundType(); 451 } 452 453 DIGenericSubrange *DIGenericSubrange::getImpl(LLVMContext &Context, 454 Metadata *CountNode, Metadata *LB, 455 Metadata *UB, Metadata *Stride, 456 StorageType Storage, 457 bool ShouldCreate) { 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); 461 } 462 463 DIGenericSubrange::BoundType DIGenericSubrange::getCount() const { 464 Metadata *CB = getRawCountNode(); 465 if (!CB) 466 return BoundType(); 467 468 assert((isa<DIVariable>(CB) || isa<DIExpression>(CB)) && 469 "Count must be signed constant or DIVariable or DIExpression"); 470 471 if (auto *MD = dyn_cast<DIVariable>(CB)) 472 return BoundType(MD); 473 474 if (auto *MD = dyn_cast<DIExpression>(CB)) 475 return BoundType(MD); 476 477 return BoundType(); 478 } 479 480 DIGenericSubrange::BoundType DIGenericSubrange::getLowerBound() const { 481 Metadata *LB = getRawLowerBound(); 482 if (!LB) 483 return BoundType(); 484 485 assert((isa<DIVariable>(LB) || isa<DIExpression>(LB)) && 486 "LowerBound must be signed constant or DIVariable or DIExpression"); 487 488 if (auto *MD = dyn_cast<DIVariable>(LB)) 489 return BoundType(MD); 490 491 if (auto *MD = dyn_cast<DIExpression>(LB)) 492 return BoundType(MD); 493 494 return BoundType(); 495 } 496 497 DIGenericSubrange::BoundType DIGenericSubrange::getUpperBound() const { 498 Metadata *UB = getRawUpperBound(); 499 if (!UB) 500 return BoundType(); 501 502 assert((isa<DIVariable>(UB) || isa<DIExpression>(UB)) && 503 "UpperBound must be signed constant or DIVariable or DIExpression"); 504 505 if (auto *MD = dyn_cast<DIVariable>(UB)) 506 return BoundType(MD); 507 508 if (auto *MD = dyn_cast<DIExpression>(UB)) 509 return BoundType(MD); 510 511 return BoundType(); 512 } 513 514 DIGenericSubrange::BoundType DIGenericSubrange::getStride() const { 515 Metadata *ST = getRawStride(); 516 if (!ST) 517 return BoundType(); 518 519 assert((isa<DIVariable>(ST) || isa<DIExpression>(ST)) && 520 "Stride must be signed constant or DIVariable or DIExpression"); 521 522 if (auto *MD = dyn_cast<DIVariable>(ST)) 523 return BoundType(MD); 524 525 if (auto *MD = dyn_cast<DIExpression>(ST)) 526 return BoundType(MD); 527 528 return BoundType(); 529 } 530 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); 538 } 539 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, 544 bool ShouldCreate) { 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, 550 Flags), Ops); 551 } 552 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; 561 default: 562 return None; 563 } 564 } 565 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, 571 bool ShouldCreate) { 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), 577 Ops); 578 } 579 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); 595 } 596 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, 605 bool ShouldCreate) { 606 assert(isCanonical(Name) && "Expected canonical MDString"); 607 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, 614 Rank, Annotations)); 615 Metadata *Ops[] = {File, Scope, Name, BaseType, 616 Elements, VTableHolder, TemplateParams, Identifier, 617 Discriminator, DataLocation, Associated, Allocated, 618 Rank, Annotations}; 619 DEFINE_GETIMPL_STORE(DICompositeType, (Tag, Line, RuntimeLang, SizeInBits, 620 AlignInBits, OffsetInBits, Flags), 621 Ops); 622 } 623 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()) 634 return nullptr; 635 auto *&CT = (*Context.pImpl->DITypeMap)[&Identifier]; 636 if (!CT) 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); 642 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)) 646 return CT; 647 648 // Mutate CT in place. Keep this in sync with getImpl. 649 CT->mutate(Tag, Line, RuntimeLang, SizeInBits, AlignInBits, OffsetInBits, 650 Flags); 651 Metadata *Ops[] = {File, Scope, Name, BaseType, 652 Elements, VTableHolder, TemplateParams, &Identifier, 653 Discriminator, DataLocation, Associated, Allocated, 654 Rank, Annotations}; 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]); 660 return CT; 661 } 662 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()) 673 return nullptr; 674 auto *&CT = (*Context.pImpl->DITypeMap)[&Identifier]; 675 if (!CT) 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); 681 return CT; 682 } 683 684 DICompositeType *DICompositeType::getODRTypeIfExists(LLVMContext &Context, 685 MDString &Identifier) { 686 assert(!Identifier.getString().empty() && "Expected valid identifier"); 687 if (!Context.isODRUniquingDebugTypes()) 688 return nullptr; 689 return Context.pImpl->DITypeMap->lookup(&Identifier); 690 } 691 692 DISubroutineType *DISubroutineType::getImpl(LLVMContext &Context, DIFlags Flags, 693 uint8_t CC, Metadata *TypeArray, 694 StorageType Storage, 695 bool ShouldCreate) { 696 DEFINE_GETIMPL_LOOKUP(DISubroutineType, (Flags, CC, TypeArray)); 697 Metadata *Ops[] = {nullptr, nullptr, nullptr, TypeArray}; 698 DEFINE_GETIMPL_STORE(DISubroutineType, (Flags, CC), Ops); 699 } 700 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] = { 704 "CSK_MD5", 705 "CSK_SHA1", 706 "CSK_SHA256", 707 }; 708 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 713 // for it here. 714 return ChecksumKindName[CSKind - 1]; 715 } 716 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) 722 .Default(None); 723 } 724 725 DIFile *DIFile::getImpl(LLVMContext &Context, MDString *Filename, 726 MDString *Directory, 727 Optional<DIFile::ChecksumInfo<MDString *>> CS, 728 Optional<MDString *> Source, StorageType Storage, 729 bool ShouldCreate) { 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); 738 } 739 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"); 753 754 Metadata *Ops[] = {File, 755 Producer, 756 Flags, 757 SplitDebugFilename, 758 EnumTypes, 759 RetainedTypes, 760 GlobalVariables, 761 ImportedEntities, 762 Macros, 763 SysRoot, 764 SDK}; 765 return storeImpl(new (array_lengthof(Ops)) DICompileUnit( 766 Context, Storage, SourceLanguage, IsOptimized, 767 RuntimeVersion, EmissionKind, DWOId, SplitDebugInlining, 768 DebugInfoForProfiling, NameTableKind, RangesBaseAddress, 769 Ops), 770 Storage); 771 } 772 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) 780 .Default(None); 781 } 782 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) 789 .Default(None); 790 } 791 792 const char *DICompileUnit::emissionKindString(DebugEmissionKind EK) { 793 switch (EK) { 794 case NoDebug: return "NoDebug"; 795 case FullDebug: return "FullDebug"; 796 case LineTablesOnly: return "LineTablesOnly"; 797 case DebugDirectivesOnly: return "DebugDirectivesOnly"; 798 } 799 return nullptr; 800 } 801 802 const char *DICompileUnit::nameTableKindString(DebugNameTableKind NTK) { 803 switch (NTK) { 804 case DebugNameTableKind::Default: 805 return nullptr; 806 case DebugNameTableKind::GNU: 807 return "GNU"; 808 case DebugNameTableKind::None: 809 return "None"; 810 } 811 return nullptr; 812 } 813 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)); 818 } 819 820 DILocalScope *DILocalScope::getNonLexicalBlockFileScope() const { 821 if (auto *File = dyn_cast<DILexicalBlockFile>(this)) 822 return File->getScope()->getNonLexicalBlockFileScope(); 823 return const_cast<DILocalScope *>(this); 824 } 825 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); 831 } 832 833 StringRef DISubprogram::getFlagString(DISPFlags Flag) { 834 switch (Flag) { 835 // Appease a warning. 836 case SPFlagVirtuality: 837 return ""; 838 #define HANDLE_DISP_FLAG(ID, NAME) \ 839 case SPFlag##NAME: \ 840 return "DISPFlag" #NAME; 841 #include "llvm/IR/DebugInfoFlags.def" 842 } 843 return ""; 844 } 845 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); \ 855 Flags &= ~Bit; \ 856 } 857 #include "llvm/IR/DebugInfoFlags.def" 858 return Flags; 859 } 860 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, Metadata *Annotations, StorageType Storage, 868 bool ShouldCreate) { 869 assert(isCanonical(Name) && "Expected canonical MDString"); 870 assert(isCanonical(LinkageName) && "Expected canonical MDString"); 871 DEFINE_GETIMPL_LOOKUP(DISubprogram, 872 (Scope, Name, LinkageName, File, Line, Type, ScopeLine, 873 ContainingType, VirtualIndex, ThisAdjustment, Flags, 874 SPFlags, Unit, TemplateParams, Declaration, 875 RetainedNodes, ThrownTypes, Annotations)); 876 SmallVector<Metadata *, 12> Ops = { 877 File, Scope, Name, LinkageName, Type, Unit, 878 Declaration, RetainedNodes, ContainingType, TemplateParams, ThrownTypes, 879 Annotations}; 880 if (!Annotations) { 881 Ops.pop_back(); 882 if (!ThrownTypes) { 883 Ops.pop_back(); 884 if (!TemplateParams) { 885 Ops.pop_back(); 886 if (!ContainingType) 887 Ops.pop_back(); 888 } 889 } 890 } 891 DEFINE_GETIMPL_STORE_N( 892 DISubprogram, 893 (Line, ScopeLine, VirtualIndex, ThisAdjustment, Flags, SPFlags), Ops, 894 Ops.size()); 895 } 896 897 bool DISubprogram::describes(const Function *F) const { 898 assert(F && "Invalid function"); 899 return F->getSubprogram() == this; 900 } 901 902 DILexicalBlock *DILexicalBlock::getImpl(LLVMContext &Context, Metadata *Scope, 903 Metadata *File, unsigned Line, 904 unsigned Column, StorageType Storage, 905 bool ShouldCreate) { 906 // Fixup column. 907 adjustColumn(Column); 908 909 assert(Scope && "Expected scope"); 910 DEFINE_GETIMPL_LOOKUP(DILexicalBlock, (Scope, File, Line, Column)); 911 Metadata *Ops[] = {File, Scope}; 912 DEFINE_GETIMPL_STORE(DILexicalBlock, (Line, Column), Ops); 913 } 914 915 DILexicalBlockFile *DILexicalBlockFile::getImpl(LLVMContext &Context, 916 Metadata *Scope, Metadata *File, 917 unsigned Discriminator, 918 StorageType Storage, 919 bool ShouldCreate) { 920 assert(Scope && "Expected scope"); 921 DEFINE_GETIMPL_LOOKUP(DILexicalBlockFile, (Scope, File, Discriminator)); 922 Metadata *Ops[] = {File, Scope}; 923 DEFINE_GETIMPL_STORE(DILexicalBlockFile, (Discriminator), Ops); 924 } 925 926 DINamespace *DINamespace::getImpl(LLVMContext &Context, Metadata *Scope, 927 MDString *Name, bool ExportSymbols, 928 StorageType Storage, bool ShouldCreate) { 929 assert(isCanonical(Name) && "Expected canonical MDString"); 930 DEFINE_GETIMPL_LOOKUP(DINamespace, (Scope, Name, ExportSymbols)); 931 // The nullptr is for DIScope's File operand. This should be refactored. 932 Metadata *Ops[] = {nullptr, Scope, Name}; 933 DEFINE_GETIMPL_STORE(DINamespace, (ExportSymbols), Ops); 934 } 935 936 DICommonBlock *DICommonBlock::getImpl(LLVMContext &Context, Metadata *Scope, 937 Metadata *Decl, MDString *Name, 938 Metadata *File, unsigned LineNo, 939 StorageType Storage, bool ShouldCreate) { 940 assert(isCanonical(Name) && "Expected canonical MDString"); 941 DEFINE_GETIMPL_LOOKUP(DICommonBlock, (Scope, Decl, Name, File, LineNo)); 942 // The nullptr is for DIScope's File operand. This should be refactored. 943 Metadata *Ops[] = {Scope, Decl, Name, File}; 944 DEFINE_GETIMPL_STORE(DICommonBlock, (LineNo), Ops); 945 } 946 947 DIModule *DIModule::getImpl(LLVMContext &Context, Metadata *File, 948 Metadata *Scope, MDString *Name, 949 MDString *ConfigurationMacros, 950 MDString *IncludePath, MDString *APINotesFile, 951 unsigned LineNo, bool IsDecl, StorageType Storage, 952 bool ShouldCreate) { 953 assert(isCanonical(Name) && "Expected canonical MDString"); 954 DEFINE_GETIMPL_LOOKUP(DIModule, (File, Scope, Name, ConfigurationMacros, 955 IncludePath, APINotesFile, LineNo, IsDecl)); 956 Metadata *Ops[] = {File, Scope, Name, ConfigurationMacros, 957 IncludePath, APINotesFile}; 958 DEFINE_GETIMPL_STORE(DIModule, (LineNo, IsDecl), Ops); 959 } 960 961 DITemplateTypeParameter * 962 DITemplateTypeParameter::getImpl(LLVMContext &Context, MDString *Name, 963 Metadata *Type, bool isDefault, 964 StorageType Storage, bool ShouldCreate) { 965 assert(isCanonical(Name) && "Expected canonical MDString"); 966 DEFINE_GETIMPL_LOOKUP(DITemplateTypeParameter, (Name, Type, isDefault)); 967 Metadata *Ops[] = {Name, Type}; 968 DEFINE_GETIMPL_STORE(DITemplateTypeParameter, (isDefault), Ops); 969 } 970 971 DITemplateValueParameter *DITemplateValueParameter::getImpl( 972 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *Type, 973 bool isDefault, Metadata *Value, StorageType Storage, bool ShouldCreate) { 974 assert(isCanonical(Name) && "Expected canonical MDString"); 975 DEFINE_GETIMPL_LOOKUP(DITemplateValueParameter, 976 (Tag, Name, Type, isDefault, Value)); 977 Metadata *Ops[] = {Name, Type, Value}; 978 DEFINE_GETIMPL_STORE(DITemplateValueParameter, (Tag, isDefault), Ops); 979 } 980 981 DIGlobalVariable * 982 DIGlobalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name, 983 MDString *LinkageName, Metadata *File, unsigned Line, 984 Metadata *Type, bool IsLocalToUnit, bool IsDefinition, 985 Metadata *StaticDataMemberDeclaration, 986 Metadata *TemplateParams, uint32_t AlignInBits, 987 Metadata *Annotations, StorageType Storage, 988 bool ShouldCreate) { 989 assert(isCanonical(Name) && "Expected canonical MDString"); 990 assert(isCanonical(LinkageName) && "Expected canonical MDString"); 991 DEFINE_GETIMPL_LOOKUP(DIGlobalVariable, (Scope, Name, LinkageName, File, Line, 992 Type, IsLocalToUnit, IsDefinition, 993 StaticDataMemberDeclaration, 994 TemplateParams, AlignInBits, 995 Annotations)); 996 Metadata *Ops[] = {Scope, 997 Name, 998 File, 999 Type, 1000 Name, 1001 LinkageName, 1002 StaticDataMemberDeclaration, 1003 TemplateParams, 1004 Annotations}; 1005 DEFINE_GETIMPL_STORE(DIGlobalVariable, 1006 (Line, IsLocalToUnit, IsDefinition, AlignInBits), Ops); 1007 } 1008 1009 DILocalVariable *DILocalVariable::getImpl(LLVMContext &Context, Metadata *Scope, 1010 MDString *Name, Metadata *File, 1011 unsigned Line, Metadata *Type, 1012 unsigned Arg, DIFlags Flags, 1013 uint32_t AlignInBits, 1014 StorageType Storage, 1015 bool ShouldCreate) { 1016 // 64K ought to be enough for any frontend. 1017 assert(Arg <= UINT16_MAX && "Expected argument number to fit in 16-bits"); 1018 1019 assert(Scope && "Expected scope"); 1020 assert(isCanonical(Name) && "Expected canonical MDString"); 1021 DEFINE_GETIMPL_LOOKUP(DILocalVariable, 1022 (Scope, Name, File, Line, Type, Arg, Flags, 1023 AlignInBits)); 1024 Metadata *Ops[] = {Scope, Name, File, Type}; 1025 DEFINE_GETIMPL_STORE(DILocalVariable, (Line, Arg, Flags, AlignInBits), Ops); 1026 } 1027 1028 Optional<uint64_t> DIVariable::getSizeInBits() const { 1029 // This is used by the Verifier so be mindful of broken types. 1030 const Metadata *RawType = getRawType(); 1031 while (RawType) { 1032 // Try to get the size directly. 1033 if (auto *T = dyn_cast<DIType>(RawType)) 1034 if (uint64_t Size = T->getSizeInBits()) 1035 return Size; 1036 1037 if (auto *DT = dyn_cast<DIDerivedType>(RawType)) { 1038 // Look at the base type. 1039 RawType = DT->getRawBaseType(); 1040 continue; 1041 } 1042 1043 // Missing type or size. 1044 break; 1045 } 1046 1047 // Fail gracefully. 1048 return None; 1049 } 1050 1051 DILabel *DILabel::getImpl(LLVMContext &Context, Metadata *Scope, 1052 MDString *Name, Metadata *File, unsigned Line, 1053 StorageType Storage, 1054 bool ShouldCreate) { 1055 assert(Scope && "Expected scope"); 1056 assert(isCanonical(Name) && "Expected canonical MDString"); 1057 DEFINE_GETIMPL_LOOKUP(DILabel, 1058 (Scope, Name, File, Line)); 1059 Metadata *Ops[] = {Scope, Name, File}; 1060 DEFINE_GETIMPL_STORE(DILabel, (Line), Ops); 1061 } 1062 1063 DIExpression *DIExpression::getImpl(LLVMContext &Context, 1064 ArrayRef<uint64_t> Elements, 1065 StorageType Storage, bool ShouldCreate) { 1066 DEFINE_GETIMPL_LOOKUP(DIExpression, (Elements)); 1067 DEFINE_GETIMPL_STORE_NO_OPS(DIExpression, (Elements)); 1068 } 1069 1070 unsigned DIExpression::ExprOperand::getSize() const { 1071 uint64_t Op = getOp(); 1072 1073 if (Op >= dwarf::DW_OP_breg0 && Op <= dwarf::DW_OP_breg31) 1074 return 2; 1075 1076 switch (Op) { 1077 case dwarf::DW_OP_LLVM_convert: 1078 case dwarf::DW_OP_LLVM_fragment: 1079 case dwarf::DW_OP_bregx: 1080 return 3; 1081 case dwarf::DW_OP_constu: 1082 case dwarf::DW_OP_consts: 1083 case dwarf::DW_OP_deref_size: 1084 case dwarf::DW_OP_plus_uconst: 1085 case dwarf::DW_OP_LLVM_tag_offset: 1086 case dwarf::DW_OP_LLVM_entry_value: 1087 case dwarf::DW_OP_LLVM_arg: 1088 case dwarf::DW_OP_regx: 1089 return 2; 1090 default: 1091 return 1; 1092 } 1093 } 1094 1095 bool DIExpression::isValid() const { 1096 for (auto I = expr_op_begin(), E = expr_op_end(); I != E; ++I) { 1097 // Check that there's space for the operand. 1098 if (I->get() + I->getSize() > E->get()) 1099 return false; 1100 1101 uint64_t Op = I->getOp(); 1102 if ((Op >= dwarf::DW_OP_reg0 && Op <= dwarf::DW_OP_reg31) || 1103 (Op >= dwarf::DW_OP_breg0 && Op <= dwarf::DW_OP_breg31)) 1104 return true; 1105 1106 // Check that the operand is valid. 1107 switch (Op) { 1108 default: 1109 return false; 1110 case dwarf::DW_OP_LLVM_fragment: 1111 // A fragment operator must appear at the end. 1112 return I->get() + I->getSize() == E->get(); 1113 case dwarf::DW_OP_stack_value: { 1114 // Must be the last one or followed by a DW_OP_LLVM_fragment. 1115 if (I->get() + I->getSize() == E->get()) 1116 break; 1117 auto J = I; 1118 if ((++J)->getOp() != dwarf::DW_OP_LLVM_fragment) 1119 return false; 1120 break; 1121 } 1122 case dwarf::DW_OP_swap: { 1123 // Must be more than one implicit element on the stack. 1124 1125 // FIXME: A better way to implement this would be to add a local variable 1126 // that keeps track of the stack depth and introduce something like a 1127 // DW_LLVM_OP_implicit_location as a placeholder for the location this 1128 // DIExpression is attached to, or else pass the number of implicit stack 1129 // elements into isValid. 1130 if (getNumElements() == 1) 1131 return false; 1132 break; 1133 } 1134 case dwarf::DW_OP_LLVM_entry_value: { 1135 // An entry value operator must appear at the beginning and the number of 1136 // operations it cover can currently only be 1, because we support only 1137 // entry values of a simple register location. One reason for this is that 1138 // we currently can't calculate the size of the resulting DWARF block for 1139 // other expressions. 1140 return I->get() == expr_op_begin()->get() && I->getArg(0) == 1; 1141 } 1142 case dwarf::DW_OP_LLVM_implicit_pointer: 1143 case dwarf::DW_OP_LLVM_convert: 1144 case dwarf::DW_OP_LLVM_arg: 1145 case dwarf::DW_OP_LLVM_tag_offset: 1146 case dwarf::DW_OP_constu: 1147 case dwarf::DW_OP_plus_uconst: 1148 case dwarf::DW_OP_plus: 1149 case dwarf::DW_OP_minus: 1150 case dwarf::DW_OP_mul: 1151 case dwarf::DW_OP_div: 1152 case dwarf::DW_OP_mod: 1153 case dwarf::DW_OP_or: 1154 case dwarf::DW_OP_and: 1155 case dwarf::DW_OP_xor: 1156 case dwarf::DW_OP_shl: 1157 case dwarf::DW_OP_shr: 1158 case dwarf::DW_OP_shra: 1159 case dwarf::DW_OP_deref: 1160 case dwarf::DW_OP_deref_size: 1161 case dwarf::DW_OP_xderef: 1162 case dwarf::DW_OP_lit0: 1163 case dwarf::DW_OP_not: 1164 case dwarf::DW_OP_dup: 1165 case dwarf::DW_OP_regx: 1166 case dwarf::DW_OP_bregx: 1167 case dwarf::DW_OP_push_object_address: 1168 case dwarf::DW_OP_over: 1169 case dwarf::DW_OP_consts: 1170 break; 1171 } 1172 } 1173 return true; 1174 } 1175 1176 bool DIExpression::isImplicit() const { 1177 if (!isValid()) 1178 return false; 1179 1180 if (getNumElements() == 0) 1181 return false; 1182 1183 for (const auto &It : expr_ops()) { 1184 switch (It.getOp()) { 1185 default: 1186 break; 1187 case dwarf::DW_OP_stack_value: 1188 case dwarf::DW_OP_LLVM_tag_offset: 1189 return true; 1190 } 1191 } 1192 1193 return false; 1194 } 1195 1196 bool DIExpression::isComplex() const { 1197 if (!isValid()) 1198 return false; 1199 1200 if (getNumElements() == 0) 1201 return false; 1202 1203 // If there are any elements other than fragment or tag_offset, then some 1204 // kind of complex computation occurs. 1205 for (const auto &It : expr_ops()) { 1206 switch (It.getOp()) { 1207 case dwarf::DW_OP_LLVM_tag_offset: 1208 case dwarf::DW_OP_LLVM_fragment: 1209 continue; 1210 default: return true; 1211 } 1212 } 1213 1214 return false; 1215 } 1216 1217 Optional<DIExpression::FragmentInfo> 1218 DIExpression::getFragmentInfo(expr_op_iterator Start, expr_op_iterator End) { 1219 for (auto I = Start; I != End; ++I) 1220 if (I->getOp() == dwarf::DW_OP_LLVM_fragment) { 1221 DIExpression::FragmentInfo Info = {I->getArg(1), I->getArg(0)}; 1222 return Info; 1223 } 1224 return None; 1225 } 1226 1227 void DIExpression::appendOffset(SmallVectorImpl<uint64_t> &Ops, 1228 int64_t Offset) { 1229 if (Offset > 0) { 1230 Ops.push_back(dwarf::DW_OP_plus_uconst); 1231 Ops.push_back(Offset); 1232 } else if (Offset < 0) { 1233 Ops.push_back(dwarf::DW_OP_constu); 1234 Ops.push_back(-Offset); 1235 Ops.push_back(dwarf::DW_OP_minus); 1236 } 1237 } 1238 1239 bool DIExpression::extractIfOffset(int64_t &Offset) const { 1240 if (getNumElements() == 0) { 1241 Offset = 0; 1242 return true; 1243 } 1244 1245 if (getNumElements() == 2 && Elements[0] == dwarf::DW_OP_plus_uconst) { 1246 Offset = Elements[1]; 1247 return true; 1248 } 1249 1250 if (getNumElements() == 3 && Elements[0] == dwarf::DW_OP_constu) { 1251 if (Elements[2] == dwarf::DW_OP_plus) { 1252 Offset = Elements[1]; 1253 return true; 1254 } 1255 if (Elements[2] == dwarf::DW_OP_minus) { 1256 Offset = -Elements[1]; 1257 return true; 1258 } 1259 } 1260 1261 return false; 1262 } 1263 1264 bool DIExpression::hasAllLocationOps(unsigned N) const { 1265 SmallDenseSet<uint64_t, 4> SeenOps; 1266 for (auto ExprOp : expr_ops()) 1267 if (ExprOp.getOp() == dwarf::DW_OP_LLVM_arg) 1268 SeenOps.insert(ExprOp.getArg(0)); 1269 for (uint64_t Idx = 0; Idx < N; ++Idx) 1270 if (!is_contained(SeenOps, Idx)) 1271 return false; 1272 return true; 1273 } 1274 1275 const DIExpression *DIExpression::extractAddressClass(const DIExpression *Expr, 1276 unsigned &AddrClass) { 1277 // FIXME: This seems fragile. Nothing that verifies that these elements 1278 // actually map to ops and not operands. 1279 const unsigned PatternSize = 4; 1280 if (Expr->Elements.size() >= PatternSize && 1281 Expr->Elements[PatternSize - 4] == dwarf::DW_OP_constu && 1282 Expr->Elements[PatternSize - 2] == dwarf::DW_OP_swap && 1283 Expr->Elements[PatternSize - 1] == dwarf::DW_OP_xderef) { 1284 AddrClass = Expr->Elements[PatternSize - 3]; 1285 1286 if (Expr->Elements.size() == PatternSize) 1287 return nullptr; 1288 return DIExpression::get(Expr->getContext(), 1289 makeArrayRef(&*Expr->Elements.begin(), 1290 Expr->Elements.size() - PatternSize)); 1291 } 1292 return Expr; 1293 } 1294 1295 DIExpression *DIExpression::prepend(const DIExpression *Expr, uint8_t Flags, 1296 int64_t Offset) { 1297 SmallVector<uint64_t, 8> Ops; 1298 if (Flags & DIExpression::DerefBefore) 1299 Ops.push_back(dwarf::DW_OP_deref); 1300 1301 appendOffset(Ops, Offset); 1302 if (Flags & DIExpression::DerefAfter) 1303 Ops.push_back(dwarf::DW_OP_deref); 1304 1305 bool StackValue = Flags & DIExpression::StackValue; 1306 bool EntryValue = Flags & DIExpression::EntryValue; 1307 1308 return prependOpcodes(Expr, Ops, StackValue, EntryValue); 1309 } 1310 1311 DIExpression *DIExpression::appendOpsToArg(const DIExpression *Expr, 1312 ArrayRef<uint64_t> Ops, 1313 unsigned ArgNo, bool StackValue) { 1314 assert(Expr && "Can't add ops to this expression"); 1315 1316 // Handle non-variadic intrinsics by prepending the opcodes. 1317 if (!any_of(Expr->expr_ops(), 1318 [](auto Op) { return Op.getOp() == dwarf::DW_OP_LLVM_arg; })) { 1319 assert(ArgNo == 0 && 1320 "Location Index must be 0 for a non-variadic expression."); 1321 SmallVector<uint64_t, 8> NewOps(Ops.begin(), Ops.end()); 1322 return DIExpression::prependOpcodes(Expr, NewOps, StackValue); 1323 } 1324 1325 SmallVector<uint64_t, 8> NewOps; 1326 for (auto Op : Expr->expr_ops()) { 1327 Op.appendToVector(NewOps); 1328 if (Op.getOp() == dwarf::DW_OP_LLVM_arg && Op.getArg(0) == ArgNo) 1329 NewOps.insert(NewOps.end(), Ops.begin(), Ops.end()); 1330 } 1331 1332 return DIExpression::get(Expr->getContext(), NewOps); 1333 } 1334 1335 DIExpression *DIExpression::replaceArg(const DIExpression *Expr, 1336 uint64_t OldArg, uint64_t NewArg) { 1337 assert(Expr && "Can't replace args in this expression"); 1338 1339 SmallVector<uint64_t, 8> NewOps; 1340 1341 for (auto Op : Expr->expr_ops()) { 1342 if (Op.getOp() != dwarf::DW_OP_LLVM_arg || Op.getArg(0) < OldArg) { 1343 Op.appendToVector(NewOps); 1344 continue; 1345 } 1346 NewOps.push_back(dwarf::DW_OP_LLVM_arg); 1347 uint64_t Arg = Op.getArg(0) == OldArg ? NewArg : Op.getArg(0); 1348 // OldArg has been deleted from the Op list, so decrement all indices 1349 // greater than it. 1350 if (Arg > OldArg) 1351 --Arg; 1352 NewOps.push_back(Arg); 1353 } 1354 return DIExpression::get(Expr->getContext(), NewOps); 1355 } 1356 1357 DIExpression *DIExpression::prependOpcodes(const DIExpression *Expr, 1358 SmallVectorImpl<uint64_t> &Ops, 1359 bool StackValue, 1360 bool EntryValue) { 1361 assert(Expr && "Can't prepend ops to this expression"); 1362 1363 if (EntryValue) { 1364 Ops.push_back(dwarf::DW_OP_LLVM_entry_value); 1365 // Use a block size of 1 for the target register operand. The 1366 // DWARF backend currently cannot emit entry values with a block 1367 // size > 1. 1368 Ops.push_back(1); 1369 } 1370 1371 // If there are no ops to prepend, do not even add the DW_OP_stack_value. 1372 if (Ops.empty()) 1373 StackValue = false; 1374 for (auto Op : Expr->expr_ops()) { 1375 // A DW_OP_stack_value comes at the end, but before a DW_OP_LLVM_fragment. 1376 if (StackValue) { 1377 if (Op.getOp() == dwarf::DW_OP_stack_value) 1378 StackValue = false; 1379 else if (Op.getOp() == dwarf::DW_OP_LLVM_fragment) { 1380 Ops.push_back(dwarf::DW_OP_stack_value); 1381 StackValue = false; 1382 } 1383 } 1384 Op.appendToVector(Ops); 1385 } 1386 if (StackValue) 1387 Ops.push_back(dwarf::DW_OP_stack_value); 1388 return DIExpression::get(Expr->getContext(), Ops); 1389 } 1390 1391 DIExpression *DIExpression::append(const DIExpression *Expr, 1392 ArrayRef<uint64_t> Ops) { 1393 assert(Expr && !Ops.empty() && "Can't append ops to this expression"); 1394 1395 // Copy Expr's current op list. 1396 SmallVector<uint64_t, 16> NewOps; 1397 for (auto Op : Expr->expr_ops()) { 1398 // Append new opcodes before DW_OP_{stack_value, LLVM_fragment}. 1399 if (Op.getOp() == dwarf::DW_OP_stack_value || 1400 Op.getOp() == dwarf::DW_OP_LLVM_fragment) { 1401 NewOps.append(Ops.begin(), Ops.end()); 1402 1403 // Ensure that the new opcodes are only appended once. 1404 Ops = None; 1405 } 1406 Op.appendToVector(NewOps); 1407 } 1408 1409 NewOps.append(Ops.begin(), Ops.end()); 1410 auto *result = DIExpression::get(Expr->getContext(), NewOps); 1411 assert(result->isValid() && "concatenated expression is not valid"); 1412 return result; 1413 } 1414 1415 DIExpression *DIExpression::appendToStack(const DIExpression *Expr, 1416 ArrayRef<uint64_t> Ops) { 1417 assert(Expr && !Ops.empty() && "Can't append ops to this expression"); 1418 assert(none_of(Ops, 1419 [](uint64_t Op) { 1420 return Op == dwarf::DW_OP_stack_value || 1421 Op == dwarf::DW_OP_LLVM_fragment; 1422 }) && 1423 "Can't append this op"); 1424 1425 // Append a DW_OP_deref after Expr's current op list if it's non-empty and 1426 // has no DW_OP_stack_value. 1427 // 1428 // Match .* DW_OP_stack_value (DW_OP_LLVM_fragment A B)?. 1429 Optional<FragmentInfo> FI = Expr->getFragmentInfo(); 1430 unsigned DropUntilStackValue = FI.hasValue() ? 3 : 0; 1431 ArrayRef<uint64_t> ExprOpsBeforeFragment = 1432 Expr->getElements().drop_back(DropUntilStackValue); 1433 bool NeedsDeref = (Expr->getNumElements() > DropUntilStackValue) && 1434 (ExprOpsBeforeFragment.back() != dwarf::DW_OP_stack_value); 1435 bool NeedsStackValue = NeedsDeref || ExprOpsBeforeFragment.empty(); 1436 1437 // Append a DW_OP_deref after Expr's current op list if needed, then append 1438 // the new ops, and finally ensure that a single DW_OP_stack_value is present. 1439 SmallVector<uint64_t, 16> NewOps; 1440 if (NeedsDeref) 1441 NewOps.push_back(dwarf::DW_OP_deref); 1442 NewOps.append(Ops.begin(), Ops.end()); 1443 if (NeedsStackValue) 1444 NewOps.push_back(dwarf::DW_OP_stack_value); 1445 return DIExpression::append(Expr, NewOps); 1446 } 1447 1448 Optional<DIExpression *> DIExpression::createFragmentExpression( 1449 const DIExpression *Expr, unsigned OffsetInBits, unsigned SizeInBits) { 1450 SmallVector<uint64_t, 8> Ops; 1451 // Copy over the expression, but leave off any trailing DW_OP_LLVM_fragment. 1452 if (Expr) { 1453 for (auto Op : Expr->expr_ops()) { 1454 switch (Op.getOp()) { 1455 default: break; 1456 case dwarf::DW_OP_shr: 1457 case dwarf::DW_OP_shra: 1458 case dwarf::DW_OP_shl: 1459 case dwarf::DW_OP_plus: 1460 case dwarf::DW_OP_plus_uconst: 1461 case dwarf::DW_OP_minus: 1462 // We can't safely split arithmetic or shift operations into multiple 1463 // fragments because we can't express carry-over between fragments. 1464 // 1465 // FIXME: We *could* preserve the lowest fragment of a constant offset 1466 // operation if the offset fits into SizeInBits. 1467 return None; 1468 case dwarf::DW_OP_LLVM_fragment: { 1469 // Make the new offset point into the existing fragment. 1470 uint64_t FragmentOffsetInBits = Op.getArg(0); 1471 uint64_t FragmentSizeInBits = Op.getArg(1); 1472 (void)FragmentSizeInBits; 1473 assert((OffsetInBits + SizeInBits <= FragmentSizeInBits) && 1474 "new fragment outside of original fragment"); 1475 OffsetInBits += FragmentOffsetInBits; 1476 continue; 1477 } 1478 } 1479 Op.appendToVector(Ops); 1480 } 1481 } 1482 assert(Expr && "Unknown DIExpression"); 1483 Ops.push_back(dwarf::DW_OP_LLVM_fragment); 1484 Ops.push_back(OffsetInBits); 1485 Ops.push_back(SizeInBits); 1486 return DIExpression::get(Expr->getContext(), Ops); 1487 } 1488 1489 std::pair<DIExpression *, const ConstantInt *> 1490 DIExpression::constantFold(const ConstantInt *CI) { 1491 // Copy the APInt so we can modify it. 1492 APInt NewInt = CI->getValue(); 1493 SmallVector<uint64_t, 8> Ops; 1494 1495 // Fold operators only at the beginning of the expression. 1496 bool First = true; 1497 bool Changed = false; 1498 for (auto Op : expr_ops()) { 1499 switch (Op.getOp()) { 1500 default: 1501 // We fold only the leading part of the expression; if we get to a part 1502 // that we're going to copy unchanged, and haven't done any folding, 1503 // then the entire expression is unchanged and we can return early. 1504 if (!Changed) 1505 return {this, CI}; 1506 First = false; 1507 break; 1508 case dwarf::DW_OP_LLVM_convert: 1509 if (!First) 1510 break; 1511 Changed = true; 1512 if (Op.getArg(1) == dwarf::DW_ATE_signed) 1513 NewInt = NewInt.sextOrTrunc(Op.getArg(0)); 1514 else { 1515 assert(Op.getArg(1) == dwarf::DW_ATE_unsigned && "Unexpected operand"); 1516 NewInt = NewInt.zextOrTrunc(Op.getArg(0)); 1517 } 1518 continue; 1519 } 1520 Op.appendToVector(Ops); 1521 } 1522 if (!Changed) 1523 return {this, CI}; 1524 return {DIExpression::get(getContext(), Ops), 1525 ConstantInt::get(getContext(), NewInt)}; 1526 } 1527 1528 uint64_t DIExpression::getNumLocationOperands() const { 1529 uint64_t Result = 0; 1530 for (auto ExprOp : expr_ops()) 1531 if (ExprOp.getOp() == dwarf::DW_OP_LLVM_arg) 1532 Result = std::max(Result, ExprOp.getArg(0) + 1); 1533 assert(hasAllLocationOps(Result) && 1534 "Expression is missing one or more location operands."); 1535 return Result; 1536 } 1537 1538 llvm::Optional<DIExpression::SignedOrUnsignedConstant> 1539 DIExpression::isConstant() const { 1540 1541 // Recognize signed and unsigned constants. 1542 // An signed constants can be represented as DW_OP_consts C DW_OP_stack_value 1543 // (DW_OP_LLVM_fragment of Len). 1544 // An unsigned constant can be represented as 1545 // DW_OP_constu C DW_OP_stack_value (DW_OP_LLVM_fragment of Len). 1546 1547 if ((getNumElements() != 2 && getNumElements() != 3 && 1548 getNumElements() != 6) || 1549 (getElement(0) != dwarf::DW_OP_consts && 1550 getElement(0) != dwarf::DW_OP_constu)) 1551 return None; 1552 1553 if (getNumElements() == 2 && getElement(0) == dwarf::DW_OP_consts) 1554 return SignedOrUnsignedConstant::SignedConstant; 1555 1556 if ((getNumElements() == 3 && getElement(2) != dwarf::DW_OP_stack_value) || 1557 (getNumElements() == 6 && (getElement(2) != dwarf::DW_OP_stack_value || 1558 getElement(3) != dwarf::DW_OP_LLVM_fragment))) 1559 return None; 1560 return getElement(0) == dwarf::DW_OP_constu 1561 ? SignedOrUnsignedConstant::UnsignedConstant 1562 : SignedOrUnsignedConstant::SignedConstant; 1563 } 1564 1565 DIExpression::ExtOps DIExpression::getExtOps(unsigned FromSize, unsigned ToSize, 1566 bool Signed) { 1567 dwarf::TypeKind TK = Signed ? dwarf::DW_ATE_signed : dwarf::DW_ATE_unsigned; 1568 DIExpression::ExtOps Ops{{dwarf::DW_OP_LLVM_convert, FromSize, TK, 1569 dwarf::DW_OP_LLVM_convert, ToSize, TK}}; 1570 return Ops; 1571 } 1572 1573 DIExpression *DIExpression::appendExt(const DIExpression *Expr, 1574 unsigned FromSize, unsigned ToSize, 1575 bool Signed) { 1576 return appendToStack(Expr, getExtOps(FromSize, ToSize, Signed)); 1577 } 1578 1579 DIGlobalVariableExpression * 1580 DIGlobalVariableExpression::getImpl(LLVMContext &Context, Metadata *Variable, 1581 Metadata *Expression, StorageType Storage, 1582 bool ShouldCreate) { 1583 DEFINE_GETIMPL_LOOKUP(DIGlobalVariableExpression, (Variable, Expression)); 1584 Metadata *Ops[] = {Variable, Expression}; 1585 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DIGlobalVariableExpression, Ops); 1586 } 1587 1588 DIObjCProperty *DIObjCProperty::getImpl( 1589 LLVMContext &Context, MDString *Name, Metadata *File, unsigned Line, 1590 MDString *GetterName, MDString *SetterName, unsigned Attributes, 1591 Metadata *Type, StorageType Storage, bool ShouldCreate) { 1592 assert(isCanonical(Name) && "Expected canonical MDString"); 1593 assert(isCanonical(GetterName) && "Expected canonical MDString"); 1594 assert(isCanonical(SetterName) && "Expected canonical MDString"); 1595 DEFINE_GETIMPL_LOOKUP(DIObjCProperty, (Name, File, Line, GetterName, 1596 SetterName, Attributes, Type)); 1597 Metadata *Ops[] = {Name, File, GetterName, SetterName, Type}; 1598 DEFINE_GETIMPL_STORE(DIObjCProperty, (Line, Attributes), Ops); 1599 } 1600 1601 DIImportedEntity *DIImportedEntity::getImpl(LLVMContext &Context, unsigned Tag, 1602 Metadata *Scope, Metadata *Entity, 1603 Metadata *File, unsigned Line, 1604 MDString *Name, StorageType Storage, 1605 bool ShouldCreate) { 1606 assert(isCanonical(Name) && "Expected canonical MDString"); 1607 DEFINE_GETIMPL_LOOKUP(DIImportedEntity, 1608 (Tag, Scope, Entity, File, Line, Name)); 1609 Metadata *Ops[] = {Scope, Entity, Name, File}; 1610 DEFINE_GETIMPL_STORE(DIImportedEntity, (Tag, Line), Ops); 1611 } 1612 1613 DIMacro *DIMacro::getImpl(LLVMContext &Context, unsigned MIType, 1614 unsigned Line, MDString *Name, MDString *Value, 1615 StorageType Storage, bool ShouldCreate) { 1616 assert(isCanonical(Name) && "Expected canonical MDString"); 1617 DEFINE_GETIMPL_LOOKUP(DIMacro, (MIType, Line, Name, Value)); 1618 Metadata *Ops[] = { Name, Value }; 1619 DEFINE_GETIMPL_STORE(DIMacro, (MIType, Line), Ops); 1620 } 1621 1622 DIMacroFile *DIMacroFile::getImpl(LLVMContext &Context, unsigned MIType, 1623 unsigned Line, Metadata *File, 1624 Metadata *Elements, StorageType Storage, 1625 bool ShouldCreate) { 1626 DEFINE_GETIMPL_LOOKUP(DIMacroFile, 1627 (MIType, Line, File, Elements)); 1628 Metadata *Ops[] = { File, Elements }; 1629 DEFINE_GETIMPL_STORE(DIMacroFile, (MIType, Line), Ops); 1630 } 1631 1632 DIArgList *DIArgList::getImpl(LLVMContext &Context, 1633 ArrayRef<ValueAsMetadata *> Args, 1634 StorageType Storage, bool ShouldCreate) { 1635 DEFINE_GETIMPL_LOOKUP(DIArgList, (Args)); 1636 DEFINE_GETIMPL_STORE_NO_OPS(DIArgList, (Args)); 1637 } 1638 1639 void DIArgList::handleChangedOperand(void *Ref, Metadata *New) { 1640 ValueAsMetadata **OldVMPtr = static_cast<ValueAsMetadata **>(Ref); 1641 assert((!New || isa<ValueAsMetadata>(New)) && 1642 "DIArgList must be passed a ValueAsMetadata"); 1643 untrack(); 1644 ValueAsMetadata *NewVM = cast_or_null<ValueAsMetadata>(New); 1645 for (ValueAsMetadata *&VM : Args) { 1646 if (&VM == OldVMPtr) { 1647 if (NewVM) 1648 VM = NewVM; 1649 else 1650 VM = ValueAsMetadata::get(UndefValue::get(VM->getValue()->getType())); 1651 } 1652 } 1653 track(); 1654 } 1655 void DIArgList::track() { 1656 for (ValueAsMetadata *&VAM : Args) 1657 if (VAM) 1658 MetadataTracking::track(&VAM, *VAM, *this); 1659 } 1660 void DIArgList::untrack() { 1661 for (ValueAsMetadata *&VAM : Args) 1662 if (VAM) 1663 MetadataTracking::untrack(&VAM, *VAM); 1664 } 1665 void DIArgList::dropAllReferences() { 1666 untrack(); 1667 Args.clear(); 1668 MDNode::dropAllReferences(); 1669 } 1670