Home | History | Annotate | Download | only in IR
      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 "SymbolTableListTraitsImpl.h"
     17 #include "llvm/ADT/DenseMap.h"
     18 #include "llvm/ADT/STLExtras.h"
     19 #include "llvm/ADT/SmallSet.h"
     20 #include "llvm/ADT/SmallString.h"
     21 #include "llvm/ADT/StringMap.h"
     22 #include "llvm/IR/ConstantRange.h"
     23 #include "llvm/IR/Instruction.h"
     24 #include "llvm/IR/LLVMContext.h"
     25 #include "llvm/IR/LeakDetector.h"
     26 #include "llvm/IR/Module.h"
     27 #include "llvm/IR/ValueHandle.h"
     28 using namespace llvm;
     29 
     30 //===----------------------------------------------------------------------===//
     31 // MDString implementation.
     32 //
     33 
     34 void MDString::anchor() { }
     35 
     36 MDString::MDString(LLVMContext &C)
     37   : Value(Type::getMetadataTy(C), Value::MDStringVal) {}
     38 
     39 MDString *MDString::get(LLVMContext &Context, StringRef Str) {
     40   LLVMContextImpl *pImpl = Context.pImpl;
     41   StringMapEntry<Value*> &Entry =
     42     pImpl->MDStringCache.GetOrCreateValue(Str);
     43   Value *&S = Entry.getValue();
     44   if (!S) S = new MDString(Context);
     45   S->setValueName(&Entry);
     46   return cast<MDString>(S);
     47 }
     48 
     49 //===----------------------------------------------------------------------===//
     50 // MDNodeOperand implementation.
     51 //
     52 
     53 // Use CallbackVH to hold MDNode operands.
     54 namespace llvm {
     55 class MDNodeOperand : public CallbackVH {
     56   MDNode *getParent() {
     57     MDNodeOperand *Cur = this;
     58 
     59     while (Cur->getValPtrInt() != 1)
     60       --Cur;
     61 
     62     assert(Cur->getValPtrInt() == 1 &&
     63            "Couldn't find the beginning of the operand list!");
     64     return reinterpret_cast<MDNode*>(Cur) - 1;
     65   }
     66 
     67 public:
     68   MDNodeOperand(Value *V) : CallbackVH(V) {}
     69   virtual ~MDNodeOperand();
     70 
     71   void set(Value *V) {
     72     unsigned IsFirst = this->getValPtrInt();
     73     this->setValPtr(V);
     74     this->setAsFirstOperand(IsFirst);
     75   }
     76 
     77   /// setAsFirstOperand - Accessor method to mark the operand as the first in
     78   /// the list.
     79   void setAsFirstOperand(unsigned V) { this->setValPtrInt(V); }
     80 
     81   void deleted() override;
     82   void allUsesReplacedWith(Value *NV) override;
     83 };
     84 } // end namespace llvm.
     85 
     86 // Provide out-of-line definition to prevent weak vtable.
     87 MDNodeOperand::~MDNodeOperand() {}
     88 
     89 void MDNodeOperand::deleted() {
     90   getParent()->replaceOperand(this, nullptr);
     91 }
     92 
     93 void MDNodeOperand::allUsesReplacedWith(Value *NV) {
     94   getParent()->replaceOperand(this, NV);
     95 }
     96 
     97 //===----------------------------------------------------------------------===//
     98 // MDNode implementation.
     99 //
    100 
    101 /// getOperandPtr - Helper function to get the MDNodeOperand's coallocated on
    102 /// the end of the MDNode.
    103 static MDNodeOperand *getOperandPtr(MDNode *N, unsigned Op) {
    104   // Use <= instead of < to permit a one-past-the-end address.
    105   assert(Op <= N->getNumOperands() && "Invalid operand number");
    106   return reinterpret_cast<MDNodeOperand*>(N + 1) + Op;
    107 }
    108 
    109 void MDNode::replaceOperandWith(unsigned i, Value *Val) {
    110   MDNodeOperand *Op = getOperandPtr(this, i);
    111   replaceOperand(Op, Val);
    112 }
    113 
    114 MDNode::MDNode(LLVMContext &C, ArrayRef<Value*> Vals, bool isFunctionLocal)
    115 : Value(Type::getMetadataTy(C), Value::MDNodeVal) {
    116   NumOperands = Vals.size();
    117 
    118   if (isFunctionLocal)
    119     setValueSubclassData(getSubclassDataFromValue() | FunctionLocalBit);
    120 
    121   // Initialize the operand list, which is co-allocated on the end of the node.
    122   unsigned i = 0;
    123   for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
    124        Op != E; ++Op, ++i) {
    125     new (Op) MDNodeOperand(Vals[i]);
    126 
    127     // Mark the first MDNodeOperand as being the first in the list of operands.
    128     if (i == 0)
    129       Op->setAsFirstOperand(1);
    130   }
    131 }
    132 
    133 /// ~MDNode - Destroy MDNode.
    134 MDNode::~MDNode() {
    135   assert((getSubclassDataFromValue() & DestroyFlag) != 0 &&
    136          "Not being destroyed through destroy()?");
    137   LLVMContextImpl *pImpl = getType()->getContext().pImpl;
    138   if (isNotUniqued()) {
    139     pImpl->NonUniquedMDNodes.erase(this);
    140   } else {
    141     pImpl->MDNodeSet.RemoveNode(this);
    142   }
    143 
    144   // Destroy the operands.
    145   for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
    146        Op != E; ++Op)
    147     Op->~MDNodeOperand();
    148 }
    149 
    150 static const Function *getFunctionForValue(Value *V) {
    151   if (!V) return nullptr;
    152   if (Instruction *I = dyn_cast<Instruction>(V)) {
    153     BasicBlock *BB = I->getParent();
    154     return BB ? BB->getParent() : nullptr;
    155   }
    156   if (Argument *A = dyn_cast<Argument>(V))
    157     return A->getParent();
    158   if (BasicBlock *BB = dyn_cast<BasicBlock>(V))
    159     return BB->getParent();
    160   if (MDNode *MD = dyn_cast<MDNode>(V))
    161     return MD->getFunction();
    162   return nullptr;
    163 }
    164 
    165 #ifndef NDEBUG
    166 static const Function *assertLocalFunction(const MDNode *N) {
    167   if (!N->isFunctionLocal()) return nullptr;
    168 
    169   // FIXME: This does not handle cyclic function local metadata.
    170   const Function *F = nullptr, *NewF = nullptr;
    171   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
    172     if (Value *V = N->getOperand(i)) {
    173       if (MDNode *MD = dyn_cast<MDNode>(V))
    174         NewF = assertLocalFunction(MD);
    175       else
    176         NewF = getFunctionForValue(V);
    177     }
    178     if (!F)
    179       F = NewF;
    180     else
    181       assert((NewF == nullptr || F == NewF) &&
    182              "inconsistent function-local metadata");
    183   }
    184   return F;
    185 }
    186 #endif
    187 
    188 // getFunction - If this metadata is function-local and recursively has a
    189 // function-local operand, return the first such operand's parent function.
    190 // Otherwise, return null. getFunction() should not be used for performance-
    191 // critical code because it recursively visits all the MDNode's operands.
    192 const Function *MDNode::getFunction() const {
    193 #ifndef NDEBUG
    194   return assertLocalFunction(this);
    195 #else
    196   if (!isFunctionLocal()) return nullptr;
    197   for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
    198     if (const Function *F = getFunctionForValue(getOperand(i)))
    199       return F;
    200   return nullptr;
    201 #endif
    202 }
    203 
    204 // destroy - Delete this node.  Only when there are no uses.
    205 void MDNode::destroy() {
    206   setValueSubclassData(getSubclassDataFromValue() | DestroyFlag);
    207   // Placement delete, then free the memory.
    208   this->~MDNode();
    209   free(this);
    210 }
    211 
    212 /// isFunctionLocalValue - Return true if this is a value that would require a
    213 /// function-local MDNode.
    214 static bool isFunctionLocalValue(Value *V) {
    215   return isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) ||
    216          (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal());
    217 }
    218 
    219 MDNode *MDNode::getMDNode(LLVMContext &Context, ArrayRef<Value*> Vals,
    220                           FunctionLocalness FL, bool Insert) {
    221   LLVMContextImpl *pImpl = Context.pImpl;
    222 
    223   // Add all the operand pointers. Note that we don't have to add the
    224   // isFunctionLocal bit because that's implied by the operands.
    225   // Note that if the operands are later nulled out, the node will be
    226   // removed from the uniquing map.
    227   FoldingSetNodeID ID;
    228   for (Value *V : Vals)
    229     ID.AddPointer(V);
    230 
    231   void *InsertPoint;
    232   MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
    233 
    234   if (N || !Insert)
    235     return N;
    236 
    237   bool isFunctionLocal = false;
    238   switch (FL) {
    239   case FL_Unknown:
    240     for (Value *V : Vals) {
    241       if (!V) continue;
    242       if (isFunctionLocalValue(V)) {
    243         isFunctionLocal = true;
    244         break;
    245       }
    246     }
    247     break;
    248   case FL_No:
    249     isFunctionLocal = false;
    250     break;
    251   case FL_Yes:
    252     isFunctionLocal = true;
    253     break;
    254   }
    255 
    256   // Coallocate space for the node and Operands together, then placement new.
    257   void *Ptr = malloc(sizeof(MDNode) + Vals.size() * sizeof(MDNodeOperand));
    258   N = new (Ptr) MDNode(Context, Vals, isFunctionLocal);
    259 
    260   // Cache the operand hash.
    261   N->Hash = ID.ComputeHash();
    262 
    263   // InsertPoint will have been set by the FindNodeOrInsertPos call.
    264   pImpl->MDNodeSet.InsertNode(N, InsertPoint);
    265 
    266   return N;
    267 }
    268 
    269 MDNode *MDNode::get(LLVMContext &Context, ArrayRef<Value*> Vals) {
    270   return getMDNode(Context, Vals, FL_Unknown);
    271 }
    272 
    273 MDNode *MDNode::getWhenValsUnresolved(LLVMContext &Context,
    274                                       ArrayRef<Value*> Vals,
    275                                       bool isFunctionLocal) {
    276   return getMDNode(Context, Vals, isFunctionLocal ? FL_Yes : FL_No);
    277 }
    278 
    279 MDNode *MDNode::getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals) {
    280   return getMDNode(Context, Vals, FL_Unknown, false);
    281 }
    282 
    283 MDNode *MDNode::getTemporary(LLVMContext &Context, ArrayRef<Value*> Vals) {
    284   MDNode *N =
    285     (MDNode *)malloc(sizeof(MDNode) + Vals.size() * sizeof(MDNodeOperand));
    286   N = new (N) MDNode(Context, Vals, FL_No);
    287   N->setValueSubclassData(N->getSubclassDataFromValue() |
    288                           NotUniquedBit);
    289   LeakDetector::addGarbageObject(N);
    290   return N;
    291 }
    292 
    293 void MDNode::deleteTemporary(MDNode *N) {
    294   assert(N->use_empty() && "Temporary MDNode has uses!");
    295   assert(!N->getContext().pImpl->MDNodeSet.RemoveNode(N) &&
    296          "Deleting a non-temporary uniqued node!");
    297   assert(!N->getContext().pImpl->NonUniquedMDNodes.erase(N) &&
    298          "Deleting a non-temporary non-uniqued node!");
    299   assert((N->getSubclassDataFromValue() & NotUniquedBit) &&
    300          "Temporary MDNode does not have NotUniquedBit set!");
    301   assert((N->getSubclassDataFromValue() & DestroyFlag) == 0 &&
    302          "Temporary MDNode has DestroyFlag set!");
    303   LeakDetector::removeGarbageObject(N);
    304   N->destroy();
    305 }
    306 
    307 /// getOperand - Return specified operand.
    308 Value *MDNode::getOperand(unsigned i) const {
    309   assert(i < getNumOperands() && "Invalid operand number");
    310   return *getOperandPtr(const_cast<MDNode*>(this), i);
    311 }
    312 
    313 void MDNode::Profile(FoldingSetNodeID &ID) const {
    314   // Add all the operand pointers. Note that we don't have to add the
    315   // isFunctionLocal bit because that's implied by the operands.
    316   // Note that if the operands are later nulled out, the node will be
    317   // removed from the uniquing map.
    318   for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
    319     ID.AddPointer(getOperand(i));
    320 }
    321 
    322 void MDNode::setIsNotUniqued() {
    323   setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
    324   LLVMContextImpl *pImpl = getType()->getContext().pImpl;
    325   pImpl->NonUniquedMDNodes.insert(this);
    326 }
    327 
    328 // Replace value from this node's operand list.
    329 void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
    330   Value *From = *Op;
    331 
    332   // If is possible that someone did GV->RAUW(inst), replacing a global variable
    333   // with an instruction or some other function-local object.  If this is a
    334   // non-function-local MDNode, it can't point to a function-local object.
    335   // Handle this case by implicitly dropping the MDNode reference to null.
    336   // Likewise if the MDNode is function-local but for a different function.
    337   if (To && isFunctionLocalValue(To)) {
    338     if (!isFunctionLocal())
    339       To = nullptr;
    340     else {
    341       const Function *F = getFunction();
    342       const Function *FV = getFunctionForValue(To);
    343       // Metadata can be function-local without having an associated function.
    344       // So only consider functions to have changed if non-null.
    345       if (F && FV && F != FV)
    346         To = nullptr;
    347     }
    348   }
    349 
    350   if (From == To)
    351     return;
    352 
    353   // Update the operand.
    354   Op->set(To);
    355 
    356   // If this node is already not being uniqued (because one of the operands
    357   // already went to null), then there is nothing else to do here.
    358   if (isNotUniqued()) return;
    359 
    360   LLVMContextImpl *pImpl = getType()->getContext().pImpl;
    361 
    362   // Remove "this" from the context map.  FoldingSet doesn't have to reprofile
    363   // this node to remove it, so we don't care what state the operands are in.
    364   pImpl->MDNodeSet.RemoveNode(this);
    365 
    366   // If we are dropping an argument to null, we choose to not unique the MDNode
    367   // anymore.  This commonly occurs during destruction, and uniquing these
    368   // brings little reuse.  Also, this means we don't need to include
    369   // isFunctionLocal bits in FoldingSetNodeIDs for MDNodes.
    370   if (!To) {
    371     setIsNotUniqued();
    372     return;
    373   }
    374 
    375   // Now that the node is out of the folding set, get ready to reinsert it.
    376   // First, check to see if another node with the same operands already exists
    377   // in the set.  If so, then this node is redundant.
    378   FoldingSetNodeID ID;
    379   Profile(ID);
    380   void *InsertPoint;
    381   if (MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)) {
    382     replaceAllUsesWith(N);
    383     destroy();
    384     return;
    385   }
    386 
    387   // Cache the operand hash.
    388   Hash = ID.ComputeHash();
    389   // InsertPoint will have been set by the FindNodeOrInsertPos call.
    390   pImpl->MDNodeSet.InsertNode(this, InsertPoint);
    391 
    392   // If this MDValue was previously function-local but no longer is, clear
    393   // its function-local flag.
    394   if (isFunctionLocal() && !isFunctionLocalValue(To)) {
    395     bool isStillFunctionLocal = false;
    396     for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
    397       Value *V = getOperand(i);
    398       if (!V) continue;
    399       if (isFunctionLocalValue(V)) {
    400         isStillFunctionLocal = true;
    401         break;
    402       }
    403     }
    404     if (!isStillFunctionLocal)
    405       setValueSubclassData(getSubclassDataFromValue() & ~FunctionLocalBit);
    406   }
    407 }
    408 
    409 MDNode *MDNode::getMostGenericFPMath(MDNode *A, MDNode *B) {
    410   if (!A || !B)
    411     return nullptr;
    412 
    413   APFloat AVal = cast<ConstantFP>(A->getOperand(0))->getValueAPF();
    414   APFloat BVal = cast<ConstantFP>(B->getOperand(0))->getValueAPF();
    415   if (AVal.compare(BVal) == APFloat::cmpLessThan)
    416     return A;
    417   return B;
    418 }
    419 
    420 static bool isContiguous(const ConstantRange &A, const ConstantRange &B) {
    421   return A.getUpper() == B.getLower() || A.getLower() == B.getUpper();
    422 }
    423 
    424 static bool canBeMerged(const ConstantRange &A, const ConstantRange &B) {
    425   return !A.intersectWith(B).isEmptySet() || isContiguous(A, B);
    426 }
    427 
    428 static bool tryMergeRange(SmallVectorImpl<Value *> &EndPoints, ConstantInt *Low,
    429                           ConstantInt *High) {
    430   ConstantRange NewRange(Low->getValue(), High->getValue());
    431   unsigned Size = EndPoints.size();
    432   APInt LB = cast<ConstantInt>(EndPoints[Size - 2])->getValue();
    433   APInt LE = cast<ConstantInt>(EndPoints[Size - 1])->getValue();
    434   ConstantRange LastRange(LB, LE);
    435   if (canBeMerged(NewRange, LastRange)) {
    436     ConstantRange Union = LastRange.unionWith(NewRange);
    437     Type *Ty = High->getType();
    438     EndPoints[Size - 2] = ConstantInt::get(Ty, Union.getLower());
    439     EndPoints[Size - 1] = ConstantInt::get(Ty, Union.getUpper());
    440     return true;
    441   }
    442   return false;
    443 }
    444 
    445 static void addRange(SmallVectorImpl<Value *> &EndPoints, ConstantInt *Low,
    446                      ConstantInt *High) {
    447   if (!EndPoints.empty())
    448     if (tryMergeRange(EndPoints, Low, High))
    449       return;
    450 
    451   EndPoints.push_back(Low);
    452   EndPoints.push_back(High);
    453 }
    454 
    455 MDNode *MDNode::getMostGenericRange(MDNode *A, MDNode *B) {
    456   // Given two ranges, we want to compute the union of the ranges. This
    457   // is slightly complitade by having to combine the intervals and merge
    458   // the ones that overlap.
    459 
    460   if (!A || !B)
    461     return nullptr;
    462 
    463   if (A == B)
    464     return A;
    465 
    466   // First, walk both lists in older of the lower boundary of each interval.
    467   // At each step, try to merge the new interval to the last one we adedd.
    468   SmallVector<Value*, 4> EndPoints;
    469   int AI = 0;
    470   int BI = 0;
    471   int AN = A->getNumOperands() / 2;
    472   int BN = B->getNumOperands() / 2;
    473   while (AI < AN && BI < BN) {
    474     ConstantInt *ALow = cast<ConstantInt>(A->getOperand(2 * AI));
    475     ConstantInt *BLow = cast<ConstantInt>(B->getOperand(2 * BI));
    476 
    477     if (ALow->getValue().slt(BLow->getValue())) {
    478       addRange(EndPoints, ALow, cast<ConstantInt>(A->getOperand(2 * AI + 1)));
    479       ++AI;
    480     } else {
    481       addRange(EndPoints, BLow, cast<ConstantInt>(B->getOperand(2 * BI + 1)));
    482       ++BI;
    483     }
    484   }
    485   while (AI < AN) {
    486     addRange(EndPoints, cast<ConstantInt>(A->getOperand(2 * AI)),
    487              cast<ConstantInt>(A->getOperand(2 * AI + 1)));
    488     ++AI;
    489   }
    490   while (BI < BN) {
    491     addRange(EndPoints, cast<ConstantInt>(B->getOperand(2 * BI)),
    492              cast<ConstantInt>(B->getOperand(2 * BI + 1)));
    493     ++BI;
    494   }
    495 
    496   // If we have more than 2 ranges (4 endpoints) we have to try to merge
    497   // the last and first ones.
    498   unsigned Size = EndPoints.size();
    499   if (Size > 4) {
    500     ConstantInt *FB = cast<ConstantInt>(EndPoints[0]);
    501     ConstantInt *FE = cast<ConstantInt>(EndPoints[1]);
    502     if (tryMergeRange(EndPoints, FB, FE)) {
    503       for (unsigned i = 0; i < Size - 2; ++i) {
    504         EndPoints[i] = EndPoints[i + 2];
    505       }
    506       EndPoints.resize(Size - 2);
    507     }
    508   }
    509 
    510   // If in the end we have a single range, it is possible that it is now the
    511   // full range. Just drop the metadata in that case.
    512   if (EndPoints.size() == 2) {
    513     ConstantRange Range(cast<ConstantInt>(EndPoints[0])->getValue(),
    514                         cast<ConstantInt>(EndPoints[1])->getValue());
    515     if (Range.isFullSet())
    516       return nullptr;
    517   }
    518 
    519   return MDNode::get(A->getContext(), EndPoints);
    520 }
    521 
    522 //===----------------------------------------------------------------------===//
    523 // NamedMDNode implementation.
    524 //
    525 
    526 static SmallVector<TrackingVH<MDNode>, 4> &getNMDOps(void *Operands) {
    527   return *(SmallVector<TrackingVH<MDNode>, 4>*)Operands;
    528 }
    529 
    530 NamedMDNode::NamedMDNode(const Twine &N)
    531   : Name(N.str()), Parent(nullptr),
    532     Operands(new SmallVector<TrackingVH<MDNode>, 4>()) {
    533 }
    534 
    535 NamedMDNode::~NamedMDNode() {
    536   dropAllReferences();
    537   delete &getNMDOps(Operands);
    538 }
    539 
    540 /// getNumOperands - Return number of NamedMDNode operands.
    541 unsigned NamedMDNode::getNumOperands() const {
    542   return (unsigned)getNMDOps(Operands).size();
    543 }
    544 
    545 /// getOperand - Return specified operand.
    546 MDNode *NamedMDNode::getOperand(unsigned i) const {
    547   assert(i < getNumOperands() && "Invalid Operand number!");
    548   return dyn_cast<MDNode>(&*getNMDOps(Operands)[i]);
    549 }
    550 
    551 /// addOperand - Add metadata Operand.
    552 void NamedMDNode::addOperand(MDNode *M) {
    553   assert(!M->isFunctionLocal() &&
    554          "NamedMDNode operands must not be function-local!");
    555   getNMDOps(Operands).push_back(TrackingVH<MDNode>(M));
    556 }
    557 
    558 /// eraseFromParent - Drop all references and remove the node from parent
    559 /// module.
    560 void NamedMDNode::eraseFromParent() {
    561   getParent()->eraseNamedMetadata(this);
    562 }
    563 
    564 /// dropAllReferences - Remove all uses and clear node vector.
    565 void NamedMDNode::dropAllReferences() {
    566   getNMDOps(Operands).clear();
    567 }
    568 
    569 /// getName - Return a constant reference to this named metadata's name.
    570 StringRef NamedMDNode::getName() const {
    571   return StringRef(Name);
    572 }
    573 
    574 //===----------------------------------------------------------------------===//
    575 // Instruction Metadata method implementations.
    576 //
    577 
    578 void Instruction::setMetadata(StringRef Kind, MDNode *Node) {
    579   if (!Node && !hasMetadata()) return;
    580   setMetadata(getContext().getMDKindID(Kind), Node);
    581 }
    582 
    583 MDNode *Instruction::getMetadataImpl(StringRef Kind) const {
    584   return getMetadataImpl(getContext().getMDKindID(Kind));
    585 }
    586 
    587 void Instruction::dropUnknownMetadata(ArrayRef<unsigned> KnownIDs) {
    588   SmallSet<unsigned, 5> KnownSet;
    589   KnownSet.insert(KnownIDs.begin(), KnownIDs.end());
    590 
    591   // Drop debug if needed
    592   if (KnownSet.erase(LLVMContext::MD_dbg))
    593     DbgLoc = DebugLoc();
    594 
    595   if (!hasMetadataHashEntry())
    596     return; // Nothing to remove!
    597 
    598   DenseMap<const Instruction *, LLVMContextImpl::MDMapTy> &MetadataStore =
    599       getContext().pImpl->MetadataStore;
    600 
    601   if (KnownSet.empty()) {
    602     // Just drop our entry at the store.
    603     MetadataStore.erase(this);
    604     setHasMetadataHashEntry(false);
    605     return;
    606   }
    607 
    608   LLVMContextImpl::MDMapTy &Info = MetadataStore[this];
    609   unsigned I;
    610   unsigned E;
    611   // Walk the array and drop any metadata we don't know.
    612   for (I = 0, E = Info.size(); I != E;) {
    613     if (KnownSet.count(Info[I].first)) {
    614       ++I;
    615       continue;
    616     }
    617 
    618     Info[I] = Info.back();
    619     Info.pop_back();
    620     --E;
    621   }
    622   assert(E == Info.size());
    623 
    624   if (E == 0) {
    625     // Drop our entry at the store.
    626     MetadataStore.erase(this);
    627     setHasMetadataHashEntry(false);
    628   }
    629 }
    630 
    631 /// setMetadata - Set the metadata of of the specified kind to the specified
    632 /// node.  This updates/replaces metadata if already present, or removes it if
    633 /// Node is null.
    634 void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
    635   if (!Node && !hasMetadata()) return;
    636 
    637   // Handle 'dbg' as a special case since it is not stored in the hash table.
    638   if (KindID == LLVMContext::MD_dbg) {
    639     DbgLoc = DebugLoc::getFromDILocation(Node);
    640     return;
    641   }
    642 
    643   // Handle the case when we're adding/updating metadata on an instruction.
    644   if (Node) {
    645     LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
    646     assert(!Info.empty() == hasMetadataHashEntry() &&
    647            "HasMetadata bit is wonked");
    648     if (Info.empty()) {
    649       setHasMetadataHashEntry(true);
    650     } else {
    651       // Handle replacement of an existing value.
    652       for (auto &P : Info)
    653         if (P.first == KindID) {
    654           P.second = Node;
    655           return;
    656         }
    657     }
    658 
    659     // No replacement, just add it to the list.
    660     Info.push_back(std::make_pair(KindID, Node));
    661     return;
    662   }
    663 
    664   // Otherwise, we're removing metadata from an instruction.
    665   assert((hasMetadataHashEntry() ==
    666           (getContext().pImpl->MetadataStore.count(this) > 0)) &&
    667          "HasMetadata bit out of date!");
    668   if (!hasMetadataHashEntry())
    669     return;  // Nothing to remove!
    670   LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
    671 
    672   // Common case is removing the only entry.
    673   if (Info.size() == 1 && Info[0].first == KindID) {
    674     getContext().pImpl->MetadataStore.erase(this);
    675     setHasMetadataHashEntry(false);
    676     return;
    677   }
    678 
    679   // Handle removal of an existing value.
    680   for (unsigned i = 0, e = Info.size(); i != e; ++i)
    681     if (Info[i].first == KindID) {
    682       Info[i] = Info.back();
    683       Info.pop_back();
    684       assert(!Info.empty() && "Removing last entry should be handled above");
    685       return;
    686     }
    687   // Otherwise, removing an entry that doesn't exist on the instruction.
    688 }
    689 
    690 MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
    691   // Handle 'dbg' as a special case since it is not stored in the hash table.
    692   if (KindID == LLVMContext::MD_dbg)
    693     return DbgLoc.getAsMDNode(getContext());
    694 
    695   if (!hasMetadataHashEntry()) return nullptr;
    696 
    697   LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
    698   assert(!Info.empty() && "bit out of sync with hash table");
    699 
    700   for (const auto &I : Info)
    701     if (I.first == KindID)
    702       return I.second;
    703   return nullptr;
    704 }
    705 
    706 void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
    707                                        MDNode*> > &Result) const {
    708   Result.clear();
    709 
    710   // Handle 'dbg' as a special case since it is not stored in the hash table.
    711   if (!DbgLoc.isUnknown()) {
    712     Result.push_back(std::make_pair((unsigned)LLVMContext::MD_dbg,
    713                                     DbgLoc.getAsMDNode(getContext())));
    714     if (!hasMetadataHashEntry()) return;
    715   }
    716 
    717   assert(hasMetadataHashEntry() &&
    718          getContext().pImpl->MetadataStore.count(this) &&
    719          "Shouldn't have called this");
    720   const LLVMContextImpl::MDMapTy &Info =
    721     getContext().pImpl->MetadataStore.find(this)->second;
    722   assert(!Info.empty() && "Shouldn't have called this");
    723 
    724   Result.append(Info.begin(), Info.end());
    725 
    726   // Sort the resulting array so it is stable.
    727   if (Result.size() > 1)
    728     array_pod_sort(Result.begin(), Result.end());
    729 }
    730 
    731 void Instruction::
    732 getAllMetadataOtherThanDebugLocImpl(SmallVectorImpl<std::pair<unsigned,
    733                                     MDNode*> > &Result) const {
    734   Result.clear();
    735   assert(hasMetadataHashEntry() &&
    736          getContext().pImpl->MetadataStore.count(this) &&
    737          "Shouldn't have called this");
    738   const LLVMContextImpl::MDMapTy &Info =
    739     getContext().pImpl->MetadataStore.find(this)->second;
    740   assert(!Info.empty() && "Shouldn't have called this");
    741   Result.append(Info.begin(), Info.end());
    742 
    743   // Sort the resulting array so it is stable.
    744   if (Result.size() > 1)
    745     array_pod_sort(Result.begin(), Result.end());
    746 }
    747 
    748 /// clearMetadataHashEntries - Clear all hashtable-based metadata from
    749 /// this instruction.
    750 void Instruction::clearMetadataHashEntries() {
    751   assert(hasMetadataHashEntry() && "Caller should check");
    752   getContext().pImpl->MetadataStore.erase(this);
    753   setHasMetadataHashEntry(false);
    754 }
    755 
    756