LLVM  3.7.0
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"
16 #include "MetadataImpl.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/ADT/SmallSet.h"
21 #include "llvm/ADT/SmallString.h"
22 #include "llvm/ADT/StringMap.h"
23 #include "llvm/IR/ConstantRange.h"
25 #include "llvm/IR/Instruction.h"
26 #include "llvm/IR/LLVMContext.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/IR/ValueHandle.h"
29 
30 using namespace llvm;
31 
32 MetadataAsValue::MetadataAsValue(Type *Ty, Metadata *MD)
33  : Value(Ty, MetadataAsValueVal), MD(MD) {
34  track();
35 }
36 
37 MetadataAsValue::~MetadataAsValue() {
38  getType()->getContext().pImpl->MetadataAsValues.erase(MD);
39  untrack();
40 }
41 
42 /// \brief Canonicalize metadata arguments to intrinsics.
43 ///
44 /// To support bitcode upgrades (and assembly semantic sugar) for \a
45 /// MetadataAsValue, we need to canonicalize certain metadata.
46 ///
47 /// - nullptr is replaced by an empty MDNode.
48 /// - An MDNode with a single null operand is replaced by an empty MDNode.
49 /// - An MDNode whose only operand is a \a ConstantAsMetadata gets skipped.
50 ///
51 /// This maintains readability of bitcode from when metadata was a type of
52 /// value, and these bridges were unnecessary.
54  Metadata *MD) {
55  if (!MD)
56  // !{}
57  return MDNode::get(Context, None);
58 
59  // Return early if this isn't a single-operand MDNode.
60  auto *N = dyn_cast<MDNode>(MD);
61  if (!N || N->getNumOperands() != 1)
62  return MD;
63 
64  if (!N->getOperand(0))
65  // !{}
66  return MDNode::get(Context, None);
67 
68  if (auto *C = dyn_cast<ConstantAsMetadata>(N->getOperand(0)))
69  // Look through the MDNode.
70  return C;
71 
72  return MD;
73 }
74 
76  MD = canonicalizeMetadataForValue(Context, MD);
77  auto *&Entry = Context.pImpl->MetadataAsValues[MD];
78  if (!Entry)
79  Entry = new MetadataAsValue(Type::getMetadataTy(Context), MD);
80  return Entry;
81 }
82 
84  Metadata *MD) {
85  MD = canonicalizeMetadataForValue(Context, MD);
86  auto &Store = Context.pImpl->MetadataAsValues;
87  return Store.lookup(MD);
88 }
89 
90 void MetadataAsValue::handleChangedMetadata(Metadata *MD) {
91  LLVMContext &Context = getContext();
92  MD = canonicalizeMetadataForValue(Context, MD);
93  auto &Store = Context.pImpl->MetadataAsValues;
94 
95  // Stop tracking the old metadata.
96  Store.erase(this->MD);
97  untrack();
98  this->MD = nullptr;
99 
100  // Start tracking MD, or RAUW if necessary.
101  auto *&Entry = Store[MD];
102  if (Entry) {
103  replaceAllUsesWith(Entry);
104  delete this;
105  return;
106  }
107 
108  this->MD = MD;
109  track();
110  Entry = this;
111 }
112 
113 void MetadataAsValue::track() {
114  if (MD)
115  MetadataTracking::track(&MD, *MD, *this);
116 }
117 
118 void MetadataAsValue::untrack() {
119  if (MD)
121 }
122 
123 void ReplaceableMetadataImpl::addRef(void *Ref, OwnerTy Owner) {
124  bool WasInserted =
125  UseMap.insert(std::make_pair(Ref, std::make_pair(Owner, NextIndex)))
126  .second;
127  (void)WasInserted;
128  assert(WasInserted && "Expected to add a reference");
129 
130  ++NextIndex;
131  assert(NextIndex != 0 && "Unexpected overflow");
132 }
133 
134 void ReplaceableMetadataImpl::dropRef(void *Ref) {
135  bool WasErased = UseMap.erase(Ref);
136  (void)WasErased;
137  assert(WasErased && "Expected to drop a reference");
138 }
139 
140 void ReplaceableMetadataImpl::moveRef(void *Ref, void *New,
141  const Metadata &MD) {
142  auto I = UseMap.find(Ref);
143  assert(I != UseMap.end() && "Expected to move a reference");
144  auto OwnerAndIndex = I->second;
145  UseMap.erase(I);
146  bool WasInserted = UseMap.insert(std::make_pair(New, OwnerAndIndex)).second;
147  (void)WasInserted;
148  assert(WasInserted && "Expected to add a reference");
149 
150  // Check that the references are direct if there's no owner.
151  (void)MD;
152  assert((OwnerAndIndex.first || *static_cast<Metadata **>(Ref) == &MD) &&
153  "Reference without owner must be direct");
154  assert((OwnerAndIndex.first || *static_cast<Metadata **>(New) == &MD) &&
155  "Reference without owner must be direct");
156 }
157 
159  assert(!(MD && isa<MDNode>(MD) && cast<MDNode>(MD)->isTemporary()) &&
160  "Expected non-temp node");
161 
162  if (UseMap.empty())
163  return;
164 
165  // Copy out uses since UseMap will get touched below.
166  typedef std::pair<void *, std::pair<OwnerTy, uint64_t>> UseTy;
167  SmallVector<UseTy, 8> Uses(UseMap.begin(), UseMap.end());
168  std::sort(Uses.begin(), Uses.end(), [](const UseTy &L, const UseTy &R) {
169  return L.second.second < R.second.second;
170  });
171  for (const auto &Pair : Uses) {
172  // Check that this Ref hasn't disappeared after RAUW (when updating a
173  // previous Ref).
174  if (!UseMap.count(Pair.first))
175  continue;
176 
177  OwnerTy Owner = Pair.second.first;
178  if (!Owner) {
179  // Update unowned tracking references directly.
180  Metadata *&Ref = *static_cast<Metadata **>(Pair.first);
181  Ref = MD;
182  if (MD)
184  UseMap.erase(Pair.first);
185  continue;
186  }
187 
188  // Check for MetadataAsValue.
189  if (Owner.is<MetadataAsValue *>()) {
190  Owner.get<MetadataAsValue *>()->handleChangedMetadata(MD);
191  continue;
192  }
193 
194  // There's a Metadata owner -- dispatch.
195  Metadata *OwnerMD = Owner.get<Metadata *>();
196  switch (OwnerMD->getMetadataID()) {
197 #define HANDLE_METADATA_LEAF(CLASS) \
198  case Metadata::CLASS##Kind: \
199  cast<CLASS>(OwnerMD)->handleChangedOperand(Pair.first, MD); \
200  continue;
201 #include "llvm/IR/Metadata.def"
202  default:
203  llvm_unreachable("Invalid metadata subclass");
204  }
205  }
206  assert(UseMap.empty() && "Expected all uses to be replaced");
207 }
208 
210  if (UseMap.empty())
211  return;
212 
213  if (!ResolveUsers) {
214  UseMap.clear();
215  return;
216  }
217 
218  // Copy out uses since UseMap could get touched below.
219  typedef std::pair<void *, std::pair<OwnerTy, uint64_t>> UseTy;
220  SmallVector<UseTy, 8> Uses(UseMap.begin(), UseMap.end());
221  std::sort(Uses.begin(), Uses.end(), [](const UseTy &L, const UseTy &R) {
222  return L.second.second < R.second.second;
223  });
224  UseMap.clear();
225  for (const auto &Pair : Uses) {
226  auto Owner = Pair.second.first;
227  if (!Owner)
228  continue;
229  if (Owner.is<MetadataAsValue *>())
230  continue;
231 
232  // Resolve MDNodes that point at this.
233  auto *OwnerMD = dyn_cast<MDNode>(Owner.get<Metadata *>());
234  if (!OwnerMD)
235  continue;
236  if (OwnerMD->isResolved())
237  continue;
238  OwnerMD->decrementUnresolvedOperandCount();
239  }
240 }
241 
243  assert(V && "Expected value");
244  if (auto *A = dyn_cast<Argument>(V))
245  return A->getParent();
246  if (BasicBlock *BB = cast<Instruction>(V)->getParent())
247  return BB->getParent();
248  return nullptr;
249 }
250 
252  assert(V && "Unexpected null Value");
253 
254  auto &Context = V->getContext();
255  auto *&Entry = Context.pImpl->ValuesAsMetadata[V];
256  if (!Entry) {
257  assert((isa<Constant>(V) || isa<Argument>(V) || isa<Instruction>(V)) &&
258  "Expected constant or function-local value");
259  assert(!V->IsUsedByMD &&
260  "Expected this to be the only metadata use");
261  V->IsUsedByMD = true;
262  if (auto *C = dyn_cast<Constant>(V))
263  Entry = new ConstantAsMetadata(C);
264  else
265  Entry = new LocalAsMetadata(V);
266  }
267 
268  return Entry;
269 }
270 
272  assert(V && "Unexpected null Value");
273  return V->getContext().pImpl->ValuesAsMetadata.lookup(V);
274 }
275 
277  assert(V && "Expected valid value");
278 
279  auto &Store = V->getType()->getContext().pImpl->ValuesAsMetadata;
280  auto I = Store.find(V);
281  if (I == Store.end())
282  return;
283 
284  // Remove old entry from the map.
285  ValueAsMetadata *MD = I->second;
286  assert(MD && "Expected valid metadata");
287  assert(MD->getValue() == V && "Expected valid mapping");
288  Store.erase(I);
289 
290  // Delete the metadata.
291  MD->replaceAllUsesWith(nullptr);
292  delete MD;
293 }
294 
296  assert(From && "Expected valid value");
297  assert(To && "Expected valid value");
298  assert(From != To && "Expected changed value");
299  assert(From->getType() == To->getType() && "Unexpected type change");
300 
301  LLVMContext &Context = From->getType()->getContext();
302  auto &Store = Context.pImpl->ValuesAsMetadata;
303  auto I = Store.find(From);
304  if (I == Store.end()) {
305  assert(!From->IsUsedByMD &&
306  "Expected From not to be used by metadata");
307  return;
308  }
309 
310  // Remove old entry from the map.
311  assert(From->IsUsedByMD &&
312  "Expected From to be used by metadata");
313  From->IsUsedByMD = false;
314  ValueAsMetadata *MD = I->second;
315  assert(MD && "Expected valid metadata");
316  assert(MD->getValue() == From && "Expected valid mapping");
317  Store.erase(I);
318 
319  if (isa<LocalAsMetadata>(MD)) {
320  if (auto *C = dyn_cast<Constant>(To)) {
321  // Local became a constant.
323  delete MD;
324  return;
325  }
326  if (getLocalFunction(From) && getLocalFunction(To) &&
327  getLocalFunction(From) != getLocalFunction(To)) {
328  // Function changed.
329  MD->replaceAllUsesWith(nullptr);
330  delete MD;
331  return;
332  }
333  } else if (!isa<Constant>(To)) {
334  // Changed to function-local value.
335  MD->replaceAllUsesWith(nullptr);
336  delete MD;
337  return;
338  }
339 
340  auto *&Entry = Store[To];
341  if (Entry) {
342  // The target already exists.
343  MD->replaceAllUsesWith(Entry);
344  delete MD;
345  return;
346  }
347 
348  // Update MD in place (and update the map entry).
349  assert(!To->IsUsedByMD &&
350  "Expected this to be the only metadata use");
351  To->IsUsedByMD = true;
352  MD->V = To;
353  Entry = MD;
354 }
355 
356 //===----------------------------------------------------------------------===//
357 // MDString implementation.
358 //
359 
361  auto &Store = Context.pImpl->MDStringCache;
362  auto I = Store.find(Str);
363  if (I != Store.end())
364  return &I->second;
365 
366  auto *Entry =
367  StringMapEntry<MDString>::Create(Str, Store.getAllocator(), MDString());
368  bool WasInserted = Store.insert(Entry);
369  (void)WasInserted;
370  assert(WasInserted && "Expected entry to be inserted");
371  Entry->second.Entry = Entry;
372  return &Entry->second;
373 }
374 
376  assert(Entry && "Expected to find string map entry");
377  return Entry->first();
378 }
379 
380 //===----------------------------------------------------------------------===//
381 // MDNode implementation.
382 //
383 
384 // Assert that the MDNode types will not be unaligned by the objects
385 // prepended to them.
386 #define HANDLE_MDNODE_LEAF(CLASS) \
387  static_assert( \
388  llvm::AlignOf<uint64_t>::Alignment >= llvm::AlignOf<CLASS>::Alignment, \
389  "Alignment is insufficient after objects prepended to " #CLASS);
390 #include "llvm/IR/Metadata.def"
391 
392 void *MDNode::operator new(size_t Size, unsigned NumOps) {
393  size_t OpSize = NumOps * sizeof(MDOperand);
394  // uint64_t is the most aligned type we need support (ensured by static_assert
395  // above)
396  OpSize = RoundUpToAlignment(OpSize, llvm::alignOf<uint64_t>());
397  void *Ptr = reinterpret_cast<char *>(::operator new(OpSize + Size)) + OpSize;
398  MDOperand *O = static_cast<MDOperand *>(Ptr);
399  for (MDOperand *E = O - NumOps; O != E; --O)
400  (void)new (O - 1) MDOperand;
401  return Ptr;
402 }
403 
404 void MDNode::operator delete(void *Mem) {
405  MDNode *N = static_cast<MDNode *>(Mem);
406  size_t OpSize = N->NumOperands * sizeof(MDOperand);
407  OpSize = RoundUpToAlignment(OpSize, llvm::alignOf<uint64_t>());
408 
409  MDOperand *O = static_cast<MDOperand *>(Mem);
410  for (MDOperand *E = O - N->NumOperands; O != E; --O)
411  (O - 1)->~MDOperand();
412  ::operator delete(reinterpret_cast<char *>(Mem) - OpSize);
413 }
414 
415 MDNode::MDNode(LLVMContext &Context, unsigned ID, StorageType Storage,
417  : Metadata(ID, Storage), NumOperands(Ops1.size() + Ops2.size()),
418  NumUnresolved(0), Context(Context) {
419  unsigned Op = 0;
420  for (Metadata *MD : Ops1)
421  setOperand(Op++, MD);
422  for (Metadata *MD : Ops2)
423  setOperand(Op++, MD);
424 
425  if (isDistinct())
426  return;
427 
428  if (isUniqued())
429  // Check whether any operands are unresolved, requiring re-uniquing. If
430  // not, don't support RAUW.
431  if (!countUnresolvedOperands())
432  return;
433 
434  this->Context.makeReplaceable(make_unique<ReplaceableMetadataImpl>(Context));
435 }
436 
437 TempMDNode MDNode::clone() const {
438  switch (getMetadataID()) {
439  default:
440  llvm_unreachable("Invalid MDNode subclass");
441 #define HANDLE_MDNODE_LEAF(CLASS) \
442  case CLASS##Kind: \
443  return cast<CLASS>(this)->cloneImpl();
444 #include "llvm/IR/Metadata.def"
445  }
446 }
447 
448 static bool isOperandUnresolved(Metadata *Op) {
449  if (auto *N = dyn_cast_or_null<MDNode>(Op))
450  return !N->isResolved();
451  return false;
452 }
453 
454 unsigned MDNode::countUnresolvedOperands() {
455  assert(NumUnresolved == 0 && "Expected unresolved ops to be uncounted");
456  NumUnresolved = std::count_if(op_begin(), op_end(), isOperandUnresolved);
457  return NumUnresolved;
458 }
459 
460 void MDNode::makeUniqued() {
461  assert(isTemporary() && "Expected this to be temporary");
462  assert(!isResolved() && "Expected this to be unresolved");
463 
464  // Enable uniquing callbacks.
465  for (auto &Op : mutable_operands())
466  Op.reset(Op.get(), this);
467 
468  // Make this 'uniqued'.
469  Storage = Uniqued;
470  if (!countUnresolvedOperands())
471  resolve();
472 
473  assert(isUniqued() && "Expected this to be uniqued");
474 }
475 
476 void MDNode::makeDistinct() {
477  assert(isTemporary() && "Expected this to be temporary");
478  assert(!isResolved() && "Expected this to be unresolved");
479 
480  // Pretend to be uniqued, resolve the node, and then store in distinct table.
481  Storage = Uniqued;
482  resolve();
484 
485  assert(isDistinct() && "Expected this to be distinct");
486  assert(isResolved() && "Expected this to be resolved");
487 }
488 
489 void MDNode::resolve() {
490  assert(isUniqued() && "Expected this to be uniqued");
491  assert(!isResolved() && "Expected this to be unresolved");
492 
493  // Move the map, so that this immediately looks resolved.
494  auto Uses = Context.takeReplaceableUses();
495  NumUnresolved = 0;
496  assert(isResolved() && "Expected this to be resolved");
497 
498  // Drop RAUW support.
499  Uses->resolveAllUses();
500 }
501 
502 void MDNode::resolveAfterOperandChange(Metadata *Old, Metadata *New) {
503  assert(NumUnresolved != 0 && "Expected unresolved operands");
504 
505  // Check if an operand was resolved.
506  if (!isOperandUnresolved(Old)) {
507  if (isOperandUnresolved(New))
508  // An operand was un-resolved!
509  ++NumUnresolved;
510  } else if (!isOperandUnresolved(New))
511  decrementUnresolvedOperandCount();
512 }
513 
514 void MDNode::decrementUnresolvedOperandCount() {
515  if (!--NumUnresolved)
516  // Last unresolved operand has just been resolved.
517  resolve();
518 }
519 
521  if (isResolved())
522  return;
523 
524  // Resolve this node immediately.
525  resolve();
526 
527  // Resolve all operands.
528  for (const auto &Op : operands()) {
529  auto *N = dyn_cast_or_null<MDNode>(Op);
530  if (!N)
531  continue;
532 
533  assert(!N->isTemporary() &&
534  "Expected all forward declarations to be resolved");
535  if (!N->isResolved())
536  N->resolveCycles();
537  }
538 }
539 
540 static bool hasSelfReference(MDNode *N) {
541  for (Metadata *MD : N->operands())
542  if (MD == N)
543  return true;
544  return false;
545 }
546 
547 MDNode *MDNode::replaceWithPermanentImpl() {
548  if (hasSelfReference(this))
549  return replaceWithDistinctImpl();
550  return replaceWithUniquedImpl();
551 }
552 
553 MDNode *MDNode::replaceWithUniquedImpl() {
554  // Try to uniquify in place.
555  MDNode *UniquedNode = uniquify();
556 
557  if (UniquedNode == this) {
558  makeUniqued();
559  return this;
560  }
561 
562  // Collision, so RAUW instead.
563  replaceAllUsesWith(UniquedNode);
564  deleteAsSubclass();
565  return UniquedNode;
566 }
567 
568 MDNode *MDNode::replaceWithDistinctImpl() {
569  makeDistinct();
570  return this;
571 }
572 
573 void MDTuple::recalculateHash() {
574  setHash(MDTupleInfo::KeyTy::calculateHash(this));
575 }
576 
578  for (unsigned I = 0, E = NumOperands; I != E; ++I)
579  setOperand(I, nullptr);
580  if (!isResolved()) {
581  Context.getReplaceableUses()->resolveAllUses(/* ResolveUsers */ false);
582  (void)Context.takeReplaceableUses();
583  }
584 }
585 
586 void MDNode::handleChangedOperand(void *Ref, Metadata *New) {
587  unsigned Op = static_cast<MDOperand *>(Ref) - op_begin();
588  assert(Op < getNumOperands() && "Expected valid operand");
589 
590  if (!isUniqued()) {
591  // This node is not uniqued. Just set the operand and be done with it.
592  setOperand(Op, New);
593  return;
594  }
595 
596  // This node is uniqued.
597  eraseFromStore();
598 
599  Metadata *Old = getOperand(Op);
600  setOperand(Op, New);
601 
602  // Drop uniquing for self-reference cycles.
603  if (New == this) {
604  if (!isResolved())
605  resolve();
607  return;
608  }
609 
610  // Re-unique the node.
611  auto *Uniqued = uniquify();
612  if (Uniqued == this) {
613  if (!isResolved())
614  resolveAfterOperandChange(Old, New);
615  return;
616  }
617 
618  // Collision.
619  if (!isResolved()) {
620  // Still unresolved, so RAUW.
621  //
622  // First, clear out all operands to prevent any recursion (similar to
623  // dropAllReferences(), but we still need the use-list).
624  for (unsigned O = 0, E = getNumOperands(); O != E; ++O)
625  setOperand(O, nullptr);
626  Context.getReplaceableUses()->replaceAllUsesWith(Uniqued);
627  deleteAsSubclass();
628  return;
629  }
630 
631  // Store in non-uniqued form if RAUW isn't possible.
633 }
634 
635 void MDNode::deleteAsSubclass() {
636  switch (getMetadataID()) {
637  default:
638  llvm_unreachable("Invalid subclass of MDNode");
639 #define HANDLE_MDNODE_LEAF(CLASS) \
640  case CLASS##Kind: \
641  delete cast<CLASS>(this); \
642  break;
643 #include "llvm/IR/Metadata.def"
644  }
645 }
646 
647 template <class T, class InfoT>
649  if (T *U = getUniqued(Store, N))
650  return U;
651 
652  Store.insert(N);
653  return N;
654 }
655 
656 template <class NodeTy> struct MDNode::HasCachedHash {
657  typedef char Yes[1];
658  typedef char No[2];
659  template <class U, U Val> struct SFINAE {};
660 
661  template <class U>
662  static Yes &check(SFINAE<void (U::*)(unsigned), &U::setHash> *);
663  template <class U> static No &check(...);
664 
665  static const bool value = sizeof(check<NodeTy>(nullptr)) == sizeof(Yes);
666 };
667 
668 MDNode *MDNode::uniquify() {
669  assert(!hasSelfReference(this) && "Cannot uniquify a self-referencing node");
670 
671  // Try to insert into uniquing store.
672  switch (getMetadataID()) {
673  default:
674  llvm_unreachable("Invalid subclass of MDNode");
675 #define HANDLE_MDNODE_LEAF(CLASS) \
676  case CLASS##Kind: { \
677  CLASS *SubclassThis = cast<CLASS>(this); \
678  std::integral_constant<bool, HasCachedHash<CLASS>::value> \
679  ShouldRecalculateHash; \
680  dispatchRecalculateHash(SubclassThis, ShouldRecalculateHash); \
681  return uniquifyImpl(SubclassThis, getContext().pImpl->CLASS##s); \
682  }
683 #include "llvm/IR/Metadata.def"
684  }
685 }
686 
687 void MDNode::eraseFromStore() {
688  switch (getMetadataID()) {
689  default:
690  llvm_unreachable("Invalid subclass of MDNode");
691 #define HANDLE_MDNODE_LEAF(CLASS) \
692  case CLASS##Kind: \
693  getContext().pImpl->CLASS##s.erase(cast<CLASS>(this)); \
694  break;
695 #include "llvm/IR/Metadata.def"
696  }
697 }
698 
699 MDTuple *MDTuple::getImpl(LLVMContext &Context, ArrayRef<Metadata *> MDs,
700  StorageType Storage, bool ShouldCreate) {
701  unsigned Hash = 0;
702  if (Storage == Uniqued) {
703  MDTupleInfo::KeyTy Key(MDs);
704  if (auto *N = getUniqued(Context.pImpl->MDTuples, Key))
705  return N;
706  if (!ShouldCreate)
707  return nullptr;
708  Hash = Key.getHash();
709  } else {
710  assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
711  }
712 
713  return storeImpl(new (MDs.size()) MDTuple(Context, Storage, Hash, MDs),
714  Storage, Context.pImpl->MDTuples);
715 }
716 
718  assert(N->isTemporary() && "Expected temporary node");
719  N->replaceAllUsesWith(nullptr);
720  N->deleteAsSubclass();
721 }
722 
724  assert(isResolved() && "Expected resolved nodes");
725  Storage = Distinct;
726 
727  // Reset the hash.
728  switch (getMetadataID()) {
729  default:
730  llvm_unreachable("Invalid subclass of MDNode");
731 #define HANDLE_MDNODE_LEAF(CLASS) \
732  case CLASS##Kind: { \
733  std::integral_constant<bool, HasCachedHash<CLASS>::value> ShouldResetHash; \
734  dispatchResetHash(cast<CLASS>(this), ShouldResetHash); \
735  break; \
736  }
737 #include "llvm/IR/Metadata.def"
738  }
739 
740  getContext().pImpl->DistinctMDNodes.insert(this);
741 }
742 
743 void MDNode::replaceOperandWith(unsigned I, Metadata *New) {
744  if (getOperand(I) == New)
745  return;
746 
747  if (!isUniqued()) {
748  setOperand(I, New);
749  return;
750  }
751 
752  handleChangedOperand(mutable_begin() + I, New);
753 }
754 
755 void MDNode::setOperand(unsigned I, Metadata *New) {
756  assert(I < NumOperands);
757  mutable_begin()[I].reset(New, isUniqued() ? this : nullptr);
758 }
759 
760 /// \brief Get a node, or a self-reference that looks like it.
761 ///
762 /// Special handling for finding self-references, for use by \a
763 /// MDNode::concatenate() and \a MDNode::intersect() to maintain behaviour from
764 /// when self-referencing nodes were still uniqued. If the first operand has
765 /// the same operands as \c Ops, return the first operand instead.
767  ArrayRef<Metadata *> Ops) {
768  if (!Ops.empty())
769  if (MDNode *N = dyn_cast_or_null<MDNode>(Ops[0]))
770  if (N->getNumOperands() == Ops.size() && N == N->getOperand(0)) {
771  for (unsigned I = 1, E = Ops.size(); I != E; ++I)
772  if (Ops[I] != N->getOperand(I))
773  return MDNode::get(Context, Ops);
774  return N;
775  }
776 
777  return MDNode::get(Context, Ops);
778 }
779 
781  if (!A)
782  return B;
783  if (!B)
784  return A;
785 
787  MDs.reserve(A->getNumOperands() + B->getNumOperands());
788  MDs.append(A->op_begin(), A->op_end());
789  MDs.append(B->op_begin(), B->op_end());
790 
791  // FIXME: This preserves long-standing behaviour, but is it really the right
792  // behaviour? Or was that an unintended side-effect of node uniquing?
793  return getOrSelfReference(A->getContext(), MDs);
794 }
795 
797  if (!A || !B)
798  return nullptr;
799 
801  for (Metadata *MD : A->operands())
802  if (std::find(B->op_begin(), B->op_end(), MD) != B->op_end())
803  MDs.push_back(MD);
804 
805  // FIXME: This preserves long-standing behaviour, but is it really the right
806  // behaviour? Or was that an unintended side-effect of node uniquing?
807  return getOrSelfReference(A->getContext(), MDs);
808 }
809 
811  if (!A || !B)
812  return nullptr;
813 
815  for (Metadata *MD : A->operands())
816  if (std::find(B->op_begin(), B->op_end(), MD) == B->op_end())
817  MDs.push_back(MD);
818 
819  // FIXME: This preserves long-standing behaviour, but is it really the right
820  // behaviour? Or was that an unintended side-effect of node uniquing?
821  return getOrSelfReference(A->getContext(), MDs);
822 }
823 
825  if (!A || !B)
826  return nullptr;
827 
828  APFloat AVal = mdconst::extract<ConstantFP>(A->getOperand(0))->getValueAPF();
829  APFloat BVal = mdconst::extract<ConstantFP>(B->getOperand(0))->getValueAPF();
830  if (AVal.compare(BVal) == APFloat::cmpLessThan)
831  return A;
832  return B;
833 }
834 
835 static bool isContiguous(const ConstantRange &A, const ConstantRange &B) {
836  return A.getUpper() == B.getLower() || A.getLower() == B.getUpper();
837 }
838 
839 static bool canBeMerged(const ConstantRange &A, const ConstantRange &B) {
840  return !A.intersectWith(B).isEmptySet() || isContiguous(A, B);
841 }
842 
844  ConstantInt *Low, ConstantInt *High) {
845  ConstantRange NewRange(Low->getValue(), High->getValue());
846  unsigned Size = EndPoints.size();
847  APInt LB = EndPoints[Size - 2]->getValue();
848  APInt LE = EndPoints[Size - 1]->getValue();
849  ConstantRange LastRange(LB, LE);
850  if (canBeMerged(NewRange, LastRange)) {
851  ConstantRange Union = LastRange.unionWith(NewRange);
852  Type *Ty = High->getType();
853  EndPoints[Size - 2] =
854  cast<ConstantInt>(ConstantInt::get(Ty, Union.getLower()));
855  EndPoints[Size - 1] =
856  cast<ConstantInt>(ConstantInt::get(Ty, Union.getUpper()));
857  return true;
858  }
859  return false;
860 }
861 
863  ConstantInt *Low, ConstantInt *High) {
864  if (!EndPoints.empty())
865  if (tryMergeRange(EndPoints, Low, High))
866  return;
867 
868  EndPoints.push_back(Low);
869  EndPoints.push_back(High);
870 }
871 
873  // Given two ranges, we want to compute the union of the ranges. This
874  // is slightly complitade by having to combine the intervals and merge
875  // the ones that overlap.
876 
877  if (!A || !B)
878  return nullptr;
879 
880  if (A == B)
881  return A;
882 
883  // First, walk both lists in older of the lower boundary of each interval.
884  // At each step, try to merge the new interval to the last one we adedd.
886  int AI = 0;
887  int BI = 0;
888  int AN = A->getNumOperands() / 2;
889  int BN = B->getNumOperands() / 2;
890  while (AI < AN && BI < BN) {
891  ConstantInt *ALow = mdconst::extract<ConstantInt>(A->getOperand(2 * AI));
892  ConstantInt *BLow = mdconst::extract<ConstantInt>(B->getOperand(2 * BI));
893 
894  if (ALow->getValue().slt(BLow->getValue())) {
895  addRange(EndPoints, ALow,
896  mdconst::extract<ConstantInt>(A->getOperand(2 * AI + 1)));
897  ++AI;
898  } else {
899  addRange(EndPoints, BLow,
900  mdconst::extract<ConstantInt>(B->getOperand(2 * BI + 1)));
901  ++BI;
902  }
903  }
904  while (AI < AN) {
905  addRange(EndPoints, mdconst::extract<ConstantInt>(A->getOperand(2 * AI)),
906  mdconst::extract<ConstantInt>(A->getOperand(2 * AI + 1)));
907  ++AI;
908  }
909  while (BI < BN) {
910  addRange(EndPoints, mdconst::extract<ConstantInt>(B->getOperand(2 * BI)),
911  mdconst::extract<ConstantInt>(B->getOperand(2 * BI + 1)));
912  ++BI;
913  }
914 
915  // If we have more than 2 ranges (4 endpoints) we have to try to merge
916  // the last and first ones.
917  unsigned Size = EndPoints.size();
918  if (Size > 4) {
919  ConstantInt *FB = EndPoints[0];
920  ConstantInt *FE = EndPoints[1];
921  if (tryMergeRange(EndPoints, FB, FE)) {
922  for (unsigned i = 0; i < Size - 2; ++i) {
923  EndPoints[i] = EndPoints[i + 2];
924  }
925  EndPoints.resize(Size - 2);
926  }
927  }
928 
929  // If in the end we have a single range, it is possible that it is now the
930  // full range. Just drop the metadata in that case.
931  if (EndPoints.size() == 2) {
932  ConstantRange Range(EndPoints[0]->getValue(), EndPoints[1]->getValue());
933  if (Range.isFullSet())
934  return nullptr;
935  }
936 
938  MDs.reserve(EndPoints.size());
939  for (auto *I : EndPoints)
941  return MDNode::get(A->getContext(), MDs);
942 }
943 
944 //===----------------------------------------------------------------------===//
945 // NamedMDNode implementation.
946 //
947 
949  return *(SmallVector<TrackingMDRef, 4> *)Operands;
950 }
951 
952 NamedMDNode::NamedMDNode(const Twine &N)
953  : Name(N.str()), Parent(nullptr),
954  Operands(new SmallVector<TrackingMDRef, 4>()) {}
955 
956 NamedMDNode::~NamedMDNode() {
958  delete &getNMDOps(Operands);
959 }
960 
961 unsigned NamedMDNode::getNumOperands() const {
962  return (unsigned)getNMDOps(Operands).size();
963 }
964 
965 MDNode *NamedMDNode::getOperand(unsigned i) const {
966  assert(i < getNumOperands() && "Invalid Operand number!");
967  auto *N = getNMDOps(Operands)[i].get();
968  return cast_or_null<MDNode>(N);
969 }
970 
971 void NamedMDNode::addOperand(MDNode *M) { getNMDOps(Operands).emplace_back(M); }
972 
973 void NamedMDNode::setOperand(unsigned I, MDNode *New) {
974  assert(I < getNumOperands() && "Invalid operand number");
975  getNMDOps(Operands)[I].reset(New);
976 }
977 
979  getParent()->eraseNamedMetadata(this);
980 }
981 
983  getNMDOps(Operands).clear();
984 }
985 
987  return StringRef(Name);
988 }
989 
990 //===----------------------------------------------------------------------===//
991 // Instruction Metadata method implementations.
992 //
993 void MDAttachmentMap::set(unsigned ID, MDNode &MD) {
994  for (auto &I : Attachments)
995  if (I.first == ID) {
996  I.second.reset(&MD);
997  return;
998  }
999  Attachments.emplace_back(std::piecewise_construct, std::make_tuple(ID),
1000  std::make_tuple(&MD));
1001 }
1002 
1003 void MDAttachmentMap::erase(unsigned ID) {
1004  if (empty())
1005  return;
1006 
1007  // Common case is one/last value.
1008  if (Attachments.back().first == ID) {
1009  Attachments.pop_back();
1010  return;
1011  }
1012 
1013  for (auto I = Attachments.begin(), E = std::prev(Attachments.end()); I != E;
1014  ++I)
1015  if (I->first == ID) {
1016  *I = std::move(Attachments.back());
1017  Attachments.pop_back();
1018  return;
1019  }
1020 }
1021 
1023  for (const auto &I : Attachments)
1024  if (I.first == ID)
1025  return I.second;
1026  return nullptr;
1027 }
1028 
1030  SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
1031  Result.append(Attachments.begin(), Attachments.end());
1032 
1033  // Sort the resulting array so it is stable.
1034  if (Result.size() > 1)
1035  array_pod_sort(Result.begin(), Result.end());
1036 }
1037 
1039  if (!Node && !hasMetadata())
1040  return;
1041  setMetadata(getContext().getMDKindID(Kind), Node);
1042 }
1043 
1044 MDNode *Instruction::getMetadataImpl(StringRef Kind) const {
1045  return getMetadataImpl(getContext().getMDKindID(Kind));
1046 }
1047 
1049  SmallSet<unsigned, 5> KnownSet;
1050  KnownSet.insert(KnownIDs.begin(), KnownIDs.end());
1051 
1052  // Drop debug if needed
1053  if (KnownSet.erase(LLVMContext::MD_dbg))
1054  DbgLoc = DebugLoc();
1055 
1056  if (!hasMetadataHashEntry())
1057  return; // Nothing to remove!
1058 
1059  auto &InstructionMetadata = getContext().pImpl->InstructionMetadata;
1060 
1061  if (KnownSet.empty()) {
1062  // Just drop our entry at the store.
1063  InstructionMetadata.erase(this);
1064  setHasMetadataHashEntry(false);
1065  return;
1066  }
1067 
1068  auto &Info = InstructionMetadata[this];
1069  Info.remove_if([&KnownSet](const std::pair<unsigned, TrackingMDNodeRef> &I) {
1070  return !KnownSet.count(I.first);
1071  });
1072 
1073  if (Info.empty()) {
1074  // Drop our entry at the store.
1075  InstructionMetadata.erase(this);
1076  setHasMetadataHashEntry(false);
1077  }
1078 }
1079 
1080 /// setMetadata - Set the metadata of of the specified kind to the specified
1081 /// node. This updates/replaces metadata if already present, or removes it if
1082 /// Node is null.
1083 void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
1084  if (!Node && !hasMetadata())
1085  return;
1086 
1087  // Handle 'dbg' as a special case since it is not stored in the hash table.
1088  if (KindID == LLVMContext::MD_dbg) {
1089  DbgLoc = DebugLoc(Node);
1090  return;
1091  }
1092 
1093  // Handle the case when we're adding/updating metadata on an instruction.
1094  if (Node) {
1095  auto &Info = getContext().pImpl->InstructionMetadata[this];
1096  assert(!Info.empty() == hasMetadataHashEntry() &&
1097  "HasMetadata bit is wonked");
1098  if (Info.empty())
1099  setHasMetadataHashEntry(true);
1100  Info.set(KindID, *Node);
1101  return;
1102  }
1103 
1104  // Otherwise, we're removing metadata from an instruction.
1105  assert((hasMetadataHashEntry() ==
1106  (getContext().pImpl->InstructionMetadata.count(this) > 0)) &&
1107  "HasMetadata bit out of date!");
1108  if (!hasMetadataHashEntry())
1109  return; // Nothing to remove!
1110  auto &Info = getContext().pImpl->InstructionMetadata[this];
1111 
1112  // Handle removal of an existing value.
1113  Info.erase(KindID);
1114 
1115  if (!Info.empty())
1116  return;
1117 
1118  getContext().pImpl->InstructionMetadata.erase(this);
1119  setHasMetadataHashEntry(false);
1120 }
1121 
1126 }
1127 
1128 MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
1129  // Handle 'dbg' as a special case since it is not stored in the hash table.
1130  if (KindID == LLVMContext::MD_dbg)
1131  return DbgLoc.getAsMDNode();
1132 
1133  if (!hasMetadataHashEntry())
1134  return nullptr;
1135  auto &Info = getContext().pImpl->InstructionMetadata[this];
1136  assert(!Info.empty() && "bit out of sync with hash table");
1137 
1138  return Info.lookup(KindID);
1139 }
1140 
1141 void Instruction::getAllMetadataImpl(
1142  SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
1143  Result.clear();
1144 
1145  // Handle 'dbg' as a special case since it is not stored in the hash table.
1146  if (DbgLoc) {
1147  Result.push_back(
1148  std::make_pair((unsigned)LLVMContext::MD_dbg, DbgLoc.getAsMDNode()));
1149  if (!hasMetadataHashEntry()) return;
1150  }
1151 
1152  assert(hasMetadataHashEntry() &&
1153  getContext().pImpl->InstructionMetadata.count(this) &&
1154  "Shouldn't have called this");
1155  const auto &Info = getContext().pImpl->InstructionMetadata.find(this)->second;
1156  assert(!Info.empty() && "Shouldn't have called this");
1157  Info.getAll(Result);
1158 }
1159 
1160 void Instruction::getAllMetadataOtherThanDebugLocImpl(
1161  SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
1162  Result.clear();
1163  assert(hasMetadataHashEntry() &&
1164  getContext().pImpl->InstructionMetadata.count(this) &&
1165  "Shouldn't have called this");
1166  const auto &Info = getContext().pImpl->InstructionMetadata.find(this)->second;
1167  assert(!Info.empty() && "Shouldn't have called this");
1168  Info.getAll(Result);
1169 }
1170 
1171 /// clearMetadataHashEntries - Clear all hashtable-based metadata from
1172 /// this instruction.
1173 void Instruction::clearMetadataHashEntries() {
1174  assert(hasMetadataHashEntry() && "Caller should check");
1175  getContext().pImpl->InstructionMetadata.erase(this);
1176  setHasMetadataHashEntry(false);
1177 }
1178 
1179 MDNode *Function::getMetadata(unsigned KindID) const {
1180  if (!hasMetadata())
1181  return nullptr;
1182  return getContext().pImpl->FunctionMetadata[this].lookup(KindID);
1183 }
1184 
1186  if (!hasMetadata())
1187  return nullptr;
1188  return getMetadata(getContext().getMDKindID(Kind));
1189 }
1190 
1191 void Function::setMetadata(unsigned KindID, MDNode *MD) {
1192  if (MD) {
1193  if (!hasMetadata())
1194  setHasMetadataHashEntry(true);
1195 
1196  getContext().pImpl->FunctionMetadata[this].set(KindID, *MD);
1197  return;
1198  }
1199 
1200  // Nothing to unset.
1201  if (!hasMetadata())
1202  return;
1203 
1204  auto &Store = getContext().pImpl->FunctionMetadata[this];
1205  Store.erase(KindID);
1206  if (Store.empty())
1207  clearMetadata();
1208 }
1209 
1211  if (!MD && !hasMetadata())
1212  return;
1213  setMetadata(getContext().getMDKindID(Kind), MD);
1214 }
1215 
1217  SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs) const {
1218  MDs.clear();
1219 
1220  if (!hasMetadata())
1221  return;
1222 
1223  getContext().pImpl->FunctionMetadata[this].getAll(MDs);
1224 }
1225 
1227  if (!hasMetadata())
1228  return;
1229  if (KnownIDs.empty()) {
1230  clearMetadata();
1231  return;
1232  }
1233 
1234  SmallSet<unsigned, 5> KnownSet;
1235  KnownSet.insert(KnownIDs.begin(), KnownIDs.end());
1236 
1237  auto &Store = getContext().pImpl->FunctionMetadata[this];
1238  assert(!Store.empty());
1239 
1240  Store.remove_if([&KnownSet](const std::pair<unsigned, TrackingMDNodeRef> &I) {
1241  return !KnownSet.count(I.first);
1242  });
1243 
1244  if (Store.empty())
1245  clearMetadata();
1246 }
1247 
1248 void Function::clearMetadata() {
1249  if (!hasMetadata())
1250  return;
1251  getContext().pImpl->FunctionMetadata.erase(this);
1252  setHasMetadataHashEntry(false);
1253 }
const NoneType None
Definition: None.h:23
StringRef getName() const
Definition: Metadata.cpp:986
static void deleteTemporary(MDNode *N)
Deallocate a node created by getTemporary.
Definition: Metadata.cpp:717
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:597
IntegerType * getType() const
getType - Specialize the getType() method to always return an IntegerType, which reduces the amount o...
Definition: Constants.h:140
DenseMap< const Function *, MDAttachmentMap > FunctionMetadata
Collection of per-function metadata used in this context.
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function. ...
Definition: Function.cpp:223
MDNode * Scope
The tag for alias scope specification (used with noalias).
Definition: Metadata.h:565
MDNode * TBAA
The tag for type-based alias analysis.
Definition: Metadata.h:562
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
Definition: StringMap.h:28
void replaceOperandWith(unsigned I, Metadata *New)
Replace a specific operand.
Definition: Metadata.cpp:743
Tracking metadata reference.
Definition: TrackingMDRef.h:29
static MDString * get(LLVMContext &Context, StringRef Str)
Definition: Metadata.cpp:360
DenseSet - This implements a dense probed hash-table based set.
Definition: DenseSet.h:39
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:942
DenseMap< Value *, ValueAsMetadata * > ValuesAsMetadata
iterator end() const
Definition: ArrayRef.h:123
static MDNode * getMostGenericAliasScope(MDNode *A, MDNode *B)
Definition: Metadata.cpp:810
void addOperand(MDNode *M)
Definition: Metadata.cpp:971
This file contains the declarations for metadata subclasses.
void storeDistinctInContext()
Definition: Metadata.cpp:723
op_iterator op_begin() const
Definition: Metadata.h:928
void dropAllReferences()
Definition: Metadata.cpp:577
A debug info location.
Definition: DebugLoc.h:34
Metadata node.
Definition: Metadata.h:740
static bool canBeMerged(const ConstantRange &A, const ConstantRange &B)
Definition: Metadata.cpp:839
static T * storeImpl(T *N, StorageType Storage, StoreT &Store)
Definition: MetadataImpl.h:30
void setOperand(unsigned I, Metadata *New)
Set an operand.
Definition: Metadata.cpp:755
static Type * getMetadataTy(LLVMContext &C)
Definition: Type.cpp:230
void reserve(size_type N)
Definition: SmallVector.h:401
void setOperand(unsigned I, MDNode *New)
Definition: Metadata.cpp:973
bool erase(const T &V)
Definition: SmallSet.h:96
op_iterator op_end() const
Definition: Metadata.h:931
Tuple of metadata.
Definition: Metadata.h:972
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Definition: SmallSet.h:44
static bool tryMergeRange(SmallVectorImpl< ConstantInt * > &EndPoints, ConstantInt *Low, ConstantInt *High)
Definition: Metadata.cpp:843
void set(unsigned ID, MDNode &MD)
Set an attachment to a particular node.
Definition: Metadata.cpp:993
DenseMap< const Instruction *, MDAttachmentMap > InstructionMetadata
Collection of per-instruction metadata used in this context.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:106
void eraseNamedMetadata(NamedMDNode *NMD)
Remove the given NamedMDNode from this module and delete it.
Definition: Module.cpp:275
void eraseFromParent()
Drop all references and remove the node from parent module.
Definition: Metadata.cpp:978
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:98
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APInt.h:33
static bool track(Metadata *&MD)
Track the reference to metadata.
void setMetadata(unsigned KindID, MDNode *MD)
Set a particular kind of metadata attachment.
Definition: Metadata.cpp:1191
void resolveCycles()
Resolve cycles.
Definition: Metadata.cpp:520
static void addRange(SmallVectorImpl< ConstantInt * > &EndPoints, ConstantInt *Low, ConstantInt *High)
Definition: Metadata.cpp:862
static Metadata * canonicalizeMetadataForValue(LLVMContext &Context, Metadata *MD)
Canonicalize metadata arguments to intrinsics.
Definition: Metadata.cpp:53
static void handleRAUW(Value *From, Value *To)
Definition: Metadata.cpp:295
static SmallVector< TrackingMDRef, 4 > & getNMDOps(void *Operands)
Definition: Metadata.cpp:948
LLVMContext & getContext() const
getContext - Return the LLVMContext in which this type was uniqued.
Definition: Type.h:125
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Definition: SmallVector.h:57
static std::error_code check(std::error_code Err)
ConstantRange unionWith(const ConstantRange &CR) const
Return the range that results from the union of this range with another range.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:31
static Function * getLocalFunction(Value *V)
Definition: Metadata.cpp:242
static MDNode * intersect(MDNode *A, MDNode *B)
Definition: Metadata.cpp:796
static T * uniquifyImpl(T *N, DenseSet< T *, InfoT > &Store)
Definition: Metadata.cpp:648
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:351
Value wrapper in the Metadata hierarchy.
Definition: Metadata.h:252
void replaceAllUsesWith(Metadata *MD)
RAUW a temporary.
Definition: Metadata.h:824
bool IsUsedByMD
Definition: Value.h:110
static ConstantAsMetadata * get(Constant *C)
Definition: Metadata.h:318
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:134
void getAll(SmallVectorImpl< std::pair< unsigned, MDNode * >> &Result) const
Copy out all the attachments.
Definition: Metadata.cpp:1029
static MetadataAsValue * get(LLVMContext &Context, Metadata *MD)
Definition: Metadata.cpp:75
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:872
void array_pod_sort(IteratorTy Start, IteratorTy End)
array_pod_sort - This sorts an array with the specified start and end extent.
Definition: STLExtras.h:287
static T * getUniqued(DenseSet< T *, InfoT > &Store, const typename InfoT::KeyTy &Key)
Definition: MetadataImpl.h:23
void resolveAllUses(bool ResolveUsers=true)
Resolve all uses of this.
Definition: Metadata.cpp:209
ConstantRange intersectWith(const ConstantRange &CR) const
Return the range that results from the intersection of this range with another range.
MDNode * getMetadata(unsigned KindID) const
Get the current metadata attachment, if any.
Definition: Metadata.cpp:1179
static ValueAsMetadata * getIfExists(Value *V)
Definition: Metadata.cpp:271
cmpResult compare(const APFloat &) const
IEEE comparison with another floating point number (NaNs compare unordered, 0==-0).
Definition: APFloat.cpp:1893
bool isFullSet() const
Return true if this set contains all of the elements possible for this data-type. ...
static MetadataAsValue * getIfExists(LLVMContext &Context, Metadata *MD)
Definition: Metadata.cpp:83
void setAAMetadata(const AAMDNodes &N)
setAAMetadata - Sets the metadata on this instruction from the AAMDNodes structure.
Definition: Metadata.cpp:1122
LLVM Basic Block Representation.
Definition: BasicBlock.h:65
bool isTemporary() const
Definition: Metadata.h:819
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:41
static bool isContiguous(const ConstantRange &A, const ConstantRange &B)
Definition: Metadata.cpp:835
void dropUnknownMetadata(ArrayRef< unsigned > KnownIDs)
Drop metadata not in the given list.
Definition: Metadata.cpp:1226
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:32
StorageType
Active type of storage.
Definition: Metadata.h:53
bool hasMetadata() const
hasMetadata() - Return true if this instruction has any metadata attached to it.
Definition: Instruction.h:157
bool hasMetadata() const
Check if this has any metadata.
Definition: Function.h:571
unsigned getMetadataID() const
Definition: Metadata.h:107
MDNode * getOperand(unsigned i) const
Definition: Metadata.cpp:965
iterator begin() const
Definition: ArrayRef.h:122
SI Fold Operands
std::pair< NoneType, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition: SmallSet.h:69
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:129
Metadata wrapper in the Value hierarchy.
Definition: Metadata.h:172
void append(in_iter in_start, in_iter in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:416
bool isEmptySet() const
Return true if this set contains no members.
TempMDNode clone() const
Create a (temporary) clone of this.
Definition: Metadata.cpp:437
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:519
SmallPtrSet< MDNode *, 1 > DistinctMDNodes
void setMetadata(unsigned KindID, MDNode *Node)
setMetadata - Set the metadata of the specified kind to the specified node.
Definition: Metadata.cpp:1083
mutable_op_range mutable_operands()
Definition: Metadata.h:777
LLVMContextImpl *const pImpl
Definition: LLVMContext.h:43
static bool isOperandUnresolved(Metadata *Op)
Definition: Metadata.cpp:448
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition: SmallSet.h:53
DenseMap< Metadata *, MetadataAsValue * > MetadataAsValues
StringRef getString() const
Definition: Metadata.cpp:375
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:936
std::pair< iterator, bool > insert(const ValueT &V)
Definition: DenseSet.h:147
This is the shared class of boolean and integer constants.
Definition: Constants.h:47
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition: APInt.cpp:552
unsigned Storage
Storage flag for non-uniqued, otherwise unowned, metadata.
Definition: Metadata.h:56
T get() const
get<T>() - Return the value of the specified pointer type.
Definition: PointerUnion.h:130
static ValueAsMetadata * get(Value *V)
Definition: Metadata.cpp:251
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:222
MDNode * lookup(unsigned ID) const
Get a particular attachment (if any).
Definition: Metadata.cpp:1022
This class represents a range of values.
Definition: ConstantRange.h:43
void getAllMetadata(SmallVectorImpl< std::pair< unsigned, MDNode * >> &MDs) const
Get all current metadata attachments.
Definition: Metadata.cpp:1216
const APInt & getLower() const
Return the lower value for this range.
Definition: ConstantRange.h:87
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition: Metadata.h:548
MDNode * NoAlias
The tag specifying the noalias scope.
Definition: Metadata.h:568
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
Definition: Constants.cpp:582
void reset()
Definition: Metadata.h:614
static void untrack(Metadata *&MD)
Stop tracking a reference to metadata.
Class for arbitrary precision integers.
Definition: APInt.h:73
void erase(unsigned ID)
Remove an attachment.
Definition: Metadata.cpp:1003
LLVM_ATTRIBUTE_UNUSED_RESULT std::enable_if< !is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:285
uint64_t RoundUpToAlignment(uint64_t Value, uint64_t Align)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
Definition: MathExtras.h:609
MDNode * getAsMDNode() const
Return this as a bar MDNode.
Definition: DebugLoc.h:113
MDOperand * mutable_begin()
Definition: Metadata.h:773
bool isDistinct() const
Definition: Metadata.h:818
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1030
StringMap< MDString > MDStringCache
static MDNode * getOrSelfReference(LLVMContext &Context, ArrayRef< Metadata * > Ops)
Get a node, or a self-reference that looks like it.
Definition: Metadata.cpp:766
Value * getValue() const
Definition: Metadata.h:287
void replaceAllUsesWith(Metadata *MD)
Handle collisions after Value::replaceAllUsesWith().
Definition: Metadata.h:300
static MDNode * concatenate(MDNode *A, MDNode *B)
Methods for metadata merging.
Definition: Metadata.cpp:780
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
void size_t size
op_range operands() const
Definition: Metadata.h:934
LLVMContext & getContext() const
Definition: Metadata.h:799
void replaceAllUsesWith(Metadata *MD)
Replace all uses of this with MD.
Definition: Metadata.cpp:158
static bool hasSelfReference(MDNode *N)
Definition: Metadata.cpp:540
const APInt & getUpper() const
Return the upper value for this range.
Definition: ConstantRange.h:91
bool isUniqued() const
Definition: Metadata.h:817
const ARM::ArchExtKind Kind
static void handleDeletion(Value *V)
Definition: Metadata.cpp:276
LLVM Value Representation.
Definition: Value.h:69
void dropUnknownMetadata()
Definition: Instruction.h:211
static const Function * getParent(const Value *V)
unsigned getNumOperands() const
Definition: Metadata.cpp:961
std::string Hash(const Unit &U)
Definition: FuzzerUtil.cpp:39
void dropAllReferences()
Remove all uses and clear node vector.
Definition: Metadata.cpp:982
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
A single uniqued string.
Definition: Metadata.h:508
int is() const
is<T>() return true if the Union currently holds the type matching T.
Definition: PointerUnion.h:118
static MDNode * getMostGenericFPMath(MDNode *A, MDNode *B)
Definition: Metadata.cpp:824
bool isResolved() const
Check if node is fully resolved.
Definition: Metadata.h:815
Root of the metadata hierarchy.
Definition: Metadata.h:45
Module * getParent()
Get the module that holds this named metadata collection.
Definition: Metadata.h:1188
PointerUnion - This implements a discriminated union of two pointer types, and keeps the discriminato...
Definition: PointerUnion.h:81
void resize(size_type N)
Definition: SmallVector.h:376