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