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