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