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