• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
anchor()33 void MDString::anchor() { }
34 
MDString(LLVMContext & C)35 MDString::MDString(LLVMContext &C)
36   : Value(Type::getMetadataTy(C), Value::MDStringVal) {}
37 
get(LLVMContext & Context,StringRef Str)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 {
getParent()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:
MDNodeOperand(Value * V)67   MDNodeOperand(Value *V) : CallbackVH(V) {}
~MDNodeOperand()68   ~MDNodeOperand() {}
69 
set(Value * V)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.
setAsFirstOperand(unsigned V)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 
deleted()86 void MDNodeOperand::deleted() {
87   getParent()->replaceOperand(this, 0);
88 }
89 
allUsesReplacedWith(Value * NV)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.
getOperandPtr(MDNode * N,unsigned Op)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 
replaceOperandWith(unsigned i,Value * Val)106 void MDNode::replaceOperandWith(unsigned i, Value *Val) {
107   MDNodeOperand *Op = getOperandPtr(this, i);
108   replaceOperand(Op, Val);
109 }
110 
MDNode(LLVMContext & C,ArrayRef<Value * > Vals,bool isFunctionLocal)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.
~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 
getFunctionForValue(Value * V)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
assertLocalFunction(const MDNode * N)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.
getFunction() const188 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.
destroy()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.
isFunctionLocalValue(Value * V)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 
getMDNode(LLVMContext & Context,ArrayRef<Value * > Vals,FunctionLocalness FL,bool Insert)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 
get(LLVMContext & Context,ArrayRef<Value * > Vals)266 MDNode *MDNode::get(LLVMContext &Context, ArrayRef<Value*> Vals) {
267   return getMDNode(Context, Vals, FL_Unknown);
268 }
269 
getWhenValsUnresolved(LLVMContext & Context,ArrayRef<Value * > Vals,bool isFunctionLocal)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 
getIfExists(LLVMContext & Context,ArrayRef<Value * > Vals)276 MDNode *MDNode::getIfExists(LLVMContext &Context, ArrayRef<Value*> Vals) {
277   return getMDNode(Context, Vals, FL_Unknown, false);
278 }
279 
getTemporary(LLVMContext & Context,ArrayRef<Value * > Vals)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 
deleteTemporary(MDNode * N)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.
getOperand(unsigned i) const305 Value *MDNode::getOperand(unsigned i) const {
306   assert(i < getNumOperands() && "Invalid operand number");
307   return *getOperandPtr(const_cast<MDNode*>(this), i);
308 }
309 
Profile(FoldingSetNodeID & ID) const310 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 
setIsNotUniqued()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.
replaceOperand(MDNodeOperand * Op,Value * To)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 
getMostGenericFPMath(MDNode * A,MDNode * B)406 MDNode *MDNode::getMostGenericFPMath(MDNode *A, MDNode *B) {
407   if (!A || !B)
408     return NULL;
409 
410   APFloat AVal = cast<ConstantFP>(A->getOperand(0))->getValueAPF();
411   APFloat BVal = cast<ConstantFP>(B->getOperand(0))->getValueAPF();
412   if (AVal.compare(BVal) == APFloat::cmpLessThan)
413     return A;
414   return B;
415 }
416 
isContiguous(const ConstantRange & A,const ConstantRange & B)417 static bool isContiguous(const ConstantRange &A, const ConstantRange &B) {
418   return A.getUpper() == B.getLower() || A.getLower() == B.getUpper();
419 }
420 
canBeMerged(const ConstantRange & A,const ConstantRange & B)421 static bool canBeMerged(const ConstantRange &A, const ConstantRange &B) {
422   return !A.intersectWith(B).isEmptySet() || isContiguous(A, B);
423 }
424 
tryMergeRange(SmallVectorImpl<Value * > & EndPoints,ConstantInt * Low,ConstantInt * High)425 static bool tryMergeRange(SmallVectorImpl<Value *> &EndPoints, ConstantInt *Low,
426                           ConstantInt *High) {
427   ConstantRange NewRange(Low->getValue(), High->getValue());
428   unsigned Size = EndPoints.size();
429   APInt LB = cast<ConstantInt>(EndPoints[Size - 2])->getValue();
430   APInt LE = cast<ConstantInt>(EndPoints[Size - 1])->getValue();
431   ConstantRange LastRange(LB, LE);
432   if (canBeMerged(NewRange, LastRange)) {
433     ConstantRange Union = LastRange.unionWith(NewRange);
434     Type *Ty = High->getType();
435     EndPoints[Size - 2] = ConstantInt::get(Ty, Union.getLower());
436     EndPoints[Size - 1] = ConstantInt::get(Ty, Union.getUpper());
437     return true;
438   }
439   return false;
440 }
441 
addRange(SmallVectorImpl<Value * > & EndPoints,ConstantInt * Low,ConstantInt * High)442 static void addRange(SmallVectorImpl<Value *> &EndPoints, ConstantInt *Low,
443                      ConstantInt *High) {
444   if (!EndPoints.empty())
445     if (tryMergeRange(EndPoints, Low, High))
446       return;
447 
448   EndPoints.push_back(Low);
449   EndPoints.push_back(High);
450 }
451 
getMostGenericRange(MDNode * A,MDNode * B)452 MDNode *MDNode::getMostGenericRange(MDNode *A, MDNode *B) {
453   // Given two ranges, we want to compute the union of the ranges. This
454   // is slightly complitade by having to combine the intervals and merge
455   // the ones that overlap.
456 
457   if (!A || !B)
458     return NULL;
459 
460   if (A == B)
461     return A;
462 
463   // First, walk both lists in older of the lower boundary of each interval.
464   // At each step, try to merge the new interval to the last one we adedd.
465   SmallVector<Value*, 4> EndPoints;
466   int AI = 0;
467   int BI = 0;
468   int AN = A->getNumOperands() / 2;
469   int BN = B->getNumOperands() / 2;
470   while (AI < AN && BI < BN) {
471     ConstantInt *ALow = cast<ConstantInt>(A->getOperand(2 * AI));
472     ConstantInt *BLow = cast<ConstantInt>(B->getOperand(2 * BI));
473 
474     if (ALow->getValue().slt(BLow->getValue())) {
475       addRange(EndPoints, ALow, cast<ConstantInt>(A->getOperand(2 * AI + 1)));
476       ++AI;
477     } else {
478       addRange(EndPoints, BLow, cast<ConstantInt>(B->getOperand(2 * BI + 1)));
479       ++BI;
480     }
481   }
482   while (AI < AN) {
483     addRange(EndPoints, cast<ConstantInt>(A->getOperand(2 * AI)),
484              cast<ConstantInt>(A->getOperand(2 * AI + 1)));
485     ++AI;
486   }
487   while (BI < BN) {
488     addRange(EndPoints, cast<ConstantInt>(B->getOperand(2 * BI)),
489              cast<ConstantInt>(B->getOperand(2 * BI + 1)));
490     ++BI;
491   }
492 
493   // If we have more than 2 ranges (4 endpoints) we have to try to merge
494   // the last and first ones.
495   unsigned Size = EndPoints.size();
496   if (Size > 4) {
497     ConstantInt *FB = cast<ConstantInt>(EndPoints[0]);
498     ConstantInt *FE = cast<ConstantInt>(EndPoints[1]);
499     if (tryMergeRange(EndPoints, FB, FE)) {
500       for (unsigned i = 0; i < Size - 2; ++i) {
501         EndPoints[i] = EndPoints[i + 2];
502       }
503       EndPoints.resize(Size - 2);
504     }
505   }
506 
507   // If in the end we have a single range, it is possible that it is now the
508   // full range. Just drop the metadata in that case.
509   if (EndPoints.size() == 2) {
510     ConstantRange Range(cast<ConstantInt>(EndPoints[0])->getValue(),
511                         cast<ConstantInt>(EndPoints[1])->getValue());
512     if (Range.isFullSet())
513       return NULL;
514   }
515 
516   return MDNode::get(A->getContext(), EndPoints);
517 }
518 
519 //===----------------------------------------------------------------------===//
520 // NamedMDNode implementation.
521 //
522 
getNMDOps(void * Operands)523 static SmallVector<TrackingVH<MDNode>, 4> &getNMDOps(void *Operands) {
524   return *(SmallVector<TrackingVH<MDNode>, 4>*)Operands;
525 }
526 
NamedMDNode(const Twine & N)527 NamedMDNode::NamedMDNode(const Twine &N)
528   : Name(N.str()), Parent(0),
529     Operands(new SmallVector<TrackingVH<MDNode>, 4>()) {
530 }
531 
~NamedMDNode()532 NamedMDNode::~NamedMDNode() {
533   dropAllReferences();
534   delete &getNMDOps(Operands);
535 }
536 
537 /// getNumOperands - Return number of NamedMDNode operands.
getNumOperands() const538 unsigned NamedMDNode::getNumOperands() const {
539   return (unsigned)getNMDOps(Operands).size();
540 }
541 
542 /// getOperand - Return specified operand.
getOperand(unsigned i) const543 MDNode *NamedMDNode::getOperand(unsigned i) const {
544   assert(i < getNumOperands() && "Invalid Operand number!");
545   return dyn_cast<MDNode>(&*getNMDOps(Operands)[i]);
546 }
547 
548 /// addOperand - Add metadata Operand.
addOperand(MDNode * M)549 void NamedMDNode::addOperand(MDNode *M) {
550   assert(!M->isFunctionLocal() &&
551          "NamedMDNode operands must not be function-local!");
552   getNMDOps(Operands).push_back(TrackingVH<MDNode>(M));
553 }
554 
555 /// eraseFromParent - Drop all references and remove the node from parent
556 /// module.
eraseFromParent()557 void NamedMDNode::eraseFromParent() {
558   getParent()->eraseNamedMetadata(this);
559 }
560 
561 /// dropAllReferences - Remove all uses and clear node vector.
dropAllReferences()562 void NamedMDNode::dropAllReferences() {
563   getNMDOps(Operands).clear();
564 }
565 
566 /// getName - Return a constant reference to this named metadata's name.
getName() const567 StringRef NamedMDNode::getName() const {
568   return StringRef(Name);
569 }
570 
571 //===----------------------------------------------------------------------===//
572 // Instruction Metadata method implementations.
573 //
574 
setMetadata(StringRef Kind,MDNode * Node)575 void Instruction::setMetadata(StringRef Kind, MDNode *Node) {
576   if (Node == 0 && !hasMetadata()) return;
577   setMetadata(getContext().getMDKindID(Kind), Node);
578 }
579 
getMetadataImpl(StringRef Kind) const580 MDNode *Instruction::getMetadataImpl(StringRef Kind) const {
581   return getMetadataImpl(getContext().getMDKindID(Kind));
582 }
583 
584 /// setMetadata - Set the metadata of of the specified kind to the specified
585 /// node.  This updates/replaces metadata if already present, or removes it if
586 /// Node is null.
setMetadata(unsigned KindID,MDNode * Node)587 void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
588   if (Node == 0 && !hasMetadata()) return;
589 
590   // Handle 'dbg' as a special case since it is not stored in the hash table.
591   if (KindID == LLVMContext::MD_dbg) {
592     DbgLoc = DebugLoc::getFromDILocation(Node);
593     return;
594   }
595 
596   // Handle the case when we're adding/updating metadata on an instruction.
597   if (Node) {
598     LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
599     assert(!Info.empty() == hasMetadataHashEntry() &&
600            "HasMetadata bit is wonked");
601     if (Info.empty()) {
602       setHasMetadataHashEntry(true);
603     } else {
604       // Handle replacement of an existing value.
605       for (unsigned i = 0, e = Info.size(); i != e; ++i)
606         if (Info[i].first == KindID) {
607           Info[i].second = Node;
608           return;
609         }
610     }
611 
612     // No replacement, just add it to the list.
613     Info.push_back(std::make_pair(KindID, Node));
614     return;
615   }
616 
617   // Otherwise, we're removing metadata from an instruction.
618   assert((hasMetadataHashEntry() ==
619           getContext().pImpl->MetadataStore.count(this)) &&
620          "HasMetadata bit out of date!");
621   if (!hasMetadataHashEntry())
622     return;  // Nothing to remove!
623   LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
624 
625   // Common case is removing the only entry.
626   if (Info.size() == 1 && Info[0].first == KindID) {
627     getContext().pImpl->MetadataStore.erase(this);
628     setHasMetadataHashEntry(false);
629     return;
630   }
631 
632   // Handle removal of an existing value.
633   for (unsigned i = 0, e = Info.size(); i != e; ++i)
634     if (Info[i].first == KindID) {
635       Info[i] = Info.back();
636       Info.pop_back();
637       assert(!Info.empty() && "Removing last entry should be handled above");
638       return;
639     }
640   // Otherwise, removing an entry that doesn't exist on the instruction.
641 }
642 
getMetadataImpl(unsigned KindID) const643 MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
644   // Handle 'dbg' as a special case since it is not stored in the hash table.
645   if (KindID == LLVMContext::MD_dbg)
646     return DbgLoc.getAsMDNode(getContext());
647 
648   if (!hasMetadataHashEntry()) return 0;
649 
650   LLVMContextImpl::MDMapTy &Info = getContext().pImpl->MetadataStore[this];
651   assert(!Info.empty() && "bit out of sync with hash table");
652 
653   for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end();
654        I != E; ++I)
655     if (I->first == KindID)
656       return I->second;
657   return 0;
658 }
659 
getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,MDNode * >> & Result) const660 void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
661                                        MDNode*> > &Result) const {
662   Result.clear();
663 
664   // Handle 'dbg' as a special case since it is not stored in the hash table.
665   if (!DbgLoc.isUnknown()) {
666     Result.push_back(std::make_pair((unsigned)LLVMContext::MD_dbg,
667                                     DbgLoc.getAsMDNode(getContext())));
668     if (!hasMetadataHashEntry()) return;
669   }
670 
671   assert(hasMetadataHashEntry() &&
672          getContext().pImpl->MetadataStore.count(this) &&
673          "Shouldn't have called this");
674   const LLVMContextImpl::MDMapTy &Info =
675     getContext().pImpl->MetadataStore.find(this)->second;
676   assert(!Info.empty() && "Shouldn't have called this");
677 
678   Result.append(Info.begin(), Info.end());
679 
680   // Sort the resulting array so it is stable.
681   if (Result.size() > 1)
682     array_pod_sort(Result.begin(), Result.end());
683 }
684 
685 void Instruction::
getAllMetadataOtherThanDebugLocImpl(SmallVectorImpl<std::pair<unsigned,MDNode * >> & Result) const686 getAllMetadataOtherThanDebugLocImpl(SmallVectorImpl<std::pair<unsigned,
687                                     MDNode*> > &Result) const {
688   Result.clear();
689   assert(hasMetadataHashEntry() &&
690          getContext().pImpl->MetadataStore.count(this) &&
691          "Shouldn't have called this");
692   const LLVMContextImpl::MDMapTy &Info =
693     getContext().pImpl->MetadataStore.find(this)->second;
694   assert(!Info.empty() && "Shouldn't have called this");
695   Result.append(Info.begin(), Info.end());
696 
697   // Sort the resulting array so it is stable.
698   if (Result.size() > 1)
699     array_pod_sort(Result.begin(), Result.end());
700 }
701 
702 /// clearMetadataHashEntries - Clear all hashtable-based metadata from
703 /// this instruction.
clearMetadataHashEntries()704 void Instruction::clearMetadataHashEntries() {
705   assert(hasMetadataHashEntry() && "Caller should check");
706   getContext().pImpl->MetadataStore.erase(this);
707   setHasMetadataHashEntry(false);
708 }
709 
710