1 //===- Metadata.cpp - Implement Metadata classes --------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the Metadata classes. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/IR/Metadata.h" 15 #include "LLVMContextImpl.h" 16 #include "MetadataImpl.h" 17 #include "SymbolTableListTraitsImpl.h" 18 #include "llvm/ADT/STLExtras.h" 19 #include "llvm/ADT/SmallSet.h" 20 #include "llvm/ADT/StringMap.h" 21 #include "llvm/IR/ConstantRange.h" 22 #include "llvm/IR/DebugInfoMetadata.h" 23 #include "llvm/IR/Instruction.h" 24 #include "llvm/IR/LLVMContext.h" 25 #include "llvm/IR/Module.h" 26 #include "llvm/IR/ValueHandle.h" 27 28 using namespace llvm; 29 30 MetadataAsValue::MetadataAsValue(Type *Ty, Metadata *MD) 31 : Value(Ty, MetadataAsValueVal), MD(MD) { 32 track(); 33 } 34 35 MetadataAsValue::~MetadataAsValue() { 36 getType()->getContext().pImpl->MetadataAsValues.erase(MD); 37 untrack(); 38 } 39 40 /// Canonicalize metadata arguments to intrinsics. 41 /// 42 /// To support bitcode upgrades (and assembly semantic sugar) for \a 43 /// MetadataAsValue, we need to canonicalize certain metadata. 44 /// 45 /// - nullptr is replaced by an empty MDNode. 46 /// - An MDNode with a single null operand is replaced by an empty MDNode. 47 /// - An MDNode whose only operand is a \a ConstantAsMetadata gets skipped. 48 /// 49 /// This maintains readability of bitcode from when metadata was a type of 50 /// value, and these bridges were unnecessary. 51 static Metadata *canonicalizeMetadataForValue(LLVMContext &Context, 52 Metadata *MD) { 53 if (!MD) 54 // !{} 55 return MDNode::get(Context, None); 56 57 // Return early if this isn't a single-operand MDNode. 58 auto *N = dyn_cast<MDNode>(MD); 59 if (!N || N->getNumOperands() != 1) 60 return MD; 61 62 if (!N->getOperand(0)) 63 // !{} 64 return MDNode::get(Context, None); 65 66 if (auto *C = dyn_cast<ConstantAsMetadata>(N->getOperand(0))) 67 // Look through the MDNode. 68 return C; 69 70 return MD; 71 } 72 73 MetadataAsValue *MetadataAsValue::get(LLVMContext &Context, Metadata *MD) { 74 MD = canonicalizeMetadataForValue(Context, MD); 75 auto *&Entry = Context.pImpl->MetadataAsValues[MD]; 76 if (!Entry) 77 Entry = new MetadataAsValue(Type::getMetadataTy(Context), MD); 78 return Entry; 79 } 80 81 MetadataAsValue *MetadataAsValue::getIfExists(LLVMContext &Context, 82 Metadata *MD) { 83 MD = canonicalizeMetadataForValue(Context, MD); 84 auto &Store = Context.pImpl->MetadataAsValues; 85 return Store.lookup(MD); 86 } 87 88 void MetadataAsValue::handleChangedMetadata(Metadata *MD) { 89 LLVMContext &Context = getContext(); 90 MD = canonicalizeMetadataForValue(Context, MD); 91 auto &Store = Context.pImpl->MetadataAsValues; 92 93 // Stop tracking the old metadata. 94 Store.erase(this->MD); 95 untrack(); 96 this->MD = nullptr; 97 98 // Start tracking MD, or RAUW if necessary. 99 auto *&Entry = Store[MD]; 100 if (Entry) { 101 replaceAllUsesWith(Entry); 102 delete this; 103 return; 104 } 105 106 this->MD = MD; 107 track(); 108 Entry = this; 109 } 110 111 void MetadataAsValue::track() { 112 if (MD) 113 MetadataTracking::track(&MD, *MD, *this); 114 } 115 116 void MetadataAsValue::untrack() { 117 if (MD) 118 MetadataTracking::untrack(MD); 119 } 120 121 bool MetadataTracking::track(void *Ref, Metadata &MD, OwnerTy Owner) { 122 assert(Ref && "Expected live reference"); 123 assert((Owner || *static_cast<Metadata **>(Ref) == &MD) && 124 "Reference without owner must be direct"); 125 if (auto *R = ReplaceableMetadataImpl::getOrCreate(MD)) { 126 R->addRef(Ref, Owner); 127 return true; 128 } 129 return false; 130 } 131 132 void MetadataTracking::untrack(void *Ref, Metadata &MD) { 133 assert(Ref && "Expected live reference"); 134 if (auto *R = ReplaceableMetadataImpl::getIfExists(MD)) 135 R->dropRef(Ref); 136 } 137 138 bool MetadataTracking::retrack(void *Ref, Metadata &MD, void *New) { 139 assert(Ref && "Expected live reference"); 140 assert(New && "Expected live reference"); 141 assert(Ref != New && "Expected change"); 142 if (auto *R = ReplaceableMetadataImpl::getIfExists(MD)) { 143 R->moveRef(Ref, New, MD); 144 return true; 145 } 146 assert(!isReplaceable(MD) && 147 "Expected un-replaceable metadata, since we didn't move a reference"); 148 return false; 149 } 150 151 bool MetadataTracking::isReplaceable(const Metadata &MD) { 152 return ReplaceableMetadataImpl::isReplaceable(MD); 153 } 154 155 void ReplaceableMetadataImpl::addRef(void *Ref, OwnerTy Owner) { 156 bool WasInserted = 157 UseMap.insert(std::make_pair(Ref, std::make_pair(Owner, NextIndex))) 158 .second; 159 (void)WasInserted; 160 assert(WasInserted && "Expected to add a reference"); 161 162 ++NextIndex; 163 assert(NextIndex != 0 && "Unexpected overflow"); 164 } 165 166 void ReplaceableMetadataImpl::dropRef(void *Ref) { 167 bool WasErased = UseMap.erase(Ref); 168 (void)WasErased; 169 assert(WasErased && "Expected to drop a reference"); 170 } 171 172 void ReplaceableMetadataImpl::moveRef(void *Ref, void *New, 173 const Metadata &MD) { 174 auto I = UseMap.find(Ref); 175 assert(I != UseMap.end() && "Expected to move a reference"); 176 auto OwnerAndIndex = I->second; 177 UseMap.erase(I); 178 bool WasInserted = UseMap.insert(std::make_pair(New, OwnerAndIndex)).second; 179 (void)WasInserted; 180 assert(WasInserted && "Expected to add a reference"); 181 182 // Check that the references are direct if there's no owner. 183 (void)MD; 184 assert((OwnerAndIndex.first || *static_cast<Metadata **>(Ref) == &MD) && 185 "Reference without owner must be direct"); 186 assert((OwnerAndIndex.first || *static_cast<Metadata **>(New) == &MD) && 187 "Reference without owner must be direct"); 188 } 189 190 void ReplaceableMetadataImpl::replaceAllUsesWith(Metadata *MD) { 191 if (UseMap.empty()) 192 return; 193 194 // Copy out uses since UseMap will get touched below. 195 typedef std::pair<void *, std::pair<OwnerTy, uint64_t>> UseTy; 196 SmallVector<UseTy, 8> Uses(UseMap.begin(), UseMap.end()); 197 std::sort(Uses.begin(), Uses.end(), [](const UseTy &L, const UseTy &R) { 198 return L.second.second < R.second.second; 199 }); 200 for (const auto &Pair : Uses) { 201 // Check that this Ref hasn't disappeared after RAUW (when updating a 202 // previous Ref). 203 if (!UseMap.count(Pair.first)) 204 continue; 205 206 OwnerTy Owner = Pair.second.first; 207 if (!Owner) { 208 // Update unowned tracking references directly. 209 Metadata *&Ref = *static_cast<Metadata **>(Pair.first); 210 Ref = MD; 211 if (MD) 212 MetadataTracking::track(Ref); 213 UseMap.erase(Pair.first); 214 continue; 215 } 216 217 // Check for MetadataAsValue. 218 if (Owner.is<MetadataAsValue *>()) { 219 Owner.get<MetadataAsValue *>()->handleChangedMetadata(MD); 220 continue; 221 } 222 223 // There's a Metadata owner -- dispatch. 224 Metadata *OwnerMD = Owner.get<Metadata *>(); 225 switch (OwnerMD->getMetadataID()) { 226 #define HANDLE_METADATA_LEAF(CLASS) \ 227 case Metadata::CLASS##Kind: \ 228 cast<CLASS>(OwnerMD)->handleChangedOperand(Pair.first, MD); \ 229 continue; 230 #include "llvm/IR/Metadata.def" 231 default: 232 llvm_unreachable("Invalid metadata subclass"); 233 } 234 } 235 assert(UseMap.empty() && "Expected all uses to be replaced"); 236 } 237 238 void ReplaceableMetadataImpl::resolveAllUses(bool ResolveUsers) { 239 if (UseMap.empty()) 240 return; 241 242 if (!ResolveUsers) { 243 UseMap.clear(); 244 return; 245 } 246 247 // Copy out uses since UseMap could get touched below. 248 typedef std::pair<void *, std::pair<OwnerTy, uint64_t>> UseTy; 249 SmallVector<UseTy, 8> Uses(UseMap.begin(), UseMap.end()); 250 std::sort(Uses.begin(), Uses.end(), [](const UseTy &L, const UseTy &R) { 251 return L.second.second < R.second.second; 252 }); 253 UseMap.clear(); 254 for (const auto &Pair : Uses) { 255 auto Owner = Pair.second.first; 256 if (!Owner) 257 continue; 258 if (Owner.is<MetadataAsValue *>()) 259 continue; 260 261 // Resolve MDNodes that point at this. 262 auto *OwnerMD = dyn_cast<MDNode>(Owner.get<Metadata *>()); 263 if (!OwnerMD) 264 continue; 265 if (OwnerMD->isResolved()) 266 continue; 267 OwnerMD->decrementUnresolvedOperandCount(); 268 } 269 } 270 271 ReplaceableMetadataImpl *ReplaceableMetadataImpl::getOrCreate(Metadata &MD) { 272 if (auto *N = dyn_cast<MDNode>(&MD)) 273 return N->isResolved() ? nullptr : N->Context.getOrCreateReplaceableUses(); 274 return dyn_cast<ValueAsMetadata>(&MD); 275 } 276 277 ReplaceableMetadataImpl *ReplaceableMetadataImpl::getIfExists(Metadata &MD) { 278 if (auto *N = dyn_cast<MDNode>(&MD)) 279 return N->isResolved() ? nullptr : N->Context.getReplaceableUses(); 280 return dyn_cast<ValueAsMetadata>(&MD); 281 } 282 283 bool ReplaceableMetadataImpl::isReplaceable(const Metadata &MD) { 284 if (auto *N = dyn_cast<MDNode>(&MD)) 285 return !N->isResolved(); 286 return dyn_cast<ValueAsMetadata>(&MD); 287 } 288 289 static Function *getLocalFunction(Value *V) { 290 assert(V && "Expected value"); 291 if (auto *A = dyn_cast<Argument>(V)) 292 return A->getParent(); 293 if (BasicBlock *BB = cast<Instruction>(V)->getParent()) 294 return BB->getParent(); 295 return nullptr; 296 } 297 298 ValueAsMetadata *ValueAsMetadata::get(Value *V) { 299 assert(V && "Unexpected null Value"); 300 301 auto &Context = V->getContext(); 302 auto *&Entry = Context.pImpl->ValuesAsMetadata[V]; 303 if (!Entry) { 304 assert((isa<Constant>(V) || isa<Argument>(V) || isa<Instruction>(V)) && 305 "Expected constant or function-local value"); 306 assert(!V->IsUsedByMD && 307 "Expected this to be the only metadata use"); 308 V->IsUsedByMD = true; 309 if (auto *C = dyn_cast<Constant>(V)) 310 Entry = new ConstantAsMetadata(C); 311 else 312 Entry = new LocalAsMetadata(V); 313 } 314 315 return Entry; 316 } 317 318 ValueAsMetadata *ValueAsMetadata::getIfExists(Value *V) { 319 assert(V && "Unexpected null Value"); 320 return V->getContext().pImpl->ValuesAsMetadata.lookup(V); 321 } 322 323 void ValueAsMetadata::handleDeletion(Value *V) { 324 assert(V && "Expected valid value"); 325 326 auto &Store = V->getType()->getContext().pImpl->ValuesAsMetadata; 327 auto I = Store.find(V); 328 if (I == Store.end()) 329 return; 330 331 // Remove old entry from the map. 332 ValueAsMetadata *MD = I->second; 333 assert(MD && "Expected valid metadata"); 334 assert(MD->getValue() == V && "Expected valid mapping"); 335 Store.erase(I); 336 337 // Delete the metadata. 338 MD->replaceAllUsesWith(nullptr); 339 delete MD; 340 } 341 342 void ValueAsMetadata::handleRAUW(Value *From, Value *To) { 343 assert(From && "Expected valid value"); 344 assert(To && "Expected valid value"); 345 assert(From != To && "Expected changed value"); 346 assert(From->getType() == To->getType() && "Unexpected type change"); 347 348 LLVMContext &Context = From->getType()->getContext(); 349 auto &Store = Context.pImpl->ValuesAsMetadata; 350 auto I = Store.find(From); 351 if (I == Store.end()) { 352 assert(!From->IsUsedByMD && 353 "Expected From not to be used by metadata"); 354 return; 355 } 356 357 // Remove old entry from the map. 358 assert(From->IsUsedByMD && 359 "Expected From to be used by metadata"); 360 From->IsUsedByMD = false; 361 ValueAsMetadata *MD = I->second; 362 assert(MD && "Expected valid metadata"); 363 assert(MD->getValue() == From && "Expected valid mapping"); 364 Store.erase(I); 365 366 if (isa<LocalAsMetadata>(MD)) { 367 if (auto *C = dyn_cast<Constant>(To)) { 368 // Local became a constant. 369 MD->replaceAllUsesWith(ConstantAsMetadata::get(C)); 370 delete MD; 371 return; 372 } 373 if (getLocalFunction(From) && getLocalFunction(To) && 374 getLocalFunction(From) != getLocalFunction(To)) { 375 // Function changed. 376 MD->replaceAllUsesWith(nullptr); 377 delete MD; 378 return; 379 } 380 } else if (!isa<Constant>(To)) { 381 // Changed to function-local value. 382 MD->replaceAllUsesWith(nullptr); 383 delete MD; 384 return; 385 } 386 387 auto *&Entry = Store[To]; 388 if (Entry) { 389 // The target already exists. 390 MD->replaceAllUsesWith(Entry); 391 delete MD; 392 return; 393 } 394 395 // Update MD in place (and update the map entry). 396 assert(!To->IsUsedByMD && 397 "Expected this to be the only metadata use"); 398 To->IsUsedByMD = true; 399 MD->V = To; 400 Entry = MD; 401 } 402 403 //===----------------------------------------------------------------------===// 404 // MDString implementation. 405 // 406 407 MDString *MDString::get(LLVMContext &Context, StringRef Str) { 408 auto &Store = Context.pImpl->MDStringCache; 409 auto I = Store.emplace_second(Str); 410 auto &MapEntry = I.first->getValue(); 411 if (!I.second) 412 return &MapEntry; 413 MapEntry.Entry = &*I.first; 414 return &MapEntry; 415 } 416 417 StringRef MDString::getString() const { 418 assert(Entry && "Expected to find string map entry"); 419 return Entry->first(); 420 } 421 422 //===----------------------------------------------------------------------===// 423 // MDNode implementation. 424 // 425 426 // Assert that the MDNode types will not be unaligned by the objects 427 // prepended to them. 428 #define HANDLE_MDNODE_LEAF(CLASS) \ 429 static_assert( \ 430 llvm::AlignOf<uint64_t>::Alignment >= llvm::AlignOf<CLASS>::Alignment, \ 431 "Alignment is insufficient after objects prepended to " #CLASS); 432 #include "llvm/IR/Metadata.def" 433 434 void *MDNode::operator new(size_t Size, unsigned NumOps) { 435 size_t OpSize = NumOps * sizeof(MDOperand); 436 // uint64_t is the most aligned type we need support (ensured by static_assert 437 // above) 438 OpSize = alignTo(OpSize, llvm::alignOf<uint64_t>()); 439 void *Ptr = reinterpret_cast<char *>(::operator new(OpSize + Size)) + OpSize; 440 MDOperand *O = static_cast<MDOperand *>(Ptr); 441 for (MDOperand *E = O - NumOps; O != E; --O) 442 (void)new (O - 1) MDOperand; 443 return Ptr; 444 } 445 446 void MDNode::operator delete(void *Mem) { 447 MDNode *N = static_cast<MDNode *>(Mem); 448 size_t OpSize = N->NumOperands * sizeof(MDOperand); 449 OpSize = alignTo(OpSize, llvm::alignOf<uint64_t>()); 450 451 MDOperand *O = static_cast<MDOperand *>(Mem); 452 for (MDOperand *E = O - N->NumOperands; O != E; --O) 453 (O - 1)->~MDOperand(); 454 ::operator delete(reinterpret_cast<char *>(Mem) - OpSize); 455 } 456 457 MDNode::MDNode(LLVMContext &Context, unsigned ID, StorageType Storage, 458 ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2) 459 : Metadata(ID, Storage), NumOperands(Ops1.size() + Ops2.size()), 460 NumUnresolved(0), Context(Context) { 461 unsigned Op = 0; 462 for (Metadata *MD : Ops1) 463 setOperand(Op++, MD); 464 for (Metadata *MD : Ops2) 465 setOperand(Op++, MD); 466 467 if (!isUniqued()) 468 return; 469 470 // Count the unresolved operands. If there are any, RAUW support will be 471 // added lazily on first reference. 472 countUnresolvedOperands(); 473 } 474 475 TempMDNode MDNode::clone() const { 476 switch (getMetadataID()) { 477 default: 478 llvm_unreachable("Invalid MDNode subclass"); 479 #define HANDLE_MDNODE_LEAF(CLASS) \ 480 case CLASS##Kind: \ 481 return cast<CLASS>(this)->cloneImpl(); 482 #include "llvm/IR/Metadata.def" 483 } 484 } 485 486 static bool isOperandUnresolved(Metadata *Op) { 487 if (auto *N = dyn_cast_or_null<MDNode>(Op)) 488 return !N->isResolved(); 489 return false; 490 } 491 492 void MDNode::countUnresolvedOperands() { 493 assert(NumUnresolved == 0 && "Expected unresolved ops to be uncounted"); 494 assert(isUniqued() && "Expected this to be uniqued"); 495 NumUnresolved = std::count_if(op_begin(), op_end(), isOperandUnresolved); 496 } 497 498 void MDNode::makeUniqued() { 499 assert(isTemporary() && "Expected this to be temporary"); 500 assert(!isResolved() && "Expected this to be unresolved"); 501 502 // Enable uniquing callbacks. 503 for (auto &Op : mutable_operands()) 504 Op.reset(Op.get(), this); 505 506 // Make this 'uniqued'. 507 Storage = Uniqued; 508 countUnresolvedOperands(); 509 if (!NumUnresolved) { 510 dropReplaceableUses(); 511 assert(isResolved() && "Expected this to be resolved"); 512 } 513 514 assert(isUniqued() && "Expected this to be uniqued"); 515 } 516 517 void MDNode::makeDistinct() { 518 assert(isTemporary() && "Expected this to be temporary"); 519 assert(!isResolved() && "Expected this to be unresolved"); 520 521 // Drop RAUW support and store as a distinct node. 522 dropReplaceableUses(); 523 storeDistinctInContext(); 524 525 assert(isDistinct() && "Expected this to be distinct"); 526 assert(isResolved() && "Expected this to be resolved"); 527 } 528 529 void MDNode::resolve() { 530 assert(isUniqued() && "Expected this to be uniqued"); 531 assert(!isResolved() && "Expected this to be unresolved"); 532 533 NumUnresolved = 0; 534 dropReplaceableUses(); 535 536 assert(isResolved() && "Expected this to be resolved"); 537 } 538 539 void MDNode::dropReplaceableUses() { 540 assert(!NumUnresolved && "Unexpected unresolved operand"); 541 542 // Drop any RAUW support. 543 if (Context.hasReplaceableUses()) 544 Context.takeReplaceableUses()->resolveAllUses(); 545 } 546 547 void MDNode::resolveAfterOperandChange(Metadata *Old, Metadata *New) { 548 assert(isUniqued() && "Expected this to be uniqued"); 549 assert(NumUnresolved != 0 && "Expected unresolved operands"); 550 551 // Check if an operand was resolved. 552 if (!isOperandUnresolved(Old)) { 553 if (isOperandUnresolved(New)) 554 // An operand was un-resolved! 555 ++NumUnresolved; 556 } else if (!isOperandUnresolved(New)) 557 decrementUnresolvedOperandCount(); 558 } 559 560 void MDNode::decrementUnresolvedOperandCount() { 561 assert(!isResolved() && "Expected this to be unresolved"); 562 if (isTemporary()) 563 return; 564 565 assert(isUniqued() && "Expected this to be uniqued"); 566 if (--NumUnresolved) 567 return; 568 569 // Last unresolved operand has just been resolved. 570 dropReplaceableUses(); 571 assert(isResolved() && "Expected this to become resolved"); 572 } 573 574 void MDNode::resolveCycles() { 575 if (isResolved()) 576 return; 577 578 // Resolve this node immediately. 579 resolve(); 580 581 // Resolve all operands. 582 for (const auto &Op : operands()) { 583 auto *N = dyn_cast_or_null<MDNode>(Op); 584 if (!N) 585 continue; 586 587 assert(!N->isTemporary() && 588 "Expected all forward declarations to be resolved"); 589 if (!N->isResolved()) 590 N->resolveCycles(); 591 } 592 } 593 594 static bool hasSelfReference(MDNode *N) { 595 for (Metadata *MD : N->operands()) 596 if (MD == N) 597 return true; 598 return false; 599 } 600 601 MDNode *MDNode::replaceWithPermanentImpl() { 602 switch (getMetadataID()) { 603 default: 604 // If this type isn't uniquable, replace with a distinct node. 605 return replaceWithDistinctImpl(); 606 607 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \ 608 case CLASS##Kind: \ 609 break; 610 #include "llvm/IR/Metadata.def" 611 } 612 613 // Even if this type is uniquable, self-references have to be distinct. 614 if (hasSelfReference(this)) 615 return replaceWithDistinctImpl(); 616 return replaceWithUniquedImpl(); 617 } 618 619 MDNode *MDNode::replaceWithUniquedImpl() { 620 // Try to uniquify in place. 621 MDNode *UniquedNode = uniquify(); 622 623 if (UniquedNode == this) { 624 makeUniqued(); 625 return this; 626 } 627 628 // Collision, so RAUW instead. 629 replaceAllUsesWith(UniquedNode); 630 deleteAsSubclass(); 631 return UniquedNode; 632 } 633 634 MDNode *MDNode::replaceWithDistinctImpl() { 635 makeDistinct(); 636 return this; 637 } 638 639 void MDTuple::recalculateHash() { 640 setHash(MDTupleInfo::KeyTy::calculateHash(this)); 641 } 642 643 void MDNode::dropAllReferences() { 644 for (unsigned I = 0, E = NumOperands; I != E; ++I) 645 setOperand(I, nullptr); 646 if (Context.hasReplaceableUses()) { 647 Context.getReplaceableUses()->resolveAllUses(/* ResolveUsers */ false); 648 (void)Context.takeReplaceableUses(); 649 } 650 } 651 652 void MDNode::handleChangedOperand(void *Ref, Metadata *New) { 653 unsigned Op = static_cast<MDOperand *>(Ref) - op_begin(); 654 assert(Op < getNumOperands() && "Expected valid operand"); 655 656 if (!isUniqued()) { 657 // This node is not uniqued. Just set the operand and be done with it. 658 setOperand(Op, New); 659 return; 660 } 661 662 // This node is uniqued. 663 eraseFromStore(); 664 665 Metadata *Old = getOperand(Op); 666 setOperand(Op, New); 667 668 // Drop uniquing for self-reference cycles. 669 if (New == this) { 670 if (!isResolved()) 671 resolve(); 672 storeDistinctInContext(); 673 return; 674 } 675 676 // Re-unique the node. 677 auto *Uniqued = uniquify(); 678 if (Uniqued == this) { 679 if (!isResolved()) 680 resolveAfterOperandChange(Old, New); 681 return; 682 } 683 684 // Collision. 685 if (!isResolved()) { 686 // Still unresolved, so RAUW. 687 // 688 // First, clear out all operands to prevent any recursion (similar to 689 // dropAllReferences(), but we still need the use-list). 690 for (unsigned O = 0, E = getNumOperands(); O != E; ++O) 691 setOperand(O, nullptr); 692 if (Context.hasReplaceableUses()) 693 Context.getReplaceableUses()->replaceAllUsesWith(Uniqued); 694 deleteAsSubclass(); 695 return; 696 } 697 698 // Store in non-uniqued form if RAUW isn't possible. 699 storeDistinctInContext(); 700 } 701 702 void MDNode::deleteAsSubclass() { 703 switch (getMetadataID()) { 704 default: 705 llvm_unreachable("Invalid subclass of MDNode"); 706 #define HANDLE_MDNODE_LEAF(CLASS) \ 707 case CLASS##Kind: \ 708 delete cast<CLASS>(this); \ 709 break; 710 #include "llvm/IR/Metadata.def" 711 } 712 } 713 714 template <class T, class InfoT> 715 static T *uniquifyImpl(T *N, DenseSet<T *, InfoT> &Store) { 716 if (T *U = getUniqued(Store, N)) 717 return U; 718 719 Store.insert(N); 720 return N; 721 } 722 723 template <class NodeTy> struct MDNode::HasCachedHash { 724 typedef char Yes[1]; 725 typedef char No[2]; 726 template <class U, U Val> struct SFINAE {}; 727 728 template <class U> 729 static Yes &check(SFINAE<void (U::*)(unsigned), &U::setHash> *); 730 template <class U> static No &check(...); 731 732 static const bool value = sizeof(check<NodeTy>(nullptr)) == sizeof(Yes); 733 }; 734 735 MDNode *MDNode::uniquify() { 736 assert(!hasSelfReference(this) && "Cannot uniquify a self-referencing node"); 737 738 // Try to insert into uniquing store. 739 switch (getMetadataID()) { 740 default: 741 llvm_unreachable("Invalid or non-uniquable subclass of MDNode"); 742 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \ 743 case CLASS##Kind: { \ 744 CLASS *SubclassThis = cast<CLASS>(this); \ 745 std::integral_constant<bool, HasCachedHash<CLASS>::value> \ 746 ShouldRecalculateHash; \ 747 dispatchRecalculateHash(SubclassThis, ShouldRecalculateHash); \ 748 return uniquifyImpl(SubclassThis, getContext().pImpl->CLASS##s); \ 749 } 750 #include "llvm/IR/Metadata.def" 751 } 752 } 753 754 void MDNode::eraseFromStore() { 755 switch (getMetadataID()) { 756 default: 757 llvm_unreachable("Invalid or non-uniquable subclass of MDNode"); 758 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \ 759 case CLASS##Kind: \ 760 getContext().pImpl->CLASS##s.erase(cast<CLASS>(this)); \ 761 break; 762 #include "llvm/IR/Metadata.def" 763 } 764 } 765 766 MDTuple *MDTuple::getImpl(LLVMContext &Context, ArrayRef<Metadata *> MDs, 767 StorageType Storage, bool ShouldCreate) { 768 unsigned Hash = 0; 769 if (Storage == Uniqued) { 770 MDTupleInfo::KeyTy Key(MDs); 771 if (auto *N = getUniqued(Context.pImpl->MDTuples, Key)) 772 return N; 773 if (!ShouldCreate) 774 return nullptr; 775 Hash = Key.getHash(); 776 } else { 777 assert(ShouldCreate && "Expected non-uniqued nodes to always be created"); 778 } 779 780 return storeImpl(new (MDs.size()) MDTuple(Context, Storage, Hash, MDs), 781 Storage, Context.pImpl->MDTuples); 782 } 783 784 void MDNode::deleteTemporary(MDNode *N) { 785 assert(N->isTemporary() && "Expected temporary node"); 786 N->replaceAllUsesWith(nullptr); 787 N->deleteAsSubclass(); 788 } 789 790 void MDNode::storeDistinctInContext() { 791 assert(!Context.hasReplaceableUses() && "Unexpected replaceable uses"); 792 assert(!NumUnresolved && "Unexpected unresolved nodes"); 793 Storage = Distinct; 794 assert(isResolved() && "Expected this to be resolved"); 795 796 // Reset the hash. 797 switch (getMetadataID()) { 798 default: 799 llvm_unreachable("Invalid subclass of MDNode"); 800 #define HANDLE_MDNODE_LEAF(CLASS) \ 801 case CLASS##Kind: { \ 802 std::integral_constant<bool, HasCachedHash<CLASS>::value> ShouldResetHash; \ 803 dispatchResetHash(cast<CLASS>(this), ShouldResetHash); \ 804 break; \ 805 } 806 #include "llvm/IR/Metadata.def" 807 } 808 809 getContext().pImpl->DistinctMDNodes.insert(this); 810 } 811 812 void MDNode::replaceOperandWith(unsigned I, Metadata *New) { 813 if (getOperand(I) == New) 814 return; 815 816 if (!isUniqued()) { 817 setOperand(I, New); 818 return; 819 } 820 821 handleChangedOperand(mutable_begin() + I, New); 822 } 823 824 void MDNode::setOperand(unsigned I, Metadata *New) { 825 assert(I < NumOperands); 826 mutable_begin()[I].reset(New, isUniqued() ? this : nullptr); 827 } 828 829 /// Get a node or a self-reference that looks like it. 830 /// 831 /// Special handling for finding self-references, for use by \a 832 /// MDNode::concatenate() and \a MDNode::intersect() to maintain behaviour from 833 /// when self-referencing nodes were still uniqued. If the first operand has 834 /// the same operands as \c Ops, return the first operand instead. 835 static MDNode *getOrSelfReference(LLVMContext &Context, 836 ArrayRef<Metadata *> Ops) { 837 if (!Ops.empty()) 838 if (MDNode *N = dyn_cast_or_null<MDNode>(Ops[0])) 839 if (N->getNumOperands() == Ops.size() && N == N->getOperand(0)) { 840 for (unsigned I = 1, E = Ops.size(); I != E; ++I) 841 if (Ops[I] != N->getOperand(I)) 842 return MDNode::get(Context, Ops); 843 return N; 844 } 845 846 return MDNode::get(Context, Ops); 847 } 848 849 MDNode *MDNode::concatenate(MDNode *A, MDNode *B) { 850 if (!A) 851 return B; 852 if (!B) 853 return A; 854 855 SmallVector<Metadata *, 4> MDs; 856 MDs.reserve(A->getNumOperands() + B->getNumOperands()); 857 MDs.append(A->op_begin(), A->op_end()); 858 MDs.append(B->op_begin(), B->op_end()); 859 860 // FIXME: This preserves long-standing behaviour, but is it really the right 861 // behaviour? Or was that an unintended side-effect of node uniquing? 862 return getOrSelfReference(A->getContext(), MDs); 863 } 864 865 MDNode *MDNode::intersect(MDNode *A, MDNode *B) { 866 if (!A || !B) 867 return nullptr; 868 869 SmallVector<Metadata *, 4> MDs; 870 for (Metadata *MD : A->operands()) 871 if (std::find(B->op_begin(), B->op_end(), MD) != B->op_end()) 872 MDs.push_back(MD); 873 874 // FIXME: This preserves long-standing behaviour, but is it really the right 875 // behaviour? Or was that an unintended side-effect of node uniquing? 876 return getOrSelfReference(A->getContext(), MDs); 877 } 878 879 MDNode *MDNode::getMostGenericAliasScope(MDNode *A, MDNode *B) { 880 if (!A || !B) 881 return nullptr; 882 883 SmallVector<Metadata *, 4> MDs(B->op_begin(), B->op_end()); 884 for (Metadata *MD : A->operands()) 885 if (std::find(B->op_begin(), B->op_end(), MD) == B->op_end()) 886 MDs.push_back(MD); 887 888 // FIXME: This preserves long-standing behaviour, but is it really the right 889 // behaviour? Or was that an unintended side-effect of node uniquing? 890 return getOrSelfReference(A->getContext(), MDs); 891 } 892 893 MDNode *MDNode::getMostGenericFPMath(MDNode *A, MDNode *B) { 894 if (!A || !B) 895 return nullptr; 896 897 APFloat AVal = mdconst::extract<ConstantFP>(A->getOperand(0))->getValueAPF(); 898 APFloat BVal = mdconst::extract<ConstantFP>(B->getOperand(0))->getValueAPF(); 899 if (AVal.compare(BVal) == APFloat::cmpLessThan) 900 return A; 901 return B; 902 } 903 904 static bool isContiguous(const ConstantRange &A, const ConstantRange &B) { 905 return A.getUpper() == B.getLower() || A.getLower() == B.getUpper(); 906 } 907 908 static bool canBeMerged(const ConstantRange &A, const ConstantRange &B) { 909 return !A.intersectWith(B).isEmptySet() || isContiguous(A, B); 910 } 911 912 static bool tryMergeRange(SmallVectorImpl<ConstantInt *> &EndPoints, 913 ConstantInt *Low, ConstantInt *High) { 914 ConstantRange NewRange(Low->getValue(), High->getValue()); 915 unsigned Size = EndPoints.size(); 916 APInt LB = EndPoints[Size - 2]->getValue(); 917 APInt LE = EndPoints[Size - 1]->getValue(); 918 ConstantRange LastRange(LB, LE); 919 if (canBeMerged(NewRange, LastRange)) { 920 ConstantRange Union = LastRange.unionWith(NewRange); 921 Type *Ty = High->getType(); 922 EndPoints[Size - 2] = 923 cast<ConstantInt>(ConstantInt::get(Ty, Union.getLower())); 924 EndPoints[Size - 1] = 925 cast<ConstantInt>(ConstantInt::get(Ty, Union.getUpper())); 926 return true; 927 } 928 return false; 929 } 930 931 static void addRange(SmallVectorImpl<ConstantInt *> &EndPoints, 932 ConstantInt *Low, ConstantInt *High) { 933 if (!EndPoints.empty()) 934 if (tryMergeRange(EndPoints, Low, High)) 935 return; 936 937 EndPoints.push_back(Low); 938 EndPoints.push_back(High); 939 } 940 941 MDNode *MDNode::getMostGenericRange(MDNode *A, MDNode *B) { 942 // Given two ranges, we want to compute the union of the ranges. This 943 // is slightly complitade by having to combine the intervals and merge 944 // the ones that overlap. 945 946 if (!A || !B) 947 return nullptr; 948 949 if (A == B) 950 return A; 951 952 // First, walk both lists in older of the lower boundary of each interval. 953 // At each step, try to merge the new interval to the last one we adedd. 954 SmallVector<ConstantInt *, 4> EndPoints; 955 int AI = 0; 956 int BI = 0; 957 int AN = A->getNumOperands() / 2; 958 int BN = B->getNumOperands() / 2; 959 while (AI < AN && BI < BN) { 960 ConstantInt *ALow = mdconst::extract<ConstantInt>(A->getOperand(2 * AI)); 961 ConstantInt *BLow = mdconst::extract<ConstantInt>(B->getOperand(2 * BI)); 962 963 if (ALow->getValue().slt(BLow->getValue())) { 964 addRange(EndPoints, ALow, 965 mdconst::extract<ConstantInt>(A->getOperand(2 * AI + 1))); 966 ++AI; 967 } else { 968 addRange(EndPoints, BLow, 969 mdconst::extract<ConstantInt>(B->getOperand(2 * BI + 1))); 970 ++BI; 971 } 972 } 973 while (AI < AN) { 974 addRange(EndPoints, mdconst::extract<ConstantInt>(A->getOperand(2 * AI)), 975 mdconst::extract<ConstantInt>(A->getOperand(2 * AI + 1))); 976 ++AI; 977 } 978 while (BI < BN) { 979 addRange(EndPoints, mdconst::extract<ConstantInt>(B->getOperand(2 * BI)), 980 mdconst::extract<ConstantInt>(B->getOperand(2 * BI + 1))); 981 ++BI; 982 } 983 984 // If we have more than 2 ranges (4 endpoints) we have to try to merge 985 // the last and first ones. 986 unsigned Size = EndPoints.size(); 987 if (Size > 4) { 988 ConstantInt *FB = EndPoints[0]; 989 ConstantInt *FE = EndPoints[1]; 990 if (tryMergeRange(EndPoints, FB, FE)) { 991 for (unsigned i = 0; i < Size - 2; ++i) { 992 EndPoints[i] = EndPoints[i + 2]; 993 } 994 EndPoints.resize(Size - 2); 995 } 996 } 997 998 // If in the end we have a single range, it is possible that it is now the 999 // full range. Just drop the metadata in that case. 1000 if (EndPoints.size() == 2) { 1001 ConstantRange Range(EndPoints[0]->getValue(), EndPoints[1]->getValue()); 1002 if (Range.isFullSet()) 1003 return nullptr; 1004 } 1005 1006 SmallVector<Metadata *, 4> MDs; 1007 MDs.reserve(EndPoints.size()); 1008 for (auto *I : EndPoints) 1009 MDs.push_back(ConstantAsMetadata::get(I)); 1010 return MDNode::get(A->getContext(), MDs); 1011 } 1012 1013 MDNode *MDNode::getMostGenericAlignmentOrDereferenceable(MDNode *A, MDNode *B) { 1014 if (!A || !B) 1015 return nullptr; 1016 1017 ConstantInt *AVal = mdconst::extract<ConstantInt>(A->getOperand(0)); 1018 ConstantInt *BVal = mdconst::extract<ConstantInt>(B->getOperand(0)); 1019 if (AVal->getZExtValue() < BVal->getZExtValue()) 1020 return A; 1021 return B; 1022 } 1023 1024 //===----------------------------------------------------------------------===// 1025 // NamedMDNode implementation. 1026 // 1027 1028 static SmallVector<TrackingMDRef, 4> &getNMDOps(void *Operands) { 1029 return *(SmallVector<TrackingMDRef, 4> *)Operands; 1030 } 1031 1032 NamedMDNode::NamedMDNode(const Twine &N) 1033 : Name(N.str()), Parent(nullptr), 1034 Operands(new SmallVector<TrackingMDRef, 4>()) {} 1035 1036 NamedMDNode::~NamedMDNode() { 1037 dropAllReferences(); 1038 delete &getNMDOps(Operands); 1039 } 1040 1041 unsigned NamedMDNode::getNumOperands() const { 1042 return (unsigned)getNMDOps(Operands).size(); 1043 } 1044 1045 MDNode *NamedMDNode::getOperand(unsigned i) const { 1046 assert(i < getNumOperands() && "Invalid Operand number!"); 1047 auto *N = getNMDOps(Operands)[i].get(); 1048 return cast_or_null<MDNode>(N); 1049 } 1050 1051 void NamedMDNode::addOperand(MDNode *M) { getNMDOps(Operands).emplace_back(M); } 1052 1053 void NamedMDNode::setOperand(unsigned I, MDNode *New) { 1054 assert(I < getNumOperands() && "Invalid operand number"); 1055 getNMDOps(Operands)[I].reset(New); 1056 } 1057 1058 void NamedMDNode::eraseFromParent() { 1059 getParent()->eraseNamedMetadata(this); 1060 } 1061 1062 void NamedMDNode::dropAllReferences() { 1063 getNMDOps(Operands).clear(); 1064 } 1065 1066 StringRef NamedMDNode::getName() const { 1067 return StringRef(Name); 1068 } 1069 1070 //===----------------------------------------------------------------------===// 1071 // Instruction Metadata method implementations. 1072 // 1073 void MDAttachmentMap::set(unsigned ID, MDNode &MD) { 1074 for (auto &I : Attachments) 1075 if (I.first == ID) { 1076 I.second.reset(&MD); 1077 return; 1078 } 1079 Attachments.emplace_back(std::piecewise_construct, std::make_tuple(ID), 1080 std::make_tuple(&MD)); 1081 } 1082 1083 void MDAttachmentMap::erase(unsigned ID) { 1084 if (empty()) 1085 return; 1086 1087 // Common case is one/last value. 1088 if (Attachments.back().first == ID) { 1089 Attachments.pop_back(); 1090 return; 1091 } 1092 1093 for (auto I = Attachments.begin(), E = std::prev(Attachments.end()); I != E; 1094 ++I) 1095 if (I->first == ID) { 1096 *I = std::move(Attachments.back()); 1097 Attachments.pop_back(); 1098 return; 1099 } 1100 } 1101 1102 MDNode *MDAttachmentMap::lookup(unsigned ID) const { 1103 for (const auto &I : Attachments) 1104 if (I.first == ID) 1105 return I.second; 1106 return nullptr; 1107 } 1108 1109 void MDAttachmentMap::getAll( 1110 SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const { 1111 Result.append(Attachments.begin(), Attachments.end()); 1112 1113 // Sort the resulting array so it is stable. 1114 if (Result.size() > 1) 1115 array_pod_sort(Result.begin(), Result.end()); 1116 } 1117 1118 void Instruction::setMetadata(StringRef Kind, MDNode *Node) { 1119 if (!Node && !hasMetadata()) 1120 return; 1121 setMetadata(getContext().getMDKindID(Kind), Node); 1122 } 1123 1124 MDNode *Instruction::getMetadataImpl(StringRef Kind) const { 1125 return getMetadataImpl(getContext().getMDKindID(Kind)); 1126 } 1127 1128 void Instruction::dropUnknownNonDebugMetadata(ArrayRef<unsigned> KnownIDs) { 1129 SmallSet<unsigned, 5> KnownSet; 1130 KnownSet.insert(KnownIDs.begin(), KnownIDs.end()); 1131 1132 if (!hasMetadataHashEntry()) 1133 return; // Nothing to remove! 1134 1135 auto &InstructionMetadata = getContext().pImpl->InstructionMetadata; 1136 1137 if (KnownSet.empty()) { 1138 // Just drop our entry at the store. 1139 InstructionMetadata.erase(this); 1140 setHasMetadataHashEntry(false); 1141 return; 1142 } 1143 1144 auto &Info = InstructionMetadata[this]; 1145 Info.remove_if([&KnownSet](const std::pair<unsigned, TrackingMDNodeRef> &I) { 1146 return !KnownSet.count(I.first); 1147 }); 1148 1149 if (Info.empty()) { 1150 // Drop our entry at the store. 1151 InstructionMetadata.erase(this); 1152 setHasMetadataHashEntry(false); 1153 } 1154 } 1155 1156 void Instruction::setMetadata(unsigned KindID, MDNode *Node) { 1157 if (!Node && !hasMetadata()) 1158 return; 1159 1160 // Handle 'dbg' as a special case since it is not stored in the hash table. 1161 if (KindID == LLVMContext::MD_dbg) { 1162 DbgLoc = DebugLoc(Node); 1163 return; 1164 } 1165 1166 // Handle the case when we're adding/updating metadata on an instruction. 1167 if (Node) { 1168 auto &Info = getContext().pImpl->InstructionMetadata[this]; 1169 assert(!Info.empty() == hasMetadataHashEntry() && 1170 "HasMetadata bit is wonked"); 1171 if (Info.empty()) 1172 setHasMetadataHashEntry(true); 1173 Info.set(KindID, *Node); 1174 return; 1175 } 1176 1177 // Otherwise, we're removing metadata from an instruction. 1178 assert((hasMetadataHashEntry() == 1179 (getContext().pImpl->InstructionMetadata.count(this) > 0)) && 1180 "HasMetadata bit out of date!"); 1181 if (!hasMetadataHashEntry()) 1182 return; // Nothing to remove! 1183 auto &Info = getContext().pImpl->InstructionMetadata[this]; 1184 1185 // Handle removal of an existing value. 1186 Info.erase(KindID); 1187 1188 if (!Info.empty()) 1189 return; 1190 1191 getContext().pImpl->InstructionMetadata.erase(this); 1192 setHasMetadataHashEntry(false); 1193 } 1194 1195 void Instruction::setAAMetadata(const AAMDNodes &N) { 1196 setMetadata(LLVMContext::MD_tbaa, N.TBAA); 1197 setMetadata(LLVMContext::MD_alias_scope, N.Scope); 1198 setMetadata(LLVMContext::MD_noalias, N.NoAlias); 1199 } 1200 1201 MDNode *Instruction::getMetadataImpl(unsigned KindID) const { 1202 // Handle 'dbg' as a special case since it is not stored in the hash table. 1203 if (KindID == LLVMContext::MD_dbg) 1204 return DbgLoc.getAsMDNode(); 1205 1206 if (!hasMetadataHashEntry()) 1207 return nullptr; 1208 auto &Info = getContext().pImpl->InstructionMetadata[this]; 1209 assert(!Info.empty() && "bit out of sync with hash table"); 1210 1211 return Info.lookup(KindID); 1212 } 1213 1214 void Instruction::getAllMetadataImpl( 1215 SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const { 1216 Result.clear(); 1217 1218 // Handle 'dbg' as a special case since it is not stored in the hash table. 1219 if (DbgLoc) { 1220 Result.push_back( 1221 std::make_pair((unsigned)LLVMContext::MD_dbg, DbgLoc.getAsMDNode())); 1222 if (!hasMetadataHashEntry()) return; 1223 } 1224 1225 assert(hasMetadataHashEntry() && 1226 getContext().pImpl->InstructionMetadata.count(this) && 1227 "Shouldn't have called this"); 1228 const auto &Info = getContext().pImpl->InstructionMetadata.find(this)->second; 1229 assert(!Info.empty() && "Shouldn't have called this"); 1230 Info.getAll(Result); 1231 } 1232 1233 void Instruction::getAllMetadataOtherThanDebugLocImpl( 1234 SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const { 1235 Result.clear(); 1236 assert(hasMetadataHashEntry() && 1237 getContext().pImpl->InstructionMetadata.count(this) && 1238 "Shouldn't have called this"); 1239 const auto &Info = getContext().pImpl->InstructionMetadata.find(this)->second; 1240 assert(!Info.empty() && "Shouldn't have called this"); 1241 Info.getAll(Result); 1242 } 1243 1244 void Instruction::clearMetadataHashEntries() { 1245 assert(hasMetadataHashEntry() && "Caller should check"); 1246 getContext().pImpl->InstructionMetadata.erase(this); 1247 setHasMetadataHashEntry(false); 1248 } 1249 1250 MDNode *Function::getMetadata(unsigned KindID) const { 1251 if (!hasMetadata()) 1252 return nullptr; 1253 return getContext().pImpl->FunctionMetadata[this].lookup(KindID); 1254 } 1255 1256 MDNode *Function::getMetadata(StringRef Kind) const { 1257 if (!hasMetadata()) 1258 return nullptr; 1259 return getMetadata(getContext().getMDKindID(Kind)); 1260 } 1261 1262 void Function::setMetadata(unsigned KindID, MDNode *MD) { 1263 if (MD) { 1264 if (!hasMetadata()) 1265 setHasMetadataHashEntry(true); 1266 1267 getContext().pImpl->FunctionMetadata[this].set(KindID, *MD); 1268 return; 1269 } 1270 1271 // Nothing to unset. 1272 if (!hasMetadata()) 1273 return; 1274 1275 auto &Store = getContext().pImpl->FunctionMetadata[this]; 1276 Store.erase(KindID); 1277 if (Store.empty()) 1278 clearMetadata(); 1279 } 1280 1281 void Function::setMetadata(StringRef Kind, MDNode *MD) { 1282 if (!MD && !hasMetadata()) 1283 return; 1284 setMetadata(getContext().getMDKindID(Kind), MD); 1285 } 1286 1287 void Function::getAllMetadata( 1288 SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs) const { 1289 MDs.clear(); 1290 1291 if (!hasMetadata()) 1292 return; 1293 1294 getContext().pImpl->FunctionMetadata[this].getAll(MDs); 1295 } 1296 1297 void Function::dropUnknownMetadata(ArrayRef<unsigned> KnownIDs) { 1298 if (!hasMetadata()) 1299 return; 1300 if (KnownIDs.empty()) { 1301 clearMetadata(); 1302 return; 1303 } 1304 1305 SmallSet<unsigned, 5> KnownSet; 1306 KnownSet.insert(KnownIDs.begin(), KnownIDs.end()); 1307 1308 auto &Store = getContext().pImpl->FunctionMetadata[this]; 1309 assert(!Store.empty()); 1310 1311 Store.remove_if([&KnownSet](const std::pair<unsigned, TrackingMDNodeRef> &I) { 1312 return !KnownSet.count(I.first); 1313 }); 1314 1315 if (Store.empty()) 1316 clearMetadata(); 1317 } 1318 1319 void Function::clearMetadata() { 1320 if (!hasMetadata()) 1321 return; 1322 getContext().pImpl->FunctionMetadata.erase(this); 1323 setHasMetadataHashEntry(false); 1324 } 1325 1326 void Function::setSubprogram(DISubprogram *SP) { 1327 setMetadata(LLVMContext::MD_dbg, SP); 1328 } 1329 1330 DISubprogram *Function::getSubprogram() const { 1331 return cast_or_null<DISubprogram>(getMetadata(LLVMContext::MD_dbg)); 1332 } 1333