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