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