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