LLVM  mainline
Metadata.cpp
Go to the documentation of this file.
00001 //===- Metadata.cpp - Implement Metadata classes --------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements the Metadata classes.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/IR/Metadata.h"
00015 #include "LLVMContextImpl.h"
00016 #include "MetadataImpl.h"
00017 #include "SymbolTableListTraitsImpl.h"
00018 #include "llvm/ADT/DenseMap.h"
00019 #include "llvm/ADT/STLExtras.h"
00020 #include "llvm/ADT/SmallSet.h"
00021 #include "llvm/ADT/SmallString.h"
00022 #include "llvm/ADT/StringMap.h"
00023 #include "llvm/IR/ConstantRange.h"
00024 #include "llvm/IR/DebugInfoMetadata.h"
00025 #include "llvm/IR/Instruction.h"
00026 #include "llvm/IR/LLVMContext.h"
00027 #include "llvm/IR/Module.h"
00028 #include "llvm/IR/ValueHandle.h"
00029 
00030 using namespace llvm;
00031 
00032 MetadataAsValue::MetadataAsValue(Type *Ty, Metadata *MD)
00033     : Value(Ty, MetadataAsValueVal), MD(MD) {
00034   track();
00035 }
00036 
00037 MetadataAsValue::~MetadataAsValue() {
00038   getType()->getContext().pImpl->MetadataAsValues.erase(MD);
00039   untrack();
00040 }
00041 
00042 /// \brief Canonicalize metadata arguments to intrinsics.
00043 ///
00044 /// To support bitcode upgrades (and assembly semantic sugar) for \a
00045 /// MetadataAsValue, we need to canonicalize certain metadata.
00046 ///
00047 ///   - nullptr is replaced by an empty MDNode.
00048 ///   - An MDNode with a single null operand is replaced by an empty MDNode.
00049 ///   - An MDNode whose only operand is a \a ConstantAsMetadata gets skipped.
00050 ///
00051 /// This maintains readability of bitcode from when metadata was a type of
00052 /// value, and these bridges were unnecessary.
00053 static Metadata *canonicalizeMetadataForValue(LLVMContext &Context,
00054                                               Metadata *MD) {
00055   if (!MD)
00056     // !{}
00057     return MDNode::get(Context, None);
00058 
00059   // Return early if this isn't a single-operand MDNode.
00060   auto *N = dyn_cast<MDNode>(MD);
00061   if (!N || N->getNumOperands() != 1)
00062     return MD;
00063 
00064   if (!N->getOperand(0))
00065     // !{}
00066     return MDNode::get(Context, None);
00067 
00068   if (auto *C = dyn_cast<ConstantAsMetadata>(N->getOperand(0)))
00069     // Look through the MDNode.
00070     return C;
00071 
00072   return MD;
00073 }
00074 
00075 MetadataAsValue *MetadataAsValue::get(LLVMContext &Context, Metadata *MD) {
00076   MD = canonicalizeMetadataForValue(Context, MD);
00077   auto *&Entry = Context.pImpl->MetadataAsValues[MD];
00078   if (!Entry)
00079     Entry = new MetadataAsValue(Type::getMetadataTy(Context), MD);
00080   return Entry;
00081 }
00082 
00083 MetadataAsValue *MetadataAsValue::getIfExists(LLVMContext &Context,
00084                                               Metadata *MD) {
00085   MD = canonicalizeMetadataForValue(Context, MD);
00086   auto &Store = Context.pImpl->MetadataAsValues;
00087   return Store.lookup(MD);
00088 }
00089 
00090 void MetadataAsValue::handleChangedMetadata(Metadata *MD) {
00091   LLVMContext &Context = getContext();
00092   MD = canonicalizeMetadataForValue(Context, MD);
00093   auto &Store = Context.pImpl->MetadataAsValues;
00094 
00095   // Stop tracking the old metadata.
00096   Store.erase(this->MD);
00097   untrack();
00098   this->MD = nullptr;
00099 
00100   // Start tracking MD, or RAUW if necessary.
00101   auto *&Entry = Store[MD];
00102   if (Entry) {
00103     replaceAllUsesWith(Entry);
00104     delete this;
00105     return;
00106   }
00107 
00108   this->MD = MD;
00109   track();
00110   Entry = this;
00111 }
00112 
00113 void MetadataAsValue::track() {
00114   if (MD)
00115     MetadataTracking::track(&MD, *MD, *this);
00116 }
00117 
00118 void MetadataAsValue::untrack() {
00119   if (MD)
00120     MetadataTracking::untrack(MD);
00121 }
00122 
00123 bool MetadataTracking::track(void *Ref, Metadata &MD, OwnerTy Owner) {
00124   assert(Ref && "Expected live reference");
00125   assert((Owner || *static_cast<Metadata **>(Ref) == &MD) &&
00126          "Reference without owner must be direct");
00127   if (auto *R = ReplaceableMetadataImpl::get(MD)) {
00128     R->addRef(Ref, Owner);
00129     return true;
00130   }
00131   return false;
00132 }
00133 
00134 void MetadataTracking::untrack(void *Ref, Metadata &MD) {
00135   assert(Ref && "Expected live reference");
00136   if (auto *R = ReplaceableMetadataImpl::get(MD))
00137     R->dropRef(Ref);
00138 }
00139 
00140 bool MetadataTracking::retrack(void *Ref, Metadata &MD, void *New) {
00141   assert(Ref && "Expected live reference");
00142   assert(New && "Expected live reference");
00143   assert(Ref != New && "Expected change");
00144   if (auto *R = ReplaceableMetadataImpl::get(MD)) {
00145     R->moveRef(Ref, New, MD);
00146     return true;
00147   }
00148   return false;
00149 }
00150 
00151 bool MetadataTracking::isReplaceable(const Metadata &MD) {
00152   return ReplaceableMetadataImpl::get(const_cast<Metadata &>(MD));
00153 }
00154 
00155 void ReplaceableMetadataImpl::addRef(void *Ref, OwnerTy Owner) {
00156   bool WasInserted =
00157       UseMap.insert(std::make_pair(Ref, std::make_pair(Owner, NextIndex)))
00158           .second;
00159   (void)WasInserted;
00160   assert(WasInserted && "Expected to add a reference");
00161 
00162   ++NextIndex;
00163   assert(NextIndex != 0 && "Unexpected overflow");
00164 }
00165 
00166 void ReplaceableMetadataImpl::dropRef(void *Ref) {
00167   bool WasErased = UseMap.erase(Ref);
00168   (void)WasErased;
00169   assert(WasErased && "Expected to drop a reference");
00170 }
00171 
00172 void ReplaceableMetadataImpl::moveRef(void *Ref, void *New,
00173                                       const Metadata &MD) {
00174   auto I = UseMap.find(Ref);
00175   assert(I != UseMap.end() && "Expected to move a reference");
00176   auto OwnerAndIndex = I->second;
00177   UseMap.erase(I);
00178   bool WasInserted = UseMap.insert(std::make_pair(New, OwnerAndIndex)).second;
00179   (void)WasInserted;
00180   assert(WasInserted && "Expected to add a reference");
00181 
00182   // Check that the references are direct if there's no owner.
00183   (void)MD;
00184   assert((OwnerAndIndex.first || *static_cast<Metadata **>(Ref) == &MD) &&
00185          "Reference without owner must be direct");
00186   assert((OwnerAndIndex.first || *static_cast<Metadata **>(New) == &MD) &&
00187          "Reference without owner must be direct");
00188 }
00189 
00190 void ReplaceableMetadataImpl::replaceAllUsesWith(Metadata *MD) {
00191   assert(!(MD && isa<MDNode>(MD) && cast<MDNode>(MD)->isTemporary()) &&
00192          "Expected non-temp node");
00193   assert(CanReplace &&
00194          "Attempted to replace Metadata marked for no replacement");
00195 
00196   if (UseMap.empty())
00197     return;
00198 
00199   // Copy out uses since UseMap will get touched below.
00200   typedef std::pair<void *, std::pair<OwnerTy, uint64_t>> UseTy;
00201   SmallVector<UseTy, 8> Uses(UseMap.begin(), UseMap.end());
00202   std::sort(Uses.begin(), Uses.end(), [](const UseTy &L, const UseTy &R) {
00203     return L.second.second < R.second.second;
00204   });
00205   for (const auto &Pair : Uses) {
00206     // Check that this Ref hasn't disappeared after RAUW (when updating a
00207     // previous Ref).
00208     if (!UseMap.count(Pair.first))
00209       continue;
00210 
00211     OwnerTy Owner = Pair.second.first;
00212     if (!Owner) {
00213       // Update unowned tracking references directly.
00214       Metadata *&Ref = *static_cast<Metadata **>(Pair.first);
00215       Ref = MD;
00216       if (MD)
00217         MetadataTracking::track(Ref);
00218       UseMap.erase(Pair.first);
00219       continue;
00220     }
00221 
00222     // Check for MetadataAsValue.
00223     if (Owner.is<MetadataAsValue *>()) {
00224       Owner.get<MetadataAsValue *>()->handleChangedMetadata(MD);
00225       continue;
00226     }
00227 
00228     // There's a Metadata owner -- dispatch.
00229     Metadata *OwnerMD = Owner.get<Metadata *>();
00230     switch (OwnerMD->getMetadataID()) {
00231 #define HANDLE_METADATA_LEAF(CLASS)                                            \
00232   case Metadata::CLASS##Kind:                                                  \
00233     cast<CLASS>(OwnerMD)->handleChangedOperand(Pair.first, MD);                \
00234     continue;
00235 #include "llvm/IR/Metadata.def"
00236     default:
00237       llvm_unreachable("Invalid metadata subclass");
00238     }
00239   }
00240   assert(UseMap.empty() && "Expected all uses to be replaced");
00241 }
00242 
00243 void ReplaceableMetadataImpl::resolveAllUses(bool ResolveUsers) {
00244   if (UseMap.empty())
00245     return;
00246 
00247   if (!ResolveUsers) {
00248     UseMap.clear();
00249     return;
00250   }
00251 
00252   // Copy out uses since UseMap could get touched below.
00253   typedef std::pair<void *, std::pair<OwnerTy, uint64_t>> UseTy;
00254   SmallVector<UseTy, 8> Uses(UseMap.begin(), UseMap.end());
00255   std::sort(Uses.begin(), Uses.end(), [](const UseTy &L, const UseTy &R) {
00256     return L.second.second < R.second.second;
00257   });
00258   UseMap.clear();
00259   for (const auto &Pair : Uses) {
00260     auto Owner = Pair.second.first;
00261     if (!Owner)
00262       continue;
00263     if (Owner.is<MetadataAsValue *>())
00264       continue;
00265 
00266     // Resolve MDNodes that point at this.
00267     auto *OwnerMD = dyn_cast<MDNode>(Owner.get<Metadata *>());
00268     if (!OwnerMD)
00269       continue;
00270     if (OwnerMD->isResolved())
00271       continue;
00272     OwnerMD->decrementUnresolvedOperandCount();
00273   }
00274 }
00275 
00276 ReplaceableMetadataImpl *ReplaceableMetadataImpl::get(Metadata &MD) {
00277   if (auto *N = dyn_cast<MDNode>(&MD))
00278     return N->Context.getReplaceableUses();
00279   return dyn_cast<ValueAsMetadata>(&MD);
00280 }
00281 
00282 static Function *getLocalFunction(Value *V) {
00283   assert(V && "Expected value");
00284   if (auto *A = dyn_cast<Argument>(V))
00285     return A->getParent();
00286   if (BasicBlock *BB = cast<Instruction>(V)->getParent())
00287     return BB->getParent();
00288   return nullptr;
00289 }
00290 
00291 ValueAsMetadata *ValueAsMetadata::get(Value *V) {
00292   assert(V && "Unexpected null Value");
00293 
00294   auto &Context = V->getContext();
00295   auto *&Entry = Context.pImpl->ValuesAsMetadata[V];
00296   if (!Entry) {
00297     assert((isa<Constant>(V) || isa<Argument>(V) || isa<Instruction>(V)) &&
00298            "Expected constant or function-local value");
00299     assert(!V->IsUsedByMD &&
00300            "Expected this to be the only metadata use");
00301     V->IsUsedByMD = true;
00302     if (auto *C = dyn_cast<Constant>(V))
00303       Entry = new ConstantAsMetadata(C);
00304     else
00305       Entry = new LocalAsMetadata(V);
00306   }
00307 
00308   return Entry;
00309 }
00310 
00311 ValueAsMetadata *ValueAsMetadata::getIfExists(Value *V) {
00312   assert(V && "Unexpected null Value");
00313   return V->getContext().pImpl->ValuesAsMetadata.lookup(V);
00314 }
00315 
00316 void ValueAsMetadata::handleDeletion(Value *V) {
00317   assert(V && "Expected valid value");
00318 
00319   auto &Store = V->getType()->getContext().pImpl->ValuesAsMetadata;
00320   auto I = Store.find(V);
00321   if (I == Store.end())
00322     return;
00323 
00324   // Remove old entry from the map.
00325   ValueAsMetadata *MD = I->second;
00326   assert(MD && "Expected valid metadata");
00327   assert(MD->getValue() == V && "Expected valid mapping");
00328   Store.erase(I);
00329 
00330   // Delete the metadata.
00331   MD->replaceAllUsesWith(nullptr);
00332   delete MD;
00333 }
00334 
00335 void ValueAsMetadata::handleRAUW(Value *From, Value *To) {
00336   assert(From && "Expected valid value");
00337   assert(To && "Expected valid value");
00338   assert(From != To && "Expected changed value");
00339   assert(From->getType() == To->getType() && "Unexpected type change");
00340 
00341   LLVMContext &Context = From->getType()->getContext();
00342   auto &Store = Context.pImpl->ValuesAsMetadata;
00343   auto I = Store.find(From);
00344   if (I == Store.end()) {
00345     assert(!From->IsUsedByMD &&
00346            "Expected From not to be used by metadata");
00347     return;
00348   }
00349 
00350   // Remove old entry from the map.
00351   assert(From->IsUsedByMD &&
00352          "Expected From to be used by metadata");
00353   From->IsUsedByMD = false;
00354   ValueAsMetadata *MD = I->second;
00355   assert(MD && "Expected valid metadata");
00356   assert(MD->getValue() == From && "Expected valid mapping");
00357   Store.erase(I);
00358 
00359   if (isa<LocalAsMetadata>(MD)) {
00360     if (auto *C = dyn_cast<Constant>(To)) {
00361       // Local became a constant.
00362       MD->replaceAllUsesWith(ConstantAsMetadata::get(C));
00363       delete MD;
00364       return;
00365     }
00366     if (getLocalFunction(From) && getLocalFunction(To) &&
00367         getLocalFunction(From) != getLocalFunction(To)) {
00368       // Function changed.
00369       MD->replaceAllUsesWith(nullptr);
00370       delete MD;
00371       return;
00372     }
00373   } else if (!isa<Constant>(To)) {
00374     // Changed to function-local value.
00375     MD->replaceAllUsesWith(nullptr);
00376     delete MD;
00377     return;
00378   }
00379 
00380   auto *&Entry = Store[To];
00381   if (Entry) {
00382     // The target already exists.
00383     MD->replaceAllUsesWith(Entry);
00384     delete MD;
00385     return;
00386   }
00387 
00388   // Update MD in place (and update the map entry).
00389   assert(!To->IsUsedByMD &&
00390          "Expected this to be the only metadata use");
00391   To->IsUsedByMD = true;
00392   MD->V = To;
00393   Entry = MD;
00394 }
00395 
00396 //===----------------------------------------------------------------------===//
00397 // MDString implementation.
00398 //
00399 
00400 MDString *MDString::get(LLVMContext &Context, StringRef Str) {
00401   auto &Store = Context.pImpl->MDStringCache;
00402   auto I = Store.find(Str);
00403   if (I != Store.end())
00404     return &I->second;
00405 
00406   auto *Entry =
00407       StringMapEntry<MDString>::Create(Str, Store.getAllocator(), MDString());
00408   bool WasInserted = Store.insert(Entry);
00409   (void)WasInserted;
00410   assert(WasInserted && "Expected entry to be inserted");
00411   Entry->second.Entry = Entry;
00412   return &Entry->second;
00413 }
00414 
00415 StringRef MDString::getString() const {
00416   assert(Entry && "Expected to find string map entry");
00417   return Entry->first();
00418 }
00419 
00420 //===----------------------------------------------------------------------===//
00421 // MDNode implementation.
00422 //
00423 
00424 // Assert that the MDNode types will not be unaligned by the objects
00425 // prepended to them.
00426 #define HANDLE_MDNODE_LEAF(CLASS)                                              \
00427   static_assert(                                                               \
00428       llvm::AlignOf<uint64_t>::Alignment >= llvm::AlignOf<CLASS>::Alignment,   \
00429       "Alignment is insufficient after objects prepended to " #CLASS);
00430 #include "llvm/IR/Metadata.def"
00431 
00432 void *MDNode::operator new(size_t Size, unsigned NumOps) {
00433   size_t OpSize = NumOps * sizeof(MDOperand);
00434   // uint64_t is the most aligned type we need support (ensured by static_assert
00435   // above)
00436   OpSize = alignTo(OpSize, llvm::alignOf<uint64_t>());
00437   void *Ptr = reinterpret_cast<char *>(::operator new(OpSize + Size)) + OpSize;
00438   MDOperand *O = static_cast<MDOperand *>(Ptr);
00439   for (MDOperand *E = O - NumOps; O != E; --O)
00440     (void)new (O - 1) MDOperand;
00441   return Ptr;
00442 }
00443 
00444 void MDNode::operator delete(void *Mem) {
00445   MDNode *N = static_cast<MDNode *>(Mem);
00446   size_t OpSize = N->NumOperands * sizeof(MDOperand);
00447   OpSize = alignTo(OpSize, llvm::alignOf<uint64_t>());
00448 
00449   MDOperand *O = static_cast<MDOperand *>(Mem);
00450   for (MDOperand *E = O - N->NumOperands; O != E; --O)
00451     (O - 1)->~MDOperand();
00452   ::operator delete(reinterpret_cast<char *>(Mem) - OpSize);
00453 }
00454 
00455 MDNode::MDNode(LLVMContext &Context, unsigned ID, StorageType Storage,
00456                ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2)
00457     : Metadata(ID, Storage), NumOperands(Ops1.size() + Ops2.size()),
00458       NumUnresolved(0), Context(Context) {
00459   unsigned Op = 0;
00460   for (Metadata *MD : Ops1)
00461     setOperand(Op++, MD);
00462   for (Metadata *MD : Ops2)
00463     setOperand(Op++, MD);
00464 
00465   if (isDistinct())
00466     return;
00467 
00468   if (isUniqued())
00469     // Check whether any operands are unresolved, requiring re-uniquing.  If
00470     // not, don't support RAUW.
00471     if (!countUnresolvedOperands())
00472       return;
00473 
00474   this->Context.makeReplaceable(make_unique<ReplaceableMetadataImpl>(Context));
00475 }
00476 
00477 TempMDNode MDNode::clone() const {
00478   switch (getMetadataID()) {
00479   default:
00480     llvm_unreachable("Invalid MDNode subclass");
00481 #define HANDLE_MDNODE_LEAF(CLASS)                                              \
00482   case CLASS##Kind:                                                            \
00483     return cast<CLASS>(this)->cloneImpl();
00484 #include "llvm/IR/Metadata.def"
00485   }
00486 }
00487 
00488 static bool isOperandUnresolved(Metadata *Op) {
00489   if (auto *N = dyn_cast_or_null<MDNode>(Op))
00490     return !N->isResolved();
00491   return false;
00492 }
00493 
00494 unsigned MDNode::countUnresolvedOperands() {
00495   assert(NumUnresolved == 0 && "Expected unresolved ops to be uncounted");
00496   NumUnresolved = std::count_if(op_begin(), op_end(), isOperandUnresolved);
00497   return NumUnresolved;
00498 }
00499 
00500 void MDNode::makeUniqued() {
00501   assert(isTemporary() && "Expected this to be temporary");
00502   assert(!isResolved() && "Expected this to be unresolved");
00503 
00504   // Enable uniquing callbacks.
00505   for (auto &Op : mutable_operands())
00506     Op.reset(Op.get(), this);
00507 
00508   // Make this 'uniqued'.
00509   Storage = Uniqued;
00510   if (!countUnresolvedOperands())
00511     resolve();
00512 
00513   assert(isUniqued() && "Expected this to be uniqued");
00514 }
00515 
00516 void MDNode::makeDistinct() {
00517   assert(isTemporary() && "Expected this to be temporary");
00518   assert(!isResolved() && "Expected this to be unresolved");
00519 
00520   // Pretend to be uniqued, resolve the node, and then store in distinct table.
00521   Storage = Uniqued;
00522   resolve();
00523   storeDistinctInContext();
00524 
00525   assert(isDistinct() && "Expected this to be distinct");
00526   assert(isResolved() && "Expected this to be resolved");
00527 }
00528 
00529 void MDNode::resolve() {
00530   assert(isUniqued() && "Expected this to be uniqued");
00531   assert(!isResolved() && "Expected this to be unresolved");
00532 
00533   // Move the map, so that this immediately looks resolved.
00534   auto Uses = Context.takeReplaceableUses();
00535   NumUnresolved = 0;
00536   assert(isResolved() && "Expected this to be resolved");
00537 
00538   // Drop RAUW support.
00539   Uses->resolveAllUses();
00540 }
00541 
00542 void MDNode::resolveAfterOperandChange(Metadata *Old, Metadata *New) {
00543   assert(NumUnresolved != 0 && "Expected unresolved operands");
00544 
00545   // Check if an operand was resolved.
00546   if (!isOperandUnresolved(Old)) {
00547     if (isOperandUnresolved(New))
00548       // An operand was un-resolved!
00549       ++NumUnresolved;
00550   } else if (!isOperandUnresolved(New))
00551     decrementUnresolvedOperandCount();
00552 }
00553 
00554 void MDNode::decrementUnresolvedOperandCount() {
00555   if (!--NumUnresolved)
00556     // Last unresolved operand has just been resolved.
00557     resolve();
00558 }
00559 
00560 void MDNode::resolveRecursivelyImpl(bool AllowTemps) {
00561   if (isResolved())
00562     return;
00563 
00564   // Resolve this node immediately.
00565   resolve();
00566 
00567   // Resolve all operands.
00568   for (const auto &Op : operands()) {
00569     auto *N = dyn_cast_or_null<MDNode>(Op);
00570     if (!N)
00571       continue;
00572 
00573     if (N->isTemporary() && AllowTemps)
00574       continue;
00575     assert(!N->isTemporary() &&
00576            "Expected all forward declarations to be resolved");
00577     if (!N->isResolved())
00578       N->resolveCycles();
00579   }
00580 }
00581 
00582 static bool hasSelfReference(MDNode *N) {
00583   for (Metadata *MD : N->operands())
00584     if (MD == N)
00585       return true;
00586   return false;
00587 }
00588 
00589 MDNode *MDNode::replaceWithPermanentImpl() {
00590   switch (getMetadataID()) {
00591   default:
00592     // If this type isn't uniquable, replace with a distinct node.
00593     return replaceWithDistinctImpl();
00594 
00595 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS)                                    \
00596   case CLASS##Kind:                                                            \
00597     break;
00598 #include "llvm/IR/Metadata.def"
00599   }
00600 
00601   // Even if this type is uniquable, self-references have to be distinct.
00602   if (hasSelfReference(this))
00603     return replaceWithDistinctImpl();
00604   return replaceWithUniquedImpl();
00605 }
00606 
00607 MDNode *MDNode::replaceWithUniquedImpl() {
00608   // Try to uniquify in place.
00609   MDNode *UniquedNode = uniquify();
00610 
00611   if (UniquedNode == this) {
00612     makeUniqued();
00613     return this;
00614   }
00615 
00616   // Collision, so RAUW instead.
00617   replaceAllUsesWith(UniquedNode);
00618   deleteAsSubclass();
00619   return UniquedNode;
00620 }
00621 
00622 MDNode *MDNode::replaceWithDistinctImpl() {
00623   makeDistinct();
00624   return this;
00625 }
00626 
00627 void MDTuple::recalculateHash() {
00628   setHash(MDTupleInfo::KeyTy::calculateHash(this));
00629 }
00630 
00631 void MDNode::dropAllReferences() {
00632   for (unsigned I = 0, E = NumOperands; I != E; ++I)
00633     setOperand(I, nullptr);
00634   if (!isResolved()) {
00635     Context.getReplaceableUses()->resolveAllUses(/* ResolveUsers */ false);
00636     (void)Context.takeReplaceableUses();
00637   }
00638 }
00639 
00640 void MDNode::handleChangedOperand(void *Ref, Metadata *New) {
00641   unsigned Op = static_cast<MDOperand *>(Ref) - op_begin();
00642   assert(Op < getNumOperands() && "Expected valid operand");
00643 
00644   if (!isUniqued()) {
00645     // This node is not uniqued.  Just set the operand and be done with it.
00646     setOperand(Op, New);
00647     return;
00648   }
00649 
00650   // This node is uniqued.
00651   eraseFromStore();
00652 
00653   Metadata *Old = getOperand(Op);
00654   setOperand(Op, New);
00655 
00656   // Drop uniquing for self-reference cycles.
00657   if (New == this) {
00658     if (!isResolved())
00659       resolve();
00660     storeDistinctInContext();
00661     return;
00662   }
00663 
00664   // Re-unique the node.
00665   auto *Uniqued = uniquify();
00666   if (Uniqued == this) {
00667     if (!isResolved())
00668       resolveAfterOperandChange(Old, New);
00669     return;
00670   }
00671 
00672   // Collision.
00673   if (!isResolved()) {
00674     // Still unresolved, so RAUW.
00675     //
00676     // First, clear out all operands to prevent any recursion (similar to
00677     // dropAllReferences(), but we still need the use-list).
00678     for (unsigned O = 0, E = getNumOperands(); O != E; ++O)
00679       setOperand(O, nullptr);
00680     Context.getReplaceableUses()->replaceAllUsesWith(Uniqued);
00681     deleteAsSubclass();
00682     return;
00683   }
00684 
00685   // Store in non-uniqued form if RAUW isn't possible.
00686   storeDistinctInContext();
00687 }
00688 
00689 void MDNode::deleteAsSubclass() {
00690   switch (getMetadataID()) {
00691   default:
00692     llvm_unreachable("Invalid subclass of MDNode");
00693 #define HANDLE_MDNODE_LEAF(CLASS)                                              \
00694   case CLASS##Kind:                                                            \
00695     delete cast<CLASS>(this);                                                  \
00696     break;
00697 #include "llvm/IR/Metadata.def"
00698   }
00699 }
00700 
00701 template <class T, class InfoT>
00702 static T *uniquifyImpl(T *N, DenseSet<T *, InfoT> &Store) {
00703   if (T *U = getUniqued(Store, N))
00704     return U;
00705 
00706   Store.insert(N);
00707   return N;
00708 }
00709 
00710 template <class NodeTy> struct MDNode::HasCachedHash {
00711   typedef char Yes[1];
00712   typedef char No[2];
00713   template <class U, U Val> struct SFINAE {};
00714 
00715   template <class U>
00716   static Yes &check(SFINAE<void (U::*)(unsigned), &U::setHash> *);
00717   template <class U> static No &check(...);
00718 
00719   static const bool value = sizeof(check<NodeTy>(nullptr)) == sizeof(Yes);
00720 };
00721 
00722 MDNode *MDNode::uniquify() {
00723   assert(!hasSelfReference(this) && "Cannot uniquify a self-referencing node");
00724 
00725   // Try to insert into uniquing store.
00726   switch (getMetadataID()) {
00727   default:
00728     llvm_unreachable("Invalid or non-uniquable subclass of MDNode");
00729 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS)                                    \
00730   case CLASS##Kind: {                                                          \
00731     CLASS *SubclassThis = cast<CLASS>(this);                                   \
00732     std::integral_constant<bool, HasCachedHash<CLASS>::value>                  \
00733         ShouldRecalculateHash;                                                 \
00734     dispatchRecalculateHash(SubclassThis, ShouldRecalculateHash);              \
00735     return uniquifyImpl(SubclassThis, getContext().pImpl->CLASS##s);           \
00736   }
00737 #include "llvm/IR/Metadata.def"
00738   }
00739 }
00740 
00741 void MDNode::eraseFromStore() {
00742   switch (getMetadataID()) {
00743   default:
00744     llvm_unreachable("Invalid or non-uniquable subclass of MDNode");
00745 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS)                                    \
00746   case CLASS##Kind:                                                            \
00747     getContext().pImpl->CLASS##s.erase(cast<CLASS>(this));                     \
00748     break;
00749 #include "llvm/IR/Metadata.def"
00750   }
00751 }
00752 
00753 MDTuple *MDTuple::getImpl(LLVMContext &Context, ArrayRef<Metadata *> MDs,
00754                           StorageType Storage, bool ShouldCreate) {
00755   unsigned Hash = 0;
00756   if (Storage == Uniqued) {
00757     MDTupleInfo::KeyTy Key(MDs);
00758     if (auto *N = getUniqued(Context.pImpl->MDTuples, Key))
00759       return N;
00760     if (!ShouldCreate)
00761       return nullptr;
00762     Hash = Key.getHash();
00763   } else {
00764     assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
00765   }
00766 
00767   return storeImpl(new (MDs.size()) MDTuple(Context, Storage, Hash, MDs),
00768                    Storage, Context.pImpl->MDTuples);
00769 }
00770 
00771 void MDNode::deleteTemporary(MDNode *N) {
00772   assert(N->isTemporary() && "Expected temporary node");
00773   N->replaceAllUsesWith(nullptr);
00774   N->deleteAsSubclass();
00775 }
00776 
00777 void MDNode::storeDistinctInContext() {
00778   assert(isResolved() && "Expected resolved nodes");
00779   Storage = Distinct;
00780 
00781   // Reset the hash.
00782   switch (getMetadataID()) {
00783   default:
00784     llvm_unreachable("Invalid subclass of MDNode");
00785 #define HANDLE_MDNODE_LEAF(CLASS)                                              \
00786   case CLASS##Kind: {                                                          \
00787     std::integral_constant<bool, HasCachedHash<CLASS>::value> ShouldResetHash; \
00788     dispatchResetHash(cast<CLASS>(this), ShouldResetHash);                     \
00789     break;                                                                     \
00790   }
00791 #include "llvm/IR/Metadata.def"
00792   }
00793 
00794   getContext().pImpl->DistinctMDNodes.insert(this);
00795 }
00796 
00797 void MDNode::replaceOperandWith(unsigned I, Metadata *New) {
00798   if (getOperand(I) == New)
00799     return;
00800 
00801   if (!isUniqued()) {
00802     setOperand(I, New);
00803     return;
00804   }
00805 
00806   handleChangedOperand(mutable_begin() + I, New);
00807 }
00808 
00809 void MDNode::setOperand(unsigned I, Metadata *New) {
00810   assert(I < NumOperands);
00811   mutable_begin()[I].reset(New, isUniqued() ? this : nullptr);
00812 }
00813 
00814 /// \brief Get a node, or a self-reference that looks like it.
00815 ///
00816 /// Special handling for finding self-references, for use by \a
00817 /// MDNode::concatenate() and \a MDNode::intersect() to maintain behaviour from
00818 /// when self-referencing nodes were still uniqued.  If the first operand has
00819 /// the same operands as \c Ops, return the first operand instead.
00820 static MDNode *getOrSelfReference(LLVMContext &Context,
00821                                   ArrayRef<Metadata *> Ops) {
00822   if (!Ops.empty())
00823     if (MDNode *N = dyn_cast_or_null<MDNode>(Ops[0]))
00824       if (N->getNumOperands() == Ops.size() && N == N->getOperand(0)) {
00825         for (unsigned I = 1, E = Ops.size(); I != E; ++I)
00826           if (Ops[I] != N->getOperand(I))
00827             return MDNode::get(Context, Ops);
00828         return N;
00829       }
00830 
00831   return MDNode::get(Context, Ops);
00832 }
00833 
00834 MDNode *MDNode::concatenate(MDNode *A, MDNode *B) {
00835   if (!A)
00836     return B;
00837   if (!B)
00838     return A;
00839 
00840   SmallVector<Metadata *, 4> MDs;
00841   MDs.reserve(A->getNumOperands() + B->getNumOperands());
00842   MDs.append(A->op_begin(), A->op_end());
00843   MDs.append(B->op_begin(), B->op_end());
00844 
00845   // FIXME: This preserves long-standing behaviour, but is it really the right
00846   // behaviour?  Or was that an unintended side-effect of node uniquing?
00847   return getOrSelfReference(A->getContext(), MDs);
00848 }
00849 
00850 MDNode *MDNode::intersect(MDNode *A, MDNode *B) {
00851   if (!A || !B)
00852     return nullptr;
00853 
00854   SmallVector<Metadata *, 4> MDs;
00855   for (Metadata *MD : A->operands())
00856     if (std::find(B->op_begin(), B->op_end(), MD) != B->op_end())
00857       MDs.push_back(MD);
00858 
00859   // FIXME: This preserves long-standing behaviour, but is it really the right
00860   // behaviour?  Or was that an unintended side-effect of node uniquing?
00861   return getOrSelfReference(A->getContext(), MDs);
00862 }
00863 
00864 MDNode *MDNode::getMostGenericAliasScope(MDNode *A, MDNode *B) {
00865   if (!A || !B)
00866     return nullptr;
00867 
00868   SmallVector<Metadata *, 4> MDs(B->op_begin(), B->op_end());
00869   for (Metadata *MD : A->operands())
00870     if (std::find(B->op_begin(), B->op_end(), MD) == B->op_end())
00871       MDs.push_back(MD);
00872 
00873   // FIXME: This preserves long-standing behaviour, but is it really the right
00874   // behaviour?  Or was that an unintended side-effect of node uniquing?
00875   return getOrSelfReference(A->getContext(), MDs);
00876 }
00877 
00878 MDNode *MDNode::getMostGenericFPMath(MDNode *A, MDNode *B) {
00879   if (!A || !B)
00880     return nullptr;
00881 
00882   APFloat AVal = mdconst::extract<ConstantFP>(A->getOperand(0))->getValueAPF();
00883   APFloat BVal = mdconst::extract<ConstantFP>(B->getOperand(0))->getValueAPF();
00884   if (AVal.compare(BVal) == APFloat::cmpLessThan)
00885     return A;
00886   return B;
00887 }
00888 
00889 static bool isContiguous(const ConstantRange &A, const ConstantRange &B) {
00890   return A.getUpper() == B.getLower() || A.getLower() == B.getUpper();
00891 }
00892 
00893 static bool canBeMerged(const ConstantRange &A, const ConstantRange &B) {
00894   return !A.intersectWith(B).isEmptySet() || isContiguous(A, B);
00895 }
00896 
00897 static bool tryMergeRange(SmallVectorImpl<ConstantInt *> &EndPoints,
00898                           ConstantInt *Low, ConstantInt *High) {
00899   ConstantRange NewRange(Low->getValue(), High->getValue());
00900   unsigned Size = EndPoints.size();
00901   APInt LB = EndPoints[Size - 2]->getValue();
00902   APInt LE = EndPoints[Size - 1]->getValue();
00903   ConstantRange LastRange(LB, LE);
00904   if (canBeMerged(NewRange, LastRange)) {
00905     ConstantRange Union = LastRange.unionWith(NewRange);
00906     Type *Ty = High->getType();
00907     EndPoints[Size - 2] =
00908         cast<ConstantInt>(ConstantInt::get(Ty, Union.getLower()));
00909     EndPoints[Size - 1] =
00910         cast<ConstantInt>(ConstantInt::get(Ty, Union.getUpper()));
00911     return true;
00912   }
00913   return false;
00914 }
00915 
00916 static void addRange(SmallVectorImpl<ConstantInt *> &EndPoints,
00917                      ConstantInt *Low, ConstantInt *High) {
00918   if (!EndPoints.empty())
00919     if (tryMergeRange(EndPoints, Low, High))
00920       return;
00921 
00922   EndPoints.push_back(Low);
00923   EndPoints.push_back(High);
00924 }
00925 
00926 MDNode *MDNode::getMostGenericRange(MDNode *A, MDNode *B) {
00927   // Given two ranges, we want to compute the union of the ranges. This
00928   // is slightly complitade by having to combine the intervals and merge
00929   // the ones that overlap.
00930 
00931   if (!A || !B)
00932     return nullptr;
00933 
00934   if (A == B)
00935     return A;
00936 
00937   // First, walk both lists in older of the lower boundary of each interval.
00938   // At each step, try to merge the new interval to the last one we adedd.
00939   SmallVector<ConstantInt *, 4> EndPoints;
00940   int AI = 0;
00941   int BI = 0;
00942   int AN = A->getNumOperands() / 2;
00943   int BN = B->getNumOperands() / 2;
00944   while (AI < AN && BI < BN) {
00945     ConstantInt *ALow = mdconst::extract<ConstantInt>(A->getOperand(2 * AI));
00946     ConstantInt *BLow = mdconst::extract<ConstantInt>(B->getOperand(2 * BI));
00947 
00948     if (ALow->getValue().slt(BLow->getValue())) {
00949       addRange(EndPoints, ALow,
00950                mdconst::extract<ConstantInt>(A->getOperand(2 * AI + 1)));
00951       ++AI;
00952     } else {
00953       addRange(EndPoints, BLow,
00954                mdconst::extract<ConstantInt>(B->getOperand(2 * BI + 1)));
00955       ++BI;
00956     }
00957   }
00958   while (AI < AN) {
00959     addRange(EndPoints, mdconst::extract<ConstantInt>(A->getOperand(2 * AI)),
00960              mdconst::extract<ConstantInt>(A->getOperand(2 * AI + 1)));
00961     ++AI;
00962   }
00963   while (BI < BN) {
00964     addRange(EndPoints, mdconst::extract<ConstantInt>(B->getOperand(2 * BI)),
00965              mdconst::extract<ConstantInt>(B->getOperand(2 * BI + 1)));
00966     ++BI;
00967   }
00968 
00969   // If we have more than 2 ranges (4 endpoints) we have to try to merge
00970   // the last and first ones.
00971   unsigned Size = EndPoints.size();
00972   if (Size > 4) {
00973     ConstantInt *FB = EndPoints[0];
00974     ConstantInt *FE = EndPoints[1];
00975     if (tryMergeRange(EndPoints, FB, FE)) {
00976       for (unsigned i = 0; i < Size - 2; ++i) {
00977         EndPoints[i] = EndPoints[i + 2];
00978       }
00979       EndPoints.resize(Size - 2);
00980     }
00981   }
00982 
00983   // If in the end we have a single range, it is possible that it is now the
00984   // full range. Just drop the metadata in that case.
00985   if (EndPoints.size() == 2) {
00986     ConstantRange Range(EndPoints[0]->getValue(), EndPoints[1]->getValue());
00987     if (Range.isFullSet())
00988       return nullptr;
00989   }
00990 
00991   SmallVector<Metadata *, 4> MDs;
00992   MDs.reserve(EndPoints.size());
00993   for (auto *I : EndPoints)
00994     MDs.push_back(ConstantAsMetadata::get(I));
00995   return MDNode::get(A->getContext(), MDs);
00996 }
00997 
00998 MDNode *MDNode::getMostGenericAlignmentOrDereferenceable(MDNode *A, MDNode *B) {
00999   if (!A || !B)
01000     return nullptr;
01001 
01002   ConstantInt *AVal = mdconst::extract<ConstantInt>(A->getOperand(0));
01003   ConstantInt *BVal = mdconst::extract<ConstantInt>(B->getOperand(0));
01004   if (AVal->getZExtValue() < BVal->getZExtValue())
01005     return A;
01006   return B;
01007 }
01008 
01009 //===----------------------------------------------------------------------===//
01010 // NamedMDNode implementation.
01011 //
01012 
01013 static SmallVector<TrackingMDRef, 4> &getNMDOps(void *Operands) {
01014   return *(SmallVector<TrackingMDRef, 4> *)Operands;
01015 }
01016 
01017 NamedMDNode::NamedMDNode(const Twine &N)
01018     : Name(N.str()), Parent(nullptr),
01019       Operands(new SmallVector<TrackingMDRef, 4>()) {}
01020 
01021 NamedMDNode::~NamedMDNode() {
01022   dropAllReferences();
01023   delete &getNMDOps(Operands);
01024 }
01025 
01026 unsigned NamedMDNode::getNumOperands() const {
01027   return (unsigned)getNMDOps(Operands).size();
01028 }
01029 
01030 MDNode *NamedMDNode::getOperand(unsigned i) const {
01031   assert(i < getNumOperands() && "Invalid Operand number!");
01032   auto *N = getNMDOps(Operands)[i].get();
01033   return cast_or_null<MDNode>(N);
01034 }
01035 
01036 void NamedMDNode::addOperand(MDNode *M) { getNMDOps(Operands).emplace_back(M); }
01037 
01038 void NamedMDNode::setOperand(unsigned I, MDNode *New) {
01039   assert(I < getNumOperands() && "Invalid operand number");
01040   getNMDOps(Operands)[I].reset(New);
01041 }
01042 
01043 void NamedMDNode::eraseFromParent() {
01044   getParent()->eraseNamedMetadata(this);
01045 }
01046 
01047 void NamedMDNode::dropAllReferences() {
01048   getNMDOps(Operands).clear();
01049 }
01050 
01051 StringRef NamedMDNode::getName() const {
01052   return StringRef(Name);
01053 }
01054 
01055 //===----------------------------------------------------------------------===//
01056 // Instruction Metadata method implementations.
01057 //
01058 void MDAttachmentMap::set(unsigned ID, MDNode &MD) {
01059   for (auto &I : Attachments)
01060     if (I.first == ID) {
01061       I.second.reset(&MD);
01062       return;
01063     }
01064   Attachments.emplace_back(std::piecewise_construct, std::make_tuple(ID),
01065                            std::make_tuple(&MD));
01066 }
01067 
01068 void MDAttachmentMap::erase(unsigned ID) {
01069   if (empty())
01070     return;
01071 
01072   // Common case is one/last value.
01073   if (Attachments.back().first == ID) {
01074     Attachments.pop_back();
01075     return;
01076   }
01077 
01078   for (auto I = Attachments.begin(), E = std::prev(Attachments.end()); I != E;
01079        ++I)
01080     if (I->first == ID) {
01081       *I = std::move(Attachments.back());
01082       Attachments.pop_back();
01083       return;
01084     }
01085 }
01086 
01087 MDNode *MDAttachmentMap::lookup(unsigned ID) const {
01088   for (const auto &I : Attachments)
01089     if (I.first == ID)
01090       return I.second;
01091   return nullptr;
01092 }
01093 
01094 void MDAttachmentMap::getAll(
01095     SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
01096   Result.append(Attachments.begin(), Attachments.end());
01097 
01098   // Sort the resulting array so it is stable.
01099   if (Result.size() > 1)
01100     array_pod_sort(Result.begin(), Result.end());
01101 }
01102 
01103 void Instruction::setMetadata(StringRef Kind, MDNode *Node) {
01104   if (!Node && !hasMetadata())
01105     return;
01106   setMetadata(getContext().getMDKindID(Kind), Node);
01107 }
01108 
01109 MDNode *Instruction::getMetadataImpl(StringRef Kind) const {
01110   return getMetadataImpl(getContext().getMDKindID(Kind));
01111 }
01112 
01113 void Instruction::dropUnknownNonDebugMetadata(ArrayRef<unsigned> KnownIDs) {
01114   SmallSet<unsigned, 5> KnownSet;
01115   KnownSet.insert(KnownIDs.begin(), KnownIDs.end());
01116 
01117   if (!hasMetadataHashEntry())
01118     return; // Nothing to remove!
01119 
01120   auto &InstructionMetadata = getContext().pImpl->InstructionMetadata;
01121 
01122   if (KnownSet.empty()) {
01123     // Just drop our entry at the store.
01124     InstructionMetadata.erase(this);
01125     setHasMetadataHashEntry(false);
01126     return;
01127   }
01128 
01129   auto &Info = InstructionMetadata[this];
01130   Info.remove_if([&KnownSet](const std::pair<unsigned, TrackingMDNodeRef> &I) {
01131     return !KnownSet.count(I.first);
01132   });
01133 
01134   if (Info.empty()) {
01135     // Drop our entry at the store.
01136     InstructionMetadata.erase(this);
01137     setHasMetadataHashEntry(false);
01138   }
01139 }
01140 
01141 /// setMetadata - Set the metadata of the specified kind to the specified
01142 /// node.  This updates/replaces metadata if already present, or removes it if
01143 /// Node is null.
01144 void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
01145   if (!Node && !hasMetadata())
01146     return;
01147 
01148   // Handle 'dbg' as a special case since it is not stored in the hash table.
01149   if (KindID == LLVMContext::MD_dbg) {
01150     DbgLoc = DebugLoc(Node);
01151     return;
01152   }
01153   
01154   // Handle the case when we're adding/updating metadata on an instruction.
01155   if (Node) {
01156     auto &Info = getContext().pImpl->InstructionMetadata[this];
01157     assert(!Info.empty() == hasMetadataHashEntry() &&
01158            "HasMetadata bit is wonked");
01159     if (Info.empty())
01160       setHasMetadataHashEntry(true);
01161     Info.set(KindID, *Node);
01162     return;
01163   }
01164 
01165   // Otherwise, we're removing metadata from an instruction.
01166   assert((hasMetadataHashEntry() ==
01167           (getContext().pImpl->InstructionMetadata.count(this) > 0)) &&
01168          "HasMetadata bit out of date!");
01169   if (!hasMetadataHashEntry())
01170     return;  // Nothing to remove!
01171   auto &Info = getContext().pImpl->InstructionMetadata[this];
01172 
01173   // Handle removal of an existing value.
01174   Info.erase(KindID);
01175 
01176   if (!Info.empty())
01177     return;
01178 
01179   getContext().pImpl->InstructionMetadata.erase(this);
01180   setHasMetadataHashEntry(false);
01181 }
01182 
01183 void Instruction::setAAMetadata(const AAMDNodes &N) {
01184   setMetadata(LLVMContext::MD_tbaa, N.TBAA);
01185   setMetadata(LLVMContext::MD_alias_scope, N.Scope);
01186   setMetadata(LLVMContext::MD_noalias, N.NoAlias);
01187 }
01188 
01189 MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
01190   // Handle 'dbg' as a special case since it is not stored in the hash table.
01191   if (KindID == LLVMContext::MD_dbg)
01192     return DbgLoc.getAsMDNode();
01193 
01194   if (!hasMetadataHashEntry())
01195     return nullptr;
01196   auto &Info = getContext().pImpl->InstructionMetadata[this];
01197   assert(!Info.empty() && "bit out of sync with hash table");
01198 
01199   return Info.lookup(KindID);
01200 }
01201 
01202 void Instruction::getAllMetadataImpl(
01203     SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
01204   Result.clear();
01205   
01206   // Handle 'dbg' as a special case since it is not stored in the hash table.
01207   if (DbgLoc) {
01208     Result.push_back(
01209         std::make_pair((unsigned)LLVMContext::MD_dbg, DbgLoc.getAsMDNode()));
01210     if (!hasMetadataHashEntry()) return;
01211   }
01212 
01213   assert(hasMetadataHashEntry() &&
01214          getContext().pImpl->InstructionMetadata.count(this) &&
01215          "Shouldn't have called this");
01216   const auto &Info = getContext().pImpl->InstructionMetadata.find(this)->second;
01217   assert(!Info.empty() && "Shouldn't have called this");
01218   Info.getAll(Result);
01219 }
01220 
01221 void Instruction::getAllMetadataOtherThanDebugLocImpl(
01222     SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
01223   Result.clear();
01224   assert(hasMetadataHashEntry() &&
01225          getContext().pImpl->InstructionMetadata.count(this) &&
01226          "Shouldn't have called this");
01227   const auto &Info = getContext().pImpl->InstructionMetadata.find(this)->second;
01228   assert(!Info.empty() && "Shouldn't have called this");
01229   Info.getAll(Result);
01230 }
01231 
01232 /// clearMetadataHashEntries - Clear all hashtable-based metadata from
01233 /// this instruction.
01234 void Instruction::clearMetadataHashEntries() {
01235   assert(hasMetadataHashEntry() && "Caller should check");
01236   getContext().pImpl->InstructionMetadata.erase(this);
01237   setHasMetadataHashEntry(false);
01238 }
01239 
01240 MDNode *Function::getMetadata(unsigned KindID) const {
01241   if (!hasMetadata())
01242     return nullptr;
01243   return getContext().pImpl->FunctionMetadata[this].lookup(KindID);
01244 }
01245 
01246 MDNode *Function::getMetadata(StringRef Kind) const {
01247   if (!hasMetadata())
01248     return nullptr;
01249   return getMetadata(getContext().getMDKindID(Kind));
01250 }
01251 
01252 void Function::setMetadata(unsigned KindID, MDNode *MD) {
01253   if (MD) {
01254     if (!hasMetadata())
01255       setHasMetadataHashEntry(true);
01256 
01257     getContext().pImpl->FunctionMetadata[this].set(KindID, *MD);
01258     return;
01259   }
01260 
01261   // Nothing to unset.
01262   if (!hasMetadata())
01263     return;
01264 
01265   auto &Store = getContext().pImpl->FunctionMetadata[this];
01266   Store.erase(KindID);
01267   if (Store.empty())
01268     clearMetadata();
01269 }
01270 
01271 void Function::setMetadata(StringRef Kind, MDNode *MD) {
01272   if (!MD && !hasMetadata())
01273     return;
01274   setMetadata(getContext().getMDKindID(Kind), MD);
01275 }
01276 
01277 void Function::getAllMetadata(
01278     SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs) const {
01279   MDs.clear();
01280 
01281   if (!hasMetadata())
01282     return;
01283 
01284   getContext().pImpl->FunctionMetadata[this].getAll(MDs);
01285 }
01286 
01287 void Function::dropUnknownMetadata(ArrayRef<unsigned> KnownIDs) {
01288   if (!hasMetadata())
01289     return;
01290   if (KnownIDs.empty()) {
01291     clearMetadata();
01292     return;
01293   }
01294 
01295   SmallSet<unsigned, 5> KnownSet;
01296   KnownSet.insert(KnownIDs.begin(), KnownIDs.end());
01297 
01298   auto &Store = getContext().pImpl->FunctionMetadata[this];
01299   assert(!Store.empty());
01300 
01301   Store.remove_if([&KnownSet](const std::pair<unsigned, TrackingMDNodeRef> &I) {
01302     return !KnownSet.count(I.first);
01303   });
01304 
01305   if (Store.empty())
01306     clearMetadata();
01307 }
01308 
01309 void Function::clearMetadata() {
01310   if (!hasMetadata())
01311     return;
01312   getContext().pImpl->FunctionMetadata.erase(this);
01313   setHasMetadataHashEntry(false);
01314 }
01315 
01316 void Function::setSubprogram(DISubprogram *SP) {
01317   setMetadata(LLVMContext::MD_dbg, SP);
01318 }
01319 
01320 DISubprogram *Function::getSubprogram() const {
01321   return cast_or_null<DISubprogram>(getMetadata(LLVMContext::MD_dbg));
01322 }