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