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