Print out the target-independent attributes in a comment before the function definition.
[oota-llvm.git] / lib / IR / Metadata.cpp
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/SmallString.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/IR/Instruction.h"
22 #include "llvm/IR/LLVMContext.h"
23 #include "llvm/IR/Module.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   assert(i < getNumOperands() && "Invalid operand number");
307   return *getOperandPtr(const_cast<MDNode*>(this), i);
308 }
309
310 void MDNode::Profile(FoldingSetNodeID &ID) const {
311   // Add all the operand pointers. Note that we don't have to add the
312   // isFunctionLocal bit because that's implied by the operands.
313   // Note that if the operands are later nulled out, the node will be
314   // removed from the uniquing map.
315   for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
316     ID.AddPointer(getOperand(i));
317 }
318
319 void MDNode::setIsNotUniqued() {
320   setValueSubclassData(getSubclassDataFromValue() | NotUniquedBit);
321   LLVMContextImpl *pImpl = getType()->getContext().pImpl;
322   pImpl->NonUniquedMDNodes.insert(this);
323 }
324
325 // Replace value from this node's operand list.
326 void MDNode::replaceOperand(MDNodeOperand *Op, Value *To) {
327   Value *From = *Op;
328
329   // If is possible that someone did GV->RAUW(inst), replacing a global variable
330   // with an instruction or some other function-local object.  If this is a
331   // non-function-local MDNode, it can't point to a function-local object.
332   // Handle this case by implicitly dropping the MDNode reference to null.
333   // Likewise if the MDNode is function-local but for a different function.
334   if (To && isFunctionLocalValue(To)) {
335     if (!isFunctionLocal())
336       To = 0;
337     else {
338       const Function *F = getFunction();
339       const Function *FV = getFunctionForValue(To);
340       // Metadata can be function-local without having an associated function.
341       // So only consider functions to have changed if non-null.
342       if (F && FV && F != FV)
343         To = 0;
344     }
345   }
346   
347   if (From == To)
348     return;
349
350   // Update the operand.
351   Op->set(To);
352
353   // If this node is already not being uniqued (because one of the operands
354   // already went to null), then there is nothing else to do here.
355   if (isNotUniqued()) return;
356
357   LLVMContextImpl *pImpl = getType()->getContext().pImpl;
358
359   // Remove "this" from the context map.  FoldingSet doesn't have to reprofile
360   // this node to remove it, so we don't care what state the operands are in.
361   pImpl->MDNodeSet.RemoveNode(this);
362
363   // If we are dropping an argument to null, we choose to not unique the MDNode
364   // anymore.  This commonly occurs during destruction, and uniquing these
365   // brings little reuse.  Also, this means we don't need to include
366   // isFunctionLocal bits in FoldingSetNodeIDs for MDNodes.
367   if (To == 0) {
368     setIsNotUniqued();
369     return;
370   }
371
372   // Now that the node is out of the folding set, get ready to reinsert it.
373   // First, check to see if another node with the same operands already exists
374   // in the set.  If so, then this node is redundant.
375   FoldingSetNodeID ID;
376   Profile(ID);
377   void *InsertPoint;
378   if (MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)) {
379     replaceAllUsesWith(N);
380     destroy();
381     return;
382   }
383
384   // Cache the operand hash.
385   Hash = ID.ComputeHash();
386   // InsertPoint will have been set by the FindNodeOrInsertPos call.
387   pImpl->MDNodeSet.InsertNode(this, InsertPoint);
388
389   // If this MDValue was previously function-local but no longer is, clear
390   // its function-local flag.
391   if (isFunctionLocal() && !isFunctionLocalValue(To)) {
392     bool isStillFunctionLocal = false;
393     for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
394       Value *V = getOperand(i);
395       if (!V) continue;
396       if (isFunctionLocalValue(V)) {
397         isStillFunctionLocal = true;
398         break;
399       }
400     }
401     if (!isStillFunctionLocal)
402       setValueSubclassData(getSubclassDataFromValue() & ~FunctionLocalBit);
403   }
404 }
405
406 MDNode *MDNode::getMostGenericTBAA(MDNode *A, MDNode *B) {
407   if (!A || !B)
408     return NULL;
409
410   if (A == B)
411     return A;
412
413   SmallVector<MDNode *, 4> PathA;
414   MDNode *T = A;
415   while (T) {
416     PathA.push_back(T);
417     T = T->getNumOperands() >= 2 ? cast_or_null<MDNode>(T->getOperand(1)) : 0;
418   }
419
420   SmallVector<MDNode *, 4> PathB;
421   T = B;
422   while (T) {
423     PathB.push_back(T);
424     T = T->getNumOperands() >= 2 ? cast_or_null<MDNode>(T->getOperand(1)) : 0;
425   }
426
427   int IA = PathA.size() - 1;
428   int IB = PathB.size() - 1;
429
430   MDNode *Ret = 0;
431   while (IA >= 0 && IB >=0) {
432     if (PathA[IA] == PathB[IB])
433       Ret = PathA[IA];
434     else
435       break;
436     --IA;
437     --IB;
438   }
439   return Ret;
440 }
441
442 MDNode *MDNode::getMostGenericFPMath(MDNode *A, MDNode *B) {
443   if (!A || !B)
444     return NULL;
445
446   APFloat AVal = cast<ConstantFP>(A->getOperand(0))->getValueAPF();
447   APFloat BVal = cast<ConstantFP>(B->getOperand(0))->getValueAPF();
448   if (AVal.compare(BVal) == APFloat::cmpLessThan)
449     return A;
450   return B;
451 }
452
453 static bool isContiguous(const ConstantRange &A, const ConstantRange &B) {
454   return A.getUpper() == B.getLower() || A.getLower() == B.getUpper();
455 }
456
457 static bool canBeMerged(const ConstantRange &A, const ConstantRange &B) {
458   return !A.intersectWith(B).isEmptySet() || isContiguous(A, B);
459 }
460
461 static bool tryMergeRange(SmallVector<Value*, 4> &EndPoints, ConstantInt *Low,
462                           ConstantInt *High) {
463   ConstantRange NewRange(Low->getValue(), High->getValue());
464   unsigned Size = EndPoints.size();
465   APInt LB = cast<ConstantInt>(EndPoints[Size - 2])->getValue();
466   APInt LE = cast<ConstantInt>(EndPoints[Size - 1])->getValue();
467   ConstantRange LastRange(LB, LE);
468   if (canBeMerged(NewRange, LastRange)) {
469     ConstantRange Union = LastRange.unionWith(NewRange);
470     Type *Ty = High->getType();
471     EndPoints[Size - 2] = ConstantInt::get(Ty, Union.getLower());
472     EndPoints[Size - 1] = ConstantInt::get(Ty, Union.getUpper());
473     return true;
474   }
475   return false;
476 }
477
478 static void addRange(SmallVector<Value*, 4> &EndPoints, ConstantInt *Low,
479                      ConstantInt *High) {
480   if (!EndPoints.empty())
481     if (tryMergeRange(EndPoints, Low, High))
482       return;
483
484   EndPoints.push_back(Low);
485   EndPoints.push_back(High);
486 }
487
488 MDNode *MDNode::getMostGenericRange(MDNode *A, MDNode *B) {
489   // Given two ranges, we want to compute the union of the ranges. This
490   // is slightly complitade by having to combine the intervals and merge
491   // the ones that overlap.
492
493   if (!A || !B)
494     return NULL;
495
496   if (A == B)
497     return A;
498
499   // First, walk both lists in older of the lower boundary of each interval.
500   // At each step, try to merge the new interval to the last one we adedd.
501   SmallVector<Value*, 4> EndPoints;
502   int AI = 0;
503   int BI = 0;
504   int AN = A->getNumOperands() / 2;
505   int BN = B->getNumOperands() / 2;
506   while (AI < AN && BI < BN) {
507     ConstantInt *ALow = cast<ConstantInt>(A->getOperand(2 * AI));
508     ConstantInt *BLow = cast<ConstantInt>(B->getOperand(2 * BI));
509
510     if (ALow->getValue().slt(BLow->getValue())) {
511       addRange(EndPoints, ALow, cast<ConstantInt>(A->getOperand(2 * AI + 1)));
512       ++AI;
513     } else {
514       addRange(EndPoints, BLow, cast<ConstantInt>(B->getOperand(2 * BI + 1)));
515       ++BI;
516     }
517   }
518   while (AI < AN) {
519     addRange(EndPoints, cast<ConstantInt>(A->getOperand(2 * AI)),
520              cast<ConstantInt>(A->getOperand(2 * AI + 1)));
521     ++AI;
522   }
523   while (BI < BN) {
524     addRange(EndPoints, cast<ConstantInt>(B->getOperand(2 * BI)),
525              cast<ConstantInt>(B->getOperand(2 * BI + 1)));
526     ++BI;
527   }
528
529   // If we have more than 2 ranges (4 endpoints) we have to try to merge
530   // the last and first ones.
531   unsigned Size = EndPoints.size();
532   if (Size > 4) {
533     ConstantInt *FB = cast<ConstantInt>(EndPoints[0]);
534     ConstantInt *FE = cast<ConstantInt>(EndPoints[1]);
535     if (tryMergeRange(EndPoints, FB, FE)) {
536       for (unsigned i = 0; i < Size - 2; ++i) {
537         EndPoints[i] = EndPoints[i + 2];
538       }
539       EndPoints.resize(Size - 2);
540     }
541   }
542
543   // If in the end we have a single range, it is possible that it is now the
544   // full range. Just drop the metadata in that case.
545   if (EndPoints.size() == 2) {
546     ConstantRange Range(cast<ConstantInt>(EndPoints[0])->getValue(),
547                         cast<ConstantInt>(EndPoints[1])->getValue());
548     if (Range.isFullSet())
549       return NULL;
550   }
551
552   return MDNode::get(A->getContext(), EndPoints);
553 }
554
555 //===----------------------------------------------------------------------===//
556 // NamedMDNode implementation.
557 //
558
559 static SmallVector<TrackingVH<MDNode>, 4> &getNMDOps(void *Operands) {
560   return *(SmallVector<TrackingVH<MDNode>, 4>*)Operands;
561 }
562
563 NamedMDNode::NamedMDNode(const Twine &N)
564   : Name(N.str()), Parent(0),
565     Operands(new SmallVector<TrackingVH<MDNode>, 4>()) {
566 }
567
568 NamedMDNode::~NamedMDNode() {
569   dropAllReferences();
570   delete &getNMDOps(Operands);
571 }
572
573 /// getNumOperands - Return number of NamedMDNode operands.
574 unsigned NamedMDNode::getNumOperands() const {
575   return (unsigned)getNMDOps(Operands).size();
576 }
577
578 /// getOperand - Return specified operand.
579 MDNode *NamedMDNode::getOperand(unsigned i) const {
580   assert(i < getNumOperands() && "Invalid Operand number!");
581   return dyn_cast<MDNode>(&*getNMDOps(Operands)[i]);
582 }
583
584 /// addOperand - Add metadata Operand.
585 void NamedMDNode::addOperand(MDNode *M) {
586   assert(!M->isFunctionLocal() &&
587          "NamedMDNode operands must not be function-local!");
588   getNMDOps(Operands).push_back(TrackingVH<MDNode>(M));
589 }
590
591 /// eraseFromParent - Drop all references and remove the node from parent
592 /// module.
593 void NamedMDNode::eraseFromParent() {
594   getParent()->eraseNamedMetadata(this);
595 }
596
597 /// dropAllReferences - Remove all uses and clear node vector.
598 void NamedMDNode::dropAllReferences() {
599   getNMDOps(Operands).clear();
600 }
601
602 /// getName - Return a constant reference to this named metadata's name.
603 StringRef NamedMDNode::getName() const {
604   return StringRef(Name);
605 }
606
607 //===----------------------------------------------------------------------===//
608 // Instruction Metadata method implementations.
609 //
610
611 void Instruction::setMetadata(StringRef Kind, MDNode *Node) {
612   if (Node == 0 && !hasMetadata()) return;
613   setMetadata(getContext().getMDKindID(Kind), Node);
614 }
615
616 MDNode *Instruction::getMetadataImpl(StringRef Kind) const {
617   return getMetadataImpl(getContext().getMDKindID(Kind));
618 }
619
620 /// setMetadata - Set the metadata of of the specified kind to the specified
621 /// node.  This updates/replaces metadata if already present, or removes it if
622 /// Node is null.
623 void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
624   if (Node == 0 && !hasMetadata()) return;
625
626   // Handle 'dbg' as a special case since it is not stored in the hash table.
627   if (KindID == LLVMContext::MD_dbg) {
628     DbgLoc = DebugLoc::getFromDILocation(Node);
629     return;
630   }
631   
632   // Handle the case when we're adding/updating metadata on an instruction.
633   if (Node) {
634     LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
635     assert(!Info.empty() == hasMetadataHashEntry() &&
636            "HasMetadata bit is wonked");
637     if (Info.empty()) {
638       setHasMetadataHashEntry(true);
639     } else {
640       // Handle replacement of an existing value.
641       for (unsigned i = 0, e = Info.size(); i != e; ++i)
642         if (Info[i].first == KindID) {
643           Info[i].second = Node;
644           return;
645         }
646     }
647
648     // No replacement, just add it to the list.
649     Info.push_back(std::make_pair(KindID, Node));
650     return;
651   }
652
653   // Otherwise, we're removing metadata from an instruction.
654   assert((hasMetadataHashEntry() ==
655           getContext().pImpl->MetadataStore.count(this)) &&
656          "HasMetadata bit out of date!");
657   if (!hasMetadataHashEntry())
658     return;  // Nothing to remove!
659   LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
660
661   // Common case is removing the only entry.
662   if (Info.size() == 1 && Info[0].first == KindID) {
663     getContext().pImpl->MetadataStore.erase(this);
664     setHasMetadataHashEntry(false);
665     return;
666   }
667
668   // Handle removal of an existing value.
669   for (unsigned i = 0, e = Info.size(); i != e; ++i)
670     if (Info[i].first == KindID) {
671       Info[i] = Info.back();
672       Info.pop_back();
673       assert(!Info.empty() && "Removing last entry should be handled above");
674       return;
675     }
676   // Otherwise, removing an entry that doesn't exist on the instruction.
677 }
678
679 MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
680   // Handle 'dbg' as a special case since it is not stored in the hash table.
681   if (KindID == LLVMContext::MD_dbg)
682     return DbgLoc.getAsMDNode(getContext());
683   
684   if (!hasMetadataHashEntry()) return 0;
685   
686   LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
687   assert(!Info.empty() && "bit out of sync with hash table");
688
689   for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end();
690        I != E; ++I)
691     if (I->first == KindID)
692       return I->second;
693   return 0;
694 }
695
696 void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
697                                        MDNode*> > &Result) const {
698   Result.clear();
699   
700   // Handle 'dbg' as a special case since it is not stored in the hash table.
701   if (!DbgLoc.isUnknown()) {
702     Result.push_back(std::make_pair((unsigned)LLVMContext::MD_dbg,
703                                     DbgLoc.getAsMDNode(getContext())));
704     if (!hasMetadataHashEntry()) return;
705   }
706   
707   assert(hasMetadataHashEntry() &&
708          getContext().pImpl->MetadataStore.count(this) &&
709          "Shouldn't have called this");
710   const LLVMContextImpl::MDMapTy &Info =
711     getContext().pImpl->MetadataStore.find(this)->second;
712   assert(!Info.empty() && "Shouldn't have called this");
713
714   Result.append(Info.begin(), Info.end());
715
716   // Sort the resulting array so it is stable.
717   if (Result.size() > 1)
718     array_pod_sort(Result.begin(), Result.end());
719 }
720
721 void Instruction::
722 getAllMetadataOtherThanDebugLocImpl(SmallVectorImpl<std::pair<unsigned,
723                                     MDNode*> > &Result) const {
724   Result.clear();
725   assert(hasMetadataHashEntry() &&
726          getContext().pImpl->MetadataStore.count(this) &&
727          "Shouldn't have called this");
728   const LLVMContextImpl::MDMapTy &Info =
729     getContext().pImpl->MetadataStore.find(this)->second;
730   assert(!Info.empty() && "Shouldn't have called this");
731   Result.append(Info.begin(), Info.end());
732
733   // Sort the resulting array so it is stable.
734   if (Result.size() > 1)
735     array_pod_sort(Result.begin(), Result.end());
736 }
737
738 /// clearMetadataHashEntries - Clear all hashtable-based metadata from
739 /// this instruction.
740 void Instruction::clearMetadataHashEntries() {
741   assert(hasMetadataHashEntry() && "Caller should check");
742   getContext().pImpl->MetadataStore.erase(this);
743   setHasMetadataHashEntry(false);
744 }
745