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