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