LLVM  15.0.0git
Metadata.cpp
Go to the documentation of this file.
1 //===- Metadata.cpp - Implement Metadata classes --------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the Metadata classes.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/IR/Metadata.h"
14 #include "LLVMContextImpl.h"
15 #include "MetadataImpl.h"
16 #include "llvm/ADT/APFloat.h"
17 #include "llvm/ADT/APInt.h"
18 #include "llvm/ADT/ArrayRef.h"
19 #include "llvm/ADT/DenseSet.h"
20 #include "llvm/ADT/None.h"
21 #include "llvm/ADT/STLExtras.h"
22 #include "llvm/ADT/SetVector.h"
23 #include "llvm/ADT/SmallPtrSet.h"
24 #include "llvm/ADT/SmallSet.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/ADT/StringMap.h"
27 #include "llvm/ADT/StringRef.h"
28 #include "llvm/ADT/Twine.h"
29 #include "llvm/IR/Argument.h"
30 #include "llvm/IR/BasicBlock.h"
31 #include "llvm/IR/Constant.h"
32 #include "llvm/IR/ConstantRange.h"
33 #include "llvm/IR/Constants.h"
35 #include "llvm/IR/DebugLoc.h"
36 #include "llvm/IR/Function.h"
37 #include "llvm/IR/GlobalObject.h"
38 #include "llvm/IR/GlobalVariable.h"
39 #include "llvm/IR/Instruction.h"
40 #include "llvm/IR/LLVMContext.h"
41 #include "llvm/IR/MDBuilder.h"
42 #include "llvm/IR/Module.h"
43 #include "llvm/IR/TrackingMDRef.h"
44 #include "llvm/IR/Type.h"
45 #include "llvm/IR/Value.h"
46 #include "llvm/Support/Casting.h"
49 #include <algorithm>
50 #include <cassert>
51 #include <cstddef>
52 #include <cstdint>
53 #include <type_traits>
54 #include <utility>
55 #include <vector>
56 
57 using namespace llvm;
58 
59 MetadataAsValue::MetadataAsValue(Type *Ty, Metadata *MD)
60  : Value(Ty, MetadataAsValueVal), MD(MD) {
61  track();
62 }
63 
65  getType()->getContext().pImpl->MetadataAsValues.erase(MD);
66  untrack();
67 }
68 
69 /// Canonicalize metadata arguments to intrinsics.
70 ///
71 /// To support bitcode upgrades (and assembly semantic sugar) for \a
72 /// MetadataAsValue, we need to canonicalize certain metadata.
73 ///
74 /// - nullptr is replaced by an empty MDNode.
75 /// - An MDNode with a single null operand is replaced by an empty MDNode.
76 /// - An MDNode whose only operand is a \a ConstantAsMetadata gets skipped.
77 ///
78 /// This maintains readability of bitcode from when metadata was a type of
79 /// value, and these bridges were unnecessary.
81  Metadata *MD) {
82  if (!MD)
83  // !{}
84  return MDNode::get(Context, None);
85 
86  // Return early if this isn't a single-operand MDNode.
87  auto *N = dyn_cast<MDNode>(MD);
88  if (!N || N->getNumOperands() != 1)
89  return MD;
90 
91  if (!N->getOperand(0))
92  // !{}
93  return MDNode::get(Context, None);
94 
95  if (auto *C = dyn_cast<ConstantAsMetadata>(N->getOperand(0)))
96  // Look through the MDNode.
97  return C;
98 
99  return MD;
100 }
101 
104  auto *&Entry = Context.pImpl->MetadataAsValues[MD];
105  if (!Entry)
106  Entry = new MetadataAsValue(Type::getMetadataTy(Context), MD);
107  return Entry;
108 }
109 
111  Metadata *MD) {
114  return Store.lookup(MD);
115 }
116 
117 void MetadataAsValue::handleChangedMetadata(Metadata *MD) {
121 
122  // Stop tracking the old metadata.
123  Store.erase(this->MD);
124  untrack();
125  this->MD = nullptr;
126 
127  // Start tracking MD, or RAUW if necessary.
128  auto *&Entry = Store[MD];
129  if (Entry) {
130  replaceAllUsesWith(Entry);
131  delete this;
132  return;
133  }
134 
135  this->MD = MD;
136  track();
137  Entry = this;
138 }
139 
140 void MetadataAsValue::track() {
141  if (MD)
142  MetadataTracking::track(&MD, *MD, *this);
143 }
144 
145 void MetadataAsValue::untrack() {
146  if (MD)
148 }
149 
150 bool MetadataTracking::track(void *Ref, Metadata &MD, OwnerTy Owner) {
151  assert(Ref && "Expected live reference");
152  assert((Owner || *static_cast<Metadata **>(Ref) == &MD) &&
153  "Reference without owner must be direct");
154  if (auto *R = ReplaceableMetadataImpl::getOrCreate(MD)) {
155  R->addRef(Ref, Owner);
156  return true;
157  }
158  if (auto *PH = dyn_cast<DistinctMDOperandPlaceholder>(&MD)) {
159  assert(!PH->Use && "Placeholders can only be used once");
160  assert(!Owner && "Unexpected callback to owner");
161  PH->Use = static_cast<Metadata **>(Ref);
162  return true;
163  }
164  return false;
165 }
166 
167 void MetadataTracking::untrack(void *Ref, Metadata &MD) {
168  assert(Ref && "Expected live reference");
169  if (auto *R = ReplaceableMetadataImpl::getIfExists(MD))
170  R->dropRef(Ref);
171  else if (auto *PH = dyn_cast<DistinctMDOperandPlaceholder>(&MD))
172  PH->Use = nullptr;
173 }
174 
175 bool MetadataTracking::retrack(void *Ref, Metadata &MD, void *New) {
176  assert(Ref && "Expected live reference");
177  assert(New && "Expected live reference");
178  assert(Ref != New && "Expected change");
179  if (auto *R = ReplaceableMetadataImpl::getIfExists(MD)) {
180  R->moveRef(Ref, New, MD);
181  return true;
182  }
183  assert(!isa<DistinctMDOperandPlaceholder>(MD) &&
184  "Unexpected move of an MDOperand");
185  assert(!isReplaceable(MD) &&
186  "Expected un-replaceable metadata, since we didn't move a reference");
187  return false;
188 }
189 
191  return ReplaceableMetadataImpl::isReplaceable(MD);
192 }
193 
196  for (auto Pair : UseMap) {
197  OwnerTy Owner = Pair.second.first;
198  if (!Owner.is<Metadata *>())
199  continue;
200  Metadata *OwnerMD = Owner.get<Metadata *>();
201  if (OwnerMD->getMetadataID() == Metadata::DIArgListKind)
202  MDUsersWithID.push_back(&UseMap[Pair.first]);
203  }
204  llvm::sort(MDUsersWithID, [](auto UserA, auto UserB) {
205  return UserA->second < UserB->second;
206  });
207  SmallVector<Metadata *> MDUsers;
208  for (auto UserWithID : MDUsersWithID)
209  MDUsers.push_back(UserWithID->first.get<Metadata *>());
210  return MDUsers;
211 }
212 
213 void ReplaceableMetadataImpl::addRef(void *Ref, OwnerTy Owner) {
214  bool WasInserted =
215  UseMap.insert(std::make_pair(Ref, std::make_pair(Owner, NextIndex)))
216  .second;
217  (void)WasInserted;
218  assert(WasInserted && "Expected to add a reference");
219 
220  ++NextIndex;
221  assert(NextIndex != 0 && "Unexpected overflow");
222 }
223 
224 void ReplaceableMetadataImpl::dropRef(void *Ref) {
225  bool WasErased = UseMap.erase(Ref);
226  (void)WasErased;
227  assert(WasErased && "Expected to drop a reference");
228 }
229 
230 void ReplaceableMetadataImpl::moveRef(void *Ref, void *New,
231  const Metadata &MD) {
232  auto I = UseMap.find(Ref);
233  assert(I != UseMap.end() && "Expected to move a reference");
234  auto OwnerAndIndex = I->second;
235  UseMap.erase(I);
236  bool WasInserted = UseMap.insert(std::make_pair(New, OwnerAndIndex)).second;
237  (void)WasInserted;
238  assert(WasInserted && "Expected to add a reference");
239 
240  // Check that the references are direct if there's no owner.
241  (void)MD;
242  assert((OwnerAndIndex.first || *static_cast<Metadata **>(Ref) == &MD) &&
243  "Reference without owner must be direct");
244  assert((OwnerAndIndex.first || *static_cast<Metadata **>(New) == &MD) &&
245  "Reference without owner must be direct");
246 }
247 
249  if (!C.isUsedByMetadata()) {
250  return;
251  }
252 
253  LLVMContext &Context = C.getType()->getContext();
255  auto I = Store.find(&C);
256  ValueAsMetadata *MD = I->second;
257  using UseTy =
258  std::pair<void *, std::pair<MetadataTracking::OwnerTy, uint64_t>>;
259  // Copy out uses and update value of Constant used by debug info metadata with undef below
260  SmallVector<UseTy, 8> Uses(MD->UseMap.begin(), MD->UseMap.end());
261 
262  for (const auto &Pair : Uses) {
263  MetadataTracking::OwnerTy Owner = Pair.second.first;
264  if (!Owner)
265  continue;
266  if (!Owner.is<Metadata *>())
267  continue;
268  auto *OwnerMD = dyn_cast<MDNode>(Owner.get<Metadata *>());
269  if (!OwnerMD)
270  continue;
271  if (isa<DINode>(OwnerMD)) {
272  OwnerMD->handleChangedOperand(
273  Pair.first, ValueAsMetadata::get(UndefValue::get(C.getType())));
274  }
275  }
276 }
277 
279  if (UseMap.empty())
280  return;
281 
282  // Copy out uses since UseMap will get touched below.
283  using UseTy = std::pair<void *, std::pair<OwnerTy, uint64_t>>;
284  SmallVector<UseTy, 8> Uses(UseMap.begin(), UseMap.end());
285  llvm::sort(Uses, [](const UseTy &L, const UseTy &R) {
286  return L.second.second < R.second.second;
287  });
288  for (const auto &Pair : Uses) {
289  // Check that this Ref hasn't disappeared after RAUW (when updating a
290  // previous Ref).
291  if (!UseMap.count(Pair.first))
292  continue;
293 
294  OwnerTy Owner = Pair.second.first;
295  if (!Owner) {
296  // Update unowned tracking references directly.
297  Metadata *&Ref = *static_cast<Metadata **>(Pair.first);
298  Ref = MD;
299  if (MD)
301  UseMap.erase(Pair.first);
302  continue;
303  }
304 
305  // Check for MetadataAsValue.
306  if (Owner.is<MetadataAsValue *>()) {
307  Owner.get<MetadataAsValue *>()->handleChangedMetadata(MD);
308  continue;
309  }
310 
311  // There's a Metadata owner -- dispatch.
312  Metadata *OwnerMD = Owner.get<Metadata *>();
313  switch (OwnerMD->getMetadataID()) {
314 #define HANDLE_METADATA_LEAF(CLASS) \
315  case Metadata::CLASS##Kind: \
316  cast<CLASS>(OwnerMD)->handleChangedOperand(Pair.first, MD); \
317  continue;
318 #include "llvm/IR/Metadata.def"
319  default:
320  llvm_unreachable("Invalid metadata subclass");
321  }
322  }
323  assert(UseMap.empty() && "Expected all uses to be replaced");
324 }
325 
327  if (UseMap.empty())
328  return;
329 
330  if (!ResolveUsers) {
331  UseMap.clear();
332  return;
333  }
334 
335  // Copy out uses since UseMap could get touched below.
336  using UseTy = std::pair<void *, std::pair<OwnerTy, uint64_t>>;
337  SmallVector<UseTy, 8> Uses(UseMap.begin(), UseMap.end());
338  llvm::sort(Uses, [](const UseTy &L, const UseTy &R) {
339  return L.second.second < R.second.second;
340  });
341  UseMap.clear();
342  for (const auto &Pair : Uses) {
343  auto Owner = Pair.second.first;
344  if (!Owner)
345  continue;
346  if (Owner.is<MetadataAsValue *>())
347  continue;
348 
349  // Resolve MDNodes that point at this.
350  auto *OwnerMD = dyn_cast<MDNode>(Owner.get<Metadata *>());
351  if (!OwnerMD)
352  continue;
353  if (OwnerMD->isResolved())
354  continue;
355  OwnerMD->decrementUnresolvedOperandCount();
356  }
357 }
358 
359 ReplaceableMetadataImpl *ReplaceableMetadataImpl::getOrCreate(Metadata &MD) {
360  if (auto *N = dyn_cast<MDNode>(&MD))
361  return N->isResolved() ? nullptr : N->Context.getOrCreateReplaceableUses();
362  return dyn_cast<ValueAsMetadata>(&MD);
363 }
364 
365 ReplaceableMetadataImpl *ReplaceableMetadataImpl::getIfExists(Metadata &MD) {
366  if (auto *N = dyn_cast<MDNode>(&MD))
367  return N->isResolved() ? nullptr : N->Context.getReplaceableUses();
368  return dyn_cast<ValueAsMetadata>(&MD);
369 }
370 
371 bool ReplaceableMetadataImpl::isReplaceable(const Metadata &MD) {
372  if (auto *N = dyn_cast<MDNode>(&MD))
373  return !N->isResolved();
374  return isa<ValueAsMetadata>(&MD);
375 }
376 
378  assert(V && "Expected value");
379  if (auto *A = dyn_cast<Argument>(V)) {
380  if (auto *Fn = A->getParent())
381  return Fn->getSubprogram();
382  return nullptr;
383  }
384 
385  if (BasicBlock *BB = cast<Instruction>(V)->getParent()) {
386  if (auto *Fn = BB->getParent())
387  return Fn->getSubprogram();
388  return nullptr;
389  }
390 
391  return nullptr;
392 }
393 
395  assert(V && "Unexpected null Value");
396 
397  auto &Context = V->getContext();
398  auto *&Entry = Context.pImpl->ValuesAsMetadata[V];
399  if (!Entry) {
400  assert((isa<Constant>(V) || isa<Argument>(V) || isa<Instruction>(V)) &&
401  "Expected constant or function-local value");
402  assert(!V->IsUsedByMD && "Expected this to be the only metadata use");
403  V->IsUsedByMD = true;
404  if (auto *C = dyn_cast<Constant>(V))
405  Entry = new ConstantAsMetadata(C);
406  else
407  Entry = new LocalAsMetadata(V);
408  }
409 
410  return Entry;
411 }
412 
414  assert(V && "Unexpected null Value");
415  return V->getContext().pImpl->ValuesAsMetadata.lookup(V);
416 }
417 
419  assert(V && "Expected valid value");
420 
421  auto &Store = V->getType()->getContext().pImpl->ValuesAsMetadata;
422  auto I = Store.find(V);
423  if (I == Store.end())
424  return;
425 
426  // Remove old entry from the map.
427  ValueAsMetadata *MD = I->second;
428  assert(MD && "Expected valid metadata");
429  assert(MD->getValue() == V && "Expected valid mapping");
430  Store.erase(I);
431 
432  // Delete the metadata.
433  MD->replaceAllUsesWith(nullptr);
434  delete MD;
435 }
436 
438  assert(From && "Expected valid value");
439  assert(To && "Expected valid value");
440  assert(From != To && "Expected changed value");
441  assert(From->getType() == To->getType() && "Unexpected type change");
442 
443  LLVMContext &Context = From->getType()->getContext();
445  auto I = Store.find(From);
446  if (I == Store.end()) {
447  assert(!From->IsUsedByMD && "Expected From not to be used by metadata");
448  return;
449  }
450 
451  // Remove old entry from the map.
452  assert(From->IsUsedByMD && "Expected From to be used by metadata");
453  From->IsUsedByMD = false;
454  ValueAsMetadata *MD = I->second;
455  assert(MD && "Expected valid metadata");
456  assert(MD->getValue() == From && "Expected valid mapping");
457  Store.erase(I);
458 
459  if (isa<LocalAsMetadata>(MD)) {
460  if (auto *C = dyn_cast<Constant>(To)) {
461  // Local became a constant.
463  delete MD;
464  return;
465  }
468  // DISubprogram changed.
469  MD->replaceAllUsesWith(nullptr);
470  delete MD;
471  return;
472  }
473  } else if (!isa<Constant>(To)) {
474  // Changed to function-local value.
475  MD->replaceAllUsesWith(nullptr);
476  delete MD;
477  return;
478  }
479 
480  auto *&Entry = Store[To];
481  if (Entry) {
482  // The target already exists.
483  MD->replaceAllUsesWith(Entry);
484  delete MD;
485  return;
486  }
487 
488  // Update MD in place (and update the map entry).
489  assert(!To->IsUsedByMD && "Expected this to be the only metadata use");
490  To->IsUsedByMD = true;
491  MD->V = To;
492  Entry = MD;
493 }
494 
495 //===----------------------------------------------------------------------===//
496 // MDString implementation.
497 //
498 
500  auto &Store = Context.pImpl->MDStringCache;
501  auto I = Store.try_emplace(Str);
502  auto &MapEntry = I.first->getValue();
503  if (!I.second)
504  return &MapEntry;
505  MapEntry.Entry = &*I.first;
506  return &MapEntry;
507 }
508 
510  assert(Entry && "Expected to find string map entry");
511  return Entry->first();
512 }
513 
514 //===----------------------------------------------------------------------===//
515 // MDNode implementation.
516 //
517 
518 // Assert that the MDNode types will not be unaligned by the objects
519 // prepended to them.
520 #define HANDLE_MDNODE_LEAF(CLASS) \
521  static_assert( \
522  alignof(uint64_t) >= alignof(CLASS), \
523  "Alignment is insufficient after objects prepended to " #CLASS);
524 #include "llvm/IR/Metadata.def"
525 
526 void *MDNode::operator new(size_t Size, unsigned NumOps,
527  StorageType /* Storage */) {
528  // uint64_t is the most aligned type we need support (ensured by static_assert
529  // above)
530  size_t AllocSize = alignTo(Header::getAllocSize(NumOps), alignof(uint64_t));
531  char *Mem = reinterpret_cast<char *>(::operator new(AllocSize + Size));
532  Header *H = new (Mem + AllocSize - sizeof(Header)) Header(NumOps);
533  return reinterpret_cast<void *>(H + 1);
534 }
535 
536 void MDNode::operator delete(void *N) {
537  Header *H = reinterpret_cast<Header *>(N) - 1;
538  void *Mem = H->getAllocation();
539  H->~Header();
540  ::operator delete(Mem);
541 }
542 
545  : Metadata(ID, Storage), Context(Context) {
546  unsigned Op = 0;
547  for (Metadata *MD : Ops1)
548  setOperand(Op++, MD);
549  for (Metadata *MD : Ops2)
550  setOperand(Op++, MD);
551 
552  if (!isUniqued())
553  return;
554 
555  // Count the unresolved operands. If there are any, RAUW support will be
556  // added lazily on first reference.
557  countUnresolvedOperands();
558 }
559 
560 TempMDNode MDNode::clone() const {
561  switch (getMetadataID()) {
562  default:
563  llvm_unreachable("Invalid MDNode subclass");
564 #define HANDLE_MDNODE_LEAF(CLASS) \
565  case CLASS##Kind: \
566  return cast<CLASS>(this)->cloneImpl();
567 #include "llvm/IR/Metadata.def"
568  }
569 }
570 
571 MDNode::Header::Header(unsigned NumOps) {
572  NumOperands = NumOps;
573  MDOperand *O = reinterpret_cast<MDOperand *>(this);
574  for (MDOperand *E = O - NumOps; O != E; --O)
575  (void)new (O - 1) MDOperand();
576 }
577 
578 MDNode::Header::~Header() {
579  MDOperand *O = reinterpret_cast<MDOperand *>(this) - NumOperands;
580  for (MDOperand *E = O + NumOperands; O != E; ++O)
581  (void)O->~MDOperand();
582 }
583 
585  if (auto *N = dyn_cast_or_null<MDNode>(Op))
586  return !N->isResolved();
587  return false;
588 }
589 
590 void MDNode::countUnresolvedOperands() {
591  assert(getNumUnresolved() == 0 && "Expected unresolved ops to be uncounted");
592  assert(isUniqued() && "Expected this to be uniqued");
594 }
595 
596 void MDNode::makeUniqued() {
597  assert(isTemporary() && "Expected this to be temporary");
598  assert(!isResolved() && "Expected this to be unresolved");
599 
600  // Enable uniquing callbacks.
601  for (auto &Op : mutable_operands())
602  Op.reset(Op.get(), this);
603 
604  // Make this 'uniqued'.
605  Storage = Uniqued;
606  countUnresolvedOperands();
607  if (!getNumUnresolved()) {
608  dropReplaceableUses();
609  assert(isResolved() && "Expected this to be resolved");
610  }
611 
612  assert(isUniqued() && "Expected this to be uniqued");
613 }
614 
615 void MDNode::makeDistinct() {
616  assert(isTemporary() && "Expected this to be temporary");
617  assert(!isResolved() && "Expected this to be unresolved");
618 
619  // Drop RAUW support and store as a distinct node.
620  dropReplaceableUses();
622 
623  assert(isDistinct() && "Expected this to be distinct");
624  assert(isResolved() && "Expected this to be resolved");
625 }
626 
628  assert(isUniqued() && "Expected this to be uniqued");
629  assert(!isResolved() && "Expected this to be unresolved");
630 
631  setNumUnresolved(0);
632  dropReplaceableUses();
633 
634  assert(isResolved() && "Expected this to be resolved");
635 }
636 
637 void MDNode::dropReplaceableUses() {
638  assert(!getNumUnresolved() && "Unexpected unresolved operand");
639 
640  // Drop any RAUW support.
641  if (Context.hasReplaceableUses())
642  Context.takeReplaceableUses()->resolveAllUses();
643 }
644 
645 void MDNode::resolveAfterOperandChange(Metadata *Old, Metadata *New) {
646  assert(isUniqued() && "Expected this to be uniqued");
647  assert(getNumUnresolved() != 0 && "Expected unresolved operands");
648 
649  // Check if an operand was resolved.
650  if (!isOperandUnresolved(Old)) {
651  if (isOperandUnresolved(New))
652  // An operand was un-resolved!
654  } else if (!isOperandUnresolved(New))
655  decrementUnresolvedOperandCount();
656 }
657 
658 void MDNode::decrementUnresolvedOperandCount() {
659  assert(!isResolved() && "Expected this to be unresolved");
660  if (isTemporary())
661  return;
662 
663  assert(isUniqued() && "Expected this to be uniqued");
665  if (getNumUnresolved())
666  return;
667 
668  // Last unresolved operand has just been resolved.
669  dropReplaceableUses();
670  assert(isResolved() && "Expected this to become resolved");
671 }
672 
674  if (isResolved())
675  return;
676 
677  // Resolve this node immediately.
678  resolve();
679 
680  // Resolve all operands.
681  for (const auto &Op : operands()) {
682  auto *N = dyn_cast_or_null<MDNode>(Op);
683  if (!N)
684  continue;
685 
686  assert(!N->isTemporary() &&
687  "Expected all forward declarations to be resolved");
688  if (!N->isResolved())
689  N->resolveCycles();
690  }
691 }
692 
693 static bool hasSelfReference(MDNode *N) {
694  return llvm::is_contained(N->operands(), N);
695 }
696 
697 MDNode *MDNode::replaceWithPermanentImpl() {
698  switch (getMetadataID()) {
699  default:
700  // If this type isn't uniquable, replace with a distinct node.
701  return replaceWithDistinctImpl();
702 
703 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
704  case CLASS##Kind: \
705  break;
706 #include "llvm/IR/Metadata.def"
707  }
708 
709  // Even if this type is uniquable, self-references have to be distinct.
710  if (hasSelfReference(this))
711  return replaceWithDistinctImpl();
712  return replaceWithUniquedImpl();
713 }
714 
715 MDNode *MDNode::replaceWithUniquedImpl() {
716  // Try to uniquify in place.
717  MDNode *UniquedNode = uniquify();
718 
719  if (UniquedNode == this) {
720  makeUniqued();
721  return this;
722  }
723 
724  // Collision, so RAUW instead.
725  replaceAllUsesWith(UniquedNode);
726  deleteAsSubclass();
727  return UniquedNode;
728 }
729 
730 MDNode *MDNode::replaceWithDistinctImpl() {
731  makeDistinct();
732  return this;
733 }
734 
735 void MDTuple::recalculateHash() {
736  setHash(MDTupleInfo::KeyTy::calculateHash(this));
737 }
738 
740  for (unsigned I = 0, E = getNumOperands(); I != E; ++I)
741  setOperand(I, nullptr);
742  if (Context.hasReplaceableUses()) {
743  Context.getReplaceableUses()->resolveAllUses(/* ResolveUsers */ false);
744  (void)Context.takeReplaceableUses();
745  }
746 }
747 
748 void MDNode::handleChangedOperand(void *Ref, Metadata *New) {
749  unsigned Op = static_cast<MDOperand *>(Ref) - op_begin();
750  assert(Op < getNumOperands() && "Expected valid operand");
751 
752  if (!isUniqued()) {
753  // This node is not uniqued. Just set the operand and be done with it.
754  setOperand(Op, New);
755  return;
756  }
757 
758  // This node is uniqued.
759  eraseFromStore();
760 
761  Metadata *Old = getOperand(Op);
762  setOperand(Op, New);
763 
764  // Drop uniquing for self-reference cycles and deleted constants.
765  if (New == this || (!New && Old && isa<ConstantAsMetadata>(Old))) {
766  if (!isResolved())
767  resolve();
769  return;
770  }
771 
772  // Re-unique the node.
773  auto *Uniqued = uniquify();
774  if (Uniqued == this) {
775  if (!isResolved())
776  resolveAfterOperandChange(Old, New);
777  return;
778  }
779 
780  // Collision.
781  if (!isResolved()) {
782  // Still unresolved, so RAUW.
783  //
784  // First, clear out all operands to prevent any recursion (similar to
785  // dropAllReferences(), but we still need the use-list).
786  for (unsigned O = 0, E = getNumOperands(); O != E; ++O)
787  setOperand(O, nullptr);
788  if (Context.hasReplaceableUses())
789  Context.getReplaceableUses()->replaceAllUsesWith(Uniqued);
790  deleteAsSubclass();
791  return;
792  }
793 
794  // Store in non-uniqued form if RAUW isn't possible.
796 }
797 
798 void MDNode::deleteAsSubclass() {
799  switch (getMetadataID()) {
800  default:
801  llvm_unreachable("Invalid subclass of MDNode");
802 #define HANDLE_MDNODE_LEAF(CLASS) \
803  case CLASS##Kind: \
804  delete cast<CLASS>(this); \
805  break;
806 #include "llvm/IR/Metadata.def"
807  }
808 }
809 
810 template <class T, class InfoT>
812  if (T *U = getUniqued(Store, N))
813  return U;
814 
815  Store.insert(N);
816  return N;
817 }
818 
819 template <class NodeTy> struct MDNode::HasCachedHash {
820  using Yes = char[1];
821  using No = char[2];
822  template <class U, U Val> struct SFINAE {};
823 
824  template <class U>
825  static Yes &check(SFINAE<void (U::*)(unsigned), &U::setHash> *);
826  template <class U> static No &check(...);
827 
828  static const bool value = sizeof(check<NodeTy>(nullptr)) == sizeof(Yes);
829 };
830 
831 MDNode *MDNode::uniquify() {
832  assert(!hasSelfReference(this) && "Cannot uniquify a self-referencing node");
833 
834  // Try to insert into uniquing store.
835  switch (getMetadataID()) {
836  default:
837  llvm_unreachable("Invalid or non-uniquable subclass of MDNode");
838 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
839  case CLASS##Kind: { \
840  CLASS *SubclassThis = cast<CLASS>(this); \
841  std::integral_constant<bool, HasCachedHash<CLASS>::value> \
842  ShouldRecalculateHash; \
843  dispatchRecalculateHash(SubclassThis, ShouldRecalculateHash); \
844  return uniquifyImpl(SubclassThis, getContext().pImpl->CLASS##s); \
845  }
846 #include "llvm/IR/Metadata.def"
847  }
848 }
849 
850 void MDNode::eraseFromStore() {
851  switch (getMetadataID()) {
852  default:
853  llvm_unreachable("Invalid or non-uniquable subclass of MDNode");
854 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
855  case CLASS##Kind: \
856  getContext().pImpl->CLASS##s.erase(cast<CLASS>(this)); \
857  break;
858 #include "llvm/IR/Metadata.def"
859  }
860 }
861 
862 MDTuple *MDTuple::getImpl(LLVMContext &Context, ArrayRef<Metadata *> MDs,
863  StorageType Storage, bool ShouldCreate) {
864  unsigned Hash = 0;
865  if (Storage == Uniqued) {
866  MDTupleInfo::KeyTy Key(MDs);
867  if (auto *N = getUniqued(Context.pImpl->MDTuples, Key))
868  return N;
869  if (!ShouldCreate)
870  return nullptr;
871  Hash = Key.getHash();
872  } else {
873  assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
874  }
875 
876  return storeImpl(new (MDs.size(), Storage)
877  MDTuple(Context, Storage, Hash, MDs),
878  Storage, Context.pImpl->MDTuples);
879 }
880 
882  assert(N->isTemporary() && "Expected temporary node");
883  N->replaceAllUsesWith(nullptr);
884  N->deleteAsSubclass();
885 }
886 
888  assert(!Context.hasReplaceableUses() && "Unexpected replaceable uses");
889  assert(!getNumUnresolved() && "Unexpected unresolved nodes");
890  Storage = Distinct;
891  assert(isResolved() && "Expected this to be resolved");
892 
893  // Reset the hash.
894  switch (getMetadataID()) {
895  default:
896  llvm_unreachable("Invalid subclass of MDNode");
897 #define HANDLE_MDNODE_LEAF(CLASS) \
898  case CLASS##Kind: { \
899  std::integral_constant<bool, HasCachedHash<CLASS>::value> ShouldResetHash; \
900  dispatchResetHash(cast<CLASS>(this), ShouldResetHash); \
901  break; \
902  }
903 #include "llvm/IR/Metadata.def"
904  }
905 
906  getContext().pImpl->DistinctMDNodes.push_back(this);
907 }
908 
909 void MDNode::replaceOperandWith(unsigned I, Metadata *New) {
910  if (getOperand(I) == New)
911  return;
912 
913  if (!isUniqued()) {
914  setOperand(I, New);
915  return;
916  }
917 
918  handleChangedOperand(mutable_begin() + I, New);
919 }
920 
921 void MDNode::setOperand(unsigned I, Metadata *New) {
922  assert(I < getNumOperands());
923  mutable_begin()[I].reset(New, isUniqued() ? this : nullptr);
924 }
925 
926 /// Get a node or a self-reference that looks like it.
927 ///
928 /// Special handling for finding self-references, for use by \a
929 /// MDNode::concatenate() and \a MDNode::intersect() to maintain behaviour from
930 /// when self-referencing nodes were still uniqued. If the first operand has
931 /// the same operands as \c Ops, return the first operand instead.
933  ArrayRef<Metadata *> Ops) {
934  if (!Ops.empty())
935  if (MDNode *N = dyn_cast_or_null<MDNode>(Ops[0]))
936  if (N->getNumOperands() == Ops.size() && N == N->getOperand(0)) {
937  for (unsigned I = 1, E = Ops.size(); I != E; ++I)
938  if (Ops[I] != N->getOperand(I))
939  return MDNode::get(Context, Ops);
940  return N;
941  }
942 
943  return MDNode::get(Context, Ops);
944 }
945 
947  if (!A)
948  return B;
949  if (!B)
950  return A;
951 
952  SmallSetVector<Metadata *, 4> MDs(A->op_begin(), A->op_end());
953  MDs.insert(B->op_begin(), B->op_end());
954 
955  // FIXME: This preserves long-standing behaviour, but is it really the right
956  // behaviour? Or was that an unintended side-effect of node uniquing?
957  return getOrSelfReference(A->getContext(), MDs.getArrayRef());
958 }
959 
961  if (!A || !B)
962  return nullptr;
963 
964  SmallSetVector<Metadata *, 4> MDs(A->op_begin(), A->op_end());
965  SmallPtrSet<Metadata *, 4> BSet(B->op_begin(), B->op_end());
966  MDs.remove_if([&](Metadata *MD) { return !BSet.count(MD); });
967 
968  // FIXME: This preserves long-standing behaviour, but is it really the right
969  // behaviour? Or was that an unintended side-effect of node uniquing?
970  return getOrSelfReference(A->getContext(), MDs.getArrayRef());
971 }
972 
974  if (!A || !B)
975  return nullptr;
976 
977  // Take the intersection of domains then union the scopes
978  // within those domains
980  SmallPtrSet<const MDNode *, 16> IntersectDomains;
982  for (const MDOperand &MDOp : A->operands())
983  if (const MDNode *NAMD = dyn_cast<MDNode>(MDOp))
984  if (const MDNode *Domain = AliasScopeNode(NAMD).getDomain())
985  ADomains.insert(Domain);
986 
987  for (const MDOperand &MDOp : B->operands())
988  if (const MDNode *NAMD = dyn_cast<MDNode>(MDOp))
989  if (const MDNode *Domain = AliasScopeNode(NAMD).getDomain())
990  if (ADomains.contains(Domain)) {
991  IntersectDomains.insert(Domain);
992  MDs.insert(MDOp);
993  }
994 
995  for (const MDOperand &MDOp : A->operands())
996  if (const MDNode *NAMD = dyn_cast<MDNode>(MDOp))
997  if (const MDNode *Domain = AliasScopeNode(NAMD).getDomain())
998  if (IntersectDomains.contains(Domain))
999  MDs.insert(MDOp);
1000 
1001  return MDs.empty() ? nullptr
1002  : getOrSelfReference(A->getContext(), MDs.getArrayRef());
1003 }
1004 
1006  if (!A || !B)
1007  return nullptr;
1008 
1009  APFloat AVal = mdconst::extract<ConstantFP>(A->getOperand(0))->getValueAPF();
1010  APFloat BVal = mdconst::extract<ConstantFP>(B->getOperand(0))->getValueAPF();
1011  if (AVal < BVal)
1012  return A;
1013  return B;
1014 }
1015 
1016 static bool isContiguous(const ConstantRange &A, const ConstantRange &B) {
1017  return A.getUpper() == B.getLower() || A.getLower() == B.getUpper();
1018 }
1019 
1020 static bool canBeMerged(const ConstantRange &A, const ConstantRange &B) {
1021  return !A.intersectWith(B).isEmptySet() || isContiguous(A, B);
1022 }
1023 
1025  ConstantInt *Low, ConstantInt *High) {
1026  ConstantRange NewRange(Low->getValue(), High->getValue());
1027  unsigned Size = EndPoints.size();
1028  APInt LB = EndPoints[Size - 2]->getValue();
1029  APInt LE = EndPoints[Size - 1]->getValue();
1030  ConstantRange LastRange(LB, LE);
1031  if (canBeMerged(NewRange, LastRange)) {
1032  ConstantRange Union = LastRange.unionWith(NewRange);
1033  Type *Ty = High->getType();
1034  EndPoints[Size - 2] =
1035  cast<ConstantInt>(ConstantInt::get(Ty, Union.getLower()));
1036  EndPoints[Size - 1] =
1037  cast<ConstantInt>(ConstantInt::get(Ty, Union.getUpper()));
1038  return true;
1039  }
1040  return false;
1041 }
1042 
1044  ConstantInt *Low, ConstantInt *High) {
1045  if (!EndPoints.empty())
1046  if (tryMergeRange(EndPoints, Low, High))
1047  return;
1048 
1049  EndPoints.push_back(Low);
1050  EndPoints.push_back(High);
1051 }
1052 
1054  // Given two ranges, we want to compute the union of the ranges. This
1055  // is slightly complicated by having to combine the intervals and merge
1056  // the ones that overlap.
1057 
1058  if (!A || !B)
1059  return nullptr;
1060 
1061  if (A == B)
1062  return A;
1063 
1064  // First, walk both lists in order of the lower boundary of each interval.
1065  // At each step, try to merge the new interval to the last one we adedd.
1067  int AI = 0;
1068  int BI = 0;
1069  int AN = A->getNumOperands() / 2;
1070  int BN = B->getNumOperands() / 2;
1071  while (AI < AN && BI < BN) {
1072  ConstantInt *ALow = mdconst::extract<ConstantInt>(A->getOperand(2 * AI));
1073  ConstantInt *BLow = mdconst::extract<ConstantInt>(B->getOperand(2 * BI));
1074 
1075  if (ALow->getValue().slt(BLow->getValue())) {
1076  addRange(EndPoints, ALow,
1077  mdconst::extract<ConstantInt>(A->getOperand(2 * AI + 1)));
1078  ++AI;
1079  } else {
1080  addRange(EndPoints, BLow,
1081  mdconst::extract<ConstantInt>(B->getOperand(2 * BI + 1)));
1082  ++BI;
1083  }
1084  }
1085  while (AI < AN) {
1086  addRange(EndPoints, mdconst::extract<ConstantInt>(A->getOperand(2 * AI)),
1087  mdconst::extract<ConstantInt>(A->getOperand(2 * AI + 1)));
1088  ++AI;
1089  }
1090  while (BI < BN) {
1091  addRange(EndPoints, mdconst::extract<ConstantInt>(B->getOperand(2 * BI)),
1092  mdconst::extract<ConstantInt>(B->getOperand(2 * BI + 1)));
1093  ++BI;
1094  }
1095 
1096  // If we have more than 2 ranges (4 endpoints) we have to try to merge
1097  // the last and first ones.
1098  unsigned Size = EndPoints.size();
1099  if (Size > 4) {
1100  ConstantInt *FB = EndPoints[0];
1101  ConstantInt *FE = EndPoints[1];
1102  if (tryMergeRange(EndPoints, FB, FE)) {
1103  for (unsigned i = 0; i < Size - 2; ++i) {
1104  EndPoints[i] = EndPoints[i + 2];
1105  }
1106  EndPoints.resize(Size - 2);
1107  }
1108  }
1109 
1110  // If in the end we have a single range, it is possible that it is now the
1111  // full range. Just drop the metadata in that case.
1112  if (EndPoints.size() == 2) {
1113  ConstantRange Range(EndPoints[0]->getValue(), EndPoints[1]->getValue());
1114  if (Range.isFullSet())
1115  return nullptr;
1116  }
1117 
1119  MDs.reserve(EndPoints.size());
1120  for (auto *I : EndPoints)
1121  MDs.push_back(ConstantAsMetadata::get(I));
1122  return MDNode::get(A->getContext(), MDs);
1123 }
1124 
1126  if (!A || !B)
1127  return nullptr;
1128 
1129  ConstantInt *AVal = mdconst::extract<ConstantInt>(A->getOperand(0));
1130  ConstantInt *BVal = mdconst::extract<ConstantInt>(B->getOperand(0));
1131  if (AVal->getZExtValue() < BVal->getZExtValue())
1132  return A;
1133  return B;
1134 }
1135 
1136 //===----------------------------------------------------------------------===//
1137 // NamedMDNode implementation.
1138 //
1139 
1142 }
1143 
1144 NamedMDNode::NamedMDNode(const Twine &N)
1145  : Name(N.str()), Operands(new SmallVector<TrackingMDRef, 4>()) {}
1146 
1147 NamedMDNode::~NamedMDNode() {
1149  delete &getNMDOps(Operands);
1150 }
1151 
1153  return (unsigned)getNMDOps(Operands).size();
1154 }
1155 
1157  assert(i < getNumOperands() && "Invalid Operand number!");
1158  auto *N = getNMDOps(Operands)[i].get();
1159  return cast_or_null<MDNode>(N);
1160 }
1161 
1162 void NamedMDNode::addOperand(MDNode *M) { getNMDOps(Operands).emplace_back(M); }
1163 
1164 void NamedMDNode::setOperand(unsigned I, MDNode *New) {
1165  assert(I < getNumOperands() && "Invalid operand number");
1166  getNMDOps(Operands)[I].reset(New);
1167 }
1168 
1170 
1171 void NamedMDNode::clearOperands() { getNMDOps(Operands).clear(); }
1172 
1173 StringRef NamedMDNode::getName() const { return StringRef(Name); }
1174 
1175 //===----------------------------------------------------------------------===//
1176 // Instruction Metadata method implementations.
1177 //
1178 
1179 MDNode *MDAttachments::lookup(unsigned ID) const {
1180  for (const auto &A : Attachments)
1181  if (A.MDKind == ID)
1182  return A.Node;
1183  return nullptr;
1184 }
1185 
1186 void MDAttachments::get(unsigned ID, SmallVectorImpl<MDNode *> &Result) const {
1187  for (const auto &A : Attachments)
1188  if (A.MDKind == ID)
1189  Result.push_back(A.Node);
1190 }
1191 
1193  SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
1194  for (const auto &A : Attachments)
1195  Result.emplace_back(A.MDKind, A.Node);
1196 
1197  // Sort the resulting array so it is stable with respect to metadata IDs. We
1198  // need to preserve the original insertion order though.
1199  if (Result.size() > 1)
1200  llvm::stable_sort(Result, less_first());
1201 }
1202 
1203 void MDAttachments::set(unsigned ID, MDNode *MD) {
1204  erase(ID);
1205  if (MD)
1206  insert(ID, *MD);
1207 }
1208 
1209 void MDAttachments::insert(unsigned ID, MDNode &MD) {
1210  Attachments.push_back({ID, TrackingMDNodeRef(&MD)});
1211 }
1212 
1213 bool MDAttachments::erase(unsigned ID) {
1214  if (empty())
1215  return false;
1216 
1217  // Common case is one value.
1218  if (Attachments.size() == 1 && Attachments.back().MDKind == ID) {
1219  Attachments.pop_back();
1220  return true;
1221  }
1222 
1223  auto OldSize = Attachments.size();
1224  llvm::erase_if(Attachments,
1225  [ID](const Attachment &A) { return A.MDKind == ID; });
1226  return OldSize != Attachments.size();
1227 }
1228 
1229 MDNode *Value::getMetadata(unsigned KindID) const {
1230  if (!hasMetadata())
1231  return nullptr;
1232  const auto &Info = getContext().pImpl->ValueMetadata[this];
1233  assert(!Info.empty() && "bit out of sync with hash table");
1234  return Info.lookup(KindID);
1235 }
1236 
1238  if (!hasMetadata())
1239  return nullptr;
1240  const auto &Info = getContext().pImpl->ValueMetadata[this];
1241  assert(!Info.empty() && "bit out of sync with hash table");
1242  return Info.lookup(getContext().getMDKindID(Kind));
1243 }
1244 
1245 void Value::getMetadata(unsigned KindID, SmallVectorImpl<MDNode *> &MDs) const {
1246  if (hasMetadata())
1247  getContext().pImpl->ValueMetadata[this].get(KindID, MDs);
1248 }
1249 
1251  if (hasMetadata())
1252  getMetadata(getContext().getMDKindID(Kind), MDs);
1253 }
1254 
1256  SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs) const {
1257  if (hasMetadata()) {
1258  assert(getContext().pImpl->ValueMetadata.count(this) &&
1259  "bit out of sync with hash table");
1260  const auto &Info = getContext().pImpl->ValueMetadata.find(this)->second;
1261  assert(!Info.empty() && "Shouldn't have called this");
1262  Info.getAll(MDs);
1263  }
1264 }
1265 
1266 void Value::setMetadata(unsigned KindID, MDNode *Node) {
1267  assert(isa<Instruction>(this) || isa<GlobalObject>(this));
1268 
1269  // Handle the case when we're adding/updating metadata on a value.
1270  if (Node) {
1271  auto &Info = getContext().pImpl->ValueMetadata[this];
1272  assert(!Info.empty() == HasMetadata && "bit out of sync with hash table");
1273  if (Info.empty())
1274  HasMetadata = true;
1275  Info.set(KindID, Node);
1276  return;
1277  }
1278 
1279  // Otherwise, we're removing metadata from an instruction.
1280  assert((HasMetadata == (getContext().pImpl->ValueMetadata.count(this) > 0)) &&
1281  "bit out of sync with hash table");
1282  if (!HasMetadata)
1283  return; // Nothing to remove!
1284  auto &Info = getContext().pImpl->ValueMetadata[this];
1285 
1286  // Handle removal of an existing value.
1287  Info.erase(KindID);
1288  if (!Info.empty())
1289  return;
1290  getContext().pImpl->ValueMetadata.erase(this);
1291  HasMetadata = false;
1292 }
1293 
1295  if (!Node && !HasMetadata)
1296  return;
1297  setMetadata(getContext().getMDKindID(Kind), Node);
1298 }
1299 
1300 void Value::addMetadata(unsigned KindID, MDNode &MD) {
1301  assert(isa<Instruction>(this) || isa<GlobalObject>(this));
1302  if (!HasMetadata)
1303  HasMetadata = true;
1304  getContext().pImpl->ValueMetadata[this].insert(KindID, MD);
1305 }
1306 
1308  addMetadata(getContext().getMDKindID(Kind), MD);
1309 }
1310 
1311 bool Value::eraseMetadata(unsigned KindID) {
1312  // Nothing to unset.
1313  if (!HasMetadata)
1314  return false;
1315 
1316  auto &Store = getContext().pImpl->ValueMetadata[this];
1317  bool Changed = Store.erase(KindID);
1318  if (Store.empty())
1319  clearMetadata();
1320  return Changed;
1321 }
1322 
1324  if (!HasMetadata)
1325  return;
1326  assert(getContext().pImpl->ValueMetadata.count(this) &&
1327  "bit out of sync with hash table");
1328  getContext().pImpl->ValueMetadata.erase(this);
1329  HasMetadata = false;
1330 }
1331 
1333  if (!Node && !hasMetadata())
1334  return;
1335  setMetadata(getContext().getMDKindID(Kind), Node);
1336 }
1337 
1338 MDNode *Instruction::getMetadataImpl(StringRef Kind) const {
1339  return getMetadataImpl(getContext().getMDKindID(Kind));
1340 }
1341 
1343  if (!Value::hasMetadata())
1344  return; // Nothing to remove!
1345 
1346  if (KnownIDs.empty()) {
1347  // Just drop our entry at the store.
1348  clearMetadata();
1349  return;
1350  }
1351 
1352  SmallSet<unsigned, 4> KnownSet;
1353  KnownSet.insert(KnownIDs.begin(), KnownIDs.end());
1354 
1355  auto &MetadataStore = getContext().pImpl->ValueMetadata;
1356  auto &Info = MetadataStore[this];
1357  assert(!Info.empty() && "bit out of sync with hash table");
1358  Info.remove_if([&KnownSet](const MDAttachments::Attachment &I) {
1359  return !KnownSet.count(I.MDKind);
1360  });
1361 
1362  if (Info.empty()) {
1363  // Drop our entry at the store.
1364  clearMetadata();
1365  }
1366 }
1367 
1368 void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
1369  if (!Node && !hasMetadata())
1370  return;
1371 
1372  // Handle 'dbg' as a special case since it is not stored in the hash table.
1373  if (KindID == LLVMContext::MD_dbg) {
1374  DbgLoc = DebugLoc(Node);
1375  return;
1376  }
1377 
1378  Value::setMetadata(KindID, Node);
1379 }
1380 
1382  MDBuilder MDB(getContext());
1383 
1384  auto *Existing = getMetadata(LLVMContext::MD_annotation);
1386  bool AppendName = true;
1387  if (Existing) {
1388  auto *Tuple = cast<MDTuple>(Existing);
1389  for (auto &N : Tuple->operands()) {
1390  if (cast<MDString>(N.get())->getString() == Name)
1391  AppendName = false;
1392  Names.push_back(N.get());
1393  }
1394  }
1395  if (AppendName)
1396  Names.push_back(MDB.createString(Name));
1397 
1398  MDNode *MD = MDTuple::get(getContext(), Names);
1399  setMetadata(LLVMContext::MD_annotation, MD);
1400 }
1401 
1403  AAMDNodes Result;
1404  Result.TBAA = getMetadata(LLVMContext::MD_tbaa);
1405  Result.TBAAStruct = getMetadata(LLVMContext::MD_tbaa_struct);
1406  Result.Scope = getMetadata(LLVMContext::MD_alias_scope);
1407  Result.NoAlias = getMetadata(LLVMContext::MD_noalias);
1408  return Result;
1409 }
1410 
1412  setMetadata(LLVMContext::MD_tbaa, N.TBAA);
1413  setMetadata(LLVMContext::MD_tbaa_struct, N.TBAAStruct);
1414  setMetadata(LLVMContext::MD_alias_scope, N.Scope);
1415  setMetadata(LLVMContext::MD_noalias, N.NoAlias);
1416 }
1417 
1418 MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
1419  // Handle 'dbg' as a special case since it is not stored in the hash table.
1420  if (KindID == LLVMContext::MD_dbg)
1421  return DbgLoc.getAsMDNode();
1422  return Value::getMetadata(KindID);
1423 }
1424 
1425 void Instruction::getAllMetadataImpl(
1426  SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
1427  Result.clear();
1428 
1429  // Handle 'dbg' as a special case since it is not stored in the hash table.
1430  if (DbgLoc) {
1431  Result.push_back(
1432  std::make_pair((unsigned)LLVMContext::MD_dbg, DbgLoc.getAsMDNode()));
1433  }
1434  Value::getAllMetadata(Result);
1435 }
1436 
1438  uint64_t &FalseVal) const {
1439  assert(
1440  (getOpcode() == Instruction::Br || getOpcode() == Instruction::Select) &&
1441  "Looking for branch weights on something besides branch or select");
1442 
1443  auto *ProfileData = getMetadata(LLVMContext::MD_prof);
1444  if (!ProfileData || ProfileData->getNumOperands() != 3)
1445  return false;
1446 
1447  auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand(0));
1448  if (!ProfDataName || !ProfDataName->getString().equals("branch_weights"))
1449  return false;
1450 
1451  auto *CITrue = mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(1));
1452  auto *CIFalse = mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(2));
1453  if (!CITrue || !CIFalse)
1454  return false;
1455 
1456  TrueVal = CITrue->getValue().getZExtValue();
1457  FalseVal = CIFalse->getValue().getZExtValue();
1458 
1459  return true;
1460 }
1461 
1463  assert(
1464  (getOpcode() == Instruction::Br || getOpcode() == Instruction::Select ||
1465  getOpcode() == Instruction::Call || getOpcode() == Instruction::Invoke ||
1466  getOpcode() == Instruction::IndirectBr ||
1467  getOpcode() == Instruction::Switch) &&
1468  "Looking for branch weights on something besides branch");
1469 
1470  TotalVal = 0;
1471  auto *ProfileData = getMetadata(LLVMContext::MD_prof);
1472  if (!ProfileData)
1473  return false;
1474 
1475  auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand(0));
1476  if (!ProfDataName)
1477  return false;
1478 
1479  if (ProfDataName->getString().equals("branch_weights")) {
1480  TotalVal = 0;
1481  for (unsigned i = 1; i < ProfileData->getNumOperands(); i++) {
1482  auto *V = mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(i));
1483  if (!V)
1484  return false;
1485  TotalVal += V->getValue().getZExtValue();
1486  }
1487  return true;
1488  } else if (ProfDataName->getString().equals("VP") &&
1489  ProfileData->getNumOperands() > 3) {
1490  TotalVal = mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(2))
1491  ->getValue()
1492  .getZExtValue();
1493  return true;
1494  }
1495  return false;
1496 }
1497 
1498 void GlobalObject::copyMetadata(const GlobalObject *Other, unsigned Offset) {
1500  Other->getAllMetadata(MDs);
1501  for (auto &MD : MDs) {
1502  // We need to adjust the type metadata offset.
1503  if (Offset != 0 && MD.first == LLVMContext::MD_type) {
1504  auto *OffsetConst = cast<ConstantInt>(
1505  cast<ConstantAsMetadata>(MD.second->getOperand(0))->getValue());
1506  Metadata *TypeId = MD.second->getOperand(1);
1507  auto *NewOffsetMD = ConstantAsMetadata::get(ConstantInt::get(
1508  OffsetConst->getType(), OffsetConst->getValue() + Offset));
1509  addMetadata(LLVMContext::MD_type,
1510  *MDNode::get(getContext(), {NewOffsetMD, TypeId}));
1511  continue;
1512  }
1513  // If an offset adjustment was specified we need to modify the DIExpression
1514  // to prepend the adjustment:
1515  // !DIExpression(DW_OP_plus, Offset, [original expr])
1516  auto *Attachment = MD.second;
1517  if (Offset != 0 && MD.first == LLVMContext::MD_dbg) {
1518  DIGlobalVariable *GV = dyn_cast<DIGlobalVariable>(Attachment);
1519  DIExpression *E = nullptr;
1520  if (!GV) {
1521  auto *GVE = cast<DIGlobalVariableExpression>(Attachment);
1522  GV = GVE->getVariable();
1523  E = GVE->getExpression();
1524  }
1525  ArrayRef<uint64_t> OrigElements;
1526  if (E)
1527  OrigElements = E->getElements();
1528  std::vector<uint64_t> Elements(OrigElements.size() + 2);
1529  Elements[0] = dwarf::DW_OP_plus_uconst;
1530  Elements[1] = Offset;
1531  llvm::copy(OrigElements, Elements.begin() + 2);
1532  E = DIExpression::get(getContext(), Elements);
1533  Attachment = DIGlobalVariableExpression::get(getContext(), GV, E);
1534  }
1535  addMetadata(MD.first, *Attachment);
1536  }
1537 }
1538 
1540  addMetadata(
1541  LLVMContext::MD_type,
1544  Type::getInt64Ty(getContext()), Offset)),
1545  TypeID}));
1546 }
1547 
1549  // Remove any existing vcall visibility metadata first in case we are
1550  // updating.
1551  eraseMetadata(LLVMContext::MD_vcall_visibility);
1552  addMetadata(LLVMContext::MD_vcall_visibility,
1556 }
1557 
1559  if (MDNode *MD = getMetadata(LLVMContext::MD_vcall_visibility)) {
1560  uint64_t Val = cast<ConstantInt>(
1561  cast<ConstantAsMetadata>(MD->getOperand(0))->getValue())
1562  ->getZExtValue();
1563  assert(Val <= 2 && "unknown vcall visibility!");
1564  return (VCallVisibility)Val;
1565  }
1566  return VCallVisibility::VCallVisibilityPublic;
1567 }
1568 
1570  setMetadata(LLVMContext::MD_dbg, SP);
1571 }
1572 
1574  return cast_or_null<DISubprogram>(getMetadata(LLVMContext::MD_dbg));
1575 }
1576 
1578  if (DISubprogram *SP = getSubprogram()) {
1579  if (DICompileUnit *CU = SP->getUnit()) {
1580  return CU->getDebugInfoForProfiling();
1581  }
1582  }
1583  return false;
1584 }
1585 
1587  addMetadata(LLVMContext::MD_dbg, *GV);
1588 }
1589 
1593  getMetadata(LLVMContext::MD_dbg, MDs);
1594  for (MDNode *MD : MDs)
1595  GVs.push_back(cast<DIGlobalVariableExpression>(MD));
1596 }
llvm::ConstantRange::isFullSet
bool isFullSet() const
Return true if this set contains all of the elements possible for this data-type.
Definition: ConstantRange.cpp:366
i
i
Definition: README.txt:29
llvm::ReplaceableMetadataImpl::getAllArgListUsers
SmallVector< Metadata * > getAllArgListUsers()
Returns the list of all DIArgList users of this.
Definition: Metadata.cpp:194
llvm::alignTo
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition: Alignment.h:148
llvm::LLVMContext::pImpl
LLVMContextImpl *const pImpl
Definition: LLVMContext.h:70
canonicalizeMetadataForValue
static Metadata * canonicalizeMetadataForValue(LLVMContext &Context, Metadata *MD)
Canonicalize metadata arguments to intrinsics.
Definition: Metadata.cpp:80
getNMDOps
static SmallVector< TrackingMDRef, 4 > & getNMDOps(void *Operands)
Definition: Metadata.cpp:1140
MathExtras.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::MDNode::storeImpl
static T * storeImpl(T *N, StorageType Storage, StoreT &Store)
Definition: MetadataImpl.h:42
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::MetadataTracking::untrack
static void untrack(Metadata *&MD)
Stop tracking a reference to metadata.
Definition: Metadata.h:248
llvm::MetadataTracking::isReplaceable
static bool isReplaceable(const Metadata &MD)
Check whether metadata is replaceable.
Definition: Metadata.cpp:190
llvm::Metadata::Distinct
@ Distinct
Definition: Metadata.h:70
MetadataImpl.h
Metadata.h
llvm::NamedMDNode::getNumOperands
unsigned getNumOperands() const
Definition: Metadata.cpp:1152
llvm::GlobalVariable::getDebugInfo
void getDebugInfo(SmallVectorImpl< DIGlobalVariableExpression * > &GVs) const
Fill the vector with all debug info attachements.
Definition: Metadata.cpp:1590
DebugInfoMetadata.h
T
llvm::GlobalValue::Visibility
unsigned Visibility
Definition: GlobalValue.h:94
StringRef.h
llvm::MetadataTracking::track
static bool track(Metadata *&MD)
Track the reference to metadata.
Definition: Metadata.h:223
llvm::MDNode::setOperand
void setOperand(unsigned I, Metadata *New)
Set an operand.
Definition: Metadata.cpp:921
llvm::ConstantInt::getValue
const APInt & getValue() const
Return the constant as an APInt value reference.
Definition: Constants.h:133
High
uint64_t High
Definition: NVVMIntrRange.cpp:61
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1185
llvm::getUniqued
static T * getUniqued(DenseSet< T *, InfoT > &Store, const typename InfoT::KeyTy &Key)
Definition: MetadataImpl.h:22
llvm::MDAttachments::set
void set(unsigned ID, MDNode *MD)
Set an attachment to a particular node.
Definition: Metadata.cpp:1203
llvm::MDNode::HasCachedHash::SFINAE
Definition: Metadata.cpp:822
llvm::Function::getSubprogram
DISubprogram * getSubprogram() const
Get the attached subprogram.
Definition: Metadata.cpp:1573
ErrorHandling.h
llvm::erase_if
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
Definition: STLExtras.h:1797
llvm::DebugLoc::getAsMDNode
MDNode * getAsMDNode() const
Return this as a bar MDNode.
Definition: DebugLoc.h:99
llvm::ValueAsMetadata::handleDeletion
static void handleDeletion(Value *V)
Definition: Metadata.cpp:418
tryMergeRange
static bool tryMergeRange(SmallVectorImpl< ConstantInt * > &EndPoints, ConstantInt *Low, ConstantInt *High)
Definition: Metadata.cpp:1024
llvm::MDAttachments::erase
bool erase(unsigned ID)
Remove attachments with the given ID.
Definition: Metadata.cpp:1213
APInt.h
llvm::AAMDNodes
A collection of metadata nodes that might be associated with a memory access used by the alias-analys...
Definition: Metadata.h:652
llvm::NamedMDNode::setOperand
void setOperand(unsigned I, MDNode *New)
Definition: Metadata.cpp:1164
llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::getArrayRef
ArrayRef< T > getArrayRef() const
Definition: SetVector.h:63
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
Module.h
llvm::copy
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1658
llvm::SmallSet
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:136
getLocalFunctionMetadata
static DISubprogram * getLocalFunctionMetadata(Value *V)
Definition: Metadata.cpp:377
llvm::ConstantAsMetadata::get
static ConstantAsMetadata * get(Constant *C)
Definition: Metadata.h:420
GlobalObject.h
llvm::SmallPtrSet
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:450
llvm::MetadataTracking::retrack
static bool retrack(Metadata *&MD, Metadata *&New)
Move tracking from one reference to another.
Definition: Metadata.h:259
llvm::NamedMDNode::dropAllReferences
void dropAllReferences()
Remove all uses and clear node vector.
Definition: Metadata.h:1542
llvm::tgtok::FalseVal
@ FalseVal
Definition: TGLexer.h:61
llvm::ValueAsMetadata::get
static ValueAsMetadata * get(Value *V)
Definition: Metadata.cpp:394
llvm::LocalAsMetadata
Definition: Metadata.h:437
STLExtras.h
llvm::LLVMContextImpl::ValuesAsMetadata
DenseMap< Value *, ValueAsMetadata * > ValuesAsMetadata
Definition: LLVMContextImpl.h:1414
llvm::MDNode::MDNode
MDNode(LLVMContext &Context, unsigned ID, StorageType Storage, ArrayRef< Metadata * > Ops1, ArrayRef< Metadata * > Ops2=None)
Definition: Metadata.cpp:543
llvm::DIExpression
DWARF expression.
Definition: DebugInfoMetadata.h:2548
llvm::MetadataAsValue
Metadata wrapper in the Value hierarchy.
Definition: Metadata.h:176
llvm::count_if
auto count_if(R &&Range, UnaryPredicate P)
Wrapper function around std::count_if to count the number of times an element satisfying a given pred...
Definition: STLExtras.h:1706
llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::remove_if
bool remove_if(UnaryPredicate P)
Remove items from the set vector based on a predicate function.
Definition: SetVector.h:199
llvm::Instruction::dropUnknownNonDebugMetadata
void dropUnknownNonDebugMetadata()
Definition: Instruction.h:330
llvm::PointerUnion::get
T get() const
Returns the value of the specified pointer type.
Definition: PointerUnion.h:155
llvm::ArrayRef::empty
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:159
new
Common register allocation spilling lr str ldr sxth r3 ldr mla r4 can lr mov lr str ldr sxth r3 mla r4 and then merge mul and lr str ldr sxth r3 mla r4 It also increase the likelihood the store may become dead bb27 Successors according to LLVM ID Predecessors according to mbb< bb27, 0x8b0a7c0 > Note ADDri is not a two address instruction its result reg1037 is an operand of the PHI node in bb76 and its operand reg1039 is the result of the PHI node We should treat it as a two address code and make sure the ADDri is scheduled after any node that reads reg1039 Use info(i.e. register scavenger) to assign it a free register to allow reuse the collector could move the objects and invalidate the derived pointer This is bad enough in the first but safe points can crop up unpredictably **array_addr i32 n y store obj * new
Definition: README.txt:125
llvm::MDNode::get
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1289
llvm::RISCVFenceField::R
@ R
Definition: RISCVBaseInfo.h:240
llvm::MDNode::operands
op_range operands() const
Definition: Metadata.h:1191
llvm::Instruction::setMetadata
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
Definition: Metadata.cpp:1368
Uses
SmallPtrSet< MachineInstr *, 2 > Uses
Definition: ARMLowOverheadLoops.cpp:585
llvm::BasicBlock
LLVM Basic Block Representation.
Definition: BasicBlock.h:55
llvm::PointerUnion::is
bool is() const
Test if the Union currently holds the type matching T.
Definition: PointerUnion.h:150
llvm::MDNode::storeDistinctInContext
void storeDistinctInContext()
Definition: Metadata.cpp:887
llvm::MDNode::getNumOperands
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1199
llvm::MDOperand::reset
void reset()
Definition: Metadata.h:789
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
llvm::MDAttachments::Attachment
Definition: LLVMContextImpl.h:1306
Instruction.h
llvm::NamedMDNode::addOperand
void addOperand(MDNode *M)
Definition: Metadata.cpp:1162
llvm::ConstantInt
This is the shared class of boolean and integer constants.
Definition: Constants.h:79
llvm::Type::getMetadataTy
static Type * getMetadataTy(LLVMContext &C)
Definition: Type.cpp:228
llvm::Instruction::getOpcode
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:157
llvm::Instruction::extractProfTotalWeight
bool extractProfTotalWeight(uint64_t &TotalVal) const
Retrieve total raw weight values of a branch.
Definition: Metadata.cpp:1462
llvm::ModRefInfo::Ref
@ Ref
The access may reference the value stored in memory.
Constants.h
llvm::ValueAsMetadata::replaceAllUsesWith
void replaceAllUsesWith(Metadata *MD)
Handle collisions after Value::replaceAllUsesWith().
Definition: Metadata.h:402
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::less_first
Function object to check whether the first component of a std::pair compares less than the first comp...
Definition: STLExtras.h:1344
C
(vector float) vec_cmpeq(*A, *B) C
Definition: README_ALTIVEC.txt:86
Twine.h
llvm::GlobalObject
Definition: GlobalObject.h:27
llvm::NamedMDNode::getName
StringRef getName() const
Definition: Metadata.cpp:1173
llvm::GlobalObject::setVCallVisibilityMetadata
void setVCallVisibilityMetadata(VCallVisibility Visibility)
Definition: Metadata.cpp:1548
llvm::MDNode::mutable_operands
mutable_op_range mutable_operands()
Definition: Metadata.h:994
llvm::MDTuple
Tuple of metadata.
Definition: Metadata.h:1230
Domain
Domain
Definition: CorrelatedValuePropagation.cpp:710
llvm::AMDGPU::PALMD::Key
Key
PAL metadata keys.
Definition: AMDGPUMetadata.h:486
llvm::MDNode::HasCachedHash::Yes
char[1] Yes
Definition: Metadata.cpp:820
DenseSet.h
llvm::Metadata::StorageType
StorageType
Active type of storage.
Definition: Metadata.h:70
check
#define check(cond)
llvm::MDAttachments::lookup
MDNode * lookup(unsigned ID) const
Returns the first attachment with the given ID or nullptr if no such attachment exists.
Definition: Metadata.cpp:1179
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
llvm::Metadata::getMetadataID
unsigned getMetadataID() const
Definition: Metadata.h:102
llvm::MDNode::mutable_begin
MDOperand * mutable_begin()
Definition: Metadata.h:989
llvm::NamedMDNode::eraseFromParent
void eraseFromParent()
Drop all references and remove the node from parent module.
Definition: Metadata.cpp:1169
llvm::SPII::Store
@ Store
Definition: SparcInstrInfo.h:33
LLVMContextImpl.h
MDBuilder.h
llvm::ConstantRange::unionWith
ConstantRange unionWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the union of this range with another range.
Definition: ConstantRange.cpp:629
APFloat.h
llvm::SmallVectorImpl::resize
void resize(size_type N)
Definition: SmallVector.h:619
llvm::AArch64CC::LE
@ LE
Definition: AArch64BaseInfo.h:268
llvm::UndefValue::get
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1769
llvm::ThreadPriority::Low
@ Low
Lower the current thread's priority such that it does not affect foreground tasks significantly.
llvm::ConstantInt::get
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:919
DebugLoc.h
SmallPtrSet.h
llvm::Instruction::getAAMetadata
AAMDNodes getAAMetadata() const
Returns the AA metadata for this instruction.
Definition: Metadata.cpp:1402
Info
Analysis containing CSE Info
Definition: CSEInfo.cpp:27
StringMap.h
llvm::MCID::Call
@ Call
Definition: MCInstrDesc.h:155
llvm::Metadata
Root of the metadata hierarchy.
Definition: Metadata.h:62
llvm::MDNode::HasCachedHash::No
char[2] No
Definition: Metadata.cpp:821
llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::empty
bool empty() const
Determine if the SetVector is empty or not.
Definition: SetVector.h:72
llvm::Instruction::extractProfMetadata
bool extractProfMetadata(uint64_t &TrueVal, uint64_t &FalseVal) const
Retrieve the raw weight values of a conditional branch or select.
Definition: Metadata.cpp:1437
llvm::None
const NoneType None
Definition: None.h:24
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
Type.h
llvm::Instruction::getMetadata
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
Definition: Instruction.h:279
getDomain
Domain getDomain(Value *V, LazyValueInfo *LVI, Instruction *CxtI)
Definition: CorrelatedValuePropagation.cpp:712
Operands
mir Rename Register Operands
Definition: MIRNamerPass.cpp:74
llvm::TrackingMDNodeRef
TypedTrackingMDRef< MDNode > TrackingMDNodeRef
Definition: TrackingMDRef.h:141
llvm::MDNode::getOperand
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1193
llvm::MDBuilder::createString
MDString * createString(StringRef Str)
Return the given string as metadata.
Definition: MDBuilder.cpp:20
llvm::GlobalVariable::addDebugInfo
void addDebugInfo(DIGlobalVariableExpression *GV)
Attach a DIGlobalVariableExpression.
Definition: Metadata.cpp:1586
llvm::MDNode::resolveCycles
void resolveCycles()
Resolve cycles.
Definition: Metadata.cpp:673
llvm::LLVMContextImpl::ValueMetadata
DenseMap< const Value *, MDAttachments > ValueMetadata
Collection of metadata used in this context.
Definition: LLVMContextImpl.h:1498
llvm::DenseSet
Implements a dense probed hash-table based set.
Definition: DenseSet.h:268
BasicBlock.h
llvm::ReplaceableMetadataImpl::resolveAllUses
void resolveAllUses(bool ResolveUsers=true)
Resolve all uses of this.
Definition: Metadata.cpp:326
llvm::APFloat
Definition: APFloat.h:700
llvm::Function::setSubprogram
void setSubprogram(DISubprogram *SP)
Set the attached subprogram.
Definition: Metadata.cpp:1569
llvm::APInt::slt
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition: APInt.h:1080
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:239
llvm::MDNode::resolve
void resolve()
Resolve a unique, unresolved node.
Definition: Metadata.cpp:627
llvm::Constant
This is an important base class in LLVM.
Definition: Constant.h:41
llvm::MDNode::intersect
static MDNode * intersect(MDNode *A, MDNode *B)
Definition: Metadata.cpp:960
llvm::SmallSet::count
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition: SmallSet.h:166
llvm::Instruction::addAnnotationMetadata
void addAnnotationMetadata(StringRef Annotation)
Adds an !annotation metadata node with Annotation to this instruction.
Definition: Metadata.cpp:1381
llvm::Value::IsUsedByMD
unsigned IsUsedByMD
Definition: Value.h:117
llvm::Value::addMetadata
void addMetadata(unsigned KindID, MDNode &MD)
Add a metadata attachment.
Definition: Metadata.cpp:1300
uint64_t
llvm::Function::isDebugInfoForProfiling
bool isDebugInfoForProfiling() const
Returns true if we should emit debug info for profiling.
Definition: Metadata.cpp:1577
llvm::GlobalObject::copyMetadata
void copyMetadata(const GlobalObject *Src, unsigned Offset)
Copy metadata from Src, adjusting offsets by Offset.
Definition: Metadata.cpp:1498
llvm::DIGlobalVariable
Global variables.
Definition: DebugInfoMetadata.h:2904
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
I
#define I(x, y, z)
Definition: MD5.cpp:58
llvm::MetadataAsValue::~MetadataAsValue
~MetadataAsValue()
Definition: Metadata.cpp:64
llvm::DICompileUnit
Compile unit.
Definition: DebugInfoMetadata.h:1316
llvm::MDAttachments::empty
bool empty() const
Definition: LLVMContextImpl.h:1315
llvm::is_contained
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.
Definition: STLExtras.h:1672
ArrayRef.h
llvm::MDString::get
static MDString * get(LLVMContext &Context, StringRef Str)
Definition: Metadata.cpp:499
llvm::ReplaceableMetadataImpl
Shared implementation of use-lists for replaceable metadata.
Definition: Metadata.h:281
llvm::NamedMDNode::getOperand
MDNode * getOperand(unsigned i) const
Definition: Metadata.cpp:1156
llvm::MDNode::getMostGenericAlignmentOrDereferenceable
static MDNode * getMostGenericAlignmentOrDereferenceable(MDNode *A, MDNode *B)
Definition: Metadata.cpp:1125
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::Instruction::hasMetadata
bool hasMetadata() const
Return true if this instruction has any metadata attached to it.
Definition: Instruction.h:261
llvm::MDNode::dropAllReferences
void dropAllReferences()
Definition: Metadata.cpp:739
llvm::MDTuple::get
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1256
llvm::MDNode::getNumUnresolved
unsigned getNumUnresolved() const
Definition: Metadata.h:1130
llvm::ValueAsMetadata::getValue
Value * getValue() const
Definition: Metadata.h:385
llvm::MDNode
Metadata node.
Definition: Metadata.h:926
llvm::Value::clearMetadata
void clearMetadata()
Erase all metadata attached to this Value.
Definition: Metadata.cpp:1323
llvm::SmallPtrSetImpl::count
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:383
llvm::APInt
Class for arbitrary precision integers.
Definition: APInt.h:75
llvm::SetVector< T, SmallVector< T, N >, SmallDenseSet< T, N > >::insert
bool insert(const value_type &X)
Insert a new element into the SetVector.
Definition: SetVector.h:141
addRange
static void addRange(SmallVectorImpl< ConstantInt * > &EndPoints, ConstantInt *Low, ConstantInt *High)
Definition: Metadata.cpp:1043
llvm::MDAttachments::insert
void insert(unsigned ID, MDNode &MD)
Adds an attachment to a particular node.
Definition: Metadata.cpp:1209
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
None.h
llvm::LLVMContextImpl::MDStringCache
StringMap< MDString, BumpPtrAllocator > MDStringCache
Definition: LLVMContextImpl.h:1413
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition: ErrorHandling.h:143
llvm::Value::getType
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
llvm::MDNode::concatenate
static MDNode * concatenate(MDNode *A, MDNode *B)
Methods for metadata merging.
Definition: Metadata.cpp:946
llvm::MDNode::isUniqued
bool isUniqued() const
Definition: Metadata.h:1038
llvm::Value::replaceAllUsesWith
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:529
hasSelfReference
static bool hasSelfReference(MDNode *N)
Definition: Metadata.cpp:693
getParent
static const Function * getParent(const Value *V)
Definition: BasicAliasAnalysis.cpp:868
llvm::Value::getContext
LLVMContext & getContext() const
All values hold a context through their type.
Definition: Value.cpp:991
llvm::MDNode::getMostGenericAliasScope
static MDNode * getMostGenericAliasScope(MDNode *A, MDNode *B)
Definition: Metadata.cpp:973
llvm::Value::getMetadata
MDNode * getMetadata(unsigned KindID) const
Get the current metadata attachments for the given kind, if any.
Definition: Metadata.cpp:1229
llvm::ReplaceableMetadataImpl::SalvageDebugInfo
static void SalvageDebugInfo(const Constant &C)
Replace all uses of the constant with Undef in debug info metadata.
Definition: Metadata.cpp:248
ConstantRange.h
uniquifyImpl
static T * uniquifyImpl(T *N, DenseSet< T *, InfoT > &Store)
Definition: Metadata.cpp:811
isContiguous
static bool isContiguous(const ConstantRange &A, const ConstantRange &B)
Definition: Metadata.cpp:1016
llvm::SmallSet::insert
std::pair< NoneType, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition: SmallSet.h:182
llvm::Value::setMetadata
void setMetadata(unsigned KindID, MDNode *Node)
Set a particular kind of metadata attachment.
Definition: Metadata.cpp:1266
llvm::Metadata::Storage
unsigned char Storage
Storage flag for non-uniqued, otherwise unowned, metadata.
Definition: Metadata.h:73
llvm::Type::getContext
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
Definition: Type.h:128
getOrSelfReference
static MDNode * getOrSelfReference(LLVMContext &Context, ArrayRef< Metadata * > Ops)
Get a node or a self-reference that looks like it.
Definition: Metadata.cpp:932
llvm::PointerUnion
A discriminated union of two or more pointer types, with the discriminator in the low bit of the poin...
Definition: PointerUnion.h:112
Argument.h
llvm::ConstantInt::getZExtValue
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition: Constants.h:142
llvm::MDNode::getContext
LLVMContext & getContext() const
Definition: Metadata.h:1020
llvm::Value::HasMetadata
unsigned HasMetadata
Definition: Value.h:119
llvm::MCID::Select
@ Select
Definition: MCInstrDesc.h:164
llvm::MDNode::isDistinct
bool isDistinct() const
Definition: Metadata.h:1039
llvm::Value::eraseMetadata
bool eraseMetadata(unsigned KindID)
Erase all metadata attachments with the given kind.
Definition: Metadata.cpp:1311
llvm::ConstantAsMetadata
Definition: Metadata.h:413
Constant.h
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:83
llvm::MDNode::replaceAllUsesWith
void replaceAllUsesWith(Metadata *MD)
RAUW a temporary.
Definition: Metadata.h:1045
llvm::stable_sort
void stable_sort(R &&Range)
Definition: STLExtras.h:1751
llvm::Type::getInt64Ty
static IntegerType * getInt64Ty(LLVMContext &C)
Definition: Type.cpp:240
llvm::GlobalObject::VCallVisibility
VCallVisibility
Definition: GlobalObject.h:32
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:50
H
#define H(x, y, z)
Definition: MD5.cpp:57
llvm::NamedMDNode::getParent
Module * getParent()
Get the module that holds this named metadata collection.
Definition: Metadata.h:1547
llvm::MetadataAsValue::get
static MetadataAsValue * get(LLVMContext &Context, Metadata *MD)
Definition: Metadata.cpp:102
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:341
llvm::ArrayRef::begin
iterator begin() const
Definition: ArrayRef.h:152
llvm::GlobalObject::addTypeMetadata
void addTypeMetadata(unsigned Offset, Metadata *TypeID)
Definition: Metadata.cpp:1539
llvm::MDNode::isResolved
bool isResolved() const
Check if node is fully resolved.
Definition: Metadata.h:1036
GlobalVariable.h
Casting.h
llvm::Instruction::setAAMetadata
void setAAMetadata(const AAMDNodes &N)
Sets the AA metadata on this instruction from the AAMDNodes structure.
Definition: Metadata.cpp:1411
Function.h
TrackingMDRef.h
llvm::sort
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1552
llvm::MDNode::deleteTemporary
static void deleteTemporary(MDNode *N)
Deallocate a node created by getTemporary.
Definition: Metadata.cpp:881
llvm::DIGlobalVariableExpression
A pair of DIGlobalVariable and DIExpression.
Definition: DebugInfoMetadata.h:3348
llvm::ReplaceableMetadataImpl::replaceAllUsesWith
void replaceAllUsesWith(Metadata *MD)
Replace all uses of this with MD.
Definition: Metadata.cpp:278
llvm::ConstantRange
This class represents a range of values.
Definition: ConstantRange.h:47
llvm::ValueAsMetadata::handleRAUW
static void handleRAUW(Value *From, Value *To)
Definition: Metadata.cpp:437
llvm::MDNode::replaceOperandWith
void replaceOperandWith(unsigned I, Metadata *New)
Replace a specific operand.
Definition: Metadata.cpp:909
llvm::LLVMContextImpl::MetadataAsValues
DenseMap< Metadata *, MetadataAsValue * > MetadataAsValues
Definition: LLVMContextImpl.h:1415
llvm::MDBuilder
Definition: MDBuilder.h:35
llvm::TrackingMDRef
Tracking metadata reference.
Definition: TrackingMDRef.h:25
llvm::ValueAsMetadata::getIfExists
static ValueAsMetadata * getIfExists(Value *V)
Definition: Metadata.cpp:413
llvm::MDNode::isTemporary
bool isTemporary() const
Definition: Metadata.h:1040
llvm::MetadataAsValue::getIfExists
static MetadataAsValue * getIfExists(LLVMContext &Context, Metadata *MD)
Definition: Metadata.cpp:110
llvm::getAllocSize
Optional< APInt > getAllocSize(const CallBase *CB, const TargetLibraryInfo *TLI, std::function< const Value *(const Value *)> Mapper)
Return the size of the requested allocation.
Definition: MemoryBuiltins.cpp:361
llvm::MDAttachments::getAll
void getAll(SmallVectorImpl< std::pair< unsigned, MDNode * >> &Result) const
Appends all attachments for the global to Result, sorting by attachment ID.
Definition: Metadata.cpp:1192
llvm::MDNode::getMostGenericRange
static MDNode * getMostGenericRange(MDNode *A, MDNode *B)
Definition: Metadata.cpp:1053
SmallVector.h
llvm::AliasScopeNode
This is a simple wrapper around an MDNode which provides a higher-level interface by hiding the detai...
Definition: Metadata.h:1313
llvm::MDNode::getMostGenericFPMath
static MDNode * getMostGenericFPMath(MDNode *A, MDNode *B)
Definition: Metadata.cpp:1005
N
#define N
llvm::GlobalObject::getVCallVisibility
VCallVisibility getVCallVisibility() const
Definition: Metadata.cpp:1558
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:164
llvm::DISubprogram
Subprogram description.
Definition: DebugInfoMetadata.h:1797
llvm::Value::getAllMetadata
void getAllMetadata(SmallVectorImpl< std::pair< unsigned, MDNode * >> &MDs) const
Appends all metadata attached to this value to MDs, sorting by KindID.
Definition: Metadata.cpp:1255
llvm::SmallVectorImpl
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
llvm::ValueAsMetadata
Value wrapper in the Metadata hierarchy.
Definition: Metadata.h:345
llvm::SmallSetVector
A SetVector that performs no allocations if smaller than a certain size.
Definition: SetVector.h:307
llvm::MDNode::clone
TempMDNode clone() const
Create a (temporary) clone of this.
Definition: Metadata.cpp:560
llvm::Module::eraseNamedMetadata
void eraseNamedMetadata(NamedMDNode *NMD)
Remove the given NamedMDNode from this module and delete it.
Definition: Module.cpp:272
BB
Common register allocation spilling lr str ldr sxth r3 ldr mla r4 can lr mov lr str ldr sxth r3 mla r4 and then merge mul and lr str ldr sxth r3 mla r4 It also increase the likelihood the store may become dead bb27 Successors according to LLVM BB
Definition: README.txt:39
llvm::LLVMContextImpl::DistinctMDNodes
std::vector< MDNode * > DistinctMDNodes
Definition: LLVMContextImpl.h:1428
isOperandUnresolved
static bool isOperandUnresolved(Metadata *Op)
Definition: Metadata.cpp:584
LLVMContext.h
From
BlockVerifier::State From
Definition: BlockVerifier.cpp:55
llvm::DebugLoc
A debug info location.
Definition: DebugLoc.h:33
llvm::Type::TypeID
TypeID
Definitions of all of the base types for the Type system.
Definition: Type.h:54
llvm::MDString::getString
StringRef getString() const
Definition: Metadata.cpp:509
llvm::NamedMDNode::clearOperands
void clearOperands()
Drop all references to this node's operands.
Definition: Metadata.cpp:1171
llvm::SmallPtrSetImpl::contains
bool contains(ConstPtrType Ptr) const
Definition: SmallPtrSet.h:389
llvm::SmallVectorImpl::reserve
void reserve(size_type N)
Definition: SmallVector.h:644
llvm::MDString
A single uniqued string.
Definition: Metadata.h:612
llvm::tgtok::TrueVal
@ TrueVal
Definition: TGLexer.h:61
Value.h
llvm::Value::hasMetadata
bool hasMetadata() const
Return true if this value has any metadata attached to it.
Definition: Value.h:588
CU
Definition: AArch64AsmBackend.cpp:505
llvm::MDNode::op_begin
op_iterator op_begin() const
Definition: Metadata.h:1183
llvm::MDAttachments::get
void get(unsigned ID, SmallVectorImpl< MDNode * > &Result) const
Appends all attachments with the given ID to Result in insertion order.
Definition: Metadata.cpp:1186
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::MDNode::setNumUnresolved
void setNumUnresolved(unsigned N)
Definition: Metadata.h:1132
llvm::Metadata::Uniqued
@ Uniqued
Definition: Metadata.h:70
llvm::ArrayRef::end
iterator end() const
Definition: ArrayRef.h:153
llvm::MDOperand
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:773
SetVector.h
Other
Optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1225
canBeMerged
static bool canBeMerged(const ConstantRange &A, const ConstantRange &B)
Definition: Metadata.cpp:1020
SmallSet.h
llvm::SmallPtrSetImpl::insert
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:365
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:37