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/SmallPtrSet.h" 17 #include "llvm/ADT/StringSwitch.h" 18 #include "llvm/BinaryFormat/Dwarf.h" 19 #include "llvm/IR/DebugProgramInstruction.h" 20 #include "llvm/IR/Function.h" 21 #include "llvm/IR/IntrinsicInst.h" 22 #include "llvm/IR/Type.h" 23 #include "llvm/IR/Value.h" 24 25 #include <numeric> 26 #include <optional> 27 28 using namespace llvm; 29 30 namespace llvm { 31 // Use FS-AFDO discriminator. 32 cl::opt<bool> EnableFSDiscriminator( 33 "enable-fs-discriminator", cl::Hidden, 34 cl::desc("Enable adding flow sensitive discriminators")); 35 } // namespace llvm 36 37 const DIExpression::FragmentInfo DebugVariable::DefaultFragment = { 38 std::numeric_limits<uint64_t>::max(), std::numeric_limits<uint64_t>::min()}; 39 40 DebugVariable::DebugVariable(const DbgVariableIntrinsic *DII) 41 : Variable(DII->getVariable()), 42 Fragment(DII->getExpression()->getFragmentInfo()), 43 InlinedAt(DII->getDebugLoc().getInlinedAt()) {} 44 45 DebugVariable::DebugVariable(const DPValue *DPV) 46 : Variable(DPV->getVariable()), 47 Fragment(DPV->getExpression()->getFragmentInfo()), 48 InlinedAt(DPV->getDebugLoc().getInlinedAt()) {} 49 50 DebugVariableAggregate::DebugVariableAggregate(const DbgVariableIntrinsic *DVI) 51 : DebugVariable(DVI->getVariable(), std::nullopt, 52 DVI->getDebugLoc()->getInlinedAt()) {} 53 54 DILocation::DILocation(LLVMContext &C, StorageType Storage, unsigned Line, 55 unsigned Column, ArrayRef<Metadata *> MDs, 56 bool ImplicitCode) 57 : MDNode(C, DILocationKind, Storage, MDs) { 58 assert((MDs.size() == 1 || MDs.size() == 2) && 59 "Expected a scope and optional inlined-at"); 60 61 // Set line and column. 62 assert(Column < (1u << 16) && "Expected 16-bit column"); 63 64 SubclassData32 = Line; 65 SubclassData16 = Column; 66 67 setImplicitCode(ImplicitCode); 68 } 69 70 static void adjustColumn(unsigned &Column) { 71 // Set to unknown on overflow. We only have 16 bits to play with here. 72 if (Column >= (1u << 16)) 73 Column = 0; 74 } 75 76 DILocation *DILocation::getImpl(LLVMContext &Context, unsigned Line, 77 unsigned Column, Metadata *Scope, 78 Metadata *InlinedAt, bool ImplicitCode, 79 StorageType Storage, bool ShouldCreate) { 80 // Fixup column. 81 adjustColumn(Column); 82 83 if (Storage == Uniqued) { 84 if (auto *N = getUniqued(Context.pImpl->DILocations, 85 DILocationInfo::KeyTy(Line, Column, Scope, 86 InlinedAt, ImplicitCode))) 87 return N; 88 if (!ShouldCreate) 89 return nullptr; 90 } else { 91 assert(ShouldCreate && "Expected non-uniqued nodes to always be created"); 92 } 93 94 SmallVector<Metadata *, 2> Ops; 95 Ops.push_back(Scope); 96 if (InlinedAt) 97 Ops.push_back(InlinedAt); 98 return storeImpl(new (Ops.size(), Storage) DILocation( 99 Context, Storage, Line, Column, Ops, ImplicitCode), 100 Storage, Context.pImpl->DILocations); 101 } 102 103 DILocation *DILocation::getMergedLocations(ArrayRef<DILocation *> Locs) { 104 if (Locs.empty()) 105 return nullptr; 106 if (Locs.size() == 1) 107 return Locs[0]; 108 auto *Merged = Locs[0]; 109 for (DILocation *L : llvm::drop_begin(Locs)) { 110 Merged = getMergedLocation(Merged, L); 111 if (Merged == nullptr) 112 break; 113 } 114 return Merged; 115 } 116 117 DILocation *DILocation::getMergedLocation(DILocation *LocA, DILocation *LocB) { 118 if (!LocA || !LocB) 119 return nullptr; 120 121 if (LocA == LocB) 122 return LocA; 123 124 LLVMContext &C = LocA->getContext(); 125 126 using LocVec = SmallVector<const DILocation *>; 127 LocVec ALocs; 128 LocVec BLocs; 129 SmallDenseMap<std::pair<const DISubprogram *, const DILocation *>, unsigned, 130 4> 131 ALookup; 132 133 // Walk through LocA and its inlined-at locations, populate them in ALocs and 134 // save the index for the subprogram and inlined-at pair, which we use to find 135 // a matching starting location in LocB's chain. 136 for (auto [L, I] = std::make_pair(LocA, 0U); L; L = L->getInlinedAt(), I++) { 137 ALocs.push_back(L); 138 auto Res = ALookup.try_emplace( 139 {L->getScope()->getSubprogram(), L->getInlinedAt()}, I); 140 assert(Res.second && "Multiple <SP, InlinedAt> pairs in a location chain?"); 141 (void)Res; 142 } 143 144 LocVec::reverse_iterator ARIt = ALocs.rend(); 145 LocVec::reverse_iterator BRIt = BLocs.rend(); 146 147 // Populate BLocs and look for a matching starting location, the first 148 // location with the same subprogram and inlined-at location as in LocA's 149 // chain. Since the two locations have the same inlined-at location we do 150 // not need to look at those parts of the chains. 151 for (auto [L, I] = std::make_pair(LocB, 0U); L; L = L->getInlinedAt(), I++) { 152 BLocs.push_back(L); 153 154 if (ARIt != ALocs.rend()) 155 // We have already found a matching starting location. 156 continue; 157 158 auto IT = ALookup.find({L->getScope()->getSubprogram(), L->getInlinedAt()}); 159 if (IT == ALookup.end()) 160 continue; 161 162 // The + 1 is to account for the &*rev_it = &(it - 1) relationship. 163 ARIt = LocVec::reverse_iterator(ALocs.begin() + IT->second + 1); 164 BRIt = LocVec::reverse_iterator(BLocs.begin() + I + 1); 165 166 // If we have found a matching starting location we do not need to add more 167 // locations to BLocs, since we will only look at location pairs preceding 168 // the matching starting location, and adding more elements to BLocs could 169 // invalidate the iterator that we initialized here. 170 break; 171 } 172 173 // Merge the two locations if possible, using the supplied 174 // inlined-at location for the created location. 175 auto MergeLocPair = [&C](const DILocation *L1, const DILocation *L2, 176 DILocation *InlinedAt) -> DILocation * { 177 if (L1 == L2) 178 return DILocation::get(C, L1->getLine(), L1->getColumn(), L1->getScope(), 179 InlinedAt); 180 181 // If the locations originate from different subprograms we can't produce 182 // a common location. 183 if (L1->getScope()->getSubprogram() != L2->getScope()->getSubprogram()) 184 return nullptr; 185 186 // Return the nearest common scope inside a subprogram. 187 auto GetNearestCommonScope = [](DIScope *S1, DIScope *S2) -> DIScope * { 188 SmallPtrSet<DIScope *, 8> Scopes; 189 for (; S1; S1 = S1->getScope()) { 190 Scopes.insert(S1); 191 if (isa<DISubprogram>(S1)) 192 break; 193 } 194 195 for (; S2; S2 = S2->getScope()) { 196 if (Scopes.count(S2)) 197 return S2; 198 if (isa<DISubprogram>(S2)) 199 break; 200 } 201 202 return nullptr; 203 }; 204 205 auto Scope = GetNearestCommonScope(L1->getScope(), L2->getScope()); 206 assert(Scope && "No common scope in the same subprogram?"); 207 208 bool SameLine = L1->getLine() == L2->getLine(); 209 bool SameCol = L1->getColumn() == L2->getColumn(); 210 unsigned Line = SameLine ? L1->getLine() : 0; 211 unsigned Col = SameLine && SameCol ? L1->getColumn() : 0; 212 213 return DILocation::get(C, Line, Col, Scope, InlinedAt); 214 }; 215 216 DILocation *Result = ARIt != ALocs.rend() ? (*ARIt)->getInlinedAt() : nullptr; 217 218 // If we have found a common starting location, walk up the inlined-at chains 219 // and try to produce common locations. 220 for (; ARIt != ALocs.rend() && BRIt != BLocs.rend(); ++ARIt, ++BRIt) { 221 DILocation *Tmp = MergeLocPair(*ARIt, *BRIt, Result); 222 223 if (!Tmp) 224 // We have walked up to a point in the chains where the two locations 225 // are irreconsilable. At this point Result contains the nearest common 226 // location in the inlined-at chains of LocA and LocB, so we break here. 227 break; 228 229 Result = Tmp; 230 } 231 232 if (Result) 233 return Result; 234 235 // We ended up with LocA and LocB as irreconsilable locations. Produce a 236 // location at 0:0 with one of the locations' scope. The function has 237 // historically picked A's scope, and a nullptr inlined-at location, so that 238 // behavior is mimicked here but I am not sure if this is always the correct 239 // way to handle this. 240 return DILocation::get(C, 0, 0, LocA->getScope(), nullptr); 241 } 242 243 std::optional<unsigned> 244 DILocation::encodeDiscriminator(unsigned BD, unsigned DF, unsigned CI) { 245 std::array<unsigned, 3> Components = {BD, DF, CI}; 246 uint64_t RemainingWork = 0U; 247 // We use RemainingWork to figure out if we have no remaining components to 248 // encode. For example: if BD != 0 but DF == 0 && CI == 0, we don't need to 249 // encode anything for the latter 2. 250 // Since any of the input components is at most 32 bits, their sum will be 251 // less than 34 bits, and thus RemainingWork won't overflow. 252 RemainingWork = 253 std::accumulate(Components.begin(), Components.end(), RemainingWork); 254 255 int I = 0; 256 unsigned Ret = 0; 257 unsigned NextBitInsertionIndex = 0; 258 while (RemainingWork > 0) { 259 unsigned C = Components[I++]; 260 RemainingWork -= C; 261 unsigned EC = encodeComponent(C); 262 Ret |= (EC << NextBitInsertionIndex); 263 NextBitInsertionIndex += encodingBits(C); 264 } 265 266 // Encoding may be unsuccessful because of overflow. We determine success by 267 // checking equivalence of components before & after encoding. Alternatively, 268 // we could determine Success during encoding, but the current alternative is 269 // simpler. 270 unsigned TBD, TDF, TCI = 0; 271 decodeDiscriminator(Ret, TBD, TDF, TCI); 272 if (TBD == BD && TDF == DF && TCI == CI) 273 return Ret; 274 return std::nullopt; 275 } 276 277 void DILocation::decodeDiscriminator(unsigned D, unsigned &BD, unsigned &DF, 278 unsigned &CI) { 279 BD = getUnsignedFromPrefixEncoding(D); 280 DF = getUnsignedFromPrefixEncoding(getNextComponentInDiscriminator(D)); 281 CI = getUnsignedFromPrefixEncoding( 282 getNextComponentInDiscriminator(getNextComponentInDiscriminator(D))); 283 } 284 dwarf::Tag DINode::getTag() const { return (dwarf::Tag)SubclassData16; } 285 286 DINode::DIFlags DINode::getFlag(StringRef Flag) { 287 return StringSwitch<DIFlags>(Flag) 288 #define HANDLE_DI_FLAG(ID, NAME) .Case("DIFlag" #NAME, Flag##NAME) 289 #include "llvm/IR/DebugInfoFlags.def" 290 .Default(DINode::FlagZero); 291 } 292 293 StringRef DINode::getFlagString(DIFlags Flag) { 294 switch (Flag) { 295 #define HANDLE_DI_FLAG(ID, NAME) \ 296 case Flag##NAME: \ 297 return "DIFlag" #NAME; 298 #include "llvm/IR/DebugInfoFlags.def" 299 } 300 return ""; 301 } 302 303 DINode::DIFlags DINode::splitFlags(DIFlags Flags, 304 SmallVectorImpl<DIFlags> &SplitFlags) { 305 // Flags that are packed together need to be specially handled, so 306 // that, for example, we emit "DIFlagPublic" and not 307 // "DIFlagPrivate | DIFlagProtected". 308 if (DIFlags A = Flags & FlagAccessibility) { 309 if (A == FlagPrivate) 310 SplitFlags.push_back(FlagPrivate); 311 else if (A == FlagProtected) 312 SplitFlags.push_back(FlagProtected); 313 else 314 SplitFlags.push_back(FlagPublic); 315 Flags &= ~A; 316 } 317 if (DIFlags R = Flags & FlagPtrToMemberRep) { 318 if (R == FlagSingleInheritance) 319 SplitFlags.push_back(FlagSingleInheritance); 320 else if (R == FlagMultipleInheritance) 321 SplitFlags.push_back(FlagMultipleInheritance); 322 else 323 SplitFlags.push_back(FlagVirtualInheritance); 324 Flags &= ~R; 325 } 326 if ((Flags & FlagIndirectVirtualBase) == FlagIndirectVirtualBase) { 327 Flags &= ~FlagIndirectVirtualBase; 328 SplitFlags.push_back(FlagIndirectVirtualBase); 329 } 330 331 #define HANDLE_DI_FLAG(ID, NAME) \ 332 if (DIFlags Bit = Flags & Flag##NAME) { \ 333 SplitFlags.push_back(Bit); \ 334 Flags &= ~Bit; \ 335 } 336 #include "llvm/IR/DebugInfoFlags.def" 337 return Flags; 338 } 339 340 DIScope *DIScope::getScope() const { 341 if (auto *T = dyn_cast<DIType>(this)) 342 return T->getScope(); 343 344 if (auto *SP = dyn_cast<DISubprogram>(this)) 345 return SP->getScope(); 346 347 if (auto *LB = dyn_cast<DILexicalBlockBase>(this)) 348 return LB->getScope(); 349 350 if (auto *NS = dyn_cast<DINamespace>(this)) 351 return NS->getScope(); 352 353 if (auto *CB = dyn_cast<DICommonBlock>(this)) 354 return CB->getScope(); 355 356 if (auto *M = dyn_cast<DIModule>(this)) 357 return M->getScope(); 358 359 assert((isa<DIFile>(this) || isa<DICompileUnit>(this)) && 360 "Unhandled type of scope."); 361 return nullptr; 362 } 363 364 StringRef DIScope::getName() const { 365 if (auto *T = dyn_cast<DIType>(this)) 366 return T->getName(); 367 if (auto *SP = dyn_cast<DISubprogram>(this)) 368 return SP->getName(); 369 if (auto *NS = dyn_cast<DINamespace>(this)) 370 return NS->getName(); 371 if (auto *CB = dyn_cast<DICommonBlock>(this)) 372 return CB->getName(); 373 if (auto *M = dyn_cast<DIModule>(this)) 374 return M->getName(); 375 assert((isa<DILexicalBlockBase>(this) || isa<DIFile>(this) || 376 isa<DICompileUnit>(this)) && 377 "Unhandled type of scope."); 378 return ""; 379 } 380 381 #ifndef NDEBUG 382 static bool isCanonical(const MDString *S) { 383 return !S || !S->getString().empty(); 384 } 385 #endif 386 387 dwarf::Tag GenericDINode::getTag() const { return (dwarf::Tag)SubclassData16; } 388 GenericDINode *GenericDINode::getImpl(LLVMContext &Context, unsigned Tag, 389 MDString *Header, 390 ArrayRef<Metadata *> DwarfOps, 391 StorageType Storage, bool ShouldCreate) { 392 unsigned Hash = 0; 393 if (Storage == Uniqued) { 394 GenericDINodeInfo::KeyTy Key(Tag, Header, DwarfOps); 395 if (auto *N = getUniqued(Context.pImpl->GenericDINodes, Key)) 396 return N; 397 if (!ShouldCreate) 398 return nullptr; 399 Hash = Key.getHash(); 400 } else { 401 assert(ShouldCreate && "Expected non-uniqued nodes to always be created"); 402 } 403 404 // Use a nullptr for empty headers. 405 assert(isCanonical(Header) && "Expected canonical MDString"); 406 Metadata *PreOps[] = {Header}; 407 return storeImpl(new (DwarfOps.size() + 1, Storage) GenericDINode( 408 Context, Storage, Hash, Tag, PreOps, DwarfOps), 409 Storage, Context.pImpl->GenericDINodes); 410 } 411 412 void GenericDINode::recalculateHash() { 413 setHash(GenericDINodeInfo::KeyTy::calculateHash(this)); 414 } 415 416 #define UNWRAP_ARGS_IMPL(...) __VA_ARGS__ 417 #define UNWRAP_ARGS(ARGS) UNWRAP_ARGS_IMPL ARGS 418 #define DEFINE_GETIMPL_LOOKUP(CLASS, ARGS) \ 419 do { \ 420 if (Storage == Uniqued) { \ 421 if (auto *N = getUniqued(Context.pImpl->CLASS##s, \ 422 CLASS##Info::KeyTy(UNWRAP_ARGS(ARGS)))) \ 423 return N; \ 424 if (!ShouldCreate) \ 425 return nullptr; \ 426 } else { \ 427 assert(ShouldCreate && \ 428 "Expected non-uniqued nodes to always be created"); \ 429 } \ 430 } while (false) 431 #define DEFINE_GETIMPL_STORE(CLASS, ARGS, OPS) \ 432 return storeImpl(new (std::size(OPS), Storage) \ 433 CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS), \ 434 Storage, Context.pImpl->CLASS##s) 435 #define DEFINE_GETIMPL_STORE_NO_OPS(CLASS, ARGS) \ 436 return storeImpl(new (0u, Storage) \ 437 CLASS(Context, Storage, UNWRAP_ARGS(ARGS)), \ 438 Storage, Context.pImpl->CLASS##s) 439 #define DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(CLASS, OPS) \ 440 return storeImpl(new (std::size(OPS), Storage) CLASS(Context, Storage, OPS), \ 441 Storage, Context.pImpl->CLASS##s) 442 #define DEFINE_GETIMPL_STORE_N(CLASS, ARGS, OPS, NUM_OPS) \ 443 return storeImpl(new (NUM_OPS, Storage) \ 444 CLASS(Context, Storage, UNWRAP_ARGS(ARGS), OPS), \ 445 Storage, Context.pImpl->CLASS##s) 446 447 DISubrange::DISubrange(LLVMContext &C, StorageType Storage, 448 ArrayRef<Metadata *> Ops) 449 : DINode(C, DISubrangeKind, Storage, dwarf::DW_TAG_subrange_type, Ops) {} 450 DISubrange *DISubrange::getImpl(LLVMContext &Context, int64_t Count, int64_t Lo, 451 StorageType Storage, bool ShouldCreate) { 452 auto *CountNode = ConstantAsMetadata::get( 453 ConstantInt::getSigned(Type::getInt64Ty(Context), Count)); 454 auto *LB = ConstantAsMetadata::get( 455 ConstantInt::getSigned(Type::getInt64Ty(Context), Lo)); 456 return getImpl(Context, CountNode, LB, nullptr, nullptr, Storage, 457 ShouldCreate); 458 } 459 460 DISubrange *DISubrange::getImpl(LLVMContext &Context, Metadata *CountNode, 461 int64_t Lo, StorageType Storage, 462 bool ShouldCreate) { 463 auto *LB = ConstantAsMetadata::get( 464 ConstantInt::getSigned(Type::getInt64Ty(Context), Lo)); 465 return getImpl(Context, CountNode, LB, nullptr, nullptr, Storage, 466 ShouldCreate); 467 } 468 469 DISubrange *DISubrange::getImpl(LLVMContext &Context, Metadata *CountNode, 470 Metadata *LB, Metadata *UB, Metadata *Stride, 471 StorageType Storage, bool ShouldCreate) { 472 DEFINE_GETIMPL_LOOKUP(DISubrange, (CountNode, LB, UB, Stride)); 473 Metadata *Ops[] = {CountNode, LB, UB, Stride}; 474 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DISubrange, Ops); 475 } 476 477 DISubrange::BoundType DISubrange::getCount() const { 478 Metadata *CB = getRawCountNode(); 479 if (!CB) 480 return BoundType(); 481 482 assert((isa<ConstantAsMetadata>(CB) || isa<DIVariable>(CB) || 483 isa<DIExpression>(CB)) && 484 "Count must be signed constant or DIVariable or DIExpression"); 485 486 if (auto *MD = dyn_cast<ConstantAsMetadata>(CB)) 487 return BoundType(cast<ConstantInt>(MD->getValue())); 488 489 if (auto *MD = dyn_cast<DIVariable>(CB)) 490 return BoundType(MD); 491 492 if (auto *MD = dyn_cast<DIExpression>(CB)) 493 return BoundType(MD); 494 495 return BoundType(); 496 } 497 498 DISubrange::BoundType DISubrange::getLowerBound() const { 499 Metadata *LB = getRawLowerBound(); 500 if (!LB) 501 return BoundType(); 502 503 assert((isa<ConstantAsMetadata>(LB) || isa<DIVariable>(LB) || 504 isa<DIExpression>(LB)) && 505 "LowerBound must be signed constant or DIVariable or DIExpression"); 506 507 if (auto *MD = dyn_cast<ConstantAsMetadata>(LB)) 508 return BoundType(cast<ConstantInt>(MD->getValue())); 509 510 if (auto *MD = dyn_cast<DIVariable>(LB)) 511 return BoundType(MD); 512 513 if (auto *MD = dyn_cast<DIExpression>(LB)) 514 return BoundType(MD); 515 516 return BoundType(); 517 } 518 519 DISubrange::BoundType DISubrange::getUpperBound() const { 520 Metadata *UB = getRawUpperBound(); 521 if (!UB) 522 return BoundType(); 523 524 assert((isa<ConstantAsMetadata>(UB) || isa<DIVariable>(UB) || 525 isa<DIExpression>(UB)) && 526 "UpperBound must be signed constant or DIVariable or DIExpression"); 527 528 if (auto *MD = dyn_cast<ConstantAsMetadata>(UB)) 529 return BoundType(cast<ConstantInt>(MD->getValue())); 530 531 if (auto *MD = dyn_cast<DIVariable>(UB)) 532 return BoundType(MD); 533 534 if (auto *MD = dyn_cast<DIExpression>(UB)) 535 return BoundType(MD); 536 537 return BoundType(); 538 } 539 540 DISubrange::BoundType DISubrange::getStride() const { 541 Metadata *ST = getRawStride(); 542 if (!ST) 543 return BoundType(); 544 545 assert((isa<ConstantAsMetadata>(ST) || isa<DIVariable>(ST) || 546 isa<DIExpression>(ST)) && 547 "Stride must be signed constant or DIVariable or DIExpression"); 548 549 if (auto *MD = dyn_cast<ConstantAsMetadata>(ST)) 550 return BoundType(cast<ConstantInt>(MD->getValue())); 551 552 if (auto *MD = dyn_cast<DIVariable>(ST)) 553 return BoundType(MD); 554 555 if (auto *MD = dyn_cast<DIExpression>(ST)) 556 return BoundType(MD); 557 558 return BoundType(); 559 } 560 DIGenericSubrange::DIGenericSubrange(LLVMContext &C, StorageType Storage, 561 ArrayRef<Metadata *> Ops) 562 : DINode(C, DIGenericSubrangeKind, Storage, dwarf::DW_TAG_generic_subrange, 563 Ops) {} 564 565 DIGenericSubrange *DIGenericSubrange::getImpl(LLVMContext &Context, 566 Metadata *CountNode, Metadata *LB, 567 Metadata *UB, Metadata *Stride, 568 StorageType Storage, 569 bool ShouldCreate) { 570 DEFINE_GETIMPL_LOOKUP(DIGenericSubrange, (CountNode, LB, UB, Stride)); 571 Metadata *Ops[] = {CountNode, LB, UB, Stride}; 572 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DIGenericSubrange, Ops); 573 } 574 575 DIGenericSubrange::BoundType DIGenericSubrange::getCount() const { 576 Metadata *CB = getRawCountNode(); 577 if (!CB) 578 return BoundType(); 579 580 assert((isa<DIVariable>(CB) || isa<DIExpression>(CB)) && 581 "Count must be signed constant or DIVariable or DIExpression"); 582 583 if (auto *MD = dyn_cast<DIVariable>(CB)) 584 return BoundType(MD); 585 586 if (auto *MD = dyn_cast<DIExpression>(CB)) 587 return BoundType(MD); 588 589 return BoundType(); 590 } 591 592 DIGenericSubrange::BoundType DIGenericSubrange::getLowerBound() const { 593 Metadata *LB = getRawLowerBound(); 594 if (!LB) 595 return BoundType(); 596 597 assert((isa<DIVariable>(LB) || isa<DIExpression>(LB)) && 598 "LowerBound must be signed constant or DIVariable or DIExpression"); 599 600 if (auto *MD = dyn_cast<DIVariable>(LB)) 601 return BoundType(MD); 602 603 if (auto *MD = dyn_cast<DIExpression>(LB)) 604 return BoundType(MD); 605 606 return BoundType(); 607 } 608 609 DIGenericSubrange::BoundType DIGenericSubrange::getUpperBound() const { 610 Metadata *UB = getRawUpperBound(); 611 if (!UB) 612 return BoundType(); 613 614 assert((isa<DIVariable>(UB) || isa<DIExpression>(UB)) && 615 "UpperBound must be signed constant or DIVariable or DIExpression"); 616 617 if (auto *MD = dyn_cast<DIVariable>(UB)) 618 return BoundType(MD); 619 620 if (auto *MD = dyn_cast<DIExpression>(UB)) 621 return BoundType(MD); 622 623 return BoundType(); 624 } 625 626 DIGenericSubrange::BoundType DIGenericSubrange::getStride() const { 627 Metadata *ST = getRawStride(); 628 if (!ST) 629 return BoundType(); 630 631 assert((isa<DIVariable>(ST) || isa<DIExpression>(ST)) && 632 "Stride must be signed constant or DIVariable or DIExpression"); 633 634 if (auto *MD = dyn_cast<DIVariable>(ST)) 635 return BoundType(MD); 636 637 if (auto *MD = dyn_cast<DIExpression>(ST)) 638 return BoundType(MD); 639 640 return BoundType(); 641 } 642 643 DIEnumerator::DIEnumerator(LLVMContext &C, StorageType Storage, 644 const APInt &Value, bool IsUnsigned, 645 ArrayRef<Metadata *> Ops) 646 : DINode(C, DIEnumeratorKind, Storage, dwarf::DW_TAG_enumerator, Ops), 647 Value(Value) { 648 SubclassData32 = IsUnsigned; 649 } 650 DIEnumerator *DIEnumerator::getImpl(LLVMContext &Context, const APInt &Value, 651 bool IsUnsigned, MDString *Name, 652 StorageType Storage, bool ShouldCreate) { 653 assert(isCanonical(Name) && "Expected canonical MDString"); 654 DEFINE_GETIMPL_LOOKUP(DIEnumerator, (Value, IsUnsigned, Name)); 655 Metadata *Ops[] = {Name}; 656 DEFINE_GETIMPL_STORE(DIEnumerator, (Value, IsUnsigned), Ops); 657 } 658 659 DIBasicType *DIBasicType::getImpl(LLVMContext &Context, unsigned Tag, 660 MDString *Name, uint64_t SizeInBits, 661 uint32_t AlignInBits, unsigned Encoding, 662 DIFlags Flags, StorageType Storage, 663 bool ShouldCreate) { 664 assert(isCanonical(Name) && "Expected canonical MDString"); 665 DEFINE_GETIMPL_LOOKUP(DIBasicType, 666 (Tag, Name, SizeInBits, AlignInBits, Encoding, Flags)); 667 Metadata *Ops[] = {nullptr, nullptr, Name}; 668 DEFINE_GETIMPL_STORE(DIBasicType, 669 (Tag, SizeInBits, AlignInBits, Encoding, Flags), Ops); 670 } 671 672 std::optional<DIBasicType::Signedness> DIBasicType::getSignedness() const { 673 switch (getEncoding()) { 674 case dwarf::DW_ATE_signed: 675 case dwarf::DW_ATE_signed_char: 676 return Signedness::Signed; 677 case dwarf::DW_ATE_unsigned: 678 case dwarf::DW_ATE_unsigned_char: 679 return Signedness::Unsigned; 680 default: 681 return std::nullopt; 682 } 683 } 684 685 DIStringType *DIStringType::getImpl(LLVMContext &Context, unsigned Tag, 686 MDString *Name, Metadata *StringLength, 687 Metadata *StringLengthExp, 688 Metadata *StringLocationExp, 689 uint64_t SizeInBits, uint32_t AlignInBits, 690 unsigned Encoding, StorageType Storage, 691 bool ShouldCreate) { 692 assert(isCanonical(Name) && "Expected canonical MDString"); 693 DEFINE_GETIMPL_LOOKUP(DIStringType, 694 (Tag, Name, StringLength, StringLengthExp, 695 StringLocationExp, SizeInBits, AlignInBits, Encoding)); 696 Metadata *Ops[] = {nullptr, nullptr, Name, 697 StringLength, StringLengthExp, StringLocationExp}; 698 DEFINE_GETIMPL_STORE(DIStringType, (Tag, SizeInBits, AlignInBits, Encoding), 699 Ops); 700 } 701 DIType *DIDerivedType::getClassType() const { 702 assert(getTag() == dwarf::DW_TAG_ptr_to_member_type); 703 return cast_or_null<DIType>(getExtraData()); 704 } 705 uint32_t DIDerivedType::getVBPtrOffset() const { 706 assert(getTag() == dwarf::DW_TAG_inheritance); 707 if (auto *CM = cast_or_null<ConstantAsMetadata>(getExtraData())) 708 if (auto *CI = dyn_cast_or_null<ConstantInt>(CM->getValue())) 709 return static_cast<uint32_t>(CI->getZExtValue()); 710 return 0; 711 } 712 Constant *DIDerivedType::getStorageOffsetInBits() const { 713 assert(getTag() == dwarf::DW_TAG_member && isBitField()); 714 if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData())) 715 return C->getValue(); 716 return nullptr; 717 } 718 719 Constant *DIDerivedType::getConstant() const { 720 assert((getTag() == dwarf::DW_TAG_member || 721 getTag() == dwarf::DW_TAG_variable) && 722 isStaticMember()); 723 if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData())) 724 return C->getValue(); 725 return nullptr; 726 } 727 Constant *DIDerivedType::getDiscriminantValue() const { 728 assert(getTag() == dwarf::DW_TAG_member && !isStaticMember()); 729 if (auto *C = cast_or_null<ConstantAsMetadata>(getExtraData())) 730 return C->getValue(); 731 return nullptr; 732 } 733 734 DIDerivedType * 735 DIDerivedType::getImpl(LLVMContext &Context, unsigned Tag, MDString *Name, 736 Metadata *File, unsigned Line, Metadata *Scope, 737 Metadata *BaseType, uint64_t SizeInBits, 738 uint32_t AlignInBits, uint64_t OffsetInBits, 739 std::optional<unsigned> DWARFAddressSpace, DIFlags Flags, 740 Metadata *ExtraData, Metadata *Annotations, 741 StorageType Storage, bool ShouldCreate) { 742 assert(isCanonical(Name) && "Expected canonical MDString"); 743 DEFINE_GETIMPL_LOOKUP(DIDerivedType, 744 (Tag, Name, File, Line, Scope, BaseType, SizeInBits, 745 AlignInBits, OffsetInBits, DWARFAddressSpace, Flags, 746 ExtraData, Annotations)); 747 Metadata *Ops[] = {File, Scope, Name, BaseType, ExtraData, Annotations}; 748 DEFINE_GETIMPL_STORE(DIDerivedType, 749 (Tag, Line, SizeInBits, AlignInBits, OffsetInBits, 750 DWARFAddressSpace, Flags), 751 Ops); 752 } 753 754 DICompositeType *DICompositeType::getImpl( 755 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *File, 756 unsigned Line, Metadata *Scope, Metadata *BaseType, uint64_t SizeInBits, 757 uint32_t AlignInBits, uint64_t OffsetInBits, DIFlags Flags, 758 Metadata *Elements, unsigned RuntimeLang, Metadata *VTableHolder, 759 Metadata *TemplateParams, MDString *Identifier, Metadata *Discriminator, 760 Metadata *DataLocation, Metadata *Associated, Metadata *Allocated, 761 Metadata *Rank, Metadata *Annotations, StorageType Storage, 762 bool ShouldCreate) { 763 assert(isCanonical(Name) && "Expected canonical MDString"); 764 765 // Keep this in sync with buildODRType. 766 DEFINE_GETIMPL_LOOKUP(DICompositeType, 767 (Tag, Name, File, Line, Scope, BaseType, SizeInBits, 768 AlignInBits, OffsetInBits, Flags, Elements, 769 RuntimeLang, VTableHolder, TemplateParams, Identifier, 770 Discriminator, DataLocation, Associated, Allocated, 771 Rank, Annotations)); 772 Metadata *Ops[] = {File, Scope, Name, BaseType, 773 Elements, VTableHolder, TemplateParams, Identifier, 774 Discriminator, DataLocation, Associated, Allocated, 775 Rank, Annotations}; 776 DEFINE_GETIMPL_STORE( 777 DICompositeType, 778 (Tag, Line, RuntimeLang, SizeInBits, AlignInBits, OffsetInBits, Flags), 779 Ops); 780 } 781 782 DICompositeType *DICompositeType::buildODRType( 783 LLVMContext &Context, MDString &Identifier, unsigned Tag, MDString *Name, 784 Metadata *File, unsigned Line, Metadata *Scope, Metadata *BaseType, 785 uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits, 786 DIFlags Flags, Metadata *Elements, unsigned RuntimeLang, 787 Metadata *VTableHolder, Metadata *TemplateParams, Metadata *Discriminator, 788 Metadata *DataLocation, Metadata *Associated, Metadata *Allocated, 789 Metadata *Rank, Metadata *Annotations) { 790 assert(!Identifier.getString().empty() && "Expected valid identifier"); 791 if (!Context.isODRUniquingDebugTypes()) 792 return nullptr; 793 auto *&CT = (*Context.pImpl->DITypeMap)[&Identifier]; 794 if (!CT) 795 return CT = DICompositeType::getDistinct( 796 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, 797 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang, 798 VTableHolder, TemplateParams, &Identifier, Discriminator, 799 DataLocation, Associated, Allocated, Rank, Annotations); 800 801 if (CT->getTag() != Tag) 802 return nullptr; 803 804 // Only mutate CT if it's a forward declaration and the new operands aren't. 805 assert(CT->getRawIdentifier() == &Identifier && "Wrong ODR identifier?"); 806 if (!CT->isForwardDecl() || (Flags & DINode::FlagFwdDecl)) 807 return CT; 808 809 // Mutate CT in place. Keep this in sync with getImpl. 810 CT->mutate(Tag, Line, RuntimeLang, SizeInBits, AlignInBits, OffsetInBits, 811 Flags); 812 Metadata *Ops[] = {File, Scope, Name, BaseType, 813 Elements, VTableHolder, TemplateParams, &Identifier, 814 Discriminator, DataLocation, Associated, Allocated, 815 Rank, Annotations}; 816 assert((std::end(Ops) - std::begin(Ops)) == (int)CT->getNumOperands() && 817 "Mismatched number of operands"); 818 for (unsigned I = 0, E = CT->getNumOperands(); I != E; ++I) 819 if (Ops[I] != CT->getOperand(I)) 820 CT->setOperand(I, Ops[I]); 821 return CT; 822 } 823 824 DICompositeType *DICompositeType::getODRType( 825 LLVMContext &Context, MDString &Identifier, unsigned Tag, MDString *Name, 826 Metadata *File, unsigned Line, Metadata *Scope, Metadata *BaseType, 827 uint64_t SizeInBits, uint32_t AlignInBits, uint64_t OffsetInBits, 828 DIFlags Flags, Metadata *Elements, unsigned RuntimeLang, 829 Metadata *VTableHolder, Metadata *TemplateParams, Metadata *Discriminator, 830 Metadata *DataLocation, Metadata *Associated, Metadata *Allocated, 831 Metadata *Rank, Metadata *Annotations) { 832 assert(!Identifier.getString().empty() && "Expected valid identifier"); 833 if (!Context.isODRUniquingDebugTypes()) 834 return nullptr; 835 auto *&CT = (*Context.pImpl->DITypeMap)[&Identifier]; 836 if (!CT) { 837 CT = DICompositeType::getDistinct( 838 Context, Tag, Name, File, Line, Scope, BaseType, SizeInBits, 839 AlignInBits, OffsetInBits, Flags, Elements, RuntimeLang, VTableHolder, 840 TemplateParams, &Identifier, Discriminator, DataLocation, Associated, 841 Allocated, Rank, Annotations); 842 } else { 843 if (CT->getTag() != Tag) 844 return nullptr; 845 } 846 return CT; 847 } 848 849 DICompositeType *DICompositeType::getODRTypeIfExists(LLVMContext &Context, 850 MDString &Identifier) { 851 assert(!Identifier.getString().empty() && "Expected valid identifier"); 852 if (!Context.isODRUniquingDebugTypes()) 853 return nullptr; 854 return Context.pImpl->DITypeMap->lookup(&Identifier); 855 } 856 DISubroutineType::DISubroutineType(LLVMContext &C, StorageType Storage, 857 DIFlags Flags, uint8_t CC, 858 ArrayRef<Metadata *> Ops) 859 : DIType(C, DISubroutineTypeKind, Storage, dwarf::DW_TAG_subroutine_type, 0, 860 0, 0, 0, Flags, Ops), 861 CC(CC) {} 862 863 DISubroutineType *DISubroutineType::getImpl(LLVMContext &Context, DIFlags Flags, 864 uint8_t CC, Metadata *TypeArray, 865 StorageType Storage, 866 bool ShouldCreate) { 867 DEFINE_GETIMPL_LOOKUP(DISubroutineType, (Flags, CC, TypeArray)); 868 Metadata *Ops[] = {nullptr, nullptr, nullptr, TypeArray}; 869 DEFINE_GETIMPL_STORE(DISubroutineType, (Flags, CC), Ops); 870 } 871 872 DIFile::DIFile(LLVMContext &C, StorageType Storage, 873 std::optional<ChecksumInfo<MDString *>> CS, MDString *Src, 874 ArrayRef<Metadata *> Ops) 875 : DIScope(C, DIFileKind, Storage, dwarf::DW_TAG_file_type, Ops), 876 Checksum(CS), Source(Src) {} 877 878 // FIXME: Implement this string-enum correspondence with a .def file and macros, 879 // so that the association is explicit rather than implied. 880 static const char *ChecksumKindName[DIFile::CSK_Last] = { 881 "CSK_MD5", 882 "CSK_SHA1", 883 "CSK_SHA256", 884 }; 885 886 StringRef DIFile::getChecksumKindAsString(ChecksumKind CSKind) { 887 assert(CSKind <= DIFile::CSK_Last && "Invalid checksum kind"); 888 // The first space was originally the CSK_None variant, which is now 889 // obsolete, but the space is still reserved in ChecksumKind, so we account 890 // for it here. 891 return ChecksumKindName[CSKind - 1]; 892 } 893 894 std::optional<DIFile::ChecksumKind> 895 DIFile::getChecksumKind(StringRef CSKindStr) { 896 return StringSwitch<std::optional<DIFile::ChecksumKind>>(CSKindStr) 897 .Case("CSK_MD5", DIFile::CSK_MD5) 898 .Case("CSK_SHA1", DIFile::CSK_SHA1) 899 .Case("CSK_SHA256", DIFile::CSK_SHA256) 900 .Default(std::nullopt); 901 } 902 903 DIFile *DIFile::getImpl(LLVMContext &Context, MDString *Filename, 904 MDString *Directory, 905 std::optional<DIFile::ChecksumInfo<MDString *>> CS, 906 MDString *Source, StorageType Storage, 907 bool ShouldCreate) { 908 assert(isCanonical(Filename) && "Expected canonical MDString"); 909 assert(isCanonical(Directory) && "Expected canonical MDString"); 910 assert((!CS || isCanonical(CS->Value)) && "Expected canonical MDString"); 911 // We do *NOT* expect Source to be a canonical MDString because nullptr 912 // means none, so we need something to represent the empty file. 913 DEFINE_GETIMPL_LOOKUP(DIFile, (Filename, Directory, CS, Source)); 914 Metadata *Ops[] = {Filename, Directory, CS ? CS->Value : nullptr, Source}; 915 DEFINE_GETIMPL_STORE(DIFile, (CS, Source), Ops); 916 } 917 DICompileUnit::DICompileUnit(LLVMContext &C, StorageType Storage, 918 unsigned SourceLanguage, bool IsOptimized, 919 unsigned RuntimeVersion, unsigned EmissionKind, 920 uint64_t DWOId, bool SplitDebugInlining, 921 bool DebugInfoForProfiling, unsigned NameTableKind, 922 bool RangesBaseAddress, ArrayRef<Metadata *> Ops) 923 : DIScope(C, DICompileUnitKind, Storage, dwarf::DW_TAG_compile_unit, Ops), 924 SourceLanguage(SourceLanguage), RuntimeVersion(RuntimeVersion), 925 DWOId(DWOId), EmissionKind(EmissionKind), NameTableKind(NameTableKind), 926 IsOptimized(IsOptimized), SplitDebugInlining(SplitDebugInlining), 927 DebugInfoForProfiling(DebugInfoForProfiling), 928 RangesBaseAddress(RangesBaseAddress) { 929 assert(Storage != Uniqued); 930 } 931 932 DICompileUnit *DICompileUnit::getImpl( 933 LLVMContext &Context, unsigned SourceLanguage, Metadata *File, 934 MDString *Producer, bool IsOptimized, MDString *Flags, 935 unsigned RuntimeVersion, MDString *SplitDebugFilename, 936 unsigned EmissionKind, Metadata *EnumTypes, Metadata *RetainedTypes, 937 Metadata *GlobalVariables, Metadata *ImportedEntities, Metadata *Macros, 938 uint64_t DWOId, bool SplitDebugInlining, bool DebugInfoForProfiling, 939 unsigned NameTableKind, bool RangesBaseAddress, MDString *SysRoot, 940 MDString *SDK, StorageType Storage, bool ShouldCreate) { 941 assert(Storage != Uniqued && "Cannot unique DICompileUnit"); 942 assert(isCanonical(Producer) && "Expected canonical MDString"); 943 assert(isCanonical(Flags) && "Expected canonical MDString"); 944 assert(isCanonical(SplitDebugFilename) && "Expected canonical MDString"); 945 946 Metadata *Ops[] = {File, 947 Producer, 948 Flags, 949 SplitDebugFilename, 950 EnumTypes, 951 RetainedTypes, 952 GlobalVariables, 953 ImportedEntities, 954 Macros, 955 SysRoot, 956 SDK}; 957 return storeImpl(new (std::size(Ops), Storage) DICompileUnit( 958 Context, Storage, SourceLanguage, IsOptimized, 959 RuntimeVersion, EmissionKind, DWOId, SplitDebugInlining, 960 DebugInfoForProfiling, NameTableKind, RangesBaseAddress, 961 Ops), 962 Storage); 963 } 964 965 std::optional<DICompileUnit::DebugEmissionKind> 966 DICompileUnit::getEmissionKind(StringRef Str) { 967 return StringSwitch<std::optional<DebugEmissionKind>>(Str) 968 .Case("NoDebug", NoDebug) 969 .Case("FullDebug", FullDebug) 970 .Case("LineTablesOnly", LineTablesOnly) 971 .Case("DebugDirectivesOnly", DebugDirectivesOnly) 972 .Default(std::nullopt); 973 } 974 975 std::optional<DICompileUnit::DebugNameTableKind> 976 DICompileUnit::getNameTableKind(StringRef Str) { 977 return StringSwitch<std::optional<DebugNameTableKind>>(Str) 978 .Case("Default", DebugNameTableKind::Default) 979 .Case("GNU", DebugNameTableKind::GNU) 980 .Case("Apple", DebugNameTableKind::Apple) 981 .Case("None", DebugNameTableKind::None) 982 .Default(std::nullopt); 983 } 984 985 const char *DICompileUnit::emissionKindString(DebugEmissionKind EK) { 986 switch (EK) { 987 case NoDebug: 988 return "NoDebug"; 989 case FullDebug: 990 return "FullDebug"; 991 case LineTablesOnly: 992 return "LineTablesOnly"; 993 case DebugDirectivesOnly: 994 return "DebugDirectivesOnly"; 995 } 996 return nullptr; 997 } 998 999 const char *DICompileUnit::nameTableKindString(DebugNameTableKind NTK) { 1000 switch (NTK) { 1001 case DebugNameTableKind::Default: 1002 return nullptr; 1003 case DebugNameTableKind::GNU: 1004 return "GNU"; 1005 case DebugNameTableKind::Apple: 1006 return "Apple"; 1007 case DebugNameTableKind::None: 1008 return "None"; 1009 } 1010 return nullptr; 1011 } 1012 DISubprogram::DISubprogram(LLVMContext &C, StorageType Storage, unsigned Line, 1013 unsigned ScopeLine, unsigned VirtualIndex, 1014 int ThisAdjustment, DIFlags Flags, DISPFlags SPFlags, 1015 ArrayRef<Metadata *> Ops) 1016 : DILocalScope(C, DISubprogramKind, Storage, dwarf::DW_TAG_subprogram, Ops), 1017 Line(Line), ScopeLine(ScopeLine), VirtualIndex(VirtualIndex), 1018 ThisAdjustment(ThisAdjustment), Flags(Flags), SPFlags(SPFlags) { 1019 static_assert(dwarf::DW_VIRTUALITY_max < 4, "Virtuality out of range"); 1020 } 1021 DISubprogram::DISPFlags 1022 DISubprogram::toSPFlags(bool IsLocalToUnit, bool IsDefinition, bool IsOptimized, 1023 unsigned Virtuality, bool IsMainSubprogram) { 1024 // We're assuming virtuality is the low-order field. 1025 static_assert(int(SPFlagVirtual) == int(dwarf::DW_VIRTUALITY_virtual) && 1026 int(SPFlagPureVirtual) == 1027 int(dwarf::DW_VIRTUALITY_pure_virtual), 1028 "Virtuality constant mismatch"); 1029 return static_cast<DISPFlags>( 1030 (Virtuality & SPFlagVirtuality) | 1031 (IsLocalToUnit ? SPFlagLocalToUnit : SPFlagZero) | 1032 (IsDefinition ? SPFlagDefinition : SPFlagZero) | 1033 (IsOptimized ? SPFlagOptimized : SPFlagZero) | 1034 (IsMainSubprogram ? SPFlagMainSubprogram : SPFlagZero)); 1035 } 1036 1037 DISubprogram *DILocalScope::getSubprogram() const { 1038 if (auto *Block = dyn_cast<DILexicalBlockBase>(this)) 1039 return Block->getScope()->getSubprogram(); 1040 return const_cast<DISubprogram *>(cast<DISubprogram>(this)); 1041 } 1042 1043 DILocalScope *DILocalScope::getNonLexicalBlockFileScope() const { 1044 if (auto *File = dyn_cast<DILexicalBlockFile>(this)) 1045 return File->getScope()->getNonLexicalBlockFileScope(); 1046 return const_cast<DILocalScope *>(this); 1047 } 1048 1049 DILocalScope *DILocalScope::cloneScopeForSubprogram( 1050 DILocalScope &RootScope, DISubprogram &NewSP, LLVMContext &Ctx, 1051 DenseMap<const MDNode *, MDNode *> &Cache) { 1052 SmallVector<DIScope *> ScopeChain; 1053 DIScope *CachedResult = nullptr; 1054 1055 for (DIScope *Scope = &RootScope; !isa<DISubprogram>(Scope); 1056 Scope = Scope->getScope()) { 1057 if (auto It = Cache.find(Scope); It != Cache.end()) { 1058 CachedResult = cast<DIScope>(It->second); 1059 break; 1060 } 1061 ScopeChain.push_back(Scope); 1062 } 1063 1064 // Recreate the scope chain, bottom-up, starting at the new subprogram (or a 1065 // cached result). 1066 DIScope *UpdatedScope = CachedResult ? CachedResult : &NewSP; 1067 for (DIScope *ScopeToUpdate : reverse(ScopeChain)) { 1068 TempMDNode ClonedScope = ScopeToUpdate->clone(); 1069 cast<DILexicalBlockBase>(*ClonedScope).replaceScope(UpdatedScope); 1070 UpdatedScope = 1071 cast<DIScope>(MDNode::replaceWithUniqued(std::move(ClonedScope))); 1072 Cache[ScopeToUpdate] = UpdatedScope; 1073 } 1074 1075 return cast<DILocalScope>(UpdatedScope); 1076 } 1077 1078 DISubprogram::DISPFlags DISubprogram::getFlag(StringRef Flag) { 1079 return StringSwitch<DISPFlags>(Flag) 1080 #define HANDLE_DISP_FLAG(ID, NAME) .Case("DISPFlag" #NAME, SPFlag##NAME) 1081 #include "llvm/IR/DebugInfoFlags.def" 1082 .Default(SPFlagZero); 1083 } 1084 1085 StringRef DISubprogram::getFlagString(DISPFlags Flag) { 1086 switch (Flag) { 1087 // Appease a warning. 1088 case SPFlagVirtuality: 1089 return ""; 1090 #define HANDLE_DISP_FLAG(ID, NAME) \ 1091 case SPFlag##NAME: \ 1092 return "DISPFlag" #NAME; 1093 #include "llvm/IR/DebugInfoFlags.def" 1094 } 1095 return ""; 1096 } 1097 1098 DISubprogram::DISPFlags 1099 DISubprogram::splitFlags(DISPFlags Flags, 1100 SmallVectorImpl<DISPFlags> &SplitFlags) { 1101 // Multi-bit fields can require special handling. In our case, however, the 1102 // only multi-bit field is virtuality, and all its values happen to be 1103 // single-bit values, so the right behavior just falls out. 1104 #define HANDLE_DISP_FLAG(ID, NAME) \ 1105 if (DISPFlags Bit = Flags & SPFlag##NAME) { \ 1106 SplitFlags.push_back(Bit); \ 1107 Flags &= ~Bit; \ 1108 } 1109 #include "llvm/IR/DebugInfoFlags.def" 1110 return Flags; 1111 } 1112 1113 DISubprogram *DISubprogram::getImpl( 1114 LLVMContext &Context, Metadata *Scope, MDString *Name, 1115 MDString *LinkageName, Metadata *File, unsigned Line, Metadata *Type, 1116 unsigned ScopeLine, Metadata *ContainingType, unsigned VirtualIndex, 1117 int ThisAdjustment, DIFlags Flags, DISPFlags SPFlags, Metadata *Unit, 1118 Metadata *TemplateParams, Metadata *Declaration, Metadata *RetainedNodes, 1119 Metadata *ThrownTypes, Metadata *Annotations, MDString *TargetFuncName, 1120 StorageType Storage, bool ShouldCreate) { 1121 assert(isCanonical(Name) && "Expected canonical MDString"); 1122 assert(isCanonical(LinkageName) && "Expected canonical MDString"); 1123 assert(isCanonical(TargetFuncName) && "Expected canonical MDString"); 1124 DEFINE_GETIMPL_LOOKUP(DISubprogram, 1125 (Scope, Name, LinkageName, File, Line, Type, ScopeLine, 1126 ContainingType, VirtualIndex, ThisAdjustment, Flags, 1127 SPFlags, Unit, TemplateParams, Declaration, 1128 RetainedNodes, ThrownTypes, Annotations, 1129 TargetFuncName)); 1130 SmallVector<Metadata *, 13> Ops = { 1131 File, Scope, Name, LinkageName, 1132 Type, Unit, Declaration, RetainedNodes, 1133 ContainingType, TemplateParams, ThrownTypes, Annotations, 1134 TargetFuncName}; 1135 if (!TargetFuncName) { 1136 Ops.pop_back(); 1137 if (!Annotations) { 1138 Ops.pop_back(); 1139 if (!ThrownTypes) { 1140 Ops.pop_back(); 1141 if (!TemplateParams) { 1142 Ops.pop_back(); 1143 if (!ContainingType) 1144 Ops.pop_back(); 1145 } 1146 } 1147 } 1148 } 1149 DEFINE_GETIMPL_STORE_N( 1150 DISubprogram, 1151 (Line, ScopeLine, VirtualIndex, ThisAdjustment, Flags, SPFlags), Ops, 1152 Ops.size()); 1153 } 1154 1155 bool DISubprogram::describes(const Function *F) const { 1156 assert(F && "Invalid function"); 1157 return F->getSubprogram() == this; 1158 } 1159 DILexicalBlockBase::DILexicalBlockBase(LLVMContext &C, unsigned ID, 1160 StorageType Storage, 1161 ArrayRef<Metadata *> Ops) 1162 : DILocalScope(C, ID, Storage, dwarf::DW_TAG_lexical_block, Ops) {} 1163 1164 DILexicalBlock *DILexicalBlock::getImpl(LLVMContext &Context, Metadata *Scope, 1165 Metadata *File, unsigned Line, 1166 unsigned Column, StorageType Storage, 1167 bool ShouldCreate) { 1168 // Fixup column. 1169 adjustColumn(Column); 1170 1171 assert(Scope && "Expected scope"); 1172 DEFINE_GETIMPL_LOOKUP(DILexicalBlock, (Scope, File, Line, Column)); 1173 Metadata *Ops[] = {File, Scope}; 1174 DEFINE_GETIMPL_STORE(DILexicalBlock, (Line, Column), Ops); 1175 } 1176 1177 DILexicalBlockFile *DILexicalBlockFile::getImpl(LLVMContext &Context, 1178 Metadata *Scope, Metadata *File, 1179 unsigned Discriminator, 1180 StorageType Storage, 1181 bool ShouldCreate) { 1182 assert(Scope && "Expected scope"); 1183 DEFINE_GETIMPL_LOOKUP(DILexicalBlockFile, (Scope, File, Discriminator)); 1184 Metadata *Ops[] = {File, Scope}; 1185 DEFINE_GETIMPL_STORE(DILexicalBlockFile, (Discriminator), Ops); 1186 } 1187 1188 DINamespace::DINamespace(LLVMContext &Context, StorageType Storage, 1189 bool ExportSymbols, ArrayRef<Metadata *> Ops) 1190 : DIScope(Context, DINamespaceKind, Storage, dwarf::DW_TAG_namespace, Ops) { 1191 SubclassData1 = ExportSymbols; 1192 } 1193 DINamespace *DINamespace::getImpl(LLVMContext &Context, Metadata *Scope, 1194 MDString *Name, bool ExportSymbols, 1195 StorageType Storage, bool ShouldCreate) { 1196 assert(isCanonical(Name) && "Expected canonical MDString"); 1197 DEFINE_GETIMPL_LOOKUP(DINamespace, (Scope, Name, ExportSymbols)); 1198 // The nullptr is for DIScope's File operand. This should be refactored. 1199 Metadata *Ops[] = {nullptr, Scope, Name}; 1200 DEFINE_GETIMPL_STORE(DINamespace, (ExportSymbols), Ops); 1201 } 1202 1203 DICommonBlock::DICommonBlock(LLVMContext &Context, StorageType Storage, 1204 unsigned LineNo, ArrayRef<Metadata *> Ops) 1205 : DIScope(Context, DICommonBlockKind, Storage, dwarf::DW_TAG_common_block, 1206 Ops) { 1207 SubclassData32 = LineNo; 1208 } 1209 DICommonBlock *DICommonBlock::getImpl(LLVMContext &Context, Metadata *Scope, 1210 Metadata *Decl, MDString *Name, 1211 Metadata *File, unsigned LineNo, 1212 StorageType Storage, bool ShouldCreate) { 1213 assert(isCanonical(Name) && "Expected canonical MDString"); 1214 DEFINE_GETIMPL_LOOKUP(DICommonBlock, (Scope, Decl, Name, File, LineNo)); 1215 // The nullptr is for DIScope's File operand. This should be refactored. 1216 Metadata *Ops[] = {Scope, Decl, Name, File}; 1217 DEFINE_GETIMPL_STORE(DICommonBlock, (LineNo), Ops); 1218 } 1219 1220 DIModule::DIModule(LLVMContext &Context, StorageType Storage, unsigned LineNo, 1221 bool IsDecl, ArrayRef<Metadata *> Ops) 1222 : DIScope(Context, DIModuleKind, Storage, dwarf::DW_TAG_module, Ops) { 1223 SubclassData1 = IsDecl; 1224 SubclassData32 = LineNo; 1225 } 1226 DIModule *DIModule::getImpl(LLVMContext &Context, Metadata *File, 1227 Metadata *Scope, MDString *Name, 1228 MDString *ConfigurationMacros, 1229 MDString *IncludePath, MDString *APINotesFile, 1230 unsigned LineNo, bool IsDecl, StorageType Storage, 1231 bool ShouldCreate) { 1232 assert(isCanonical(Name) && "Expected canonical MDString"); 1233 DEFINE_GETIMPL_LOOKUP(DIModule, (File, Scope, Name, ConfigurationMacros, 1234 IncludePath, APINotesFile, LineNo, IsDecl)); 1235 Metadata *Ops[] = {File, Scope, Name, ConfigurationMacros, 1236 IncludePath, APINotesFile}; 1237 DEFINE_GETIMPL_STORE(DIModule, (LineNo, IsDecl), Ops); 1238 } 1239 DITemplateTypeParameter::DITemplateTypeParameter(LLVMContext &Context, 1240 StorageType Storage, 1241 bool IsDefault, 1242 ArrayRef<Metadata *> Ops) 1243 : DITemplateParameter(Context, DITemplateTypeParameterKind, Storage, 1244 dwarf::DW_TAG_template_type_parameter, IsDefault, 1245 Ops) {} 1246 1247 DITemplateTypeParameter * 1248 DITemplateTypeParameter::getImpl(LLVMContext &Context, MDString *Name, 1249 Metadata *Type, bool isDefault, 1250 StorageType Storage, bool ShouldCreate) { 1251 assert(isCanonical(Name) && "Expected canonical MDString"); 1252 DEFINE_GETIMPL_LOOKUP(DITemplateTypeParameter, (Name, Type, isDefault)); 1253 Metadata *Ops[] = {Name, Type}; 1254 DEFINE_GETIMPL_STORE(DITemplateTypeParameter, (isDefault), Ops); 1255 } 1256 1257 DITemplateValueParameter *DITemplateValueParameter::getImpl( 1258 LLVMContext &Context, unsigned Tag, MDString *Name, Metadata *Type, 1259 bool isDefault, Metadata *Value, StorageType Storage, bool ShouldCreate) { 1260 assert(isCanonical(Name) && "Expected canonical MDString"); 1261 DEFINE_GETIMPL_LOOKUP(DITemplateValueParameter, 1262 (Tag, Name, Type, isDefault, Value)); 1263 Metadata *Ops[] = {Name, Type, Value}; 1264 DEFINE_GETIMPL_STORE(DITemplateValueParameter, (Tag, isDefault), Ops); 1265 } 1266 1267 DIGlobalVariable * 1268 DIGlobalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name, 1269 MDString *LinkageName, Metadata *File, unsigned Line, 1270 Metadata *Type, bool IsLocalToUnit, bool IsDefinition, 1271 Metadata *StaticDataMemberDeclaration, 1272 Metadata *TemplateParams, uint32_t AlignInBits, 1273 Metadata *Annotations, StorageType Storage, 1274 bool ShouldCreate) { 1275 assert(isCanonical(Name) && "Expected canonical MDString"); 1276 assert(isCanonical(LinkageName) && "Expected canonical MDString"); 1277 DEFINE_GETIMPL_LOOKUP( 1278 DIGlobalVariable, 1279 (Scope, Name, LinkageName, File, Line, Type, IsLocalToUnit, IsDefinition, 1280 StaticDataMemberDeclaration, TemplateParams, AlignInBits, Annotations)); 1281 Metadata *Ops[] = {Scope, 1282 Name, 1283 File, 1284 Type, 1285 Name, 1286 LinkageName, 1287 StaticDataMemberDeclaration, 1288 TemplateParams, 1289 Annotations}; 1290 DEFINE_GETIMPL_STORE(DIGlobalVariable, 1291 (Line, IsLocalToUnit, IsDefinition, AlignInBits), Ops); 1292 } 1293 1294 DILocalVariable * 1295 DILocalVariable::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name, 1296 Metadata *File, unsigned Line, Metadata *Type, 1297 unsigned Arg, DIFlags Flags, uint32_t AlignInBits, 1298 Metadata *Annotations, StorageType Storage, 1299 bool ShouldCreate) { 1300 // 64K ought to be enough for any frontend. 1301 assert(Arg <= UINT16_MAX && "Expected argument number to fit in 16-bits"); 1302 1303 assert(Scope && "Expected scope"); 1304 assert(isCanonical(Name) && "Expected canonical MDString"); 1305 DEFINE_GETIMPL_LOOKUP(DILocalVariable, (Scope, Name, File, Line, Type, Arg, 1306 Flags, AlignInBits, Annotations)); 1307 Metadata *Ops[] = {Scope, Name, File, Type, Annotations}; 1308 DEFINE_GETIMPL_STORE(DILocalVariable, (Line, Arg, Flags, AlignInBits), Ops); 1309 } 1310 1311 DIVariable::DIVariable(LLVMContext &C, unsigned ID, StorageType Storage, 1312 signed Line, ArrayRef<Metadata *> Ops, 1313 uint32_t AlignInBits) 1314 : DINode(C, ID, Storage, dwarf::DW_TAG_variable, Ops), Line(Line) { 1315 SubclassData32 = AlignInBits; 1316 } 1317 std::optional<uint64_t> DIVariable::getSizeInBits() const { 1318 // This is used by the Verifier so be mindful of broken types. 1319 const Metadata *RawType = getRawType(); 1320 while (RawType) { 1321 // Try to get the size directly. 1322 if (auto *T = dyn_cast<DIType>(RawType)) 1323 if (uint64_t Size = T->getSizeInBits()) 1324 return Size; 1325 1326 if (auto *DT = dyn_cast<DIDerivedType>(RawType)) { 1327 // Look at the base type. 1328 RawType = DT->getRawBaseType(); 1329 continue; 1330 } 1331 1332 // Missing type or size. 1333 break; 1334 } 1335 1336 // Fail gracefully. 1337 return std::nullopt; 1338 } 1339 1340 DILabel::DILabel(LLVMContext &C, StorageType Storage, unsigned Line, 1341 ArrayRef<Metadata *> Ops) 1342 : DINode(C, DILabelKind, Storage, dwarf::DW_TAG_label, Ops) { 1343 SubclassData32 = Line; 1344 } 1345 DILabel *DILabel::getImpl(LLVMContext &Context, Metadata *Scope, MDString *Name, 1346 Metadata *File, unsigned Line, StorageType Storage, 1347 bool ShouldCreate) { 1348 assert(Scope && "Expected scope"); 1349 assert(isCanonical(Name) && "Expected canonical MDString"); 1350 DEFINE_GETIMPL_LOOKUP(DILabel, (Scope, Name, File, Line)); 1351 Metadata *Ops[] = {Scope, Name, File}; 1352 DEFINE_GETIMPL_STORE(DILabel, (Line), Ops); 1353 } 1354 1355 DIExpression *DIExpression::getImpl(LLVMContext &Context, 1356 ArrayRef<uint64_t> Elements, 1357 StorageType Storage, bool ShouldCreate) { 1358 DEFINE_GETIMPL_LOOKUP(DIExpression, (Elements)); 1359 DEFINE_GETIMPL_STORE_NO_OPS(DIExpression, (Elements)); 1360 } 1361 bool DIExpression::isEntryValue() const { 1362 if (auto singleLocElts = getSingleLocationExpressionElements()) { 1363 return singleLocElts->size() > 0 && 1364 (*singleLocElts)[0] == dwarf::DW_OP_LLVM_entry_value; 1365 } 1366 return false; 1367 } 1368 bool DIExpression::startsWithDeref() const { 1369 if (auto singleLocElts = getSingleLocationExpressionElements()) 1370 return singleLocElts->size() > 0 && 1371 (*singleLocElts)[0] == dwarf::DW_OP_deref; 1372 return false; 1373 } 1374 bool DIExpression::isDeref() const { 1375 if (auto singleLocElts = getSingleLocationExpressionElements()) 1376 return singleLocElts->size() == 1 && 1377 (*singleLocElts)[0] == dwarf::DW_OP_deref; 1378 return false; 1379 } 1380 1381 DIAssignID *DIAssignID::getImpl(LLVMContext &Context, StorageType Storage, 1382 bool ShouldCreate) { 1383 // Uniqued DIAssignID are not supported as the instance address *is* the ID. 1384 assert(Storage != StorageType::Uniqued && "uniqued DIAssignID unsupported"); 1385 return storeImpl(new (0u, Storage) DIAssignID(Context, Storage), Storage); 1386 } 1387 1388 unsigned DIExpression::ExprOperand::getSize() const { 1389 uint64_t Op = getOp(); 1390 1391 if (Op >= dwarf::DW_OP_breg0 && Op <= dwarf::DW_OP_breg31) 1392 return 2; 1393 1394 switch (Op) { 1395 case dwarf::DW_OP_LLVM_convert: 1396 case dwarf::DW_OP_LLVM_fragment: 1397 case dwarf::DW_OP_bregx: 1398 return 3; 1399 case dwarf::DW_OP_constu: 1400 case dwarf::DW_OP_consts: 1401 case dwarf::DW_OP_deref_size: 1402 case dwarf::DW_OP_plus_uconst: 1403 case dwarf::DW_OP_LLVM_tag_offset: 1404 case dwarf::DW_OP_LLVM_entry_value: 1405 case dwarf::DW_OP_LLVM_arg: 1406 case dwarf::DW_OP_regx: 1407 return 2; 1408 default: 1409 return 1; 1410 } 1411 } 1412 1413 bool DIExpression::isValid() const { 1414 for (auto I = expr_op_begin(), E = expr_op_end(); I != E; ++I) { 1415 // Check that there's space for the operand. 1416 if (I->get() + I->getSize() > E->get()) 1417 return false; 1418 1419 uint64_t Op = I->getOp(); 1420 if ((Op >= dwarf::DW_OP_reg0 && Op <= dwarf::DW_OP_reg31) || 1421 (Op >= dwarf::DW_OP_breg0 && Op <= dwarf::DW_OP_breg31)) 1422 return true; 1423 1424 // Check that the operand is valid. 1425 switch (Op) { 1426 default: 1427 return false; 1428 case dwarf::DW_OP_LLVM_fragment: 1429 // A fragment operator must appear at the end. 1430 return I->get() + I->getSize() == E->get(); 1431 case dwarf::DW_OP_stack_value: { 1432 // Must be the last one or followed by a DW_OP_LLVM_fragment. 1433 if (I->get() + I->getSize() == E->get()) 1434 break; 1435 auto J = I; 1436 if ((++J)->getOp() != dwarf::DW_OP_LLVM_fragment) 1437 return false; 1438 break; 1439 } 1440 case dwarf::DW_OP_swap: { 1441 // Must be more than one implicit element on the stack. 1442 1443 // FIXME: A better way to implement this would be to add a local variable 1444 // that keeps track of the stack depth and introduce something like a 1445 // DW_LLVM_OP_implicit_location as a placeholder for the location this 1446 // DIExpression is attached to, or else pass the number of implicit stack 1447 // elements into isValid. 1448 if (getNumElements() == 1) 1449 return false; 1450 break; 1451 } 1452 case dwarf::DW_OP_LLVM_entry_value: { 1453 // An entry value operator must appear at the beginning or immediately 1454 // following `DW_OP_LLVM_arg 0`, and the number of operations it cover can 1455 // currently only be 1, because we support only entry values of a simple 1456 // register location. One reason for this is that we currently can't 1457 // calculate the size of the resulting DWARF block for other expressions. 1458 auto FirstOp = expr_op_begin(); 1459 if (FirstOp->getOp() == dwarf::DW_OP_LLVM_arg && FirstOp->getArg(0) == 0) 1460 ++FirstOp; 1461 return I->get() == FirstOp->get() && I->getArg(0) == 1; 1462 } 1463 case dwarf::DW_OP_LLVM_implicit_pointer: 1464 case dwarf::DW_OP_LLVM_convert: 1465 case dwarf::DW_OP_LLVM_arg: 1466 case dwarf::DW_OP_LLVM_tag_offset: 1467 case dwarf::DW_OP_constu: 1468 case dwarf::DW_OP_plus_uconst: 1469 case dwarf::DW_OP_plus: 1470 case dwarf::DW_OP_minus: 1471 case dwarf::DW_OP_mul: 1472 case dwarf::DW_OP_div: 1473 case dwarf::DW_OP_mod: 1474 case dwarf::DW_OP_or: 1475 case dwarf::DW_OP_and: 1476 case dwarf::DW_OP_xor: 1477 case dwarf::DW_OP_shl: 1478 case dwarf::DW_OP_shr: 1479 case dwarf::DW_OP_shra: 1480 case dwarf::DW_OP_deref: 1481 case dwarf::DW_OP_deref_size: 1482 case dwarf::DW_OP_xderef: 1483 case dwarf::DW_OP_lit0: 1484 case dwarf::DW_OP_not: 1485 case dwarf::DW_OP_dup: 1486 case dwarf::DW_OP_regx: 1487 case dwarf::DW_OP_bregx: 1488 case dwarf::DW_OP_push_object_address: 1489 case dwarf::DW_OP_over: 1490 case dwarf::DW_OP_consts: 1491 case dwarf::DW_OP_eq: 1492 case dwarf::DW_OP_ne: 1493 case dwarf::DW_OP_gt: 1494 case dwarf::DW_OP_ge: 1495 case dwarf::DW_OP_lt: 1496 case dwarf::DW_OP_le: 1497 break; 1498 } 1499 } 1500 return true; 1501 } 1502 1503 bool DIExpression::isImplicit() const { 1504 if (!isValid()) 1505 return false; 1506 1507 if (getNumElements() == 0) 1508 return false; 1509 1510 for (const auto &It : expr_ops()) { 1511 switch (It.getOp()) { 1512 default: 1513 break; 1514 case dwarf::DW_OP_stack_value: 1515 case dwarf::DW_OP_LLVM_tag_offset: 1516 return true; 1517 } 1518 } 1519 1520 return false; 1521 } 1522 1523 bool DIExpression::isComplex() const { 1524 if (!isValid()) 1525 return false; 1526 1527 if (getNumElements() == 0) 1528 return false; 1529 1530 // If there are any elements other than fragment or tag_offset, then some 1531 // kind of complex computation occurs. 1532 for (const auto &It : expr_ops()) { 1533 switch (It.getOp()) { 1534 case dwarf::DW_OP_LLVM_tag_offset: 1535 case dwarf::DW_OP_LLVM_fragment: 1536 case dwarf::DW_OP_LLVM_arg: 1537 continue; 1538 default: 1539 return true; 1540 } 1541 } 1542 1543 return false; 1544 } 1545 1546 bool DIExpression::isSingleLocationExpression() const { 1547 if (!isValid()) 1548 return false; 1549 1550 if (getNumElements() == 0) 1551 return true; 1552 1553 auto ExprOpBegin = expr_ops().begin(); 1554 auto ExprOpEnd = expr_ops().end(); 1555 if (ExprOpBegin->getOp() == dwarf::DW_OP_LLVM_arg) { 1556 if (ExprOpBegin->getArg(0) != 0) 1557 return false; 1558 ++ExprOpBegin; 1559 } 1560 1561 return !std::any_of(ExprOpBegin, ExprOpEnd, [](auto Op) { 1562 return Op.getOp() == dwarf::DW_OP_LLVM_arg; 1563 }); 1564 } 1565 1566 std::optional<ArrayRef<uint64_t>> 1567 DIExpression::getSingleLocationExpressionElements() const { 1568 // Check for `isValid` covered by `isSingleLocationExpression`. 1569 if (!isSingleLocationExpression()) 1570 return std::nullopt; 1571 1572 // An empty expression is already non-variadic. 1573 if (!getNumElements()) 1574 return ArrayRef<uint64_t>(); 1575 1576 // If Expr does not have a leading DW_OP_LLVM_arg then we don't need to do 1577 // anything. 1578 if (getElements()[0] == dwarf::DW_OP_LLVM_arg) 1579 return getElements().drop_front(2); 1580 return getElements(); 1581 } 1582 1583 const DIExpression * 1584 DIExpression::convertToUndefExpression(const DIExpression *Expr) { 1585 SmallVector<uint64_t, 3> UndefOps; 1586 if (auto FragmentInfo = Expr->getFragmentInfo()) { 1587 UndefOps.append({dwarf::DW_OP_LLVM_fragment, FragmentInfo->OffsetInBits, 1588 FragmentInfo->SizeInBits}); 1589 } 1590 return DIExpression::get(Expr->getContext(), UndefOps); 1591 } 1592 1593 const DIExpression * 1594 DIExpression::convertToVariadicExpression(const DIExpression *Expr) { 1595 if (any_of(Expr->expr_ops(), [](auto ExprOp) { 1596 return ExprOp.getOp() == dwarf::DW_OP_LLVM_arg; 1597 })) 1598 return Expr; 1599 SmallVector<uint64_t> NewOps; 1600 NewOps.reserve(Expr->getNumElements() + 2); 1601 NewOps.append({dwarf::DW_OP_LLVM_arg, 0}); 1602 NewOps.append(Expr->elements_begin(), Expr->elements_end()); 1603 return DIExpression::get(Expr->getContext(), NewOps); 1604 } 1605 1606 std::optional<const DIExpression *> 1607 DIExpression::convertToNonVariadicExpression(const DIExpression *Expr) { 1608 if (!Expr) 1609 return std::nullopt; 1610 1611 if (auto Elts = Expr->getSingleLocationExpressionElements()) 1612 return DIExpression::get(Expr->getContext(), *Elts); 1613 1614 return std::nullopt; 1615 } 1616 1617 void DIExpression::canonicalizeExpressionOps(SmallVectorImpl<uint64_t> &Ops, 1618 const DIExpression *Expr, 1619 bool IsIndirect) { 1620 // If Expr is not already variadic, insert the implied `DW_OP_LLVM_arg 0` 1621 // to the existing expression ops. 1622 if (none_of(Expr->expr_ops(), [](auto ExprOp) { 1623 return ExprOp.getOp() == dwarf::DW_OP_LLVM_arg; 1624 })) 1625 Ops.append({dwarf::DW_OP_LLVM_arg, 0}); 1626 // If Expr is not indirect, we only need to insert the expression elements and 1627 // we're done. 1628 if (!IsIndirect) { 1629 Ops.append(Expr->elements_begin(), Expr->elements_end()); 1630 return; 1631 } 1632 // If Expr is indirect, insert the implied DW_OP_deref at the end of the 1633 // expression but before DW_OP_{stack_value, LLVM_fragment} if they are 1634 // present. 1635 for (auto Op : Expr->expr_ops()) { 1636 if (Op.getOp() == dwarf::DW_OP_stack_value || 1637 Op.getOp() == dwarf::DW_OP_LLVM_fragment) { 1638 Ops.push_back(dwarf::DW_OP_deref); 1639 IsIndirect = false; 1640 } 1641 Op.appendToVector(Ops); 1642 } 1643 if (IsIndirect) 1644 Ops.push_back(dwarf::DW_OP_deref); 1645 } 1646 1647 bool DIExpression::isEqualExpression(const DIExpression *FirstExpr, 1648 bool FirstIndirect, 1649 const DIExpression *SecondExpr, 1650 bool SecondIndirect) { 1651 SmallVector<uint64_t> FirstOps; 1652 DIExpression::canonicalizeExpressionOps(FirstOps, FirstExpr, FirstIndirect); 1653 SmallVector<uint64_t> SecondOps; 1654 DIExpression::canonicalizeExpressionOps(SecondOps, SecondExpr, 1655 SecondIndirect); 1656 return FirstOps == SecondOps; 1657 } 1658 1659 std::optional<DIExpression::FragmentInfo> 1660 DIExpression::getFragmentInfo(expr_op_iterator Start, expr_op_iterator End) { 1661 for (auto I = Start; I != End; ++I) 1662 if (I->getOp() == dwarf::DW_OP_LLVM_fragment) { 1663 DIExpression::FragmentInfo Info = {I->getArg(1), I->getArg(0)}; 1664 return Info; 1665 } 1666 return std::nullopt; 1667 } 1668 1669 void DIExpression::appendOffset(SmallVectorImpl<uint64_t> &Ops, 1670 int64_t Offset) { 1671 if (Offset > 0) { 1672 Ops.push_back(dwarf::DW_OP_plus_uconst); 1673 Ops.push_back(Offset); 1674 } else if (Offset < 0) { 1675 Ops.push_back(dwarf::DW_OP_constu); 1676 // Avoid UB when encountering LLONG_MIN, because in 2's complement 1677 // abs(LLONG_MIN) is LLONG_MAX+1. 1678 uint64_t AbsMinusOne = -(Offset+1); 1679 Ops.push_back(AbsMinusOne + 1); 1680 Ops.push_back(dwarf::DW_OP_minus); 1681 } 1682 } 1683 1684 bool DIExpression::extractIfOffset(int64_t &Offset) const { 1685 auto SingleLocEltsOpt = getSingleLocationExpressionElements(); 1686 if (!SingleLocEltsOpt) 1687 return false; 1688 auto SingleLocElts = *SingleLocEltsOpt; 1689 1690 if (SingleLocElts.size() == 0) { 1691 Offset = 0; 1692 return true; 1693 } 1694 1695 if (SingleLocElts.size() == 2 && 1696 SingleLocElts[0] == dwarf::DW_OP_plus_uconst) { 1697 Offset = SingleLocElts[1]; 1698 return true; 1699 } 1700 1701 if (SingleLocElts.size() == 3 && SingleLocElts[0] == dwarf::DW_OP_constu) { 1702 if (SingleLocElts[2] == dwarf::DW_OP_plus) { 1703 Offset = SingleLocElts[1]; 1704 return true; 1705 } 1706 if (SingleLocElts[2] == dwarf::DW_OP_minus) { 1707 Offset = -SingleLocElts[1]; 1708 return true; 1709 } 1710 } 1711 1712 return false; 1713 } 1714 1715 bool DIExpression::hasAllLocationOps(unsigned N) const { 1716 SmallDenseSet<uint64_t, 4> SeenOps; 1717 for (auto ExprOp : expr_ops()) 1718 if (ExprOp.getOp() == dwarf::DW_OP_LLVM_arg) 1719 SeenOps.insert(ExprOp.getArg(0)); 1720 for (uint64_t Idx = 0; Idx < N; ++Idx) 1721 if (!SeenOps.contains(Idx)) 1722 return false; 1723 return true; 1724 } 1725 1726 const DIExpression *DIExpression::extractAddressClass(const DIExpression *Expr, 1727 unsigned &AddrClass) { 1728 // FIXME: This seems fragile. Nothing that verifies that these elements 1729 // actually map to ops and not operands. 1730 auto SingleLocEltsOpt = Expr->getSingleLocationExpressionElements(); 1731 if (!SingleLocEltsOpt) 1732 return nullptr; 1733 auto SingleLocElts = *SingleLocEltsOpt; 1734 1735 const unsigned PatternSize = 4; 1736 if (SingleLocElts.size() >= PatternSize && 1737 SingleLocElts[PatternSize - 4] == dwarf::DW_OP_constu && 1738 SingleLocElts[PatternSize - 2] == dwarf::DW_OP_swap && 1739 SingleLocElts[PatternSize - 1] == dwarf::DW_OP_xderef) { 1740 AddrClass = SingleLocElts[PatternSize - 3]; 1741 1742 if (SingleLocElts.size() == PatternSize) 1743 return nullptr; 1744 return DIExpression::get( 1745 Expr->getContext(), 1746 ArrayRef(&*SingleLocElts.begin(), SingleLocElts.size() - PatternSize)); 1747 } 1748 return Expr; 1749 } 1750 1751 DIExpression *DIExpression::prepend(const DIExpression *Expr, uint8_t Flags, 1752 int64_t Offset) { 1753 SmallVector<uint64_t, 8> Ops; 1754 if (Flags & DIExpression::DerefBefore) 1755 Ops.push_back(dwarf::DW_OP_deref); 1756 1757 appendOffset(Ops, Offset); 1758 if (Flags & DIExpression::DerefAfter) 1759 Ops.push_back(dwarf::DW_OP_deref); 1760 1761 bool StackValue = Flags & DIExpression::StackValue; 1762 bool EntryValue = Flags & DIExpression::EntryValue; 1763 1764 return prependOpcodes(Expr, Ops, StackValue, EntryValue); 1765 } 1766 1767 DIExpression *DIExpression::appendOpsToArg(const DIExpression *Expr, 1768 ArrayRef<uint64_t> Ops, 1769 unsigned ArgNo, bool StackValue) { 1770 assert(Expr && "Can't add ops to this expression"); 1771 1772 // Handle non-variadic intrinsics by prepending the opcodes. 1773 if (!any_of(Expr->expr_ops(), 1774 [](auto Op) { return Op.getOp() == dwarf::DW_OP_LLVM_arg; })) { 1775 assert(ArgNo == 0 && 1776 "Location Index must be 0 for a non-variadic expression."); 1777 SmallVector<uint64_t, 8> NewOps(Ops.begin(), Ops.end()); 1778 return DIExpression::prependOpcodes(Expr, NewOps, StackValue); 1779 } 1780 1781 SmallVector<uint64_t, 8> NewOps; 1782 for (auto Op : Expr->expr_ops()) { 1783 // A DW_OP_stack_value comes at the end, but before a DW_OP_LLVM_fragment. 1784 if (StackValue) { 1785 if (Op.getOp() == dwarf::DW_OP_stack_value) 1786 StackValue = false; 1787 else if (Op.getOp() == dwarf::DW_OP_LLVM_fragment) { 1788 NewOps.push_back(dwarf::DW_OP_stack_value); 1789 StackValue = false; 1790 } 1791 } 1792 Op.appendToVector(NewOps); 1793 if (Op.getOp() == dwarf::DW_OP_LLVM_arg && Op.getArg(0) == ArgNo) 1794 NewOps.insert(NewOps.end(), Ops.begin(), Ops.end()); 1795 } 1796 if (StackValue) 1797 NewOps.push_back(dwarf::DW_OP_stack_value); 1798 1799 return DIExpression::get(Expr->getContext(), NewOps); 1800 } 1801 1802 DIExpression *DIExpression::replaceArg(const DIExpression *Expr, 1803 uint64_t OldArg, uint64_t NewArg) { 1804 assert(Expr && "Can't replace args in this expression"); 1805 1806 SmallVector<uint64_t, 8> NewOps; 1807 1808 for (auto Op : Expr->expr_ops()) { 1809 if (Op.getOp() != dwarf::DW_OP_LLVM_arg || Op.getArg(0) < OldArg) { 1810 Op.appendToVector(NewOps); 1811 continue; 1812 } 1813 NewOps.push_back(dwarf::DW_OP_LLVM_arg); 1814 uint64_t Arg = Op.getArg(0) == OldArg ? NewArg : Op.getArg(0); 1815 // OldArg has been deleted from the Op list, so decrement all indices 1816 // greater than it. 1817 if (Arg > OldArg) 1818 --Arg; 1819 NewOps.push_back(Arg); 1820 } 1821 return DIExpression::get(Expr->getContext(), NewOps); 1822 } 1823 1824 DIExpression *DIExpression::prependOpcodes(const DIExpression *Expr, 1825 SmallVectorImpl<uint64_t> &Ops, 1826 bool StackValue, bool EntryValue) { 1827 assert(Expr && "Can't prepend ops to this expression"); 1828 1829 if (EntryValue) { 1830 Ops.push_back(dwarf::DW_OP_LLVM_entry_value); 1831 // Use a block size of 1 for the target register operand. The 1832 // DWARF backend currently cannot emit entry values with a block 1833 // size > 1. 1834 Ops.push_back(1); 1835 } 1836 1837 // If there are no ops to prepend, do not even add the DW_OP_stack_value. 1838 if (Ops.empty()) 1839 StackValue = false; 1840 for (auto Op : Expr->expr_ops()) { 1841 // A DW_OP_stack_value comes at the end, but before a DW_OP_LLVM_fragment. 1842 if (StackValue) { 1843 if (Op.getOp() == dwarf::DW_OP_stack_value) 1844 StackValue = false; 1845 else if (Op.getOp() == dwarf::DW_OP_LLVM_fragment) { 1846 Ops.push_back(dwarf::DW_OP_stack_value); 1847 StackValue = false; 1848 } 1849 } 1850 Op.appendToVector(Ops); 1851 } 1852 if (StackValue) 1853 Ops.push_back(dwarf::DW_OP_stack_value); 1854 return DIExpression::get(Expr->getContext(), Ops); 1855 } 1856 1857 DIExpression *DIExpression::append(const DIExpression *Expr, 1858 ArrayRef<uint64_t> Ops) { 1859 assert(Expr && !Ops.empty() && "Can't append ops to this expression"); 1860 1861 // Copy Expr's current op list. 1862 SmallVector<uint64_t, 16> NewOps; 1863 for (auto Op : Expr->expr_ops()) { 1864 // Append new opcodes before DW_OP_{stack_value, LLVM_fragment}. 1865 if (Op.getOp() == dwarf::DW_OP_stack_value || 1866 Op.getOp() == dwarf::DW_OP_LLVM_fragment) { 1867 NewOps.append(Ops.begin(), Ops.end()); 1868 1869 // Ensure that the new opcodes are only appended once. 1870 Ops = std::nullopt; 1871 } 1872 Op.appendToVector(NewOps); 1873 } 1874 1875 NewOps.append(Ops.begin(), Ops.end()); 1876 auto *result = DIExpression::get(Expr->getContext(), NewOps); 1877 assert(result->isValid() && "concatenated expression is not valid"); 1878 return result; 1879 } 1880 1881 DIExpression *DIExpression::appendToStack(const DIExpression *Expr, 1882 ArrayRef<uint64_t> Ops) { 1883 assert(Expr && !Ops.empty() && "Can't append ops to this expression"); 1884 assert(none_of(Ops, 1885 [](uint64_t Op) { 1886 return Op == dwarf::DW_OP_stack_value || 1887 Op == dwarf::DW_OP_LLVM_fragment; 1888 }) && 1889 "Can't append this op"); 1890 1891 // Append a DW_OP_deref after Expr's current op list if it's non-empty and 1892 // has no DW_OP_stack_value. 1893 // 1894 // Match .* DW_OP_stack_value (DW_OP_LLVM_fragment A B)?. 1895 std::optional<FragmentInfo> FI = Expr->getFragmentInfo(); 1896 unsigned DropUntilStackValue = FI ? 3 : 0; 1897 ArrayRef<uint64_t> ExprOpsBeforeFragment = 1898 Expr->getElements().drop_back(DropUntilStackValue); 1899 bool NeedsDeref = (Expr->getNumElements() > DropUntilStackValue) && 1900 (ExprOpsBeforeFragment.back() != dwarf::DW_OP_stack_value); 1901 bool NeedsStackValue = NeedsDeref || ExprOpsBeforeFragment.empty(); 1902 1903 // Append a DW_OP_deref after Expr's current op list if needed, then append 1904 // the new ops, and finally ensure that a single DW_OP_stack_value is present. 1905 SmallVector<uint64_t, 16> NewOps; 1906 if (NeedsDeref) 1907 NewOps.push_back(dwarf::DW_OP_deref); 1908 NewOps.append(Ops.begin(), Ops.end()); 1909 if (NeedsStackValue) 1910 NewOps.push_back(dwarf::DW_OP_stack_value); 1911 return DIExpression::append(Expr, NewOps); 1912 } 1913 1914 std::optional<DIExpression *> DIExpression::createFragmentExpression( 1915 const DIExpression *Expr, unsigned OffsetInBits, unsigned SizeInBits) { 1916 SmallVector<uint64_t, 8> Ops; 1917 // Track whether it's safe to split the value at the top of the DWARF stack, 1918 // assuming that it'll be used as an implicit location value. 1919 bool CanSplitValue = true; 1920 // Copy over the expression, but leave off any trailing DW_OP_LLVM_fragment. 1921 if (Expr) { 1922 for (auto Op : Expr->expr_ops()) { 1923 switch (Op.getOp()) { 1924 default: 1925 break; 1926 case dwarf::DW_OP_shr: 1927 case dwarf::DW_OP_shra: 1928 case dwarf::DW_OP_shl: 1929 case dwarf::DW_OP_plus: 1930 case dwarf::DW_OP_plus_uconst: 1931 case dwarf::DW_OP_minus: 1932 // We can't safely split arithmetic or shift operations into multiple 1933 // fragments because we can't express carry-over between fragments. 1934 // 1935 // FIXME: We *could* preserve the lowest fragment of a constant offset 1936 // operation if the offset fits into SizeInBits. 1937 CanSplitValue = false; 1938 break; 1939 case dwarf::DW_OP_deref: 1940 case dwarf::DW_OP_deref_size: 1941 case dwarf::DW_OP_deref_type: 1942 case dwarf::DW_OP_xderef: 1943 case dwarf::DW_OP_xderef_size: 1944 case dwarf::DW_OP_xderef_type: 1945 // Preceeding arithmetic operations have been applied to compute an 1946 // address. It's okay to split the value loaded from that address. 1947 CanSplitValue = true; 1948 break; 1949 case dwarf::DW_OP_stack_value: 1950 // Bail if this expression computes a value that cannot be split. 1951 if (!CanSplitValue) 1952 return std::nullopt; 1953 break; 1954 case dwarf::DW_OP_LLVM_fragment: { 1955 // Make the new offset point into the existing fragment. 1956 uint64_t FragmentOffsetInBits = Op.getArg(0); 1957 uint64_t FragmentSizeInBits = Op.getArg(1); 1958 (void)FragmentSizeInBits; 1959 assert((OffsetInBits + SizeInBits <= FragmentSizeInBits) && 1960 "new fragment outside of original fragment"); 1961 OffsetInBits += FragmentOffsetInBits; 1962 continue; 1963 } 1964 } 1965 Op.appendToVector(Ops); 1966 } 1967 } 1968 assert((!Expr->isImplicit() || CanSplitValue) && "Expr can't be split"); 1969 assert(Expr && "Unknown DIExpression"); 1970 Ops.push_back(dwarf::DW_OP_LLVM_fragment); 1971 Ops.push_back(OffsetInBits); 1972 Ops.push_back(SizeInBits); 1973 return DIExpression::get(Expr->getContext(), Ops); 1974 } 1975 1976 std::pair<DIExpression *, const ConstantInt *> 1977 DIExpression::constantFold(const ConstantInt *CI) { 1978 // Copy the APInt so we can modify it. 1979 APInt NewInt = CI->getValue(); 1980 SmallVector<uint64_t, 8> Ops; 1981 1982 // Fold operators only at the beginning of the expression. 1983 bool First = true; 1984 bool Changed = false; 1985 for (auto Op : expr_ops()) { 1986 switch (Op.getOp()) { 1987 default: 1988 // We fold only the leading part of the expression; if we get to a part 1989 // that we're going to copy unchanged, and haven't done any folding, 1990 // then the entire expression is unchanged and we can return early. 1991 if (!Changed) 1992 return {this, CI}; 1993 First = false; 1994 break; 1995 case dwarf::DW_OP_LLVM_convert: 1996 if (!First) 1997 break; 1998 Changed = true; 1999 if (Op.getArg(1) == dwarf::DW_ATE_signed) 2000 NewInt = NewInt.sextOrTrunc(Op.getArg(0)); 2001 else { 2002 assert(Op.getArg(1) == dwarf::DW_ATE_unsigned && "Unexpected operand"); 2003 NewInt = NewInt.zextOrTrunc(Op.getArg(0)); 2004 } 2005 continue; 2006 } 2007 Op.appendToVector(Ops); 2008 } 2009 if (!Changed) 2010 return {this, CI}; 2011 return {DIExpression::get(getContext(), Ops), 2012 ConstantInt::get(getContext(), NewInt)}; 2013 } 2014 2015 uint64_t DIExpression::getNumLocationOperands() const { 2016 uint64_t Result = 0; 2017 for (auto ExprOp : expr_ops()) 2018 if (ExprOp.getOp() == dwarf::DW_OP_LLVM_arg) 2019 Result = std::max(Result, ExprOp.getArg(0) + 1); 2020 assert(hasAllLocationOps(Result) && 2021 "Expression is missing one or more location operands."); 2022 return Result; 2023 } 2024 2025 std::optional<DIExpression::SignedOrUnsignedConstant> 2026 DIExpression::isConstant() const { 2027 2028 // Recognize signed and unsigned constants. 2029 // An signed constants can be represented as DW_OP_consts C DW_OP_stack_value 2030 // (DW_OP_LLVM_fragment of Len). 2031 // An unsigned constant can be represented as 2032 // DW_OP_constu C DW_OP_stack_value (DW_OP_LLVM_fragment of Len). 2033 2034 if ((getNumElements() != 2 && getNumElements() != 3 && 2035 getNumElements() != 6) || 2036 (getElement(0) != dwarf::DW_OP_consts && 2037 getElement(0) != dwarf::DW_OP_constu)) 2038 return std::nullopt; 2039 2040 if (getNumElements() == 2 && getElement(0) == dwarf::DW_OP_consts) 2041 return SignedOrUnsignedConstant::SignedConstant; 2042 2043 if ((getNumElements() == 3 && getElement(2) != dwarf::DW_OP_stack_value) || 2044 (getNumElements() == 6 && (getElement(2) != dwarf::DW_OP_stack_value || 2045 getElement(3) != dwarf::DW_OP_LLVM_fragment))) 2046 return std::nullopt; 2047 return getElement(0) == dwarf::DW_OP_constu 2048 ? SignedOrUnsignedConstant::UnsignedConstant 2049 : SignedOrUnsignedConstant::SignedConstant; 2050 } 2051 2052 DIExpression::ExtOps DIExpression::getExtOps(unsigned FromSize, unsigned ToSize, 2053 bool Signed) { 2054 dwarf::TypeKind TK = Signed ? dwarf::DW_ATE_signed : dwarf::DW_ATE_unsigned; 2055 DIExpression::ExtOps Ops{{dwarf::DW_OP_LLVM_convert, FromSize, TK, 2056 dwarf::DW_OP_LLVM_convert, ToSize, TK}}; 2057 return Ops; 2058 } 2059 2060 DIExpression *DIExpression::appendExt(const DIExpression *Expr, 2061 unsigned FromSize, unsigned ToSize, 2062 bool Signed) { 2063 return appendToStack(Expr, getExtOps(FromSize, ToSize, Signed)); 2064 } 2065 2066 DIGlobalVariableExpression * 2067 DIGlobalVariableExpression::getImpl(LLVMContext &Context, Metadata *Variable, 2068 Metadata *Expression, StorageType Storage, 2069 bool ShouldCreate) { 2070 DEFINE_GETIMPL_LOOKUP(DIGlobalVariableExpression, (Variable, Expression)); 2071 Metadata *Ops[] = {Variable, Expression}; 2072 DEFINE_GETIMPL_STORE_NO_CONSTRUCTOR_ARGS(DIGlobalVariableExpression, Ops); 2073 } 2074 DIObjCProperty::DIObjCProperty(LLVMContext &C, StorageType Storage, 2075 unsigned Line, unsigned Attributes, 2076 ArrayRef<Metadata *> Ops) 2077 : DINode(C, DIObjCPropertyKind, Storage, dwarf::DW_TAG_APPLE_property, Ops), 2078 Line(Line), Attributes(Attributes) {} 2079 2080 DIObjCProperty *DIObjCProperty::getImpl( 2081 LLVMContext &Context, MDString *Name, Metadata *File, unsigned Line, 2082 MDString *GetterName, MDString *SetterName, unsigned Attributes, 2083 Metadata *Type, StorageType Storage, bool ShouldCreate) { 2084 assert(isCanonical(Name) && "Expected canonical MDString"); 2085 assert(isCanonical(GetterName) && "Expected canonical MDString"); 2086 assert(isCanonical(SetterName) && "Expected canonical MDString"); 2087 DEFINE_GETIMPL_LOOKUP(DIObjCProperty, (Name, File, Line, GetterName, 2088 SetterName, Attributes, Type)); 2089 Metadata *Ops[] = {Name, File, GetterName, SetterName, Type}; 2090 DEFINE_GETIMPL_STORE(DIObjCProperty, (Line, Attributes), Ops); 2091 } 2092 2093 DIImportedEntity *DIImportedEntity::getImpl(LLVMContext &Context, unsigned Tag, 2094 Metadata *Scope, Metadata *Entity, 2095 Metadata *File, unsigned Line, 2096 MDString *Name, Metadata *Elements, 2097 StorageType Storage, 2098 bool ShouldCreate) { 2099 assert(isCanonical(Name) && "Expected canonical MDString"); 2100 DEFINE_GETIMPL_LOOKUP(DIImportedEntity, 2101 (Tag, Scope, Entity, File, Line, Name, Elements)); 2102 Metadata *Ops[] = {Scope, Entity, Name, File, Elements}; 2103 DEFINE_GETIMPL_STORE(DIImportedEntity, (Tag, Line), Ops); 2104 } 2105 2106 DIMacro *DIMacro::getImpl(LLVMContext &Context, unsigned MIType, unsigned Line, 2107 MDString *Name, MDString *Value, StorageType Storage, 2108 bool ShouldCreate) { 2109 assert(isCanonical(Name) && "Expected canonical MDString"); 2110 DEFINE_GETIMPL_LOOKUP(DIMacro, (MIType, Line, Name, Value)); 2111 Metadata *Ops[] = {Name, Value}; 2112 DEFINE_GETIMPL_STORE(DIMacro, (MIType, Line), Ops); 2113 } 2114 2115 DIMacroFile *DIMacroFile::getImpl(LLVMContext &Context, unsigned MIType, 2116 unsigned Line, Metadata *File, 2117 Metadata *Elements, StorageType Storage, 2118 bool ShouldCreate) { 2119 DEFINE_GETIMPL_LOOKUP(DIMacroFile, (MIType, Line, File, Elements)); 2120 Metadata *Ops[] = {File, Elements}; 2121 DEFINE_GETIMPL_STORE(DIMacroFile, (MIType, Line), Ops); 2122 } 2123 2124 DIArgList *DIArgList::get(LLVMContext &Context, 2125 ArrayRef<ValueAsMetadata *> Args) { 2126 auto ExistingIt = Context.pImpl->DIArgLists.find_as(DIArgListKeyInfo(Args)); 2127 if (ExistingIt != Context.pImpl->DIArgLists.end()) 2128 return *ExistingIt; 2129 DIArgList *NewArgList = new DIArgList(Context, Args); 2130 Context.pImpl->DIArgLists.insert(NewArgList); 2131 return NewArgList; 2132 } 2133 2134 void DIArgList::handleChangedOperand(void *Ref, Metadata *New) { 2135 ValueAsMetadata **OldVMPtr = static_cast<ValueAsMetadata **>(Ref); 2136 assert((!New || isa<ValueAsMetadata>(New)) && 2137 "DIArgList must be passed a ValueAsMetadata"); 2138 untrack(); 2139 // We need to update the set storage once the Args are updated since they 2140 // form the key to the DIArgLists store. 2141 getContext().pImpl->DIArgLists.erase(this); 2142 ValueAsMetadata *NewVM = cast_or_null<ValueAsMetadata>(New); 2143 for (ValueAsMetadata *&VM : Args) { 2144 if (&VM == OldVMPtr) { 2145 if (NewVM) 2146 VM = NewVM; 2147 else 2148 VM = ValueAsMetadata::get(PoisonValue::get(VM->getValue()->getType())); 2149 } 2150 } 2151 // We've changed the contents of this DIArgList, and the set storage may 2152 // already contain a DIArgList with our new set of args; if it does, then we 2153 // must RAUW this with the existing DIArgList, otherwise we simply insert this 2154 // back into the set storage. 2155 DIArgList *ExistingArgList = getUniqued(getContext().pImpl->DIArgLists, this); 2156 if (ExistingArgList) { 2157 replaceAllUsesWith(ExistingArgList); 2158 // Clear this here so we don't try to untrack in the destructor. 2159 Args.clear(); 2160 delete this; 2161 return; 2162 } 2163 getContext().pImpl->DIArgLists.insert(this); 2164 track(); 2165 } 2166 void DIArgList::track() { 2167 for (ValueAsMetadata *&VAM : Args) 2168 if (VAM) 2169 MetadataTracking::track(&VAM, *VAM, *this); 2170 } 2171 void DIArgList::untrack() { 2172 for (ValueAsMetadata *&VAM : Args) 2173 if (VAM) 2174 MetadataTracking::untrack(&VAM, *VAM); 2175 } 2176 void DIArgList::dropAllReferences(bool Untrack) { 2177 if (Untrack) 2178 untrack(); 2179 Args.clear(); 2180 ReplaceableMetadataImpl::resolveAllUses(/* ResolveUsers */ false); 2181 } 2182