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());
286  for (const auto &Pair : Uses) {
287  // Check that this Ref hasn't disappeared after RAUW (when updating a
288  // previous Ref).
289  if (!UseMap.count(Pair.first))
290  continue;
291 
292  OwnerTy Owner = Pair.second.first;
293  if (!Owner) {
294  // Update unowned tracking references directly.
295  Metadata *&Ref = *static_cast<Metadata **>(Pair.first);
296  Ref = MD;
297  if (MD)
299  UseMap.erase(Pair.first);
300  continue;
301  }
302 
303  // Check for MetadataAsValue.
304  if (Owner.is<MetadataAsValue *>()) {
305  Owner.get<MetadataAsValue *>()->handleChangedMetadata(MD);
306  continue;
307  }
308 
309  // There's a Metadata owner -- dispatch.
310  Metadata *OwnerMD = Owner.get<Metadata *>();
311  switch (OwnerMD->getMetadataID()) {
312 #define HANDLE_METADATA_LEAF(CLASS) \
313  case Metadata::CLASS##Kind: \
314  cast<CLASS>(OwnerMD)->handleChangedOperand(Pair.first, MD); \
315  continue;
316 #include "llvm/IR/Metadata.def"
317  default:
318  llvm_unreachable("Invalid metadata subclass");
319  }
320  }
321  assert(UseMap.empty() && "Expected all uses to be replaced");
322 }
323 
325  if (UseMap.empty())
326  return;
327 
328  if (!ResolveUsers) {
329  UseMap.clear();
330  return;
331  }
332 
333  // Copy out uses since UseMap could get touched below.
334  using UseTy = std::pair<void *, std::pair<OwnerTy, uint64_t>>;
335  SmallVector<UseTy, 8> Uses(UseMap.begin(), UseMap.end());
336  llvm::sort(Uses, [](const UseTy &L, const UseTy &R) {
337  return L.second.second < R.second.second;
338  });
339  UseMap.clear();
340  for (const auto &Pair : Uses) {
341  auto Owner = Pair.second.first;
342  if (!Owner)
343  continue;
344  if (Owner.is<MetadataAsValue *>())
345  continue;
346 
347  // Resolve MDNodes that point at this.
348  auto *OwnerMD = dyn_cast<MDNode>(Owner.get<Metadata *>());
349  if (!OwnerMD)
350  continue;
351  if (OwnerMD->isResolved())
352  continue;
353  OwnerMD->decrementUnresolvedOperandCount();
354  }
355 }
356 
357 ReplaceableMetadataImpl *ReplaceableMetadataImpl::getOrCreate(Metadata &MD) {
358  if (auto *N = dyn_cast<MDNode>(&MD))
359  return N->isResolved() ? nullptr : N->Context.getOrCreateReplaceableUses();
360  return dyn_cast<ValueAsMetadata>(&MD);
361 }
362 
363 ReplaceableMetadataImpl *ReplaceableMetadataImpl::getIfExists(Metadata &MD) {
364  if (auto *N = dyn_cast<MDNode>(&MD))
365  return N->isResolved() ? nullptr : N->Context.getReplaceableUses();
366  return dyn_cast<ValueAsMetadata>(&MD);
367 }
368 
369 bool ReplaceableMetadataImpl::isReplaceable(const Metadata &MD) {
370  if (auto *N = dyn_cast<MDNode>(&MD))
371  return !N->isResolved();
372  return isa<ValueAsMetadata>(&MD);
373 }
374 
376  assert(V && "Expected value");
377  if (auto *A = dyn_cast<Argument>(V)) {
378  if (auto *Fn = A->getParent())
379  return Fn->getSubprogram();
380  return nullptr;
381  }
382 
383  if (BasicBlock *BB = cast<Instruction>(V)->getParent()) {
384  if (auto *Fn = BB->getParent())
385  return Fn->getSubprogram();
386  return nullptr;
387  }
388 
389  return nullptr;
390 }
391 
393  assert(V && "Unexpected null Value");
394 
395  auto &Context = V->getContext();
396  auto *&Entry = Context.pImpl->ValuesAsMetadata[V];
397  if (!Entry) {
398  assert((isa<Constant>(V) || isa<Argument>(V) || isa<Instruction>(V)) &&
399  "Expected constant or function-local value");
400  assert(!V->IsUsedByMD && "Expected this to be the only metadata use");
401  V->IsUsedByMD = true;
402  if (auto *C = dyn_cast<Constant>(V))
403  Entry = new ConstantAsMetadata(C);
404  else
405  Entry = new LocalAsMetadata(V);
406  }
407 
408  return Entry;
409 }
410 
412  assert(V && "Unexpected null Value");
413  return V->getContext().pImpl->ValuesAsMetadata.lookup(V);
414 }
415 
417  assert(V && "Expected valid value");
418 
419  auto &Store = V->getType()->getContext().pImpl->ValuesAsMetadata;
420  auto I = Store.find(V);
421  if (I == Store.end())
422  return;
423 
424  // Remove old entry from the map.
425  ValueAsMetadata *MD = I->second;
426  assert(MD && "Expected valid metadata");
427  assert(MD->getValue() == V && "Expected valid mapping");
428  Store.erase(I);
429 
430  // Delete the metadata.
431  MD->replaceAllUsesWith(nullptr);
432  delete MD;
433 }
434 
436  assert(From && "Expected valid value");
437  assert(To && "Expected valid value");
438  assert(From != To && "Expected changed value");
439  assert(From->getType() == To->getType() && "Unexpected type change");
440 
441  LLVMContext &Context = From->getType()->getContext();
443  auto I = Store.find(From);
444  if (I == Store.end()) {
445  assert(!From->IsUsedByMD && "Expected From not to be used by metadata");
446  return;
447  }
448 
449  // Remove old entry from the map.
450  assert(From->IsUsedByMD && "Expected From to be used by metadata");
451  From->IsUsedByMD = false;
452  ValueAsMetadata *MD = I->second;
453  assert(MD && "Expected valid metadata");
454  assert(MD->getValue() == From && "Expected valid mapping");
455  Store.erase(I);
456 
457  if (isa<LocalAsMetadata>(MD)) {
458  if (auto *C = dyn_cast<Constant>(To)) {
459  // Local became a constant.
461  delete MD;
462  return;
463  }
466  // DISubprogram changed.
467  MD->replaceAllUsesWith(nullptr);
468  delete MD;
469  return;
470  }
471  } else if (!isa<Constant>(To)) {
472  // Changed to function-local value.
473  MD->replaceAllUsesWith(nullptr);
474  delete MD;
475  return;
476  }
477 
478  auto *&Entry = Store[To];
479  if (Entry) {
480  // The target already exists.
481  MD->replaceAllUsesWith(Entry);
482  delete MD;
483  return;
484  }
485 
486  // Update MD in place (and update the map entry).
487  assert(!To->IsUsedByMD && "Expected this to be the only metadata use");
488  To->IsUsedByMD = true;
489  MD->V = To;
490  Entry = MD;
491 }
492 
493 //===----------------------------------------------------------------------===//
494 // MDString implementation.
495 //
496 
498  auto &Store = Context.pImpl->MDStringCache;
499  auto I = Store.try_emplace(Str);
500  auto &MapEntry = I.first->getValue();
501  if (!I.second)
502  return &MapEntry;
503  MapEntry.Entry = &*I.first;
504  return &MapEntry;
505 }
506 
508  assert(Entry && "Expected to find string map entry");
509  return Entry->first();
510 }
511 
512 //===----------------------------------------------------------------------===//
513 // MDNode implementation.
514 //
515 
516 // Assert that the MDNode types will not be unaligned by the objects
517 // prepended to them.
518 #define HANDLE_MDNODE_LEAF(CLASS) \
519  static_assert( \
520  alignof(uint64_t) >= alignof(CLASS), \
521  "Alignment is insufficient after objects prepended to " #CLASS);
522 #include "llvm/IR/Metadata.def"
523 
524 void *MDNode::operator new(size_t Size, unsigned NumOps,
525  StorageType /* Storage */) {
526  // uint64_t is the most aligned type we need support (ensured by static_assert
527  // above)
528  size_t AllocSize = alignTo(Header::getAllocSize(NumOps), alignof(uint64_t));
529  char *Mem = reinterpret_cast<char *>(::operator new(AllocSize + Size));
530  Header *H = new (Mem + AllocSize - sizeof(Header)) Header(NumOps);
531  return reinterpret_cast<void *>(H + 1);
532 }
533 
534 void MDNode::operator delete(void *N) {
535  Header *H = reinterpret_cast<Header *>(N) - 1;
536  void *Mem = H->getAllocation();
537  H->~Header();
538  ::operator delete(Mem);
539 }
540 
543  : Metadata(ID, Storage), Context(Context) {
544  unsigned Op = 0;
545  for (Metadata *MD : Ops1)
546  setOperand(Op++, MD);
547  for (Metadata *MD : Ops2)
548  setOperand(Op++, MD);
549 
550  if (!isUniqued())
551  return;
552 
553  // Count the unresolved operands. If there are any, RAUW support will be
554  // added lazily on first reference.
555  countUnresolvedOperands();
556 }
557 
558 TempMDNode MDNode::clone() const {
559  switch (getMetadataID()) {
560  default:
561  llvm_unreachable("Invalid MDNode subclass");
562 #define HANDLE_MDNODE_LEAF(CLASS) \
563  case CLASS##Kind: \
564  return cast<CLASS>(this)->cloneImpl();
565 #include "llvm/IR/Metadata.def"
566  }
567 }
568 
569 MDNode::Header::Header(unsigned NumOps) {
570  NumOperands = NumOps;
571  MDOperand *O = reinterpret_cast<MDOperand *>(this);
572  for (MDOperand *E = O - NumOps; O != E; --O)
573  (void)new (O - 1) MDOperand();
574 }
575 
576 MDNode::Header::~Header() {
577  MDOperand *O = reinterpret_cast<MDOperand *>(this) - NumOperands;
578  for (MDOperand *E = O + NumOperands; O != E; ++O)
579  (void)O->~MDOperand();
580 }
581 
583  if (auto *N = dyn_cast_or_null<MDNode>(Op))
584  return !N->isResolved();
585  return false;
586 }
587 
588 void MDNode::countUnresolvedOperands() {
589  assert(getNumUnresolved() == 0 && "Expected unresolved ops to be uncounted");
590  assert(isUniqued() && "Expected this to be uniqued");
592 }
593 
594 void MDNode::makeUniqued() {
595  assert(isTemporary() && "Expected this to be temporary");
596  assert(!isResolved() && "Expected this to be unresolved");
597 
598  // Enable uniquing callbacks.
599  for (auto &Op : mutable_operands())
600  Op.reset(Op.get(), this);
601 
602  // Make this 'uniqued'.
603  Storage = Uniqued;
604  countUnresolvedOperands();
605  if (!getNumUnresolved()) {
606  dropReplaceableUses();
607  assert(isResolved() && "Expected this to be resolved");
608  }
609 
610  assert(isUniqued() && "Expected this to be uniqued");
611 }
612 
613 void MDNode::makeDistinct() {
614  assert(isTemporary() && "Expected this to be temporary");
615  assert(!isResolved() && "Expected this to be unresolved");
616 
617  // Drop RAUW support and store as a distinct node.
618  dropReplaceableUses();
620 
621  assert(isDistinct() && "Expected this to be distinct");
622  assert(isResolved() && "Expected this to be resolved");
623 }
624 
626  assert(isUniqued() && "Expected this to be uniqued");
627  assert(!isResolved() && "Expected this to be unresolved");
628 
629  setNumUnresolved(0);
630  dropReplaceableUses();
631 
632  assert(isResolved() && "Expected this to be resolved");
633 }
634 
635 void MDNode::dropReplaceableUses() {
636  assert(!getNumUnresolved() && "Unexpected unresolved operand");
637 
638  // Drop any RAUW support.
639  if (Context.hasReplaceableUses())
640  Context.takeReplaceableUses()->resolveAllUses();
641 }
642 
643 void MDNode::resolveAfterOperandChange(Metadata *Old, Metadata *New) {
644  assert(isUniqued() && "Expected this to be uniqued");
645  assert(getNumUnresolved() != 0 && "Expected unresolved operands");
646 
647  // Check if an operand was resolved.
648  if (!isOperandUnresolved(Old)) {
649  if (isOperandUnresolved(New))
650  // An operand was un-resolved!
652  } else if (!isOperandUnresolved(New))
653  decrementUnresolvedOperandCount();
654 }
655 
656 void MDNode::decrementUnresolvedOperandCount() {
657  assert(!isResolved() && "Expected this to be unresolved");
658  if (isTemporary())
659  return;
660 
661  assert(isUniqued() && "Expected this to be uniqued");
663  if (getNumUnresolved())
664  return;
665 
666  // Last unresolved operand has just been resolved.
667  dropReplaceableUses();
668  assert(isResolved() && "Expected this to become resolved");
669 }
670 
672  if (isResolved())
673  return;
674 
675  // Resolve this node immediately.
676  resolve();
677 
678  // Resolve all operands.
679  for (const auto &Op : operands()) {
680  auto *N = dyn_cast_or_null<MDNode>(Op);
681  if (!N)
682  continue;
683 
684  assert(!N->isTemporary() &&
685  "Expected all forward declarations to be resolved");
686  if (!N->isResolved())
687  N->resolveCycles();
688  }
689 }
690 
691 static bool hasSelfReference(MDNode *N) {
692  return llvm::is_contained(N->operands(), N);
693 }
694 
695 MDNode *MDNode::replaceWithPermanentImpl() {
696  switch (getMetadataID()) {
697  default:
698  // If this type isn't uniquable, replace with a distinct node.
699  return replaceWithDistinctImpl();
700 
701 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
702  case CLASS##Kind: \
703  break;
704 #include "llvm/IR/Metadata.def"
705  }
706 
707  // Even if this type is uniquable, self-references have to be distinct.
708  if (hasSelfReference(this))
709  return replaceWithDistinctImpl();
710  return replaceWithUniquedImpl();
711 }
712 
713 MDNode *MDNode::replaceWithUniquedImpl() {
714  // Try to uniquify in place.
715  MDNode *UniquedNode = uniquify();
716 
717  if (UniquedNode == this) {
718  makeUniqued();
719  return this;
720  }
721 
722  // Collision, so RAUW instead.
723  replaceAllUsesWith(UniquedNode);
724  deleteAsSubclass();
725  return UniquedNode;
726 }
727 
728 MDNode *MDNode::replaceWithDistinctImpl() {
729  makeDistinct();
730  return this;
731 }
732 
733 void MDTuple::recalculateHash() {
734  setHash(MDTupleInfo::KeyTy::calculateHash(this));
735 }
736 
738  for (unsigned I = 0, E = getNumOperands(); I != E; ++I)
739  setOperand(I, nullptr);
740  if (Context.hasReplaceableUses()) {
741  Context.getReplaceableUses()->resolveAllUses(/* ResolveUsers */ false);
742  (void)Context.takeReplaceableUses();
743  }
744 }
745 
746 void MDNode::handleChangedOperand(void *Ref, Metadata *New) {
747  unsigned Op = static_cast<MDOperand *>(Ref) - op_begin();
748  assert(Op < getNumOperands() && "Expected valid operand");
749 
750  if (!isUniqued()) {
751  // This node is not uniqued. Just set the operand and be done with it.
752  setOperand(Op, New);
753  return;
754  }
755 
756  // This node is uniqued.
757  eraseFromStore();
758 
759  Metadata *Old = getOperand(Op);
760  setOperand(Op, New);
761 
762  // Drop uniquing for self-reference cycles and deleted constants.
763  if (New == this || (!New && Old && isa<ConstantAsMetadata>(Old))) {
764  if (!isResolved())
765  resolve();
767  return;
768  }
769 
770  // Re-unique the node.
771  auto *Uniqued = uniquify();
772  if (Uniqued == this) {
773  if (!isResolved())
774  resolveAfterOperandChange(Old, New);
775  return;
776  }
777 
778  // Collision.
779  if (!isResolved()) {
780  // Still unresolved, so RAUW.
781  //
782  // First, clear out all operands to prevent any recursion (similar to
783  // dropAllReferences(), but we still need the use-list).
784  for (unsigned O = 0, E = getNumOperands(); O != E; ++O)
785  setOperand(O, nullptr);
786  if (Context.hasReplaceableUses())
787  Context.getReplaceableUses()->replaceAllUsesWith(Uniqued);
788  deleteAsSubclass();
789  return;
790  }
791 
792  // Store in non-uniqued form if RAUW isn't possible.
794 }
795 
796 void MDNode::deleteAsSubclass() {
797  switch (getMetadataID()) {
798  default:
799  llvm_unreachable("Invalid subclass of MDNode");
800 #define HANDLE_MDNODE_LEAF(CLASS) \
801  case CLASS##Kind: \
802  delete cast<CLASS>(this); \
803  break;
804 #include "llvm/IR/Metadata.def"
805  }
806 }
807 
808 template <class T, class InfoT>
810  if (T *U = getUniqued(Store, N))
811  return U;
812 
813  Store.insert(N);
814  return N;
815 }
816 
817 template <class NodeTy> struct MDNode::HasCachedHash {
818  using Yes = char[1];
819  using No = char[2];
820  template <class U, U Val> struct SFINAE {};
821 
822  template <class U>
823  static Yes &check(SFINAE<void (U::*)(unsigned), &U::setHash> *);
824  template <class U> static No &check(...);
825 
826  static const bool value = sizeof(check<NodeTy>(nullptr)) == sizeof(Yes);
827 };
828 
829 MDNode *MDNode::uniquify() {
830  assert(!hasSelfReference(this) && "Cannot uniquify a self-referencing node");
831 
832  // Try to insert into uniquing store.
833  switch (getMetadataID()) {
834  default:
835  llvm_unreachable("Invalid or non-uniquable subclass of MDNode");
836 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
837  case CLASS##Kind: { \
838  CLASS *SubclassThis = cast<CLASS>(this); \
839  std::integral_constant<bool, HasCachedHash<CLASS>::value> \
840  ShouldRecalculateHash; \
841  dispatchRecalculateHash(SubclassThis, ShouldRecalculateHash); \
842  return uniquifyImpl(SubclassThis, getContext().pImpl->CLASS##s); \
843  }
844 #include "llvm/IR/Metadata.def"
845  }
846 }
847 
848 void MDNode::eraseFromStore() {
849  switch (getMetadataID()) {
850  default:
851  llvm_unreachable("Invalid or non-uniquable subclass of MDNode");
852 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
853  case CLASS##Kind: \
854  getContext().pImpl->CLASS##s.erase(cast<CLASS>(this)); \
855  break;
856 #include "llvm/IR/Metadata.def"
857  }
858 }
859 
860 MDTuple *MDTuple::getImpl(LLVMContext &Context, ArrayRef<Metadata *> MDs,
861  StorageType Storage, bool ShouldCreate) {
862  unsigned Hash = 0;
863  if (Storage == Uniqued) {
864  MDTupleInfo::KeyTy Key(MDs);
865  if (auto *N = getUniqued(Context.pImpl->MDTuples, Key))
866  return N;
867  if (!ShouldCreate)
868  return nullptr;
869  Hash = Key.getHash();
870  } else {
871  assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
872  }
873 
874  return storeImpl(new (MDs.size(), Storage)
875  MDTuple(Context, Storage, Hash, MDs),
876  Storage, Context.pImpl->MDTuples);
877 }
878 
880  assert(N->isTemporary() && "Expected temporary node");
881  N->replaceAllUsesWith(nullptr);
882  N->deleteAsSubclass();
883 }
884 
886  assert(!Context.hasReplaceableUses() && "Unexpected replaceable uses");
887  assert(!getNumUnresolved() && "Unexpected unresolved nodes");
888  Storage = Distinct;
889  assert(isResolved() && "Expected this to be resolved");
890 
891  // Reset the hash.
892  switch (getMetadataID()) {
893  default:
894  llvm_unreachable("Invalid subclass of MDNode");
895 #define HANDLE_MDNODE_LEAF(CLASS) \
896  case CLASS##Kind: { \
897  std::integral_constant<bool, HasCachedHash<CLASS>::value> ShouldResetHash; \
898  dispatchResetHash(cast<CLASS>(this), ShouldResetHash); \
899  break; \
900  }
901 #include "llvm/IR/Metadata.def"
902  }
903 
904  getContext().pImpl->DistinctMDNodes.push_back(this);
905 }
906 
907 void MDNode::replaceOperandWith(unsigned I, Metadata *New) {
908  if (getOperand(I) == New)
909  return;
910 
911  if (!isUniqued()) {
912  setOperand(I, New);
913  return;
914  }
915 
916  handleChangedOperand(mutable_begin() + I, New);
917 }
918 
919 void MDNode::setOperand(unsigned I, Metadata *New) {
920  assert(I < getNumOperands());
921  mutable_begin()[I].reset(New, isUniqued() ? this : nullptr);
922 }
923 
924 /// Get a node or a self-reference that looks like it.
925 ///
926 /// Special handling for finding self-references, for use by \a
927 /// MDNode::concatenate() and \a MDNode::intersect() to maintain behaviour from
928 /// when self-referencing nodes were still uniqued. If the first operand has
929 /// the same operands as \c Ops, return the first operand instead.
931  ArrayRef<Metadata *> Ops) {
932  if (!Ops.empty())
933  if (MDNode *N = dyn_cast_or_null<MDNode>(Ops[0]))
934  if (N->getNumOperands() == Ops.size() && N == N->getOperand(0)) {
935  for (unsigned I = 1, E = Ops.size(); I != E; ++I)
936  if (Ops[I] != N->getOperand(I))
937  return MDNode::get(Context, Ops);
938  return N;
939  }
940 
941  return MDNode::get(Context, Ops);
942 }
943 
945  if (!A)
946  return B;
947  if (!B)
948  return A;
949 
950  SmallSetVector<Metadata *, 4> MDs(A->op_begin(), A->op_end());
951  MDs.insert(B->op_begin(), B->op_end());
952 
953  // FIXME: This preserves long-standing behaviour, but is it really the right
954  // behaviour? Or was that an unintended side-effect of node uniquing?
955  return getOrSelfReference(A->getContext(), MDs.getArrayRef());
956 }
957 
959  if (!A || !B)
960  return nullptr;
961 
962  SmallSetVector<Metadata *, 4> MDs(A->op_begin(), A->op_end());
963  SmallPtrSet<Metadata *, 4> BSet(B->op_begin(), B->op_end());
964  MDs.remove_if([&](Metadata *MD) { return !BSet.count(MD); });
965 
966  // FIXME: This preserves long-standing behaviour, but is it really the right
967  // behaviour? Or was that an unintended side-effect of node uniquing?
968  return getOrSelfReference(A->getContext(), MDs.getArrayRef());
969 }
970 
972  if (!A || !B)
973  return nullptr;
974 
975  // Take the intersection of domains then union the scopes
976  // within those domains
978  SmallPtrSet<const MDNode *, 16> IntersectDomains;
980  for (const MDOperand &MDOp : A->operands())
981  if (const MDNode *NAMD = dyn_cast<MDNode>(MDOp))
982  if (const MDNode *Domain = AliasScopeNode(NAMD).getDomain())
983  ADomains.insert(Domain);
984 
985  for (const MDOperand &MDOp : B->operands())
986  if (const MDNode *NAMD = dyn_cast<MDNode>(MDOp))
987  if (const MDNode *Domain = AliasScopeNode(NAMD).getDomain())
988  if (ADomains.contains(Domain)) {
989  IntersectDomains.insert(Domain);
990  MDs.insert(MDOp);
991  }
992 
993  for (const MDOperand &MDOp : A->operands())
994  if (const MDNode *NAMD = dyn_cast<MDNode>(MDOp))
995  if (const MDNode *Domain = AliasScopeNode(NAMD).getDomain())
996  if (IntersectDomains.contains(Domain))
997  MDs.insert(MDOp);
998 
999  return MDs.empty() ? nullptr
1000  : getOrSelfReference(A->getContext(), MDs.getArrayRef());
1001 }
1002 
1004  if (!A || !B)
1005  return nullptr;
1006 
1007  APFloat AVal = mdconst::extract<ConstantFP>(A->getOperand(0))->getValueAPF();
1008  APFloat BVal = mdconst::extract<ConstantFP>(B->getOperand(0))->getValueAPF();
1009  if (AVal < BVal)
1010  return A;
1011  return B;
1012 }
1013 
1014 static bool isContiguous(const ConstantRange &A, const ConstantRange &B) {
1015  return A.getUpper() == B.getLower() || A.getLower() == B.getUpper();
1016 }
1017 
1018 static bool canBeMerged(const ConstantRange &A, const ConstantRange &B) {
1019  return !A.intersectWith(B).isEmptySet() || isContiguous(A, B);
1020 }
1021 
1023  ConstantInt *Low, ConstantInt *High) {
1024  ConstantRange NewRange(Low->getValue(), High->getValue());
1025  unsigned Size = EndPoints.size();
1026  APInt LB = EndPoints[Size - 2]->getValue();
1027  APInt LE = EndPoints[Size - 1]->getValue();
1028  ConstantRange LastRange(LB, LE);
1029  if (canBeMerged(NewRange, LastRange)) {
1030  ConstantRange Union = LastRange.unionWith(NewRange);
1031  Type *Ty = High->getType();
1032  EndPoints[Size - 2] =
1033  cast<ConstantInt>(ConstantInt::get(Ty, Union.getLower()));
1034  EndPoints[Size - 1] =
1035  cast<ConstantInt>(ConstantInt::get(Ty, Union.getUpper()));
1036  return true;
1037  }
1038  return false;
1039 }
1040 
1042  ConstantInt *Low, ConstantInt *High) {
1043  if (!EndPoints.empty())
1044  if (tryMergeRange(EndPoints, Low, High))
1045  return;
1046 
1047  EndPoints.push_back(Low);
1048  EndPoints.push_back(High);
1049 }
1050 
1052  // Given two ranges, we want to compute the union of the ranges. This
1053  // is slightly complicated by having to combine the intervals and merge
1054  // the ones that overlap.
1055 
1056  if (!A || !B)
1057  return nullptr;
1058 
1059  if (A == B)
1060  return A;
1061 
1062  // First, walk both lists in order of the lower boundary of each interval.
1063  // At each step, try to merge the new interval to the last one we adedd.
1065  int AI = 0;
1066  int BI = 0;
1067  int AN = A->getNumOperands() / 2;
1068  int BN = B->getNumOperands() / 2;
1069  while (AI < AN && BI < BN) {
1070  ConstantInt *ALow = mdconst::extract<ConstantInt>(A->getOperand(2 * AI));
1071  ConstantInt *BLow = mdconst::extract<ConstantInt>(B->getOperand(2 * BI));
1072 
1073  if (ALow->getValue().slt(BLow->getValue())) {
1074  addRange(EndPoints, ALow,
1075  mdconst::extract<ConstantInt>(A->getOperand(2 * AI + 1)));
1076  ++AI;
1077  } else {
1078  addRange(EndPoints, BLow,
1079  mdconst::extract<ConstantInt>(B->getOperand(2 * BI + 1)));
1080  ++BI;
1081  }
1082  }
1083  while (AI < AN) {
1084  addRange(EndPoints, mdconst::extract<ConstantInt>(A->getOperand(2 * AI)),
1085  mdconst::extract<ConstantInt>(A->getOperand(2 * AI + 1)));
1086  ++AI;
1087  }
1088  while (BI < BN) {
1089  addRange(EndPoints, mdconst::extract<ConstantInt>(B->getOperand(2 * BI)),
1090  mdconst::extract<ConstantInt>(B->getOperand(2 * BI + 1)));
1091  ++BI;
1092  }
1093 
1094  // If we have more than 2 ranges (4 endpoints) we have to try to merge
1095  // the last and first ones.
1096  unsigned Size = EndPoints.size();
1097  if (Size > 4) {
1098  ConstantInt *FB = EndPoints[0];
1099  ConstantInt *FE = EndPoints[1];
1100  if (tryMergeRange(EndPoints, FB, FE)) {
1101  for (unsigned i = 0; i < Size - 2; ++i) {
1102  EndPoints[i] = EndPoints[i + 2];
1103  }
1104  EndPoints.resize(Size - 2);
1105  }
1106  }
1107 
1108  // If in the end we have a single range, it is possible that it is now the
1109  // full range. Just drop the metadata in that case.
1110  if (EndPoints.size() == 2) {
1111  ConstantRange Range(EndPoints[0]->getValue(), EndPoints[1]->getValue());
1112  if (Range.isFullSet())
1113  return nullptr;
1114  }
1115 
1117  MDs.reserve(EndPoints.size());
1118  for (auto *I : EndPoints)
1119  MDs.push_back(ConstantAsMetadata::get(I));
1120  return MDNode::get(A->getContext(), MDs);
1121 }
1122 
1124  if (!A || !B)
1125  return nullptr;
1126 
1127  ConstantInt *AVal = mdconst::extract<ConstantInt>(A->getOperand(0));
1128  ConstantInt *BVal = mdconst::extract<ConstantInt>(B->getOperand(0));
1129  if (AVal->getZExtValue() < BVal->getZExtValue())
1130  return A;
1131  return B;
1132 }
1133 
1134 //===----------------------------------------------------------------------===//
1135 // NamedMDNode implementation.
1136 //
1137 
1140 }
1141 
1142 NamedMDNode::NamedMDNode(const Twine &N)
1143  : Name(N.str()), Operands(new SmallVector<TrackingMDRef, 4>()) {}
1144 
1145 NamedMDNode::~NamedMDNode() {
1147  delete &getNMDOps(Operands);
1148 }
1149 
1151  return (unsigned)getNMDOps(Operands).size();
1152 }
1153 
1155  assert(i < getNumOperands() && "Invalid Operand number!");
1156  auto *N = getNMDOps(Operands)[i].get();
1157  return cast_or_null<MDNode>(N);
1158 }
1159 
1160 void NamedMDNode::addOperand(MDNode *M) { getNMDOps(Operands).emplace_back(M); }
1161 
1162 void NamedMDNode::setOperand(unsigned I, MDNode *New) {
1163  assert(I < getNumOperands() && "Invalid operand number");
1164  getNMDOps(Operands)[I].reset(New);
1165 }
1166 
1168 
1169 void NamedMDNode::clearOperands() { getNMDOps(Operands).clear(); }
1170 
1171 StringRef NamedMDNode::getName() const { return StringRef(Name); }
1172 
1173 //===----------------------------------------------------------------------===//
1174 // Instruction Metadata method implementations.
1175 //
1176 
1177 MDNode *MDAttachments::lookup(unsigned ID) const {
1178  for (const auto &A : Attachments)
1179  if (A.MDKind == ID)
1180  return A.Node;
1181  return nullptr;
1182 }
1183 
1184 void MDAttachments::get(unsigned ID, SmallVectorImpl<MDNode *> &Result) const {
1185  for (const auto &A : Attachments)
1186  if (A.MDKind == ID)
1187  Result.push_back(A.Node);
1188 }
1189 
1191  SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
1192  for (const auto &A : Attachments)
1193  Result.emplace_back(A.MDKind, A.Node);
1194 
1195  // Sort the resulting array so it is stable with respect to metadata IDs. We
1196  // need to preserve the original insertion order though.
1197  if (Result.size() > 1)
1198  llvm::stable_sort(Result, less_first());
1199 }
1200 
1201 void MDAttachments::set(unsigned ID, MDNode *MD) {
1202  erase(ID);
1203  if (MD)
1204  insert(ID, *MD);
1205 }
1206 
1207 void MDAttachments::insert(unsigned ID, MDNode &MD) {
1208  Attachments.push_back({ID, TrackingMDNodeRef(&MD)});
1209 }
1210 
1211 bool MDAttachments::erase(unsigned ID) {
1212  if (empty())
1213  return false;
1214 
1215  // Common case is one value.
1216  if (Attachments.size() == 1 && Attachments.back().MDKind == ID) {
1217  Attachments.pop_back();
1218  return true;
1219  }
1220 
1221  auto OldSize = Attachments.size();
1222  llvm::erase_if(Attachments,
1223  [ID](const Attachment &A) { return A.MDKind == ID; });
1224  return OldSize != Attachments.size();
1225 }
1226 
1227 MDNode *Value::getMetadata(unsigned KindID) const {
1228  if (!hasMetadata())
1229  return nullptr;
1230  const auto &Info = getContext().pImpl->ValueMetadata[this];
1231  assert(!Info.empty() && "bit out of sync with hash table");
1232  return Info.lookup(KindID);
1233 }
1234 
1236  if (!hasMetadata())
1237  return nullptr;
1238  const auto &Info = getContext().pImpl->ValueMetadata[this];
1239  assert(!Info.empty() && "bit out of sync with hash table");
1240  return Info.lookup(getContext().getMDKindID(Kind));
1241 }
1242 
1243 void Value::getMetadata(unsigned KindID, SmallVectorImpl<MDNode *> &MDs) const {
1244  if (hasMetadata())
1245  getContext().pImpl->ValueMetadata[this].get(KindID, MDs);
1246 }
1247 
1249  if (hasMetadata())
1250  getMetadata(getContext().getMDKindID(Kind), MDs);
1251 }
1252 
1254  SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs) const {
1255  if (hasMetadata()) {
1256  assert(getContext().pImpl->ValueMetadata.count(this) &&
1257  "bit out of sync with hash table");
1258  const auto &Info = getContext().pImpl->ValueMetadata.find(this)->second;
1259  assert(!Info.empty() && "Shouldn't have called this");
1260  Info.getAll(MDs);
1261  }
1262 }
1263 
1264 void Value::setMetadata(unsigned KindID, MDNode *Node) {
1265  assert(isa<Instruction>(this) || isa<GlobalObject>(this));
1266 
1267  // Handle the case when we're adding/updating metadata on a value.
1268  if (Node) {
1269  auto &Info = getContext().pImpl->ValueMetadata[this];
1270  assert(!Info.empty() == HasMetadata && "bit out of sync with hash table");
1271  if (Info.empty())
1272  HasMetadata = true;
1273  Info.set(KindID, Node);
1274  return;
1275  }
1276 
1277  // Otherwise, we're removing metadata from an instruction.
1278  assert((HasMetadata == (getContext().pImpl->ValueMetadata.count(this) > 0)) &&
1279  "bit out of sync with hash table");
1280  if (!HasMetadata)
1281  return; // Nothing to remove!
1282  auto &Info = getContext().pImpl->ValueMetadata[this];
1283 
1284  // Handle removal of an existing value.
1285  Info.erase(KindID);
1286  if (!Info.empty())
1287  return;
1288  getContext().pImpl->ValueMetadata.erase(this);
1289  HasMetadata = false;
1290 }
1291 
1293  if (!Node && !HasMetadata)
1294  return;
1295  setMetadata(getContext().getMDKindID(Kind), Node);
1296 }
1297 
1298 void Value::addMetadata(unsigned KindID, MDNode &MD) {
1299  assert(isa<Instruction>(this) || isa<GlobalObject>(this));
1300  if (!HasMetadata)
1301  HasMetadata = true;
1302  getContext().pImpl->ValueMetadata[this].insert(KindID, MD);
1303 }
1304 
1306  addMetadata(getContext().getMDKindID(Kind), MD);
1307 }
1308 
1309 bool Value::eraseMetadata(unsigned KindID) {
1310  // Nothing to unset.
1311  if (!HasMetadata)
1312  return false;
1313 
1314  auto &Store = getContext().pImpl->ValueMetadata[this];
1315  bool Changed = Store.erase(KindID);
1316  if (Store.empty())
1317  clearMetadata();
1318  return Changed;
1319 }
1320 
1322  if (!HasMetadata)
1323  return;
1324  assert(getContext().pImpl->ValueMetadata.count(this) &&
1325  "bit out of sync with hash table");
1326  getContext().pImpl->ValueMetadata.erase(this);
1327  HasMetadata = false;
1328 }
1329 
1331  if (!Node && !hasMetadata())
1332  return;
1333  setMetadata(getContext().getMDKindID(Kind), Node);
1334 }
1335 
1336 MDNode *Instruction::getMetadataImpl(StringRef Kind) const {
1337  return getMetadataImpl(getContext().getMDKindID(Kind));
1338 }
1339 
1341  if (!Value::hasMetadata())
1342  return; // Nothing to remove!
1343 
1344  if (KnownIDs.empty()) {
1345  // Just drop our entry at the store.
1346  clearMetadata();
1347  return;
1348  }
1349 
1350  SmallSet<unsigned, 4> KnownSet;
1351  KnownSet.insert(KnownIDs.begin(), KnownIDs.end());
1352 
1353  auto &MetadataStore = getContext().pImpl->ValueMetadata;
1354  auto &Info = MetadataStore[this];
1355  assert(!Info.empty() && "bit out of sync with hash table");
1356  Info.remove_if([&KnownSet](const MDAttachments::Attachment &I) {
1357  return !KnownSet.count(I.MDKind);
1358  });
1359 
1360  if (Info.empty()) {
1361  // Drop our entry at the store.
1362  clearMetadata();
1363  }
1364 }
1365 
1366 void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
1367  if (!Node && !hasMetadata())
1368  return;
1369 
1370  // Handle 'dbg' as a special case since it is not stored in the hash table.
1371  if (KindID == LLVMContext::MD_dbg) {
1372  DbgLoc = DebugLoc(Node);
1373  return;
1374  }
1375 
1376  Value::setMetadata(KindID, Node);
1377 }
1378 
1380  MDBuilder MDB(getContext());
1381 
1382  auto *Existing = getMetadata(LLVMContext::MD_annotation);
1384  bool AppendName = true;
1385  if (Existing) {
1386  auto *Tuple = cast<MDTuple>(Existing);
1387  for (auto &N : Tuple->operands()) {
1388  if (cast<MDString>(N.get())->getString() == Name)
1389  AppendName = false;
1390  Names.push_back(N.get());
1391  }
1392  }
1393  if (AppendName)
1394  Names.push_back(MDB.createString(Name));
1395 
1396  MDNode *MD = MDTuple::get(getContext(), Names);
1397  setMetadata(LLVMContext::MD_annotation, MD);
1398 }
1399 
1401  AAMDNodes Result;
1402  Result.TBAA = getMetadata(LLVMContext::MD_tbaa);
1403  Result.TBAAStruct = getMetadata(LLVMContext::MD_tbaa_struct);
1404  Result.Scope = getMetadata(LLVMContext::MD_alias_scope);
1405  Result.NoAlias = getMetadata(LLVMContext::MD_noalias);
1406  return Result;
1407 }
1408 
1410  setMetadata(LLVMContext::MD_tbaa, N.TBAA);
1411  setMetadata(LLVMContext::MD_tbaa_struct, N.TBAAStruct);
1412  setMetadata(LLVMContext::MD_alias_scope, N.Scope);
1413  setMetadata(LLVMContext::MD_noalias, N.NoAlias);
1414 }
1415 
1416 MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
1417  // Handle 'dbg' as a special case since it is not stored in the hash table.
1418  if (KindID == LLVMContext::MD_dbg)
1419  return DbgLoc.getAsMDNode();
1420  return Value::getMetadata(KindID);
1421 }
1422 
1423 void Instruction::getAllMetadataImpl(
1424  SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
1425  Result.clear();
1426 
1427  // Handle 'dbg' as a special case since it is not stored in the hash table.
1428  if (DbgLoc) {
1429  Result.push_back(
1430  std::make_pair((unsigned)LLVMContext::MD_dbg, DbgLoc.getAsMDNode()));
1431  }
1432  Value::getAllMetadata(Result);
1433 }
1434 
1436  uint64_t &FalseVal) const {
1437  assert(
1438  (getOpcode() == Instruction::Br || getOpcode() == Instruction::Select) &&
1439  "Looking for branch weights on something besides branch or select");
1440 
1441  auto *ProfileData = getMetadata(LLVMContext::MD_prof);
1442  if (!ProfileData || ProfileData->getNumOperands() != 3)
1443  return false;
1444 
1445  auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand(0));
1446  if (!ProfDataName || !ProfDataName->getString().equals("branch_weights"))
1447  return false;
1448 
1449  auto *CITrue = mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(1));
1450  auto *CIFalse = mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(2));
1451  if (!CITrue || !CIFalse)
1452  return false;
1453 
1454  TrueVal = CITrue->getValue().getZExtValue();
1455  FalseVal = CIFalse->getValue().getZExtValue();
1456 
1457  return true;
1458 }
1459 
1461  assert(
1462  (getOpcode() == Instruction::Br || getOpcode() == Instruction::Select ||
1463  getOpcode() == Instruction::Call || getOpcode() == Instruction::Invoke ||
1464  getOpcode() == Instruction::IndirectBr ||
1465  getOpcode() == Instruction::Switch) &&
1466  "Looking for branch weights on something besides branch");
1467 
1468  TotalVal = 0;
1469  auto *ProfileData = getMetadata(LLVMContext::MD_prof);
1470  if (!ProfileData)
1471  return false;
1472 
1473  auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand(0));
1474  if (!ProfDataName)
1475  return false;
1476 
1477  if (ProfDataName->getString().equals("branch_weights")) {
1478  TotalVal = 0;
1479  for (unsigned i = 1; i < ProfileData->getNumOperands(); i++) {
1480  auto *V = mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(i));
1481  if (!V)
1482  return false;
1483  TotalVal += V->getValue().getZExtValue();
1484  }
1485  return true;
1486  } else if (ProfDataName->getString().equals("VP") &&
1487  ProfileData->getNumOperands() > 3) {
1488  TotalVal = mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(2))
1489  ->getValue()
1490  .getZExtValue();
1491  return true;
1492  }
1493  return false;
1494 }
1495 
1496 void GlobalObject::copyMetadata(const GlobalObject *Other, unsigned Offset) {
1498  Other->getAllMetadata(MDs);
1499  for (auto &MD : MDs) {
1500  // We need to adjust the type metadata offset.
1501  if (Offset != 0 && MD.first == LLVMContext::MD_type) {
1502  auto *OffsetConst = cast<ConstantInt>(
1503  cast<ConstantAsMetadata>(MD.second->getOperand(0))->getValue());
1504  Metadata *TypeId = MD.second->getOperand(1);
1505  auto *NewOffsetMD = ConstantAsMetadata::get(ConstantInt::get(
1506  OffsetConst->getType(), OffsetConst->getValue() + Offset));
1507  addMetadata(LLVMContext::MD_type,
1508  *MDNode::get(getContext(), {NewOffsetMD, TypeId}));
1509  continue;
1510  }
1511  // If an offset adjustment was specified we need to modify the DIExpression
1512  // to prepend the adjustment:
1513  // !DIExpression(DW_OP_plus, Offset, [original expr])
1514  auto *Attachment = MD.second;
1515  if (Offset != 0 && MD.first == LLVMContext::MD_dbg) {
1516  DIGlobalVariable *GV = dyn_cast<DIGlobalVariable>(Attachment);
1517  DIExpression *E = nullptr;
1518  if (!GV) {
1519  auto *GVE = cast<DIGlobalVariableExpression>(Attachment);
1520  GV = GVE->getVariable();
1521  E = GVE->getExpression();
1522  }
1523  ArrayRef<uint64_t> OrigElements;
1524  if (E)
1525  OrigElements = E->getElements();
1526  std::vector<uint64_t> Elements(OrigElements.size() + 2);
1527  Elements[0] = dwarf::DW_OP_plus_uconst;
1528  Elements[1] = Offset;
1529  llvm::copy(OrigElements, Elements.begin() + 2);
1530  E = DIExpression::get(getContext(), Elements);
1531  Attachment = DIGlobalVariableExpression::get(getContext(), GV, E);
1532  }
1533  addMetadata(MD.first, *Attachment);
1534  }
1535 }
1536 
1538  addMetadata(
1539  LLVMContext::MD_type,
1542  Type::getInt64Ty(getContext()), Offset)),
1543  TypeID}));
1544 }
1545 
1547  // Remove any existing vcall visibility metadata first in case we are
1548  // updating.
1549  eraseMetadata(LLVMContext::MD_vcall_visibility);
1550  addMetadata(LLVMContext::MD_vcall_visibility,
1554 }
1555 
1557  if (MDNode *MD = getMetadata(LLVMContext::MD_vcall_visibility)) {
1558  uint64_t Val = cast<ConstantInt>(
1559  cast<ConstantAsMetadata>(MD->getOperand(0))->getValue())
1560  ->getZExtValue();
1561  assert(Val <= 2 && "unknown vcall visibility!");
1562  return (VCallVisibility)Val;
1563  }
1564  return VCallVisibility::VCallVisibilityPublic;
1565 }
1566 
1568  setMetadata(LLVMContext::MD_dbg, SP);
1569 }
1570 
1572  return cast_or_null<DISubprogram>(getMetadata(LLVMContext::MD_dbg));
1573 }
1574 
1576  if (DISubprogram *SP = getSubprogram()) {
1577  if (DICompileUnit *CU = SP->getUnit()) {
1578  return CU->getDebugInfoForProfiling();
1579  }
1580  }
1581  return false;
1582 }
1583 
1585  addMetadata(LLVMContext::MD_dbg, *GV);
1586 }
1587 
1591  getMetadata(LLVMContext::MD_dbg, MDs);
1592  for (MDNode *MD : MDs)
1593  GVs.push_back(cast<DIGlobalVariableExpression>(MD));
1594 }
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:156
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
llvm::less_second
Function object to check whether the second component of a std::pair compares less than the second co...
Definition: STLExtras.h:1362
getNMDOps
static SmallVector< TrackingMDRef, 4 > & getNMDOps(void *Operands)
Definition: Metadata.cpp:1138
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:1150
llvm::GlobalVariable::getDebugInfo
void getDebugInfo(SmallVectorImpl< DIGlobalVariableExpression * > &GVs) const
Fill the vector with all debug info attachements.
Definition: Metadata.cpp:1588
DebugInfoMetadata.h
llvm::GlobalValue::Visibility
unsigned Visibility
Definition: GlobalValue.h:95
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:919
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:1201
llvm::MDNode::HasCachedHash::SFINAE
Definition: Metadata.cpp:820
llvm::Function::getSubprogram
DISubprogram * getSubprogram() const
Get the attached subprogram.
Definition: Metadata.cpp:1571
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:1807
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:416
tryMergeRange
static bool tryMergeRange(SmallVectorImpl< ConstantInt * > &EndPoints, ConstantInt *Low, ConstantInt *High)
Definition: Metadata.cpp:1022
llvm::MDAttachments::erase
bool erase(unsigned ID)
Remove attachments with the given ID.
Definition: Metadata.cpp:1211
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:1162
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:1668
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:375
T
#define T
Definition: Mips16ISelLowering.cpp:341
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:1553
llvm::tgtok::FalseVal
@ FalseVal
Definition: TGLexer.h:62
llvm::ValueAsMetadata::get
static ValueAsMetadata * get(Value *V)
Definition: Metadata.cpp:392
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:541
llvm::DIExpression
DWARF expression.
Definition: DebugInfoMetadata.h:2558
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:1716
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:1300
llvm::RISCVFenceField::R
@ R
Definition: RISCVBaseInfo.h:240
llvm::MDNode::operands
op_range operands() const
Definition: Metadata.h:1202
llvm::Instruction::setMetadata
void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
Definition: Metadata.cpp:1366
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:885
llvm::MDNode::getNumOperands
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1210
llvm::MDOperand::reset
void reset()
Definition: Metadata.h:800
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:1160
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:1460
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:1354
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:1171
llvm::GlobalObject::setVCallVisibilityMetadata
void setVCallVisibilityMetadata(VCallVisibility Visibility)
Definition: Metadata.cpp:1546
llvm::MDNode::mutable_operands
mutable_op_range mutable_operands()
Definition: Metadata.h:1005
llvm::MDTuple
Tuple of metadata.
Definition: Metadata.h:1241
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:818
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:1177
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:1000
llvm::NamedMDNode::eraseFromParent
void eraseFromParent()
Drop all references and remove the node from parent module.
Definition: Metadata.cpp:1167
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:1777
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:928
DebugLoc.h
SmallPtrSet.h
llvm::Instruction::getAAMetadata
AAMDNodes getAAMetadata() const
Returns the AA metadata for this instruction.
Definition: Metadata.cpp:1400
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:819
llvm::SPII::Store
@ Store
Definition: SparcInstrInfo.h:33
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:1435
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:1204
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:1584
llvm::MDNode::resolveCycles
void resolveCycles()
Resolve cycles.
Definition: Metadata.cpp:671
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:324
llvm::APFloat
Definition: APFloat.h:701
llvm::Function::setSubprogram
void setSubprogram(DISubprogram *SP)
Set the attached subprogram.
Definition: Metadata.cpp:1567
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:625
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:958
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:1379
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:1298
uint64_t
llvm::Function::isDebugInfoForProfiling
bool isDebugInfoForProfiling() const
Returns true if we should emit debug info for profiling.
Definition: Metadata.cpp:1575
llvm::GlobalObject::copyMetadata
void copyMetadata(const GlobalObject *Src, unsigned Offset)
Copy metadata from Src, adjusting offsets by Offset.
Definition: Metadata.cpp:1496
llvm::DIGlobalVariable
Global variables.
Definition: DebugInfoMetadata.h:2914
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:1322
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:1682
ArrayRef.h
llvm::MDString::get
static MDString * get(LLVMContext &Context, StringRef Str)
Definition: Metadata.cpp:497
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:1154
llvm::MDNode::getMostGenericAlignmentOrDereferenceable
static MDNode * getMostGenericAlignmentOrDereferenceable(MDNode *A, MDNode *B)
Definition: Metadata.cpp:1123
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:737
llvm::MDTuple::get
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1267
llvm::MDNode::getNumUnresolved
unsigned getNumUnresolved() const
Definition: Metadata.h:1141
llvm::ValueAsMetadata::getValue
Value * getValue() const
Definition: Metadata.h:385
llvm::MDNode
Metadata node.
Definition: Metadata.h:937
llvm::Value::clearMetadata
void clearMetadata()
Erase all metadata attached to this Value.
Definition: Metadata.cpp:1321
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:1041
llvm::MDAttachments::insert
void insert(unsigned ID, MDNode &MD)
Adds an attachment to a particular node.
Definition: Metadata.cpp:1207
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:944
llvm::MDNode::isUniqued
bool isUniqued() const
Definition: Metadata.h:1049
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:691
getParent
static const Function * getParent(const Value *V)
Definition: BasicAliasAnalysis.cpp:845
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:971
llvm::Value::getMetadata
MDNode * getMetadata(unsigned KindID) const
Get the current metadata attachments for the given kind, if any.
Definition: Metadata.cpp:1227
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:809
isContiguous
static bool isContiguous(const ConstantRange &A, const ConstantRange &B)
Definition: Metadata.cpp:1014
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:1264
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:930
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:1031
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:1050
llvm::Value::eraseMetadata
bool eraseMetadata(unsigned KindID)
Erase all metadata attachments with the given kind.
Definition: Metadata.cpp:1309
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:1056
llvm::stable_sort
void stable_sort(R &&Range)
Definition: STLExtras.h:1761
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:1558
llvm::MetadataAsValue::get
static MetadataAsValue * get(LLVMContext &Context, Metadata *MD)
Definition: Metadata.cpp:102
llvm::AMDGPU::SendMsg::Op
Op
Definition: SIDefines.h:345
llvm::ArrayRef::begin
iterator begin() const
Definition: ArrayRef.h:152
llvm::GlobalObject::addTypeMetadata
void addTypeMetadata(unsigned Offset, Metadata *TypeID)
Definition: Metadata.cpp:1537
llvm::MDNode::isResolved
bool isResolved() const
Check if node is fully resolved.
Definition: Metadata.h:1047
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:1409
Function.h
TrackingMDRef.h
llvm::sort
void sort(IteratorTy Start, IteratorTy End)
Definition: STLExtras.h:1562
llvm::MDNode::deleteTemporary
static void deleteTemporary(MDNode *N)
Deallocate a node created by getTemporary.
Definition: Metadata.cpp:879
llvm::DIGlobalVariableExpression
A pair of DIGlobalVariable and DIExpression.
Definition: DebugInfoMetadata.h:3358
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:435
llvm::MDNode::replaceOperandWith
void replaceOperandWith(unsigned I, Metadata *New)
Replace a specific operand.
Definition: Metadata.cpp:907
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:411
llvm::MDNode::isTemporary
bool isTemporary() const
Definition: Metadata.h:1051
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:1190
llvm::MDNode::getMostGenericRange
static MDNode * getMostGenericRange(MDNode *A, MDNode *B)
Definition: Metadata.cpp:1051
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:1324
llvm::MDNode::getMostGenericFPMath
static MDNode * getMostGenericFPMath(MDNode *A, MDNode *B)
Definition: Metadata.cpp:1003
N
#define N
llvm::GlobalObject::getVCallVisibility
VCallVisibility getVCallVisibility() const
Definition: Metadata.cpp:1556
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:164
llvm::DISubprogram
Subprogram description.
Definition: DebugInfoMetadata.h:1803
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:1253
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:558
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:582
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:507
llvm::NamedMDNode::clearOperands
void clearOperands()
Drop all references to this node's operands.
Definition: Metadata.cpp:1169
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:62
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:504
llvm::MDNode::op_begin
op_iterator op_begin() const
Definition: Metadata.h:1194
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:1184
llvm::Value
LLVM Value Representation.
Definition: Value.h:74
llvm::MDNode::setNumUnresolved
void setNumUnresolved(unsigned N)
Definition: Metadata.h:1143
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:1236
canBeMerged
static bool canBeMerged(const ConstantRange &A, const ConstantRange &B)
Definition: Metadata.cpp:1018
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:38