brintos

brintos / llvm-project-archived public Read only

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