LCOV - code coverage report
Current view: top level - lib/IR - Metadata.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 688 723 95.2 %
Date: 2017-09-14 15:23:50 Functions: 130 135 96.3 %
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       54860 : MetadataAsValue::MetadataAsValue(Type *Ty, Metadata *MD)
      64       54860 :     : Value(Ty, MetadataAsValueVal), MD(MD) {
      65       54860 :   track();
      66       54860 : }
      67             : 
      68       77878 : MetadataAsValue::~MetadataAsValue() {
      69       38939 :   getType()->getContext().pImpl->MetadataAsValues.erase(MD);
      70       38939 :   untrack();
      71       38939 : }
      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      207100 : static Metadata *canonicalizeMetadataForValue(LLVMContext &Context,
      85             :                                               Metadata *MD) {
      86      207100 :   if (!MD)
      87             :     // !{}
      88       58224 :     return MDNode::get(Context, None);
      89             : 
      90             :   // Return early if this isn't a single-operand MDNode.
      91       64031 :   auto *N = dyn_cast<MDNode>(MD);
      92       64031 :   if (!N || N->getNumOperands() != 1)
      93             :     return MD;
      94             : 
      95        1394 :   if (!N->getOperand(0))
      96             :     // !{}
      97          28 :     return MDNode::get(Context, None);
      98             : 
      99        1121 :   if (auto *C = dyn_cast<ConstantAsMetadata>(N->getOperand(0)))
     100             :     // Look through the MDNode.
     101             :     return C;
     102             : 
     103         438 :   return MD;
     104             : }
     105             : 
     106      145596 : MetadataAsValue *MetadataAsValue::get(LLVMContext &Context, Metadata *MD) {
     107      145596 :   MD = canonicalizeMetadataForValue(Context, MD);
     108      291192 :   auto *&Entry = Context.pImpl->MetadataAsValues[MD];
     109      145596 :   if (!Entry)
     110       54860 :     Entry = new MetadataAsValue(Type::getMetadataTy(Context), MD);
     111      145596 :   return Entry;
     112             : }
     113             : 
     114       27181 : MetadataAsValue *MetadataAsValue::getIfExists(LLVMContext &Context,
     115             :                                               Metadata *MD) {
     116       27181 :   MD = canonicalizeMetadataForValue(Context, MD);
     117       27181 :   auto &Store = Context.pImpl->MetadataAsValues;
     118       27181 :   return Store.lookup(MD);
     119             : }
     120             : 
     121       34323 : void MetadataAsValue::handleChangedMetadata(Metadata *MD) {
     122       34323 :   LLVMContext &Context = getContext();
     123       34323 :   MD = canonicalizeMetadataForValue(Context, MD);
     124       34323 :   auto &Store = Context.pImpl->MetadataAsValues;
     125             : 
     126             :   // Stop tracking the old metadata.
     127       34323 :   Store.erase(this->MD);
     128       34323 :   untrack();
     129       34323 :   this->MD = nullptr;
     130             : 
     131             :   // Start tracking MD, or RAUW if necessary.
     132       34323 :   auto *&Entry = Store[MD];
     133       34323 :   if (Entry) {
     134       31053 :     replaceAllUsesWith(Entry);
     135       31053 :     delete this;
     136             :     return;
     137             :   }
     138             : 
     139        3270 :   this->MD = MD;
     140        3270 :   track();
     141        3270 :   Entry = this;
     142             : }
     143             : 
     144       58130 : void MetadataAsValue::track() {
     145       58130 :   if (MD)
     146       58130 :     MetadataTracking::track(&MD, *MD, *this);
     147       58130 : }
     148             : 
     149       73262 : void MetadataAsValue::untrack() {
     150       73262 :   if (MD)
     151       34323 :     MetadataTracking::untrack(MD);
     152       73262 : }
     153             : 
     154   101638193 : 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   101638193 :   if (auto *R = ReplaceableMetadataImpl::getOrCreate(MD)) {
     159      430137 :     R->addRef(Ref, Owner);
     160      430137 :     return true;
     161             :   }
     162         942 :   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         942 :     PH->Use = static_cast<Metadata **>(Ref);
     166         942 :     return true;
     167             :   }
     168             :   return false;
     169             : }
     170             : 
     171    88098711 : void MetadataTracking::untrack(void *Ref, Metadata &MD) {
     172             :   assert(Ref && "Expected live reference");
     173    88098711 :   if (auto *R = ReplaceableMetadataImpl::getIfExists(MD))
     174      169232 :     R->dropRef(Ref);
     175         942 :   else if (auto *PH = dyn_cast<DistinctMDOperandPlaceholder>(&MD))
     176         942 :     PH->Use = nullptr;
     177    88098711 : }
     178             : 
     179    60771325 : 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    60771325 :   if (auto *R = ReplaceableMetadataImpl::getIfExists(MD)) {
     184       54180 :     R->moveRef(Ref, New, MD);
     185       54180 :     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      430137 : void ReplaceableMetadataImpl::addRef(void *Ref, OwnerTy Owner) {
     199             :   bool WasInserted =
     200     2150685 :       UseMap.insert(std::make_pair(Ref, std::make_pair(Owner, NextIndex)))
     201      430137 :           .second;
     202             :   (void)WasInserted;
     203             :   assert(WasInserted && "Expected to add a reference");
     204             : 
     205      430137 :   ++NextIndex;
     206             :   assert(NextIndex != 0 && "Unexpected overflow");
     207      430137 : }
     208             : 
     209      169232 : void ReplaceableMetadataImpl::dropRef(void *Ref) {
     210      169232 :   bool WasErased = UseMap.erase(Ref);
     211             :   (void)WasErased;
     212             :   assert(WasErased && "Expected to drop a reference");
     213      169232 : }
     214             : 
     215       54180 : void ReplaceableMetadataImpl::moveRef(void *Ref, void *New,
     216             :                                       const Metadata &MD) {
     217       54180 :   auto I = UseMap.find(Ref);
     218             :   assert(I != UseMap.end() && "Expected to move a reference");
     219       54180 :   auto OwnerAndIndex = I->second;
     220      108360 :   UseMap.erase(I);
     221      162540 :   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       54180 : }
     232             : 
     233      243998 : void ReplaceableMetadataImpl::replaceAllUsesWith(Metadata *MD) {
     234      487996 :   if (UseMap.empty())
     235      132967 :     return;
     236             : 
     237             :   // Copy out uses since UseMap will get touched below.
     238             :   using UseTy = std::pair<void *, std::pair<OwnerTy, uint64_t>>;
     239      444124 :   SmallVector<UseTy, 8> Uses(UseMap.begin(), UseMap.end());
     240      333093 :   std::sort(Uses.begin(), Uses.end(), [](const UseTy &L, const UseTy &R) {
     241             :     return L.second.second < R.second.second;
     242             :   });
     243      518145 :   for (const auto &Pair : Uses) {
     244             :     // Check that this Ref hasn't disappeared after RAUW (when updating a
     245             :     // previous Ref).
     246      370104 :     if (!UseMap.count(Pair.first))
     247           0 :       continue;
     248             : 
     249      185052 :     OwnerTy Owner = Pair.second.first;
     250      185052 :     if (!Owner) {
     251             :       // Update unowned tracking references directly.
     252      118886 :       Metadata *&Ref = *static_cast<Metadata **>(Pair.first);
     253      118886 :       Ref = MD;
     254      118886 :       if (MD)
     255             :         MetadataTracking::track(Ref);
     256      118886 :       UseMap.erase(Pair.first);
     257      118886 :       continue;
     258             :     }
     259             : 
     260             :     // Check for MetadataAsValue.
     261      100489 :     if (Owner.is<MetadataAsValue *>()) {
     262       34323 :       Owner.get<MetadataAsValue *>()->handleChangedMetadata(MD);
     263       34323 :       continue;
     264             :     }
     265             : 
     266             :     // There's a Metadata owner -- dispatch.
     267       31843 :     Metadata *OwnerMD = Owner.get<Metadata *>();
     268       63686 :     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      196520 : void ReplaceableMetadataImpl::resolveAllUses(bool ResolveUsers) {
     282      393040 :   if (UseMap.empty())
     283      132744 :     return;
     284             : 
     285       64046 :   if (!ResolveUsers) {
     286         270 :     UseMap.clear();
     287         270 :     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      255104 :   SmallVector<UseTy, 8> Uses(UseMap.begin(), UseMap.end());
     293      191328 :   std::sort(Uses.begin(), Uses.end(), [](const UseTy &L, const UseTy &R) {
     294             :     return L.second.second < R.second.second;
     295             :   });
     296       63776 :   UseMap.clear();
     297      313795 :   for (const auto &Pair : Uses) {
     298      122467 :     auto Owner = Pair.second.first;
     299      122467 :     if (!Owner)
     300       63476 :       continue;
     301       58991 :     if (Owner.is<MetadataAsValue *>())
     302        2228 :       continue;
     303             : 
     304             :     // Resolve MDNodes that point at this.
     305      113526 :     auto *OwnerMD = dyn_cast<MDNode>(Owner.get<Metadata *>());
     306           0 :     if (!OwnerMD)
     307           0 :       continue;
     308       56763 :     if (OwnerMD->isResolved())
     309       20539 :       continue;
     310       36224 :     OwnerMD->decrementUnresolvedOperandCount();
     311             :   }
     312             : }
     313             : 
     314   101638193 : ReplaceableMetadataImpl *ReplaceableMetadataImpl::getOrCreate(Metadata &MD) {
     315   101638193 :   if (auto *N = dyn_cast<MDNode>(&MD))
     316      300810 :     return N->isResolved() ? nullptr : N->Context.getOrCreateReplaceableUses();
     317      129327 :   return dyn_cast<ValueAsMetadata>(&MD);
     318             : }
     319             : 
     320   148870036 : ReplaceableMetadataImpl *ReplaceableMetadataImpl::getIfExists(Metadata &MD) {
     321   148870036 :   if (auto *N = dyn_cast<MDNode>(&MD))
     322      109786 :     return N->isResolved() ? nullptr : N->Context.getReplaceableUses();
     323      113627 :   return dyn_cast<ValueAsMetadata>(&MD);
     324             : }
     325             : 
     326           0 : bool ReplaceableMetadataImpl::isReplaceable(const Metadata &MD) {
     327           0 :   if (auto *N = dyn_cast<MDNode>(&MD))
     328           0 :     return !N->isResolved();
     329           0 :   return dyn_cast<ValueAsMetadata>(&MD);
     330             : }
     331             : 
     332             : static Function *getLocalFunction(Value *V) {
     333             :   assert(V && "Expected value");
     334        1625 :   if (auto *A = dyn_cast<Argument>(V))
     335             :     return A->getParent();
     336       13900 :   if (BasicBlock *BB = cast<Instruction>(V)->getParent())
     337             :     return BB->getParent();
     338             :   return nullptr;
     339             : }
     340             : 
     341      208628 : ValueAsMetadata *ValueAsMetadata::get(Value *V) {
     342             :   assert(V && "Unexpected null Value");
     343             : 
     344      208628 :   auto &Context = V->getContext();
     345      417256 :   auto *&Entry = Context.pImpl->ValuesAsMetadata[V];
     346      208628 :   if (!Entry) {
     347             :     assert((isa<Constant>(V) || isa<Argument>(V) || isa<Instruction>(V)) &&
     348             :            "Expected constant or function-local value");
     349             :     assert(!V->IsUsedByMD && "Expected this to be the only metadata use");
     350       83128 :     V->IsUsedByMD = true;
     351      128788 :     if (auto *C = dyn_cast<Constant>(V))
     352       91320 :       Entry = new ConstantAsMetadata(C);
     353             :     else
     354       74936 :       Entry = new LocalAsMetadata(V);
     355             :   }
     356             : 
     357      208628 :   return Entry;
     358             : }
     359             : 
     360      616480 : ValueAsMetadata *ValueAsMetadata::getIfExists(Value *V) {
     361             :   assert(V && "Unexpected null Value");
     362     1232960 :   return V->getContext().pImpl->ValuesAsMetadata.lookup(V);
     363             : }
     364             : 
     365       70240 : void ValueAsMetadata::handleDeletion(Value *V) {
     366             :   assert(V && "Expected valid value");
     367             : 
     368       70240 :   auto &Store = V->getType()->getContext().pImpl->ValuesAsMetadata;
     369       70240 :   auto I = Store.find(V);
     370      140480 :   if (I == Store.end())
     371           1 :     return;
     372             : 
     373             :   // Remove old entry from the map.
     374       70239 :   ValueAsMetadata *MD = I->second;
     375             :   assert(MD && "Expected valid metadata");
     376             :   assert(MD->getValue() == V && "Expected valid mapping");
     377      140478 :   Store.erase(I);
     378             : 
     379             :   // Delete the metadata.
     380       70239 :   MD->replaceAllUsesWith(nullptr);
     381      140478 :   delete MD;
     382             : }
     383             : 
     384        6614 : void ValueAsMetadata::handleRAUW(Value *From, Value *To) {
     385             :   assert(From && "Expected valid value");
     386             :   assert(To && "Expected valid value");
     387             :   assert(From != To && "Expected changed value");
     388             :   assert(From->getType() == To->getType() && "Unexpected type change");
     389             : 
     390        6614 :   LLVMContext &Context = From->getType()->getContext();
     391        6614 :   auto &Store = Context.pImpl->ValuesAsMetadata;
     392        6614 :   auto I = Store.find(From);
     393       13228 :   if (I == Store.end()) {
     394             :     assert(!From->IsUsedByMD && "Expected From not to be used by metadata");
     395        3038 :     return;
     396             :   }
     397             : 
     398             :   // Remove old entry from the map.
     399             :   assert(From->IsUsedByMD && "Expected From to be used by metadata");
     400        6614 :   From->IsUsedByMD = false;
     401        6614 :   ValueAsMetadata *MD = I->second;
     402             :   assert(MD && "Expected valid metadata");
     403             :   assert(MD->getValue() == From && "Expected valid mapping");
     404       13228 :   Store.erase(I);
     405             : 
     406       13228 :   if (isa<LocalAsMetadata>(MD)) {
     407        8403 :     if (auto *C = dyn_cast<Constant>(To)) {
     408             :       // Local became a constant.
     409        3842 :       MD->replaceAllUsesWith(ConstantAsMetadata::get(C));
     410        1921 :       delete MD;
     411             :       return;
     412             :     }
     413       11579 :     if (getLocalFunction(From) && getLocalFunction(To) &&
     414        7018 :         getLocalFunction(From) != getLocalFunction(To)) {
     415             :       // Function changed.
     416          32 :       MD->replaceAllUsesWith(nullptr);
     417          16 :       delete MD;
     418             :       return;
     419             :     }
     420         132 :   } else if (!isa<Constant>(To)) {
     421             :     // Changed to function-local value.
     422           2 :     MD->replaceAllUsesWith(nullptr);
     423           1 :     delete MD;
     424             :     return;
     425             :   }
     426             : 
     427        4676 :   auto *&Entry = Store[To];
     428        4676 :   if (Entry) {
     429             :     // The target already exists.
     430        2200 :     MD->replaceAllUsesWith(Entry);
     431        1100 :     delete MD;
     432             :     return;
     433             :   }
     434             : 
     435             :   // Update MD in place (and update the map entry).
     436             :   assert(!To->IsUsedByMD && "Expected this to be the only metadata use");
     437        3576 :   To->IsUsedByMD = true;
     438        3576 :   MD->V = To;
     439        3576 :   Entry = MD;
     440             : }
     441             : 
     442             : //===----------------------------------------------------------------------===//
     443             : // MDString implementation.
     444             : //
     445             : 
     446      223249 : MDString *MDString::get(LLVMContext &Context, StringRef Str) {
     447      223249 :   auto &Store = Context.pImpl->MDStringCache;
     448      223249 :   auto I = Store.try_emplace(Str);
     449      446498 :   auto &MapEntry = I.first->getValue();
     450      223249 :   if (!I.second)
     451             :     return &MapEntry;
     452      137166 :   MapEntry.Entry = &*I.first;
     453      137166 :   return &MapEntry;
     454             : }
     455             : 
     456     8883423 : StringRef MDString::getString() const {
     457             :   assert(Entry && "Expected to find string map entry");
     458    17766846 :   return Entry->first();
     459             : }
     460             : 
     461             : //===----------------------------------------------------------------------===//
     462             : // MDNode implementation.
     463             : //
     464             : 
     465             : // Assert that the MDNode types will not be unaligned by the objects
     466             : // prepended to them.
     467             : #define HANDLE_MDNODE_LEAF(CLASS)                                              \
     468             :   static_assert(                                                               \
     469             :       alignof(uint64_t) >= alignof(CLASS),                                     \
     470             :       "Alignment is insufficient after objects prepended to " #CLASS);
     471             : #include "llvm/IR/Metadata.def"
     472             : 
     473     1183021 : void *MDNode::operator new(size_t Size, unsigned NumOps) {
     474     1183021 :   size_t OpSize = NumOps * sizeof(MDOperand);
     475             :   // uint64_t is the most aligned type we need support (ensured by static_assert
     476             :   // above)
     477     1183021 :   OpSize = alignTo(OpSize, alignof(uint64_t));
     478     1183021 :   void *Ptr = reinterpret_cast<char *>(::operator new(OpSize + Size)) + OpSize;
     479     1183021 :   MDOperand *O = static_cast<MDOperand *>(Ptr);
     480     1183021 :   for (MDOperand *E = O - NumOps; O != E; --O)
     481     4892056 :     (void)new (O - 1) MDOperand;
     482     1183021 :   return Ptr;
     483             : }
     484             : 
     485      225816 : void MDNode::operator delete(void *Mem) {
     486      225816 :   MDNode *N = static_cast<MDNode *>(Mem);
     487      225816 :   size_t OpSize = N->NumOperands * sizeof(MDOperand);
     488      225816 :   OpSize = alignTo(OpSize, alignof(uint64_t));
     489             : 
     490      225816 :   MDOperand *O = static_cast<MDOperand *>(Mem);
     491      660168 :   for (MDOperand *E = O - N->NumOperands; O != E; --O)
     492      868704 :     (O - 1)->~MDOperand();
     493      225816 :   ::operator delete(reinterpret_cast<char *>(Mem) - OpSize);
     494      225816 : }
     495             : 
     496     1183021 : MDNode::MDNode(LLVMContext &Context, unsigned ID, StorageType Storage,
     497     1183021 :                ArrayRef<Metadata *> Ops1, ArrayRef<Metadata *> Ops2)
     498     1183021 :     : Metadata(ID, Storage), NumOperands(Ops1.size() + Ops2.size()),
     499     4732084 :       NumUnresolved(0), Context(Context) {
     500     1183021 :   unsigned Op = 0;
     501     4812035 :   for (Metadata *MD : Ops1)
     502     2445993 :     setOperand(Op++, MD);
     503     2366077 :   for (Metadata *MD : Ops2)
     504          35 :     setOperand(Op++, MD);
     505             : 
     506     1183021 :   if (!isUniqued())
     507             :     return;
     508             : 
     509             :   // Count the unresolved operands.  If there are any, RAUW support will be
     510             :   // added lazily on first reference.
     511      886608 :   countUnresolvedOperands();
     512             : }
     513             : 
     514       14023 : TempMDNode MDNode::clone() const {
     515       28046 :   switch (getMetadataID()) {
     516           0 :   default:
     517           0 :     llvm_unreachable("Invalid MDNode subclass");
     518             : #define HANDLE_MDNODE_LEAF(CLASS)                                              \
     519             :   case CLASS##Kind:                                                            \
     520             :     return cast<CLASS>(this)->cloneImpl();
     521             : #include "llvm/IR/Metadata.def"
     522             :   }
     523             : }
     524             : 
     525     1854913 : static bool isOperandUnresolved(Metadata *Op) {
     526     1458507 :   if (auto *N = dyn_cast_or_null<MDNode>(Op))
     527     1458507 :     return !N->isResolved();
     528             :   return false;
     529             : }
     530             : 
     531      894519 : void MDNode::countUnresolvedOperands() {
     532             :   assert(NumUnresolved == 0 && "Expected unresolved ops to be uncounted");
     533             :   assert(isUniqued() && "Expected this to be uniqued");
     534     1789038 :   NumUnresolved = count_if(operands(), isOperandUnresolved);
     535      894519 : }
     536             : 
     537        7911 : void MDNode::makeUniqued() {
     538             :   assert(isTemporary() && "Expected this to be temporary");
     539             :   assert(!isResolved() && "Expected this to be unresolved");
     540             : 
     541             :   // Enable uniquing callbacks.
     542       63852 :   for (auto &Op : mutable_operands())
     543      111882 :     Op.reset(Op.get(), this);
     544             : 
     545             :   // Make this 'uniqued'.
     546        7911 :   Storage = Uniqued;
     547        7911 :   countUnresolvedOperands();
     548        7911 :   if (!NumUnresolved) {
     549        7745 :     dropReplaceableUses();
     550             :     assert(isResolved() && "Expected this to be resolved");
     551             :   }
     552             : 
     553             :   assert(isUniqued() && "Expected this to be uniqued");
     554        7911 : }
     555             : 
     556        2809 : void MDNode::makeDistinct() {
     557             :   assert(isTemporary() && "Expected this to be temporary");
     558             :   assert(!isResolved() && "Expected this to be unresolved");
     559             : 
     560             :   // Drop RAUW support and store as a distinct node.
     561        2809 :   dropReplaceableUses();
     562        2809 :   storeDistinctInContext();
     563             : 
     564             :   assert(isDistinct() && "Expected this to be distinct");
     565             :   assert(isResolved() && "Expected this to be resolved");
     566        2809 : }
     567             : 
     568       20459 : void MDNode::resolve() {
     569             :   assert(isUniqued() && "Expected this to be uniqued");
     570             :   assert(!isResolved() && "Expected this to be unresolved");
     571             : 
     572       20459 :   NumUnresolved = 0;
     573       20459 :   dropReplaceableUses();
     574             : 
     575             :   assert(isResolved() && "Expected this to be resolved");
     576       20459 : }
     577             : 
     578       65919 : void MDNode::dropReplaceableUses() {
     579             :   assert(!NumUnresolved && "Unexpected unresolved operand");
     580             : 
     581             :   // Drop any RAUW support.
     582      131838 :   if (Context.hasReplaceableUses())
     583      255104 :     Context.takeReplaceableUses()->resolveAllUses();
     584       65919 : }
     585             : 
     586       23877 : void MDNode::resolveAfterOperandChange(Metadata *Old, Metadata *New) {
     587             :   assert(isUniqued() && "Expected this to be uniqued");
     588             :   assert(NumUnresolved != 0 && "Expected unresolved operands");
     589             : 
     590             :   // Check if an operand was resolved.
     591       23876 :   if (!isOperandUnresolved(Old)) {
     592           2 :     if (isOperandUnresolved(New))
     593             :       // An operand was un-resolved!
     594           1 :       ++NumUnresolved;
     595       23643 :   } else if (!isOperandUnresolved(New))
     596       15457 :     decrementUnresolvedOperandCount();
     597       23877 : }
     598             : 
     599       51681 : void MDNode::decrementUnresolvedOperandCount() {
     600             :   assert(!isResolved() && "Expected this to be unresolved");
     601       51681 :   if (isTemporary())
     602             :     return;
     603             : 
     604             :   assert(isUniqued() && "Expected this to be uniqued");
     605       51681 :   if (--NumUnresolved)
     606             :     return;
     607             : 
     608             :   // Last unresolved operand has just been resolved.
     609       34906 :   dropReplaceableUses();
     610             :   assert(isResolved() && "Expected this to become resolved");
     611             : }
     612             : 
     613        1955 : void MDNode::resolveCycles() {
     614         613 :   if (isResolved())
     615             :     return;
     616             : 
     617             :   // Resolve this node immediately.
     618         613 :   resolve();
     619             : 
     620             :   // Resolve all operands.
     621        4216 :   for (const auto &Op : operands()) {
     622        1385 :     auto *N = dyn_cast_or_null<MDNode>(Op);
     623        2218 :     if (!N)
     624        2218 :       continue;
     625             : 
     626             :     assert(!N->isTemporary() &&
     627             :            "Expected all forward declarations to be resolved");
     628         432 :     if (!N->isResolved())
     629         432 :       N->resolveCycles();
     630             :   }
     631             : }
     632             : 
     633             : static bool hasSelfReference(MDNode *N) {
     634           8 :   for (Metadata *MD : N->operands())
     635           3 :     if (MD == N)
     636             :       return true;
     637             :   return false;
     638             : }
     639             : 
     640           4 : MDNode *MDNode::replaceWithPermanentImpl() {
     641           4 :   switch (getMetadataID()) {
     642           1 :   default:
     643             :     // If this type isn't uniquable, replace with a distinct node.
     644           1 :     return replaceWithDistinctImpl();
     645             : 
     646             : #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS)                                    \
     647             :   case CLASS##Kind:                                                            \
     648             :     break;
     649             : #include "llvm/IR/Metadata.def"
     650             :   }
     651             : 
     652             :   // Even if this type is uniquable, self-references have to be distinct.
     653           3 :   if (hasSelfReference(this))
     654           1 :     return replaceWithDistinctImpl();
     655           2 :   return replaceWithUniquedImpl();
     656             : }
     657             : 
     658       20283 : MDNode *MDNode::replaceWithUniquedImpl() {
     659             :   // Try to uniquify in place.
     660       20283 :   MDNode *UniquedNode = uniquify();
     661             : 
     662       20283 :   if (UniquedNode == this) {
     663        7911 :     makeUniqued();
     664        7911 :     return this;
     665             :   }
     666             : 
     667             :   // Collision, so RAUW instead.
     668       12372 :   replaceAllUsesWith(UniquedNode);
     669       12372 :   deleteAsSubclass();
     670       12372 :   return UniquedNode;
     671             : }
     672             : 
     673        2809 : MDNode *MDNode::replaceWithDistinctImpl() {
     674        2809 :   makeDistinct();
     675        2809 :   return this;
     676             : }
     677             : 
     678       16981 : void MDTuple::recalculateHash() {
     679       33962 :   setHash(MDTupleInfo::KeyTy::calculateHash(this));
     680       16981 : }
     681             : 
     682      292444 : void MDNode::dropAllReferences() {
     683      851636 :   for (unsigned I = 0, E = NumOperands; I != E; ++I)
     684      559192 :     setOperand(I, nullptr);
     685      584888 :   if (Context.hasReplaceableUses()) {
     686      184668 :     Context.getReplaceableUses()->resolveAllUses(/* ResolveUsers */ false);
     687      277002 :     (void)Context.takeReplaceableUses();
     688             :   }
     689      292444 : }
     690             : 
     691       49213 : void MDNode::handleChangedOperand(void *Ref, Metadata *New) {
     692       98426 :   unsigned Op = static_cast<MDOperand *>(Ref) - op_begin();
     693             :   assert(Op < getNumOperands() && "Expected valid operand");
     694             : 
     695       49213 :   if (!isUniqued()) {
     696             :     // This node is not uniqued.  Just set the operand and be done with it.
     697        2390 :     setOperand(Op, New);
     698        2390 :     return;
     699             :   }
     700             : 
     701             :   // This node is uniqued.
     702       46823 :   eraseFromStore();
     703             : 
     704       93646 :   Metadata *Old = getOperand(Op);
     705       46823 :   setOperand(Op, New);
     706             : 
     707             :   // Drop uniquing for self-reference cycles and deleted constants.
     708       47595 :   if (New == this || (!New && Old && isa<ConstantAsMetadata>(Old))) {
     709       19846 :     if (!isResolved())
     710       19846 :       resolve();
     711       22707 :     storeDistinctInContext();
     712       22707 :     return;
     713             :   }
     714             : 
     715             :   // Re-unique the node.
     716       24116 :   auto *Uniqued = uniquify();
     717       24116 :   if (Uniqued == this) {
     718       23877 :     if (!isResolved())
     719       23877 :       resolveAfterOperandChange(Old, New);
     720             :     return;
     721             :   }
     722             : 
     723             :   // Collision.
     724          88 :   if (!isResolved()) {
     725             :     // Still unresolved, so RAUW.
     726             :     //
     727             :     // First, clear out all operands to prevent any recursion (similar to
     728             :     // dropAllReferences(), but we still need the use-list).
     729         732 :     for (unsigned O = 0, E = getNumOperands(); O != E; ++O)
     730         281 :       setOperand(O, nullptr);
     731         170 :     if (Context.hasReplaceableUses())
     732         170 :       Context.getReplaceableUses()->replaceAllUsesWith(Uniqued);
     733          85 :     deleteAsSubclass();
     734          85 :     return;
     735             :   }
     736             : 
     737             :   // Store in non-uniqued form if RAUW isn't possible.
     738           3 :   storeDistinctInContext();
     739             : }
     740             : 
     741      122003 : void MDNode::deleteAsSubclass() {
     742      244006 :   switch (getMetadataID()) {
     743           0 :   default:
     744           0 :     llvm_unreachable("Invalid subclass of MDNode");
     745             : #define HANDLE_MDNODE_LEAF(CLASS)                                              \
     746             :   case CLASS##Kind:                                                            \
     747             :     delete cast<CLASS>(this);                                                  \
     748             :     break;
     749             : #include "llvm/IR/Metadata.def"
     750             :   }
     751      122003 : }
     752             : 
     753             : template <class T, class InfoT>
     754       44399 : static T *uniquifyImpl(T *N, DenseSet<T *, InfoT> &Store) {
     755      123644 :   if (T *U = getUniqued(Store, N))
     756             :     return U;
     757             : 
     758       63878 :   Store.insert(N);
     759       31939 :   return N;
     760             : }
     761             : 
     762             : template <class NodeTy> struct MDNode::HasCachedHash {
     763             :   using Yes = char[1];
     764             :   using No = char[2];
     765             :   template <class U, U Val> struct SFINAE {};
     766             : 
     767             :   template <class U>
     768             :   static Yes &check(SFINAE<void (U::*)(unsigned), &U::setHash> *);
     769             :   template <class U> static No &check(...);
     770             : 
     771             :   static const bool value = sizeof(check<NodeTy>(nullptr)) == sizeof(Yes);
     772             : };
     773             : 
     774       44399 : MDNode *MDNode::uniquify() {
     775             :   assert(!hasSelfReference(this) && "Cannot uniquify a self-referencing node");
     776             : 
     777             :   // Try to insert into uniquing store.
     778       88798 :   switch (getMetadataID()) {
     779           0 :   default:
     780           0 :     llvm_unreachable("Invalid or non-uniquable subclass of MDNode");
     781             : #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS)                                    \
     782             :   case CLASS##Kind: {                                                          \
     783             :     CLASS *SubclassThis = cast<CLASS>(this);                                   \
     784             :     std::integral_constant<bool, HasCachedHash<CLASS>::value>                  \
     785             :         ShouldRecalculateHash;                                                 \
     786             :     dispatchRecalculateHash(SubclassThis, ShouldRecalculateHash);              \
     787             :     return uniquifyImpl(SubclassThis, getContext().pImpl->CLASS##s);           \
     788             :   }
     789             : #include "llvm/IR/Metadata.def"
     790             :   }
     791             : }
     792             : 
     793       46823 : void MDNode::eraseFromStore() {
     794       93646 :   switch (getMetadataID()) {
     795           0 :   default:
     796           0 :     llvm_unreachable("Invalid or non-uniquable subclass of MDNode");
     797             : #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS)                                    \
     798             :   case CLASS##Kind:                                                            \
     799             :     getContext().pImpl->CLASS##s.erase(cast<CLASS>(this));                     \
     800             :     break;
     801             : #include "llvm/IR/Metadata.def"
     802             :   }
     803       46823 : }
     804             : 
     805      406609 : MDTuple *MDTuple::getImpl(LLVMContext &Context, ArrayRef<Metadata *> MDs,
     806             :                           StorageType Storage, bool ShouldCreate) {
     807      406609 :   unsigned Hash = 0;
     808      406609 :   if (Storage == Uniqued) {
     809      313436 :     MDTupleInfo::KeyTy Key(MDs);
     810      507089 :     if (auto *N = getUniqued(Context.pImpl->MDTuples, Key))
     811      193654 :       return N;
     812      119783 :     if (!ShouldCreate)
     813             :       return nullptr;
     814      119782 :     Hash = Key.getHash();
     815             :   } else {
     816             :     assert(ShouldCreate && "Expected non-uniqued nodes to always be created");
     817             :   }
     818             : 
     819      425910 :   return storeImpl(new (MDs.size()) MDTuple(Context, Storage, Hash, MDs),
     820      425910 :                    Storage, Context.pImpl->MDTuples);
     821             : }
     822             : 
     823       92590 : void MDNode::deleteTemporary(MDNode *N) {
     824             :   assert(N->isTemporary() && "Expected temporary node");
     825       92590 :   N->replaceAllUsesWith(nullptr);
     826       92590 :   N->deleteAsSubclass();
     827       92590 : }
     828             : 
     829      206250 : void MDNode::storeDistinctInContext() {
     830             :   assert(!Context.hasReplaceableUses() && "Unexpected replaceable uses");
     831             :   assert(!NumUnresolved && "Unexpected unresolved nodes");
     832      206250 :   Storage = Distinct;
     833             :   assert(isResolved() && "Expected this to be resolved");
     834             : 
     835             :   // Reset the hash.
     836      206250 :   switch (getMetadataID()) {
     837           0 :   default:
     838           0 :     llvm_unreachable("Invalid subclass of MDNode");
     839             : #define HANDLE_MDNODE_LEAF(CLASS)                                              \
     840             :   case CLASS##Kind: {                                                          \
     841             :     std::integral_constant<bool, HasCachedHash<CLASS>::value> ShouldResetHash; \
     842             :     dispatchResetHash(cast<CLASS>(this), ShouldResetHash);                     \
     843             :     break;                                                                     \
     844             :   }
     845             : #include "llvm/IR/Metadata.def"
     846             :   }
     847             : 
     848      412500 :   getContext().pImpl->DistinctMDNodes.push_back(this);
     849      206250 : }
     850             : 
     851       25268 : void MDNode::replaceOperandWith(unsigned I, Metadata *New) {
     852       50536 :   if (getOperand(I) == New)
     853             :     return;
     854             : 
     855       22930 :   if (!isUniqued()) {
     856        5560 :     setOperand(I, New);
     857        5560 :     return;
     858             :   }
     859             : 
     860       17370 :   handleChangedOperand(mutable_begin() + I, New);
     861             : }
     862             : 
     863     3060283 : void MDNode::setOperand(unsigned I, Metadata *New) {
     864             :   assert(I < NumOperands);
     865     3060283 :   mutable_begin()[I].reset(New, isUniqued() ? this : nullptr);
     866     3060283 : }
     867             : 
     868             : /// Get a node or a self-reference that looks like it.
     869             : ///
     870             : /// Special handling for finding self-references, for use by \a
     871             : /// MDNode::concatenate() and \a MDNode::intersect() to maintain behaviour from
     872             : /// when self-referencing nodes were still uniqued.  If the first operand has
     873             : /// the same operands as \c Ops, return the first operand instead.
     874       24168 : static MDNode *getOrSelfReference(LLVMContext &Context,
     875             :                                   ArrayRef<Metadata *> Ops) {
     876       24168 :   if (!Ops.empty())
     877       48300 :     if (MDNode *N = dyn_cast_or_null<MDNode>(Ops[0]))
     878       24517 :       if (N->getNumOperands() == Ops.size() && N == N->getOperand(0)) {
     879         205 :         for (unsigned I = 1, E = Ops.size(); I != E; ++I)
     880         780 :           if (Ops[I] != N->getOperand(I))
     881         175 :             return MDNode::get(Context, Ops);
     882             :         return N;
     883             :       }
     884             : 
     885       23983 :   return MDNode::get(Context, Ops);
     886             : }
     887             : 
     888       42495 : MDNode *MDNode::concatenate(MDNode *A, MDNode *B) {
     889       42495 :   if (!A)
     890             :     return B;
     891       13427 :   if (!B)
     892             :     return A;
     893             : 
     894       26846 :   SmallSetVector<Metadata *, 4> MDs(A->op_begin(), A->op_end());
     895       26846 :   MDs.insert(B->op_begin(), B->op_end());
     896             : 
     897             :   // FIXME: This preserves long-standing behaviour, but is it really the right
     898             :   // behaviour?  Or was that an unintended side-effect of node uniquing?
     899       26846 :   return getOrSelfReference(A->getContext(), MDs.getArrayRef());
     900             : }
     901             : 
     902       10881 : MDNode *MDNode::intersect(MDNode *A, MDNode *B) {
     903       10881 :   if (!A || !B)
     904             :     return nullptr;
     905             : 
     906       21490 :   SmallSetVector<Metadata *, 4> MDs(A->op_begin(), A->op_end());
     907       32235 :   SmallPtrSet<Metadata *, 4> BSet(B->op_begin(), B->op_end());
     908       23007 :   MDs.remove_if([&](Metadata *MD) { return !is_contained(BSet, MD); });
     909             : 
     910             :   // FIXME: This preserves long-standing behaviour, but is it really the right
     911             :   // behaviour?  Or was that an unintended side-effect of node uniquing?
     912       21490 :   return getOrSelfReference(A->getContext(), MDs.getArrayRef());
     913             : }
     914             : 
     915         137 : MDNode *MDNode::getMostGenericAliasScope(MDNode *A, MDNode *B) {
     916         137 :   if (!A || !B)
     917             :     return nullptr;
     918             : 
     919           7 :   return concatenate(A, B);
     920             : }
     921             : 
     922          10 : MDNode *MDNode::getMostGenericFPMath(MDNode *A, MDNode *B) {
     923          10 :   if (!A || !B)
     924             :     return nullptr;
     925             : 
     926          24 :   APFloat AVal = mdconst::extract<ConstantFP>(A->getOperand(0))->getValueAPF();
     927          32 :   APFloat BVal = mdconst::extract<ConstantFP>(B->getOperand(0))->getValueAPF();
     928           8 :   if (AVal.compare(BVal) == APFloat::cmpLessThan)
     929             :     return A;
     930           6 :   return B;
     931             : }
     932             : 
     933          19 : static bool isContiguous(const ConstantRange &A, const ConstantRange &B) {
     934         108 :   return A.getUpper() == B.getLower() || A.getLower() == B.getUpper();
     935             : }
     936             : 
     937          22 : static bool canBeMerged(const ConstantRange &A, const ConstantRange &B) {
     938          22 :   return !A.intersectWith(B).isEmptySet() || isContiguous(A, B);
     939             : }
     940             : 
     941          22 : static bool tryMergeRange(SmallVectorImpl<ConstantInt *> &EndPoints,
     942             :                           ConstantInt *Low, ConstantInt *High) {
     943         154 :   ConstantRange NewRange(Low->getValue(), High->getValue());
     944          44 :   unsigned Size = EndPoints.size();
     945         110 :   APInt LB = EndPoints[Size - 2]->getValue();
     946         110 :   APInt LE = EndPoints[Size - 1]->getValue();
     947         110 :   ConstantRange LastRange(LB, LE);
     948          22 :   if (canBeMerged(NewRange, LastRange)) {
     949          18 :     ConstantRange Union = LastRange.unionWith(NewRange);
     950           9 :     Type *Ty = High->getType();
     951          27 :     EndPoints[Size - 2] =
     952          18 :         cast<ConstantInt>(ConstantInt::get(Ty, Union.getLower()));
     953          27 :     EndPoints[Size - 1] =
     954          18 :         cast<ConstantInt>(ConstantInt::get(Ty, Union.getUpper()));
     955             :     return true;
     956             :   }
     957             :   return false;
     958             : }
     959             : 
     960          38 : static void addRange(SmallVectorImpl<ConstantInt *> &EndPoints,
     961             :                      ConstantInt *Low, ConstantInt *High) {
     962          38 :   if (!EndPoints.empty())
     963          20 :     if (tryMergeRange(EndPoints, Low, High))
     964             :       return;
     965             : 
     966          31 :   EndPoints.push_back(Low);
     967          31 :   EndPoints.push_back(High);
     968             : }
     969             : 
     970          26 : MDNode *MDNode::getMostGenericRange(MDNode *A, MDNode *B) {
     971             :   // Given two ranges, we want to compute the union of the ranges. This
     972             :   // is slightly complicated by having to combine the intervals and merge
     973             :   // the ones that overlap.
     974             : 
     975          26 :   if (!A || !B)
     976             :     return nullptr;
     977             : 
     978          22 :   if (A == B)
     979             :     return A;
     980             : 
     981             :   // First, walk both lists in order of the lower boundary of each interval.
     982             :   // At each step, try to merge the new interval to the last one we adedd.
     983          18 :   SmallVector<ConstantInt *, 4> EndPoints;
     984          18 :   int AI = 0;
     985          18 :   int BI = 0;
     986          18 :   int AN = A->getNumOperands() / 2;
     987          18 :   int BN = B->getNumOperands() / 2;
     988          38 :   while (AI < AN && BI < BN) {
     989          60 :     ConstantInt *ALow = mdconst::extract<ConstantInt>(A->getOperand(2 * AI));
     990          60 :     ConstantInt *BLow = mdconst::extract<ConstantInt>(B->getOperand(2 * BI));
     991             : 
     992          60 :     if (ALow->getValue().slt(BLow->getValue())) {
     993           0 :       addRange(EndPoints, ALow,
     994           0 :                mdconst::extract<ConstantInt>(A->getOperand(2 * AI + 1)));
     995           0 :       ++AI;
     996             :     } else {
     997          60 :       addRange(EndPoints, BLow,
     998          20 :                mdconst::extract<ConstantInt>(B->getOperand(2 * BI + 1)));
     999          20 :       ++BI;
    1000             :     }
    1001             :   }
    1002          54 :   while (AI < AN) {
    1003          90 :     addRange(EndPoints, mdconst::extract<ConstantInt>(A->getOperand(2 * AI)),
    1004          18 :              mdconst::extract<ConstantInt>(A->getOperand(2 * AI + 1)));
    1005          18 :     ++AI;
    1006             :   }
    1007          18 :   while (BI < BN) {
    1008           0 :     addRange(EndPoints, mdconst::extract<ConstantInt>(B->getOperand(2 * BI)),
    1009           0 :              mdconst::extract<ConstantInt>(B->getOperand(2 * BI + 1)));
    1010           0 :     ++BI;
    1011             :   }
    1012             : 
    1013             :   // If we have more than 2 ranges (4 endpoints) we have to try to merge
    1014             :   // the last and first ones.
    1015          18 :   unsigned Size = EndPoints.size();
    1016          18 :   if (Size > 4) {
    1017           2 :     ConstantInt *FB = EndPoints[0];
    1018           2 :     ConstantInt *FE = EndPoints[1];
    1019           2 :     if (tryMergeRange(EndPoints, FB, FE)) {
    1020          18 :       for (unsigned i = 0; i < Size - 2; ++i) {
    1021          24 :         EndPoints[i] = EndPoints[i + 2];
    1022             :       }
    1023           2 :       EndPoints.resize(Size - 2);
    1024             :     }
    1025             :   }
    1026             : 
    1027             :   // If in the end we have a single range, it is possible that it is now the
    1028             :   // full range. Just drop the metadata in that case.
    1029          18 :   if (EndPoints.size() == 2) {
    1030          61 :     ConstantRange Range(EndPoints[0]->getValue(), EndPoints[1]->getValue());
    1031           7 :     if (Range.isFullSet())
    1032           2 :       return nullptr;
    1033             :   }
    1034             : 
    1035          16 :   SmallVector<Metadata *, 4> MDs;
    1036          16 :   MDs.reserve(EndPoints.size());
    1037         102 :   for (auto *I : EndPoints)
    1038          54 :     MDs.push_back(ConstantAsMetadata::get(I));
    1039          48 :   return MDNode::get(A->getContext(), MDs);
    1040             : }
    1041             : 
    1042           9 : MDNode *MDNode::getMostGenericAlignmentOrDereferenceable(MDNode *A, MDNode *B) {
    1043           9 :   if (!A || !B)
    1044             :     return nullptr;
    1045             : 
    1046          18 :   ConstantInt *AVal = mdconst::extract<ConstantInt>(A->getOperand(0));
    1047          18 :   ConstantInt *BVal = mdconst::extract<ConstantInt>(B->getOperand(0));
    1048          18 :   if (AVal->getZExtValue() < BVal->getZExtValue())
    1049             :     return A;
    1050           9 :   return B;
    1051             : }
    1052             : 
    1053             : //===----------------------------------------------------------------------===//
    1054             : // NamedMDNode implementation.
    1055             : //
    1056             : 
    1057             : static SmallVector<TrackingMDRef, 4> &getNMDOps(void *Operands) {
    1058             :   return *(SmallVector<TrackingMDRef, 4> *)Operands;
    1059             : }
    1060             : 
    1061       18765 : NamedMDNode::NamedMDNode(const Twine &N)
    1062       56295 :     : Name(N.str()), Operands(new SmallVector<TrackingMDRef, 4>()) {}
    1063             : 
    1064       53322 : NamedMDNode::~NamedMDNode() {
    1065       17774 :   dropAllReferences();
    1066       35548 :   delete &getNMDOps(Operands);
    1067       17774 : }
    1068             : 
    1069     1130892 : unsigned NamedMDNode::getNumOperands() const {
    1070     2261784 :   return (unsigned)getNMDOps(Operands).size();
    1071             : }
    1072             : 
    1073     4626791 : MDNode *NamedMDNode::getOperand(unsigned i) const {
    1074             :   assert(i < getNumOperands() && "Invalid Operand number!");
    1075    13880373 :   auto *N = getNMDOps(Operands)[i].get();
    1076     4626791 :   return cast_or_null<MDNode>(N);
    1077             : }
    1078             : 
    1079       32142 : void NamedMDNode::addOperand(MDNode *M) { getNMDOps(Operands).emplace_back(M); }
    1080             : 
    1081         294 : void NamedMDNode::setOperand(unsigned I, MDNode *New) {
    1082             :   assert(I < getNumOperands() && "Invalid operand number");
    1083         588 :   getNMDOps(Operands)[I].reset(New);
    1084         294 : }
    1085             : 
    1086          76 : void NamedMDNode::eraseFromParent() { getParent()->eraseNamedMetadata(this); }
    1087             : 
    1088       17796 : void NamedMDNode::clearOperands() { getNMDOps(Operands).clear(); }
    1089             : 
    1090      168008 : StringRef NamedMDNode::getName() const { return StringRef(Name); }
    1091             : 
    1092             : //===----------------------------------------------------------------------===//
    1093             : // Instruction Metadata method implementations.
    1094             : //
    1095      407304 : void MDAttachmentMap::set(unsigned ID, MDNode &MD) {
    1096     1248810 :   for (auto &I : Attachments)
    1097       57018 :     if (I.first == ID) {
    1098       30120 :       I.second.reset(&MD);
    1099             :       return;
    1100             :     }
    1101      754368 :   Attachments.emplace_back(std::piecewise_construct, std::make_tuple(ID),
    1102     1131552 :                            std::make_tuple(&MD));
    1103             : }
    1104             : 
    1105      178586 : void MDAttachmentMap::erase(unsigned ID) {
    1106      178586 :   if (empty())
    1107             :     return;
    1108             : 
    1109             :   // Common case is one/last value.
    1110      357172 :   if (Attachments.back().first == ID) {
    1111         275 :     Attachments.pop_back();
    1112             :     return;
    1113             :   }
    1114             : 
    1115      366499 :   for (auto I = Attachments.begin(), E = std::prev(Attachments.end()); I != E;
    1116             :        ++I)
    1117        9886 :     if (I->first == ID) {
    1118          27 :       *I = std::move(Attachments.back());
    1119           9 :       Attachments.pop_back();
    1120             :       return;
    1121             :     }
    1122             : }
    1123             : 
    1124    14865025 : MDNode *MDAttachmentMap::lookup(unsigned ID) const {
    1125    56994375 :   for (const auto &I : Attachments)
    1126    15446770 :     if (I.first == ID)
    1127     6094940 :       return I.second;
    1128             :   return nullptr;
    1129             : }
    1130             : 
    1131      693857 : void MDAttachmentMap::getAll(
    1132             :     SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
    1133     2081571 :   Result.append(Attachments.begin(), Attachments.end());
    1134             : 
    1135             :   // Sort the resulting array so it is stable.
    1136     1387714 :   if (Result.size() > 1)
    1137      106378 :     array_pod_sort(Result.begin(), Result.end());
    1138      693857 : }
    1139             : 
    1140       34078 : void MDGlobalAttachmentMap::insert(unsigned ID, MDNode &MD) {
    1141      102234 :   Attachments.push_back({ID, TrackingMDNodeRef(&MD)});
    1142       34078 : }
    1143             : 
    1144     2279389 : void MDGlobalAttachmentMap::get(unsigned ID,
    1145             :                                 SmallVectorImpl<MDNode *> &Result) {
    1146    11445127 :   for (auto A : Attachments)
    1147     2303480 :     if (A.MDKind == ID)
    1148     4029764 :       Result.push_back(A.Node);
    1149     2279389 : }
    1150             : 
    1151        1305 : void MDGlobalAttachmentMap::erase(unsigned ID) {
    1152        2610 :   auto Follower = Attachments.begin();
    1153        5520 :   for (auto Leader = Attachments.begin(), E = Attachments.end(); Leader != E;
    1154             :        ++Leader) {
    1155        2910 :     if (Leader->MDKind != ID) {
    1156        2725 :       if (Follower != Leader)
    1157           7 :         *Follower = std::move(*Leader);
    1158        2725 :       ++Follower;
    1159             :     }
    1160             :   }
    1161        2610 :   Attachments.resize(Follower - Attachments.begin());
    1162        1305 : }
    1163             : 
    1164       15728 : void MDGlobalAttachmentMap::getAll(
    1165             :     SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
    1166       69366 :   for (auto &A : Attachments)
    1167       22182 :     Result.emplace_back(A.MDKind, A.Node);
    1168             : 
    1169             :   // Sort the resulting array so it is stable with respect to metadata IDs. We
    1170             :   // need to preserve the original insertion order though.
    1171       62912 :   std::stable_sort(
    1172             :       Result.begin(), Result.end(),
    1173             :       [](const std::pair<unsigned, MDNode *> &A,
    1174             :          const std::pair<unsigned, MDNode *> &B) { return A.first < B.first; });
    1175       15728 : }
    1176             : 
    1177       20588 : void Instruction::setMetadata(StringRef Kind, MDNode *Node) {
    1178       20588 :   if (!Node && !hasMetadata())
    1179             :     return;
    1180       20132 :   setMetadata(getContext().getMDKindID(Kind), Node);
    1181             : }
    1182             : 
    1183        7537 : MDNode *Instruction::getMetadataImpl(StringRef Kind) const {
    1184        7537 :   return getMetadataImpl(getContext().getMDKindID(Kind));
    1185             : }
    1186             : 
    1187       14774 : void Instruction::dropUnknownNonDebugMetadata(ArrayRef<unsigned> KnownIDs) {
    1188       14774 :   if (!hasMetadataHashEntry())
    1189       11821 :     return; // Nothing to remove!
    1190             : 
    1191        3238 :   auto &InstructionMetadata = getContext().pImpl->InstructionMetadata;
    1192             : 
    1193        6191 :   SmallSet<unsigned, 4> KnownSet;
    1194        6476 :   KnownSet.insert(KnownIDs.begin(), KnownIDs.end());
    1195         285 :   if (KnownSet.empty()) {
    1196             :     // Just drop our entry at the store.
    1197         285 :     InstructionMetadata.erase(this);
    1198         285 :     setHasMetadataHashEntry(false);
    1199         285 :     return;
    1200             :   }
    1201             : 
    1202        5906 :   auto &Info = InstructionMetadata[this];
    1203        2953 :   Info.remove_if([&KnownSet](const std::pair<unsigned, TrackingMDNodeRef> &I) {
    1204        2979 :     return !KnownSet.count(I.first);
    1205             :   });
    1206             : 
    1207        2953 :   if (Info.empty()) {
    1208             :     // Drop our entry at the store.
    1209          96 :     InstructionMetadata.erase(this);
    1210             :     setHasMetadataHashEntry(false);
    1211             :   }
    1212             : }
    1213             : 
    1214     3118105 : void Instruction::setMetadata(unsigned KindID, MDNode *Node) {
    1215     3118105 :   if (!Node && !hasMetadata())
    1216             :     return;
    1217             : 
    1218             :   // Handle 'dbg' as a special case since it is not stored in the hash table.
    1219     2416303 :   if (KindID == LLVMContext::MD_dbg) {
    1220       52449 :     DbgLoc = DebugLoc(Node);
    1221       17483 :     return;
    1222             :   }
    1223             : 
    1224             :   // Handle the case when we're adding/updating metadata on an instruction.
    1225     2398820 :   if (Node) {
    1226      814608 :     auto &Info = getContext().pImpl->InstructionMetadata[this];
    1227             :     assert(!Info.empty() == hasMetadataHashEntry() &&
    1228             :            "HasMetadata bit is wonked");
    1229      407304 :     if (Info.empty())
    1230             :       setHasMetadataHashEntry(true);
    1231      407304 :     Info.set(KindID, *Node);
    1232      407304 :     return;
    1233             :   }
    1234             : 
    1235             :   // Otherwise, we're removing metadata from an instruction.
    1236             :   assert((hasMetadataHashEntry() ==
    1237             :           (getContext().pImpl->InstructionMetadata.count(this) > 0)) &&
    1238             :          "HasMetadata bit out of date!");
    1239     1991516 :   if (!hasMetadataHashEntry())
    1240             :     return; // Nothing to remove!
    1241      357172 :   auto &Info = getContext().pImpl->InstructionMetadata[this];
    1242             : 
    1243             :   // Handle removal of an existing value.
    1244      178586 :   Info.erase(KindID);
    1245             : 
    1246      178586 :   if (!Info.empty())
    1247             :     return;
    1248             : 
    1249         270 :   getContext().pImpl->InstructionMetadata.erase(this);
    1250             :   setHasMetadataHashEntry(false);
    1251             : }
    1252             : 
    1253      152192 : void Instruction::setAAMetadata(const AAMDNodes &N) {
    1254      152192 :   setMetadata(LLVMContext::MD_tbaa, N.TBAA);
    1255      152192 :   setMetadata(LLVMContext::MD_alias_scope, N.Scope);
    1256      152192 :   setMetadata(LLVMContext::MD_noalias, N.NoAlias);
    1257      152192 : }
    1258             : 
    1259    83622362 : MDNode *Instruction::getMetadataImpl(unsigned KindID) const {
    1260             :   // Handle 'dbg' as a special case since it is not stored in the hash table.
    1261    83622362 :   if (KindID == LLVMContext::MD_dbg)
    1262           0 :     return DbgLoc.getAsMDNode();
    1263             : 
    1264    83622362 :   if (!hasMetadataHashEntry())
    1265             :     return nullptr;
    1266    29730050 :   auto &Info = getContext().pImpl->InstructionMetadata[this];
    1267             :   assert(!Info.empty() && "bit out of sync with hash table");
    1268             : 
    1269    14865025 :   return Info.lookup(KindID);
    1270             : }
    1271             : 
    1272     1444926 : void Instruction::getAllMetadataImpl(
    1273             :     SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
    1274     1444926 :   Result.clear();
    1275             : 
    1276             :   // Handle 'dbg' as a special case since it is not stored in the hash table.
    1277     2889852 :   if (DbgLoc) {
    1278     2674460 :     Result.push_back(
    1279     5348920 :         std::make_pair((unsigned)LLVMContext::MD_dbg, DbgLoc.getAsMDNode()));
    1280     1337230 :     if (!hasMetadataHashEntry())
    1281             :       return;
    1282             :   }
    1283             : 
    1284             :   assert(hasMetadataHashEntry() &&
    1285             :          getContext().pImpl->InstructionMetadata.count(this) &&
    1286             :          "Shouldn't have called this");
    1287      196659 :   const auto &Info = getContext().pImpl->InstructionMetadata.find(this)->second;
    1288             :   assert(!Info.empty() && "Shouldn't have called this");
    1289      196659 :   Info.getAll(Result);
    1290             : }
    1291             : 
    1292      497198 : void Instruction::getAllMetadataOtherThanDebugLocImpl(
    1293             :     SmallVectorImpl<std::pair<unsigned, MDNode *>> &Result) const {
    1294      497198 :   Result.clear();
    1295             :   assert(hasMetadataHashEntry() &&
    1296             :          getContext().pImpl->InstructionMetadata.count(this) &&
    1297             :          "Shouldn't have called this");
    1298      497198 :   const auto &Info = getContext().pImpl->InstructionMetadata.find(this)->second;
    1299             :   assert(!Info.empty() && "Shouldn't have called this");
    1300      497198 :   Info.getAll(Result);
    1301      497198 : }
    1302             : 
    1303       50875 : bool Instruction::extractProfMetadata(uint64_t &TrueVal,
    1304             :                                       uint64_t &FalseVal) const {
    1305             :   assert(
    1306             :       (getOpcode() == Instruction::Br || getOpcode() == Instruction::Select) &&
    1307             :       "Looking for branch weights on something besides branch or select");
    1308             : 
    1309       47941 :   auto *ProfileData = getMetadata(LLVMContext::MD_prof);
    1310       47941 :   if (!ProfileData || ProfileData->getNumOperands() != 3)
    1311             :     return false;
    1312             : 
    1313         332 :   auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand(0));
    1314         332 :   if (!ProfDataName || !ProfDataName->getString().equals("branch_weights"))
    1315             :     return false;
    1316             : 
    1317         330 :   auto *CITrue = mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(1));
    1318         330 :   auto *CIFalse = mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(2));
    1319         165 :   if (!CITrue || !CIFalse)
    1320             :     return false;
    1321             : 
    1322         330 :   TrueVal = CITrue->getValue().getZExtValue();
    1323         330 :   FalseVal = CIFalse->getValue().getZExtValue();
    1324             : 
    1325         165 :   return true;
    1326             : }
    1327             : 
    1328        2216 : bool Instruction::extractProfTotalWeight(uint64_t &TotalVal) const {
    1329             :   assert((getOpcode() == Instruction::Br ||
    1330             :           getOpcode() == Instruction::Select ||
    1331             :           getOpcode() == Instruction::Call ||
    1332             :           getOpcode() == Instruction::Invoke ||
    1333             :           getOpcode() == Instruction::Switch) &&
    1334             :          "Looking for branch weights on something besides branch");
    1335             : 
    1336        2216 :   TotalVal = 0;
    1337        1301 :   auto *ProfileData = getMetadata(LLVMContext::MD_prof);
    1338        1301 :   if (!ProfileData)
    1339             :     return false;
    1340             : 
    1341          96 :   auto *ProfDataName = dyn_cast<MDString>(ProfileData->getOperand(0));
    1342             :   if (!ProfDataName)
    1343             :     return false;
    1344             : 
    1345          50 :   if (ProfDataName->getString().equals("branch_weights")) {
    1346          46 :     TotalVal = 0;
    1347         188 :     for (unsigned i = 1; i < ProfileData->getNumOperands(); i++) {
    1348          96 :       auto *V = mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(i));
    1349             :       if (!V)
    1350             :         return false;
    1351          96 :       TotalVal += V->getValue().getZExtValue();
    1352             :     }
    1353             :     return true;
    1354           4 :   } else if (ProfDataName->getString().equals("VP") &&
    1355             :              ProfileData->getNumOperands() > 3) {
    1356           2 :     TotalVal = mdconst::dyn_extract<ConstantInt>(ProfileData->getOperand(2))
    1357           4 :                    ->getValue()
    1358           2 :                    .getZExtValue();
    1359           2 :     return true;
    1360             :   }
    1361             :   return false;
    1362             : }
    1363             : 
    1364      281080 : void Instruction::clearMetadataHashEntries() {
    1365             :   assert(hasMetadataHashEntry() && "Caller should check");
    1366      281080 :   getContext().pImpl->InstructionMetadata.erase(this);
    1367      281080 :   setHasMetadataHashEntry(false);
    1368      281080 : }
    1369             : 
    1370     7635694 : void GlobalObject::getMetadata(unsigned KindID,
    1371             :                                SmallVectorImpl<MDNode *> &MDs) const {
    1372     7635694 :   if (hasMetadata())
    1373     4558778 :     getContext().pImpl->GlobalObjectMetadata[this].get(KindID, MDs);
    1374     7635694 : }
    1375             : 
    1376           0 : void GlobalObject::getMetadata(StringRef Kind,
    1377             :                                SmallVectorImpl<MDNode *> &MDs) const {
    1378           0 :   if (hasMetadata())
    1379           0 :     getMetadata(getContext().getMDKindID(Kind), MDs);
    1380           0 : }
    1381             : 
    1382       34078 : void GlobalObject::addMetadata(unsigned KindID, MDNode &MD) {
    1383       34078 :   if (!hasMetadata())
    1384             :     setHasMetadataHashEntry(true);
    1385             : 
    1386       68156 :   getContext().pImpl->GlobalObjectMetadata[this].insert(KindID, MD);
    1387       34078 : }
    1388             : 
    1389           0 : void GlobalObject::addMetadata(StringRef Kind, MDNode &MD) {
    1390           0 :   addMetadata(getContext().getMDKindID(Kind), MD);
    1391           0 : }
    1392             : 
    1393       29300 : void GlobalObject::eraseMetadata(unsigned KindID) {
    1394             :   // Nothing to unset.
    1395       29300 :   if (!hasMetadata())
    1396             :     return;
    1397             : 
    1398        2610 :   auto &Store = getContext().pImpl->GlobalObjectMetadata[this];
    1399        1305 :   Store.erase(KindID);
    1400        1305 :   if (Store.empty())
    1401         108 :     clearMetadata();
    1402             : }
    1403             : 
    1404     1249814 : void GlobalObject::getAllMetadata(
    1405             :     SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs) const {
    1406     1249814 :   MDs.clear();
    1407             : 
    1408     1249814 :   if (!hasMetadata())
    1409             :     return;
    1410             : 
    1411       31456 :   getContext().pImpl->GlobalObjectMetadata[this].getAll(MDs);
    1412             : }
    1413             : 
    1414      948782 : void GlobalObject::clearMetadata() {
    1415      948782 :   if (!hasMetadata())
    1416             :     return;
    1417       25868 :   getContext().pImpl->GlobalObjectMetadata.erase(this);
    1418             :   setHasMetadataHashEntry(false);
    1419             : }
    1420             : 
    1421       29194 : void GlobalObject::setMetadata(unsigned KindID, MDNode *N) {
    1422       29194 :   eraseMetadata(KindID);
    1423       29194 :   if (N)
    1424       28758 :     addMetadata(KindID, *N);
    1425       29194 : }
    1426             : 
    1427        1251 : void GlobalObject::setMetadata(StringRef Kind, MDNode *N) {
    1428        1251 :   setMetadata(getContext().getMDKindID(Kind), N);
    1429        1251 : }
    1430             : 
    1431     7421609 : MDNode *GlobalObject::getMetadata(unsigned KindID) const {
    1432    14843219 :   SmallVector<MDNode *, 1> MDs;
    1433     7421609 :   getMetadata(KindID, MDs);
    1434             :   assert(MDs.size() <= 1 && "Expected at most one metadata attachment");
    1435     7421610 :   if (MDs.empty())
    1436             :     return nullptr;
    1437     2012480 :   return MDs[0];
    1438             : }
    1439             : 
    1440       21592 : MDNode *GlobalObject::getMetadata(StringRef Kind) const {
    1441       21592 :   return getMetadata(getContext().getMDKindID(Kind));
    1442             : }
    1443             : 
    1444        1926 : void GlobalObject::copyMetadata(const GlobalObject *Other, unsigned Offset) {
    1445        3852 :   SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;
    1446        1926 :   Other->getAllMetadata(MDs);
    1447        5965 :   for (auto &MD : MDs) {
    1448             :     // We need to adjust the type metadata offset.
    1449         187 :     if (Offset != 0 && MD.first == LLVMContext::MD_type) {
    1450          84 :       auto *OffsetConst = cast<ConstantInt>(
    1451          56 :           cast<ConstantAsMetadata>(MD.second->getOperand(0))->getValue());
    1452          84 :       Metadata *TypeId = MD.second->getOperand(1);
    1453          28 :       auto *NewOffsetMD = ConstantAsMetadata::get(ConstantInt::get(
    1454         224 :           OffsetConst->getType(), OffsetConst->getValue() + Offset));
    1455          28 :       addMetadata(LLVMContext::MD_type,
    1456         112 :                   *MDNode::get(getContext(), {NewOffsetMD, TypeId}));
    1457          28 :       continue;
    1458             :     }
    1459             :     // If an offset adjustment was specified we need to modify the DIExpression
    1460             :     // to prepend the adjustment:
    1461             :     // !DIExpression(DW_OP_plus, Offset, [original expr])
    1462         159 :     auto *Attachment = MD.second;
    1463         159 :     if (Offset != 0 && MD.first == LLVMContext::MD_dbg) {
    1464             :       DIGlobalVariable *GV = dyn_cast<DIGlobalVariable>(Attachment);
    1465             :       DIExpression *E = nullptr;
    1466             :       if (!GV) {
    1467           5 :         auto *GVE = cast<DIGlobalVariableExpression>(Attachment);
    1468           5 :         GV = GVE->getVariable();
    1469           5 :         E = GVE->getExpression();
    1470             :       }
    1471           5 :       ArrayRef<uint64_t> OrigElements;
    1472           5 :       if (E)
    1473           5 :         OrigElements = E->getElements();
    1474          15 :       std::vector<uint64_t> Elements(OrigElements.size() + 2);
    1475           5 :       Elements[0] = dwarf::DW_OP_plus_uconst;
    1476           5 :       Elements[1] = Offset;
    1477          20 :       std::copy(OrigElements.begin(), OrigElements.end(), Elements.begin() + 2);
    1478          10 :       E = DIExpression::get(getContext(), Elements);
    1479          10 :       Attachment = DIGlobalVariableExpression::get(getContext(), GV, E);
    1480             :     }
    1481         159 :     addMetadata(MD.first, *Attachment);
    1482             :   }
    1483        1926 : }
    1484             : 
    1485         207 : void GlobalObject::addTypeMetadata(unsigned Offset, Metadata *TypeID) {
    1486         207 :   addMetadata(
    1487             :       LLVMContext::MD_type,
    1488         828 :       *MDTuple::get(getContext(),
    1489         414 :                     {ConstantAsMetadata::get(ConstantInt::get(
    1490             :                          Type::getInt64Ty(getContext()), Offset)),
    1491             :                      TypeID}));
    1492         207 : }
    1493             : 
    1494       27506 : void Function::setSubprogram(DISubprogram *SP) {
    1495       27506 :   setMetadata(LLVMContext::MD_dbg, SP);
    1496       27506 : }
    1497             : 
    1498     4834859 : DISubprogram *Function::getSubprogram() const {
    1499     9669719 :   return cast_or_null<DISubprogram>(getMetadata(LLVMContext::MD_dbg));
    1500             : }
    1501             : 
    1502         755 : bool Function::isDebugInfoForProfiling() const {
    1503         755 :   if (DISubprogram *SP = getSubprogram()) {
    1504         406 :     if (DICompileUnit *CU = SP->getUnit()) {
    1505         406 :       return CU->getDebugInfoForProfiling();
    1506             :     }
    1507             :   }
    1508             :   return false;
    1509             : }
    1510             : 
    1511         508 : void GlobalVariable::addDebugInfo(DIGlobalVariableExpression *GV) {
    1512         508 :   addMetadata(LLVMContext::MD_dbg, *GV);
    1513         508 : }
    1514             : 
    1515       71000 : void GlobalVariable::getDebugInfo(
    1516             :     SmallVectorImpl<DIGlobalVariableExpression *> &GVs) const {
    1517      142000 :   SmallVector<MDNode *, 1> MDs;
    1518       71000 :   getMetadata(LLVMContext::MD_dbg, MDs);
    1519      213361 :   for (MDNode *MD : MDs)
    1520         722 :     GVs.push_back(cast<DIGlobalVariableExpression>(MD));
    1521       71000 : }

Generated by: LCOV version 1.13