LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Metadata.cpp
Go to the documentation of this file.
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"
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"
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:
68  virtual ~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 // Provide out-of-line definition to prevent weak vtable.
87 
89  getParent()->replaceOperand(this, 0);
90 }
91 
93  getParent()->replaceOperand(this, NV);
94 }
95 
96 //===----------------------------------------------------------------------===//
97 // MDNode implementation.
98 //
99 
100 /// getOperandPtr - Helper function to get the MDNodeOperand's coallocated on
101 /// the end of the MDNode.
102 static MDNodeOperand *getOperandPtr(MDNode *N, unsigned Op) {
103  // Use <= instead of < to permit a one-past-the-end address.
104  assert(Op <= N->getNumOperands() && "Invalid operand number");
105  return reinterpret_cast<MDNodeOperand*>(N + 1) + Op;
106 }
107 
108 void MDNode::replaceOperandWith(unsigned i, Value *Val) {
109  MDNodeOperand *Op = getOperandPtr(this, i);
110  replaceOperand(Op, Val);
111 }
112 
113 MDNode::MDNode(LLVMContext &C, ArrayRef<Value*> Vals, bool isFunctionLocal)
114 : Value(Type::getMetadataTy(C), Value::MDNodeVal) {
115  NumOperands = Vals.size();
116 
117  if (isFunctionLocal)
118  setValueSubclassData(getSubclassDataFromValue() | FunctionLocalBit);
119 
120  // Initialize the operand list, which is co-allocated on the end of the node.
121  unsigned i = 0;
122  for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
123  Op != E; ++Op, ++i) {
124  new (Op) MDNodeOperand(Vals[i]);
125 
126  // Mark the first MDNodeOperand as being the first in the list of operands.
127  if (i == 0)
128  Op->setAsFirstOperand(1);
129  }
130 }
131 
132 /// ~MDNode - Destroy MDNode.
133 MDNode::~MDNode() {
134  assert((getSubclassDataFromValue() & DestroyFlag) != 0 &&
135  "Not being destroyed through destroy()?");
136  LLVMContextImpl *pImpl = getType()->getContext().pImpl;
137  if (isNotUniqued()) {
138  pImpl->NonUniquedMDNodes.erase(this);
139  } else {
140  pImpl->MDNodeSet.RemoveNode(this);
141  }
142 
143  // Destroy the operands.
144  for (MDNodeOperand *Op = getOperandPtr(this, 0), *E = Op+NumOperands;
145  Op != E; ++Op)
146  Op->~MDNodeOperand();
147 }
148 
149 static const Function *getFunctionForValue(Value *V) {
150  if (!V) return NULL;
151  if (Instruction *I = dyn_cast<Instruction>(V)) {
152  BasicBlock *BB = I->getParent();
153  return BB ? BB->getParent() : 0;
154  }
155  if (Argument *A = dyn_cast<Argument>(V))
156  return A->getParent();
157  if (BasicBlock *BB = dyn_cast<BasicBlock>(V))
158  return BB->getParent();
159  if (MDNode *MD = dyn_cast<MDNode>(V))
160  return MD->getFunction();
161  return NULL;
162 }
163 
164 #ifndef NDEBUG
165 static const Function *assertLocalFunction(const MDNode *N) {
166  if (!N->isFunctionLocal()) return 0;
167 
168  // FIXME: This does not handle cyclic function local metadata.
169  const Function *F = 0, *NewF = 0;
170  for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
171  if (Value *V = N->getOperand(i)) {
172  if (MDNode *MD = dyn_cast<MDNode>(V))
173  NewF = assertLocalFunction(MD);
174  else
175  NewF = getFunctionForValue(V);
176  }
177  if (F == 0)
178  F = NewF;
179  else
180  assert((NewF == 0 || F == NewF) &&"inconsistent function-local metadata");
181  }
182  return F;
183 }
184 #endif
185 
186 // getFunction - If this metadata is function-local and recursively has a
187 // function-local operand, return the first such operand's parent function.
188 // Otherwise, return null. getFunction() should not be used for performance-
189 // critical code because it recursively visits all the MDNode's operands.
191 #ifndef NDEBUG
192  return assertLocalFunction(this);
193 #else
194  if (!isFunctionLocal()) return NULL;
195  for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
196  if (const Function *F = getFunctionForValue(getOperand(i)))
197  return F;
198  return NULL;
199 #endif
200 }
201 
202 // destroy - Delete this node. Only when there are no uses.
203 void MDNode::destroy() {
204  setValueSubclassData(getSubclassDataFromValue() | DestroyFlag);
205  // Placement delete, then free the memory.
206  this->~MDNode();
207  free(this);
208 }
209 
210 /// isFunctionLocalValue - Return true if this is a value that would require a
211 /// function-local MDNode.
212 static bool isFunctionLocalValue(Value *V) {
213  return isa<Instruction>(V) || isa<Argument>(V) || isa<BasicBlock>(V) ||
214  (isa<MDNode>(V) && cast<MDNode>(V)->isFunctionLocal());
215 }
216 
217 MDNode *MDNode::getMDNode(LLVMContext &Context, ArrayRef<Value*> Vals,
218  FunctionLocalness FL, bool Insert) {
219  LLVMContextImpl *pImpl = Context.pImpl;
220 
221  // Add all the operand pointers. Note that we don't have to add the
222  // isFunctionLocal bit because that's implied by the operands.
223  // Note that if the operands are later nulled out, the node will be
224  // removed from the uniquing map.
226  for (unsigned i = 0; i != Vals.size(); ++i)
227  ID.AddPointer(Vals[i]);
228 
229  void *InsertPoint;
230  MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
231 
232  if (N || !Insert)
233  return N;
234 
235  bool isFunctionLocal = false;
236  switch (FL) {
237  case FL_Unknown:
238  for (unsigned i = 0; i != Vals.size(); ++i) {
239  Value *V = Vals[i];
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 
269  return getMDNode(Context, Vals, FL_Unknown);
270 }
271 
273  ArrayRef<Value*> Vals,
274  bool isFunctionLocal) {
275  return getMDNode(Context, Vals, isFunctionLocal ? FL_Yes : FL_No);
276 }
277 
279  return getMDNode(Context, Vals, FL_Unknown, false);
280 }
281 
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);
289  return N;
290 }
291 
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!");
303  N->destroy();
304 }
305 
306 /// getOperand - Return specified operand.
307 Value *MDNode::getOperand(unsigned i) const {
308  assert(i < getNumOperands() && "Invalid operand number");
309  return *getOperandPtr(const_cast<MDNode*>(this), i);
310 }
311 
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 
321 void 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.
328 void 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.
378  Profile(ID);
379  void *InsertPoint;
380  if (MDNode *N = pImpl->MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint)) {
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 
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 
419 static bool isContiguous(const ConstantRange &A, const ConstantRange &B) {
420  return A.getUpper() == B.getLower() || A.getLower() == B.getUpper();
421 }
422 
423 static bool canBeMerged(const ConstantRange &A, const ConstantRange &B) {
424  return !A.intersectWith(B).isEmptySet() || isContiguous(A, B);
425 }
426 
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 
444 static 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 
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 
525 static SmallVector<TrackingVH<MDNode>, 4> &getNMDOps(void *Operands) {
526  return *(SmallVector<TrackingVH<MDNode>, 4>*)Operands;
527 }
528 
529 NamedMDNode::NamedMDNode(const Twine &N)
530  : Name(N.str()), Parent(0),
531  Operands(new SmallVector<TrackingVH<MDNode>, 4>()) {
532 }
533 
534 NamedMDNode::~NamedMDNode() {
536  delete &getNMDOps(Operands);
537 }
538 
539 /// getNumOperands - Return number of NamedMDNode operands.
540 unsigned NamedMDNode::getNumOperands() const {
541  return (unsigned)getNMDOps(Operands).size();
542 }
543 
544 /// getOperand - Return specified operand.
545 MDNode *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.
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.
560  getParent()->eraseNamedMetadata(this);
561 }
562 
563 /// dropAllReferences - Remove all uses and clear node vector.
565  getNMDOps(Operands).clear();
566 }
567 
568 /// getName - Return a constant reference to this named metadata's name.
570  return StringRef(Name);
571 }
572 
573 //===----------------------------------------------------------------------===//
574 // Instruction Metadata method implementations.
575 //
576 
578  if (Node == 0 && !hasMetadata()) return;
579  setMetadata(getContext().getMDKindID(Kind), Node);
580 }
581 
582 MDNode *Instruction::getMetadataImpl(StringRef Kind) const {
583  return getMetadataImpl(getContext().getMDKindID(Kind));
584 }
585 
586 /// setMetadata - Set the metadata of of the specified kind to the specified
587 /// node. This updates/replaces metadata if already present, or removes it if
588 /// Node is null.
589 void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
590  if (Node == 0 && !hasMetadata()) return;
591 
592  // Handle 'dbg' as a special case since it is not stored in the hash table.
593  if (KindID == LLVMContext::MD_dbg) {
594  DbgLoc = DebugLoc::getFromDILocation(Node);
595  return;
596  }
597 
598  // Handle the case when we're adding/updating metadata on an instruction.
599  if (Node) {
601  assert(!Info.empty() == hasMetadataHashEntry() &&
602  "HasMetadata bit is wonked");
603  if (Info.empty()) {
604  setHasMetadataHashEntry(true);
605  } else {
606  // Handle replacement of an existing value.
607  for (unsigned i = 0, e = Info.size(); i != e; ++i)
608  if (Info[i].first == KindID) {
609  Info[i].second = Node;
610  return;
611  }
612  }
613 
614  // No replacement, just add it to the list.
615  Info.push_back(std::make_pair(KindID, Node));
616  return;
617  }
618 
619  // Otherwise, we're removing metadata from an instruction.
620  assert((hasMetadataHashEntry() ==
621  getContext().pImpl->MetadataStore.count(this)) &&
622  "HasMetadata bit out of date!");
623  if (!hasMetadataHashEntry())
624  return; // Nothing to remove!
626 
627  // Common case is removing the only entry.
628  if (Info.size() == 1 && Info[0].first == KindID) {
629  getContext().pImpl->MetadataStore.erase(this);
630  setHasMetadataHashEntry(false);
631  return;
632  }
633 
634  // Handle removal of an existing value.
635  for (unsigned i = 0, e = Info.size(); i != e; ++i)
636  if (Info[i].first == KindID) {
637  Info[i] = Info.back();
638  Info.pop_back();
639  assert(!Info.empty() && "Removing last entry should be handled above");
640  return;
641  }
642  // Otherwise, removing an entry that doesn't exist on the instruction.
643 }
644 
645 MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
646  // Handle 'dbg' as a special case since it is not stored in the hash table.
647  if (KindID == LLVMContext::MD_dbg)
648  return DbgLoc.getAsMDNode(getContext());
649 
650  if (!hasMetadataHashEntry()) return 0;
651 
653  assert(!Info.empty() && "bit out of sync with hash table");
654 
655  for (LLVMContextImpl::MDMapTy::iterator I = Info.begin(), E = Info.end();
656  I != E; ++I)
657  if (I->first == KindID)
658  return I->second;
659  return 0;
660 }
661 
662 void Instruction::getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,
663  MDNode*> > &Result) const {
664  Result.clear();
665 
666  // Handle 'dbg' as a special case since it is not stored in the hash table.
667  if (!DbgLoc.isUnknown()) {
668  Result.push_back(std::make_pair((unsigned)LLVMContext::MD_dbg,
669  DbgLoc.getAsMDNode(getContext())));
670  if (!hasMetadataHashEntry()) return;
671  }
672 
673  assert(hasMetadataHashEntry() &&
674  getContext().pImpl->MetadataStore.count(this) &&
675  "Shouldn't have called this");
676  const LLVMContextImpl::MDMapTy &Info =
677  getContext().pImpl->MetadataStore.find(this)->second;
678  assert(!Info.empty() && "Shouldn't have called this");
679 
680  Result.append(Info.begin(), Info.end());
681 
682  // Sort the resulting array so it is stable.
683  if (Result.size() > 1)
684  array_pod_sort(Result.begin(), Result.end());
685 }
686 
687 void Instruction::
688 getAllMetadataOtherThanDebugLocImpl(SmallVectorImpl<std::pair<unsigned,
689  MDNode*> > &Result) const {
690  Result.clear();
691  assert(hasMetadataHashEntry() &&
692  getContext().pImpl->MetadataStore.count(this) &&
693  "Shouldn't have called this");
694  const LLVMContextImpl::MDMapTy &Info =
695  getContext().pImpl->MetadataStore.find(this)->second;
696  assert(!Info.empty() && "Shouldn't have called this");
697  Result.append(Info.begin(), Info.end());
698 
699  // Sort the resulting array so it is stable.
700  if (Result.size() > 1)
701  array_pod_sort(Result.begin(), Result.end());
702 }
703 
704 /// clearMetadataHashEntries - Clear all hashtable-based metadata from
705 /// this instruction.
706 void Instruction::clearMetadataHashEntries() {
707  assert(hasMetadataHashEntry() && "Caller should check");
708  getContext().pImpl->MetadataStore.erase(this);
709  setHasMetadataHashEntry(false);
710 }
711 
void AddPointer(const void *Ptr)
Definition: FoldingSet.cpp:52
StringRef getName() const
getName - Return a constant reference to this named metadata's name.
Definition: Metadata.cpp:569
void replaceOperandWith(unsigned i, Value *NewVal)
replaceOperandWith - Replace a specific operand.
Definition: Metadata.cpp:108
static void deleteTemporary(MDNode *N)
Definition: Metadata.cpp:292
IntegerType * getType() const
Definition: Constants.h:139
static MDNode * getTemporary(LLVMContext &Context, ArrayRef< Value * > Vals)
Definition: Metadata.cpp:282
const ValueTy & getValue() const
Definition: StringMap.h:132
static MDNodeOperand * getOperandPtr(MDNode *N, unsigned Op)
Definition: Metadata.cpp:102
LLVM Argument representation.
Definition: Argument.h:35
enable_if_c<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:266
unsigned getNumOperands() const
getNumOperands - Return number of MDNode operands.
Definition: Metadata.h:142
void addOperand(MDNode *M)
addOperand - Add metadata operand.
Definition: Metadata.cpp:551
static const Function * getFunctionForValue(Value *V)
Definition: Metadata.cpp:149
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:116
MDNode * getAsMDNode(const LLVMContext &Ctx) const
Definition: DebugLoc.cpp:100
MDNode - a tuple of other values.
Definition: Metadata.h:69
static bool canBeMerged(const ConstantRange &A, const ConstantRange &B)
Definition: Metadata.cpp:423
F(f)
static bool isFunctionLocalValue(Value *V)
Definition: Metadata.cpp:212
static MDNode * get(LLVMContext &Context, ArrayRef< Value * > Vals)
Definition: Metadata.cpp:268
Value * getOperand(unsigned i) const LLVM_READONLY
getOperand - Return specified operand.
Definition: Metadata.cpp:307
virtual ~MDNodeOperand()
Definition: Metadata.cpp:86
void setValPtr(Value *P)
Definition: ValueHandle.h:349
bool isUnknown() const
isUnknown - Return true if this is an unknown location.
Definition: DebugLoc.h:70
static void removeGarbageObject(void *Object)
Definition: LeakDetector.h:47
const APInt & getValue() const
Return the constant's value.
Definition: Constants.h:105
void eraseNamedMetadata(NamedMDNode *NMD)
Definition: Module.cpp:308
void eraseFromParent()
Definition: Metadata.cpp:559
ID
LLVM Calling Convention Representation.
Definition: CallingConv.h:26
void setAsFirstOperand(unsigned V)
Definition: Metadata.cpp:78
LLVMContext & getContext() const
getContext - Return the LLVMContext in which this type was uniqued.
Definition: Type.h:128
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Definition: SmallVector.h:56
static const Function * assertLocalFunction(const MDNode *N)
Definition: Metadata.cpp:165
ConstantRange unionWith(const ConstantRange &CR) const
void replaceAllUsesWith(Value *V)
Definition: Value.cpp:303
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:109
virtual void allUsesReplacedWith(Value *NV)
Definition: Metadata.cpp:92
A self-contained host- and target-independent arbitrary-precision floating-point software implementat...
Definition: APFloat.h:122
static MDNode * getMostGenericRange(MDNode *A, MDNode *B)
Definition: Metadata.cpp:454
void array_pod_sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:289
ConstantRange intersectWith(const ConstantRange &CR) const
cmpResult compare(const APFloat &) const
Definition: APFloat.cpp:1859
bool isFullSet() const
LLVM Basic Block Representation.
Definition: BasicBlock.h:72
void Profile(FoldingSetNodeID &ID) const
Definition: Metadata.cpp:312
static bool isContiguous(const ConstantRange &A, const ConstantRange &B)
Definition: Metadata.cpp:419
FoldingSet< MDNode > MDNodeSet
void free(void *ptr);
static void addRange(SmallVectorImpl< Value * > &EndPoints, ConstantInt *Low, ConstantInt *High)
Definition: Metadata.cpp:444
DenseMap< const Instruction *, MDMapTy > MetadataStore
bool hasMetadata() const
Definition: Instruction.h:128
MDNode * getOperand(unsigned i) const
getOperand - Return specified operand.
Definition: Metadata.cpp:545
SmallPtrSet< MDNode *, 1 > NonUniquedMDNodes
bool isEmptySet() const
static SmallVector< TrackingVH< MDNode >, 4 > & getNMDOps(void *Operands)
Definition: Metadata.cpp:525
void setValueName(ValueName *VN)
Definition: Value.h:119
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:517
void setMetadata(unsigned KindID, MDNode *Node)
Definition: Metadata.cpp:589
LLVMContextImpl *const pImpl
Definition: LLVMContext.h:39
static MDNode * getWhenValsUnresolved(LLVMContext &Context, ArrayRef< Value * > Vals, bool isFunctionLocal)
Definition: Metadata.cpp:272
Class for constant integers.
Definition: Constants.h:51
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition: APInt.cpp:547
static MDNode * getIfExists(LLVMContext &Context, ArrayRef< Value * > Vals)
Definition: Metadata.cpp:278
Type * getType() const
Definition: Value.h:111
const APInt & getLower() const
Definition: ConstantRange.h:79
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
Definition: Constants.cpp:492
Class for arbitrary precision integers.
Definition: APInt.h:75
static void addGarbageObject(void *Object)
Definition: LeakDetector.h:37
unsigned getValPtrInt() const
Definition: ValueHandle.h:106
friend class MDNodeOperand
Definition: Metadata.h:72
MapEntryTy & GetOrCreateValue(StringRef Key, InitTy Val)
Definition: StringMap.h:361
virtual void deleted()
Definition: Metadata.cpp:88
bool isFunctionLocal() const
isFunctionLocal - Return whether MDNode is local to a function.
Definition: Metadata.h:145
unsigned short getSubclassDataFromValue() const
Definition: Value.h:347
void *malloc(size_t size);
StringMap< Value * > MDStringCache
unsigned ComputeHash() const
Definition: FoldingSet.cpp:145
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
void resize(unsigned N)
Definition: SmallVector.h:401
static DebugLoc getFromDILocation(MDNode *N)
getFromDILocation - Translate the DILocation quad into a DebugLoc.
Definition: DebugLoc.cpp:117
bool use_empty() const
Definition: Value.h:149
const APInt & getUpper() const
Definition: ConstantRange.h:83
LLVM Value Representation.
Definition: Value.h:66
void set(Value *V)
Definition: Metadata.cpp:70
unsigned getNumOperands() const
getNumOperands - Return the number of NamedMDNode operands.
Definition: Metadata.cpp:540
void dropAllReferences()
dropAllReferences - Remove all uses and clear node vector.
Definition: Metadata.cpp:564
void setValPtrInt(unsigned K)
Definition: ValueHandle.h:105
static bool tryMergeRange(SmallVectorImpl< Value * > &EndPoints, ConstantInt *Low, ConstantInt *High)
Definition: Metadata.cpp:427
MDNodeOperand(Value *V)
Definition: Metadata.cpp:67
static MDNode * getMostGenericFPMath(MDNode *A, MDNode *B)
Definition: Metadata.cpp:408
const Function * getFunction() const
Definition: Metadata.cpp:190
Module * getParent()
getParent - Get the module that holds this named metadata collection.
Definition: Metadata.h:218