LCOV - code coverage report
Current view: top level - lib/IR - Metadata.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 601 636 94.5 %
Date: 2018-06-17 00:07:59 Functions: 132 138 95.7 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13