1337 lines · cpp
1//===-- Value.cpp - Implement the Value class -----------------------------===//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 Value, ValueHandle, and User classes.10//11//===----------------------------------------------------------------------===//12 13#include "llvm/IR/Value.h"14#include "LLVMContextImpl.h"15#include "llvm/ADT/DenseMap.h"16#include "llvm/ADT/SmallString.h"17#include "llvm/IR/Constant.h"18#include "llvm/IR/Constants.h"19#include "llvm/IR/DataLayout.h"20#include "llvm/IR/DebugInfo.h"21#include "llvm/IR/DerivedTypes.h"22#include "llvm/IR/DerivedUser.h"23#include "llvm/IR/GetElementPtrTypeIterator.h"24#include "llvm/IR/InstrTypes.h"25#include "llvm/IR/Instructions.h"26#include "llvm/IR/IntrinsicInst.h"27#include "llvm/IR/Module.h"28#include "llvm/IR/Operator.h"29#include "llvm/IR/TypedPointerType.h"30#include "llvm/IR/ValueHandle.h"31#include "llvm/IR/ValueSymbolTable.h"32#include "llvm/Support/CommandLine.h"33#include "llvm/Support/ErrorHandling.h"34#include "llvm/Support/raw_ostream.h"35#include <algorithm>36 37using namespace llvm;38 39static cl::opt<bool> UseDerefAtPointSemantics(40 "use-dereferenceable-at-point-semantics", cl::Hidden, cl::init(false),41 cl::desc("Deref attributes and metadata infer facts at definition only"));42 43//===----------------------------------------------------------------------===//44// Value Class45//===----------------------------------------------------------------------===//46static inline Type *checkType(Type *Ty) {47 assert(Ty && "Value defined with a null type: Error!");48 assert(!isa<TypedPointerType>(Ty->getScalarType()) &&49 "Cannot have values with typed pointer types");50 return Ty;51}52 53Value::Value(Type *ty, unsigned scid)54 : SubclassID(scid), HasValueHandle(0), SubclassOptionalData(0),55 SubclassData(0), NumUserOperands(0), IsUsedByMD(false), HasName(false),56 HasMetadata(false), VTy(checkType(ty)) {57 static_assert(ConstantFirstVal == 0, "!(SubclassID < ConstantFirstVal)");58 // FIXME: Why isn't this in the subclass gunk??59 // Note, we cannot call isa<CallInst> before the CallInst has been60 // constructed.61 unsigned OpCode = 0;62 if (SubclassID >= InstructionVal)63 OpCode = SubclassID - InstructionVal;64 if (OpCode == Instruction::Call || OpCode == Instruction::Invoke ||65 OpCode == Instruction::CallBr)66 assert((VTy->isFirstClassType() || VTy->isVoidTy() || VTy->isStructTy()) &&67 "invalid CallBase type!");68 else if (SubclassID != BasicBlockVal &&69 (/*SubclassID < ConstantFirstVal ||*/ SubclassID > ConstantLastVal))70 assert((VTy->isFirstClassType() || VTy->isVoidTy()) &&71 "Cannot create non-first-class values except for constants!");72 static_assert(sizeof(Value) == 2 * sizeof(void *) + 2 * sizeof(unsigned),73 "Value too big");74}75 76Value::~Value() {77 // Notify all ValueHandles (if present) that this value is going away.78 if (HasValueHandle)79 ValueHandleBase::ValueIsDeleted(this);80 if (isUsedByMetadata())81 ValueAsMetadata::handleDeletion(this);82 83 // Remove associated metadata from context.84 if (HasMetadata)85 clearMetadata();86 87#ifndef NDEBUG // Only in -g mode...88 // Check to make sure that there are no uses of this value that are still89 // around when the value is destroyed. If there are, then we have a dangling90 // reference and something is wrong. This code is here to print out where91 // the value is still being referenced.92 //93 // Note that use_empty() cannot be called here, as it eventually downcasts94 // 'this' to GlobalValue (derived class of Value), but GlobalValue has already95 // been destructed, so accessing it is UB.96 //97 if (!materialized_use_empty()) {98 dbgs() << "While deleting: " << *VTy << " %" << getName() << "\n";99 for (auto *U : users())100 dbgs() << "Use still stuck around after Def is destroyed:" << *U << "\n";101 102 llvm_unreachable("Uses remain when a value is destroyed!");103 }104#endif105 106 // If this value is named, destroy the name. This should not be in a symtab107 // at this point.108 destroyValueName();109}110 111void Value::deleteValue() {112 switch (getValueID()) {113#define HANDLE_VALUE(Name) \114 case Value::Name##Val: \115 delete static_cast<Name *>(this); \116 break;117#define HANDLE_MEMORY_VALUE(Name) \118 case Value::Name##Val: \119 static_cast<DerivedUser *>(this)->DeleteValue( \120 static_cast<DerivedUser *>(this)); \121 break;122#define HANDLE_CONSTANT(Name) \123 case Value::Name##Val: \124 llvm_unreachable("constants should be destroyed with destroyConstant"); \125 break;126#define HANDLE_INSTRUCTION(Name) /* nothing */127#include "llvm/IR/Value.def"128 129#define HANDLE_INST(N, OPC, CLASS) \130 case Value::InstructionVal + Instruction::OPC: \131 delete static_cast<CLASS *>(this); \132 break;133#define HANDLE_USER_INST(N, OPC, CLASS)134#include "llvm/IR/Instruction.def"135 136 default:137 llvm_unreachable("attempting to delete unknown value kind");138 }139}140 141void Value::destroyValueName() {142 ValueName *Name = getValueName();143 if (Name) {144 MallocAllocator Allocator;145 Name->Destroy(Allocator);146 }147 setValueName(nullptr);148}149 150bool Value::hasNUses(unsigned N) const {151 if (!UseList)152 return N == 0;153 154 // TODO: Disallow for ConstantData and remove !UseList check?155 return hasNItems(use_begin(), use_end(), N);156}157 158bool Value::hasNUsesOrMore(unsigned N) const {159 // TODO: Disallow for ConstantData and remove !UseList check?160 if (!UseList)161 return N == 0;162 163 return hasNItemsOrMore(use_begin(), use_end(), N);164}165 166bool Value::hasOneUser() const {167 if (use_empty())168 return false;169 if (hasOneUse())170 return true;171 return std::equal(++user_begin(), user_end(), user_begin());172}173 174static bool isUnDroppableUser(const User *U) { return !U->isDroppable(); }175 176Use *Value::getSingleUndroppableUse() {177 Use *Result = nullptr;178 for (Use &U : uses()) {179 if (!U.getUser()->isDroppable()) {180 if (Result)181 return nullptr;182 Result = &U;183 }184 }185 return Result;186}187 188User *Value::getUniqueUndroppableUser() {189 User *Result = nullptr;190 for (auto *U : users()) {191 if (!U->isDroppable()) {192 if (Result && Result != U)193 return nullptr;194 Result = U;195 }196 }197 return Result;198}199 200bool Value::hasNUndroppableUses(unsigned int N) const {201 return hasNItems(user_begin(), user_end(), N, isUnDroppableUser);202}203 204bool Value::hasNUndroppableUsesOrMore(unsigned int N) const {205 return hasNItemsOrMore(user_begin(), user_end(), N, isUnDroppableUser);206}207 208void Value::dropDroppableUses(209 llvm::function_ref<bool(const Use *)> ShouldDrop) {210 SmallVector<Use *, 8> ToBeEdited;211 for (Use &U : uses())212 if (U.getUser()->isDroppable() && ShouldDrop(&U))213 ToBeEdited.push_back(&U);214 for (Use *U : ToBeEdited)215 dropDroppableUse(*U);216}217 218void Value::dropDroppableUsesIn(User &Usr) {219 assert(Usr.isDroppable() && "Expected a droppable user!");220 for (Use &UsrOp : Usr.operands()) {221 if (UsrOp.get() == this)222 dropDroppableUse(UsrOp);223 }224}225 226void Value::dropDroppableUse(Use &U) {227 if (auto *Assume = dyn_cast<AssumeInst>(U.getUser())) {228 unsigned OpNo = U.getOperandNo();229 if (OpNo == 0)230 U.set(ConstantInt::getTrue(Assume->getContext()));231 else {232 U.set(PoisonValue::get(U.get()->getType()));233 CallInst::BundleOpInfo &BOI = Assume->getBundleOpInfoForOperand(OpNo);234 BOI.Tag = Assume->getContext().pImpl->getOrInsertBundleTag("ignore");235 }236 return;237 }238 239 llvm_unreachable("unknown droppable use");240}241 242bool Value::isUsedInBasicBlock(const BasicBlock *BB) const {243 assert(hasUseList() && "ConstantData has no use-list");244 245 // This can be computed either by scanning the instructions in BB, or by246 // scanning the use list of this Value. Both lists can be very long, but247 // usually one is quite short.248 //249 // Scan both lists simultaneously until one is exhausted. This limits the250 // search to the shorter list.251 BasicBlock::const_iterator BI = BB->begin(), BE = BB->end();252 const_user_iterator UI = user_begin(), UE = user_end();253 for (; BI != BE && UI != UE; ++BI, ++UI) {254 // Scan basic block: Check if this Value is used by the instruction at BI.255 if (is_contained(BI->operands(), this))256 return true;257 // Scan use list: Check if the use at UI is in BB.258 const auto *User = dyn_cast<Instruction>(*UI);259 if (User && User->getParent() == BB)260 return true;261 }262 return false;263}264 265unsigned Value::getNumUses() const {266 // TODO: Disallow for ConstantData and remove !UseList check?267 if (!UseList)268 return 0;269 return (unsigned)std::distance(use_begin(), use_end());270}271 272static bool getSymTab(Value *V, ValueSymbolTable *&ST) {273 ST = nullptr;274 if (Instruction *I = dyn_cast<Instruction>(V)) {275 if (BasicBlock *P = I->getParent())276 if (Function *PP = P->getParent())277 ST = PP->getValueSymbolTable();278 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {279 if (Function *P = BB->getParent())280 ST = P->getValueSymbolTable();281 } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {282 if (Module *P = GV->getParent())283 ST = &P->getValueSymbolTable();284 } else if (Argument *A = dyn_cast<Argument>(V)) {285 if (Function *P = A->getParent())286 ST = P->getValueSymbolTable();287 } else {288 assert(isa<Constant>(V) && "Unknown value type!");289 return true; // no name is setable for this.290 }291 return false;292}293 294ValueName *Value::getValueName() const {295 if (!HasName) return nullptr;296 297 LLVMContext &Ctx = getContext();298 auto I = Ctx.pImpl->ValueNames.find(this);299 assert(I != Ctx.pImpl->ValueNames.end() &&300 "No name entry found!");301 302 return I->second;303}304 305void Value::setValueName(ValueName *VN) {306 LLVMContext &Ctx = getContext();307 308 assert(HasName == Ctx.pImpl->ValueNames.count(this) &&309 "HasName bit out of sync!");310 311 if (!VN) {312 if (HasName)313 Ctx.pImpl->ValueNames.erase(this);314 HasName = false;315 return;316 }317 318 HasName = true;319 Ctx.pImpl->ValueNames[this] = VN;320}321 322StringRef Value::getName() const {323 // Make sure the empty string is still a C string. For historical reasons,324 // some clients want to call .data() on the result and expect it to be null325 // terminated.326 if (!hasName())327 return StringRef("", 0);328 return getValueName()->getKey();329}330 331void Value::setNameImpl(const Twine &NewName) {332 bool NeedNewName =333 !getContext().shouldDiscardValueNames() || isa<GlobalValue>(this);334 335 // Fast-path: LLVMContext can be set to strip out non-GlobalValue names336 // and there is no need to delete the old name.337 if (!NeedNewName && !hasName())338 return;339 340 // Fast path for common IRBuilder case of setName("") when there is no name.341 if (NewName.isTriviallyEmpty() && !hasName())342 return;343 344 SmallString<256> NameData;345 StringRef NameRef = NeedNewName ? NewName.toStringRef(NameData) : "";346 assert(!NameRef.contains(0) && "Null bytes are not allowed in names");347 348 // Name isn't changing?349 if (getName() == NameRef)350 return;351 352 assert(!getType()->isVoidTy() && "Cannot assign a name to void values!");353 354 // Get the symbol table to update for this object.355 ValueSymbolTable *ST;356 if (getSymTab(this, ST))357 return; // Cannot set a name on this value (e.g. constant).358 359 if (!ST) { // No symbol table to update? Just do the change.360 // NOTE: Could optimize for the case the name is shrinking to not deallocate361 // then reallocated.362 destroyValueName();363 364 if (!NameRef.empty()) {365 // Create the new name.366 assert(NeedNewName);367 MallocAllocator Allocator;368 setValueName(ValueName::create(NameRef, Allocator));369 getValueName()->setValue(this);370 }371 return;372 }373 374 // NOTE: Could optimize for the case the name is shrinking to not deallocate375 // then reallocated.376 if (hasName()) {377 // Remove old name.378 ST->removeValueName(getValueName());379 destroyValueName();380 381 if (NameRef.empty())382 return;383 }384 385 // Name is changing to something new.386 assert(NeedNewName);387 setValueName(ST->createValueName(NameRef, this));388}389 390void Value::setName(const Twine &NewName) {391 setNameImpl(NewName);392 if (Function *F = dyn_cast<Function>(this))393 F->updateAfterNameChange();394}395 396void Value::takeName(Value *V) {397 assert(V != this && "Illegal call to this->takeName(this)!");398 ValueSymbolTable *ST = nullptr;399 // If this value has a name, drop it.400 if (hasName()) {401 // Get the symtab this is in.402 if (getSymTab(this, ST)) {403 // We can't set a name on this value, but we need to clear V's name if404 // it has one.405 if (V->hasName()) V->setName("");406 return; // Cannot set a name on this value (e.g. constant).407 }408 409 // Remove old name.410 if (ST)411 ST->removeValueName(getValueName());412 destroyValueName();413 }414 415 // Now we know that this has no name.416 417 // If V has no name either, we're done.418 if (!V->hasName()) return;419 420 // Get this's symtab if we didn't before.421 if (!ST) {422 if (getSymTab(this, ST)) {423 // Clear V's name.424 V->setName("");425 return; // Cannot set a name on this value (e.g. constant).426 }427 }428 429 // Get V's ST, this should always succeed, because V has a name.430 ValueSymbolTable *VST;431 bool Failure = getSymTab(V, VST);432 assert(!Failure && "V has a name, so it should have a ST!"); (void)Failure;433 434 // If these values are both in the same symtab, we can do this very fast.435 // This works even if both values have no symtab yet.436 if (ST == VST) {437 // Take the name!438 setValueName(V->getValueName());439 V->setValueName(nullptr);440 getValueName()->setValue(this);441 return;442 }443 444 // Otherwise, things are slightly more complex. Remove V's name from VST and445 // then reinsert it into ST.446 447 if (VST)448 VST->removeValueName(V->getValueName());449 setValueName(V->getValueName());450 V->setValueName(nullptr);451 getValueName()->setValue(this);452 453 if (ST)454 ST->reinsertValue(this);455}456 457std::string Value::getNameOrAsOperand() const {458 if (!getName().empty())459 return std::string(getName());460 461 std::string BBName;462 raw_string_ostream OS(BBName);463 printAsOperand(OS, false);464 return OS.str();465}466 467void Value::assertModuleIsMaterializedImpl() const {468#ifndef NDEBUG469 const GlobalValue *GV = dyn_cast<GlobalValue>(this);470 if (!GV)471 return;472 const Module *M = GV->getParent();473 if (!M)474 return;475 assert(M->isMaterialized());476#endif477}478 479#ifndef NDEBUG480static bool contains(SmallPtrSetImpl<ConstantExpr *> &Cache, ConstantExpr *Expr,481 Constant *C) {482 if (!Cache.insert(Expr).second)483 return false;484 485 for (auto &O : Expr->operands()) {486 if (O == C)487 return true;488 auto *CE = dyn_cast<ConstantExpr>(O);489 if (!CE)490 continue;491 if (contains(Cache, CE, C))492 return true;493 }494 return false;495}496 497static bool contains(Value *Expr, Value *V) {498 if (Expr == V)499 return true;500 501 auto *C = dyn_cast<Constant>(V);502 if (!C)503 return false;504 505 auto *CE = dyn_cast<ConstantExpr>(Expr);506 if (!CE)507 return false;508 509 SmallPtrSet<ConstantExpr *, 4> Cache;510 return contains(Cache, CE, C);511}512#endif // NDEBUG513 514void Value::doRAUW(Value *New, ReplaceMetadataUses ReplaceMetaUses) {515 assert(hasUseList() && "Cannot replace constant data");516 assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");517 assert(!contains(New, this) &&518 "this->replaceAllUsesWith(expr(this)) is NOT valid!");519 assert(New->getType() == getType() &&520 "replaceAllUses of value with new value of different type!");521 522 // Notify all ValueHandles (if present) that this value is going away.523 if (HasValueHandle)524 ValueHandleBase::ValueIsRAUWd(this, New);525 if (ReplaceMetaUses == ReplaceMetadataUses::Yes && isUsedByMetadata())526 ValueAsMetadata::handleRAUW(this, New);527 528 while (!materialized_use_empty()) {529 Use &U = *UseList;530 // Must handle Constants specially, we cannot call replaceUsesOfWith on a531 // constant because they are uniqued.532 if (auto *C = dyn_cast<Constant>(U.getUser())) {533 if (!isa<GlobalValue>(C)) {534 C->handleOperandChange(this, New);535 continue;536 }537 }538 539 U.set(New);540 }541 542 if (BasicBlock *BB = dyn_cast<BasicBlock>(this))543 BB->replaceSuccessorsPhiUsesWith(cast<BasicBlock>(New));544}545 546void Value::replaceAllUsesWith(Value *New) {547 doRAUW(New, ReplaceMetadataUses::Yes);548}549 550void Value::replaceNonMetadataUsesWith(Value *New) {551 doRAUW(New, ReplaceMetadataUses::No);552}553 554void Value::replaceUsesWithIf(Value *New,555 llvm::function_ref<bool(Use &U)> ShouldReplace) {556 assert(New && "Value::replaceUsesWithIf(<null>) is invalid!");557 assert(New->getType() == getType() &&558 "replaceUses of value with new value of different type!");559 560 SmallVector<TrackingVH<Constant>, 8> Consts;561 SmallPtrSet<Constant *, 8> Visited;562 563 for (Use &U : llvm::make_early_inc_range(uses())) {564 if (!ShouldReplace(U))565 continue;566 // Must handle Constants specially, we cannot call replaceUsesOfWith on a567 // constant because they are uniqued.568 if (auto *C = dyn_cast<Constant>(U.getUser())) {569 if (!isa<GlobalValue>(C)) {570 if (Visited.insert(C).second)571 Consts.push_back(TrackingVH<Constant>(C));572 continue;573 }574 }575 U.set(New);576 }577 578 while (!Consts.empty()) {579 // FIXME: handleOperandChange() updates all the uses in a given Constant,580 // not just the one passed to ShouldReplace581 Consts.pop_back_val()->handleOperandChange(this, New);582 }583}584 585/// Replace debug record uses of MetadataAsValue(ValueAsMetadata(V)) outside BB586/// with New.587static void replaceDbgUsesOutsideBlock(Value *V, Value *New, BasicBlock *BB) {588 SmallVector<DbgVariableRecord *> DPUsers;589 findDbgUsers(V, DPUsers);590 for (auto *DVR : DPUsers) {591 DbgMarker *Marker = DVR->getMarker();592 if (Marker->getParent() != BB)593 DVR->replaceVariableLocationOp(V, New);594 }595}596 597// Like replaceAllUsesWith except it does not handle constants or basic blocks.598// This routine leaves uses within BB.599void Value::replaceUsesOutsideBlock(Value *New, BasicBlock *BB) {600 assert(New && "Value::replaceUsesOutsideBlock(<null>, BB) is invalid!");601 assert(!contains(New, this) &&602 "this->replaceUsesOutsideBlock(expr(this), BB) is NOT valid!");603 assert(New->getType() == getType() &&604 "replaceUses of value with new value of different type!");605 assert(BB && "Basic block that may contain a use of 'New' must be defined\n");606 607 replaceDbgUsesOutsideBlock(this, New, BB);608 replaceUsesWithIf(New, [BB](Use &U) {609 auto *I = dyn_cast<Instruction>(U.getUser());610 // Don't replace if it's an instruction in the BB basic block.611 return !I || I->getParent() != BB;612 });613}614 615namespace {616// Various metrics for how much to strip off of pointers.617enum PointerStripKind {618 PSK_ZeroIndices,619 PSK_ZeroIndicesAndAliases,620 PSK_ZeroIndicesSameRepresentation,621 PSK_ForAliasAnalysis,622 PSK_InBoundsConstantIndices,623 PSK_InBounds624};625} // end anonymous namespace626 627template <PointerStripKind StripKind> static void NoopCallback(const Value *) {}628 629template <PointerStripKind StripKind>630static const Value *stripPointerCastsAndOffsets(631 const Value *V,632 function_ref<void(const Value *)> Func = NoopCallback<StripKind>) {633 if (!V->getType()->isPointerTy())634 return V;635 636 // Even though we don't look through PHI nodes, we could be called on an637 // instruction in an unreachable block, which may be on a cycle.638 SmallPtrSet<const Value *, 4> Visited;639 640 Visited.insert(V);641 do {642 Func(V);643 if (auto *GEP = dyn_cast<GEPOperator>(V)) {644 switch (StripKind) {645 case PSK_ZeroIndices:646 case PSK_ZeroIndicesAndAliases:647 case PSK_ZeroIndicesSameRepresentation:648 case PSK_ForAliasAnalysis:649 if (!GEP->hasAllZeroIndices())650 return V;651 break;652 case PSK_InBoundsConstantIndices:653 if (!GEP->hasAllConstantIndices())654 return V;655 [[fallthrough]];656 case PSK_InBounds:657 if (!GEP->isInBounds())658 return V;659 break;660 }661 V = GEP->getPointerOperand();662 } else if (Operator::getOpcode(V) == Instruction::BitCast) {663 Value *NewV = cast<Operator>(V)->getOperand(0);664 if (!NewV->getType()->isPointerTy())665 return V;666 V = NewV;667 } else if (StripKind != PSK_ZeroIndicesSameRepresentation &&668 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {669 // TODO: If we know an address space cast will not change the670 // representation we could look through it here as well.671 V = cast<Operator>(V)->getOperand(0);672 } else if (StripKind == PSK_ZeroIndicesAndAliases && isa<GlobalAlias>(V)) {673 V = cast<GlobalAlias>(V)->getAliasee();674 } else if (StripKind == PSK_ForAliasAnalysis && isa<PHINode>(V) &&675 cast<PHINode>(V)->getNumIncomingValues() == 1) {676 V = cast<PHINode>(V)->getIncomingValue(0);677 } else {678 if (const auto *Call = dyn_cast<CallBase>(V)) {679 if (const Value *RV = Call->getReturnedArgOperand()) {680 V = RV;681 continue;682 }683 // The result of launder.invariant.group must alias it's argument,684 // but it can't be marked with returned attribute, that's why it needs685 // special case.686 if (StripKind == PSK_ForAliasAnalysis &&687 (Call->getIntrinsicID() == Intrinsic::launder_invariant_group ||688 Call->getIntrinsicID() == Intrinsic::strip_invariant_group)) {689 V = Call->getArgOperand(0);690 continue;691 }692 }693 return V;694 }695 assert(V->getType()->isPointerTy() && "Unexpected operand type!");696 } while (Visited.insert(V).second);697 698 return V;699}700 701const Value *Value::stripPointerCasts() const {702 return stripPointerCastsAndOffsets<PSK_ZeroIndices>(this);703}704 705const Value *Value::stripPointerCastsAndAliases() const {706 return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndAliases>(this);707}708 709const Value *Value::stripPointerCastsSameRepresentation() const {710 return stripPointerCastsAndOffsets<PSK_ZeroIndicesSameRepresentation>(this);711}712 713const Value *Value::stripInBoundsConstantOffsets() const {714 return stripPointerCastsAndOffsets<PSK_InBoundsConstantIndices>(this);715}716 717const Value *Value::stripPointerCastsForAliasAnalysis() const {718 return stripPointerCastsAndOffsets<PSK_ForAliasAnalysis>(this);719}720 721const Value *Value::stripAndAccumulateConstantOffsets(722 const DataLayout &DL, APInt &Offset, bool AllowNonInbounds,723 bool AllowInvariantGroup,724 function_ref<bool(Value &, APInt &)> ExternalAnalysis,725 bool LookThroughIntToPtr) const {726 if (!getType()->isPtrOrPtrVectorTy())727 return this;728 729 unsigned BitWidth = Offset.getBitWidth();730 assert(BitWidth == DL.getIndexTypeSizeInBits(getType()) &&731 "The offset bit width does not match the DL specification.");732 733 // Even though we don't look through PHI nodes, we could be called on an734 // instruction in an unreachable block, which may be on a cycle.735 SmallPtrSet<const Value *, 4> Visited;736 Visited.insert(this);737 const Value *V = this;738 do {739 if (auto *GEP = dyn_cast<GEPOperator>(V)) {740 // If in-bounds was requested, we do not strip non-in-bounds GEPs.741 if (!AllowNonInbounds && !GEP->isInBounds())742 return V;743 744 // If one of the values we have visited is an addrspacecast, then745 // the pointer type of this GEP may be different from the type746 // of the Ptr parameter which was passed to this function. This747 // means when we construct GEPOffset, we need to use the size748 // of GEP's pointer type rather than the size of the original749 // pointer type.750 APInt GEPOffset(DL.getIndexTypeSizeInBits(V->getType()), 0);751 if (!GEP->accumulateConstantOffset(DL, GEPOffset, ExternalAnalysis))752 return V;753 754 // Stop traversal if the pointer offset wouldn't fit in the bit-width755 // provided by the Offset argument. This can happen due to AddrSpaceCast756 // stripping.757 if (GEPOffset.getSignificantBits() > BitWidth)758 return V;759 760 // External Analysis can return a result higher/lower than the value761 // represents. We need to detect overflow/underflow.762 APInt GEPOffsetST = GEPOffset.sextOrTrunc(BitWidth);763 if (!ExternalAnalysis) {764 Offset += GEPOffsetST;765 } else {766 bool Overflow = false;767 APInt OldOffset = Offset;768 Offset = Offset.sadd_ov(GEPOffsetST, Overflow);769 if (Overflow) {770 Offset = OldOffset;771 return V;772 }773 }774 V = GEP->getPointerOperand();775 } else if (Operator::getOpcode(V) == Instruction::BitCast ||776 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {777 V = cast<Operator>(V)->getOperand(0);778 } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {779 if (!GA->isInterposable())780 V = GA->getAliasee();781 } else if (const auto *Call = dyn_cast<CallBase>(V)) {782 if (const Value *RV = Call->getReturnedArgOperand())783 V = RV;784 if (AllowInvariantGroup && Call->isLaunderOrStripInvariantGroup())785 V = Call->getArgOperand(0);786 } else if (auto *Int2Ptr = dyn_cast<Operator>(V)) {787 // Try to accumulate across (inttoptr (add (ptrtoint p), off)).788 if (!AllowNonInbounds || !LookThroughIntToPtr || !Int2Ptr ||789 Int2Ptr->getOpcode() != Instruction::IntToPtr ||790 Int2Ptr->getOperand(0)->getType()->getScalarSizeInBits() != BitWidth)791 return V;792 793 auto *Add = dyn_cast<AddOperator>(Int2Ptr->getOperand(0));794 if (!Add)795 return V;796 797 auto *Ptr2Int = dyn_cast<PtrToIntOperator>(Add->getOperand(0));798 auto *CI = dyn_cast<ConstantInt>(Add->getOperand(1));799 if (!Ptr2Int || !CI)800 return V;801 802 Offset += CI->getValue();803 V = Ptr2Int->getOperand(0);804 }805 assert(V->getType()->isPtrOrPtrVectorTy() && "Unexpected operand type!");806 } while (Visited.insert(V).second);807 808 return V;809}810 811const Value *812Value::stripInBoundsOffsets(function_ref<void(const Value *)> Func) const {813 return stripPointerCastsAndOffsets<PSK_InBounds>(this, Func);814}815 816bool Value::canBeFreed() const {817 assert(getType()->isPointerTy());818 819 // Cases that can simply never be deallocated820 // *) Constants aren't allocated per se, thus not deallocated either.821 if (isa<Constant>(this))822 return false;823 824 // Handle byval/byref/sret/inalloca/preallocated arguments. The storage825 // lifetime is guaranteed to be longer than the callee's lifetime.826 if (auto *A = dyn_cast<Argument>(this)) {827 if (A->hasPointeeInMemoryValueAttr())828 return false;829 // A pointer to an object in a function which neither frees, nor can arrange830 // for another thread to free on its behalf, can not be freed in the scope831 // of the function. Note that this logic is restricted to memory832 // allocations in existance before the call; a nofree function *is* allowed833 // to free memory it allocated.834 const Function *F = A->getParent();835 if (F->doesNotFreeMemory() && F->hasNoSync())836 return false;837 }838 839 if (isa<IntToPtrInst>(this) && getMetadata(LLVMContext::MD_nofree))840 return false;841 842 const Function *F = nullptr;843 if (auto *I = dyn_cast<Instruction>(this))844 F = I->getFunction();845 if (auto *A = dyn_cast<Argument>(this))846 F = A->getParent();847 848 if (!F)849 return true;850 851 // With garbage collection, deallocation typically occurs solely at or after852 // safepoints. If we're compiling for a collector which uses the853 // gc.statepoint infrastructure, safepoints aren't explicitly present854 // in the IR until after lowering from abstract to physical machine model.855 // The collector could chose to mix explicit deallocation and gc'd objects856 // which is why we need the explicit opt in on a per collector basis.857 if (!F->hasGC())858 return true;859 860 const auto &GCName = F->getGC();861 if (GCName == "statepoint-example") {862 auto *PT = cast<PointerType>(this->getType());863 if (PT->getAddressSpace() != 1)864 // For the sake of this example GC, we arbitrarily pick addrspace(1) as865 // our GC managed heap. This must match the same check in866 // RewriteStatepointsForGC (and probably needs better factored.)867 return true;868 869 // It is cheaper to scan for a declaration than to scan for a use in this870 // function. Note that gc.statepoint is a type overloaded function so the871 // usual trick of requesting declaration of the intrinsic from the module872 // doesn't work.873 for (auto &Fn : *F->getParent())874 if (Fn.getIntrinsicID() == Intrinsic::experimental_gc_statepoint)875 return true;876 return false;877 }878 return true;879}880 881uint64_t Value::getPointerDereferenceableBytes(const DataLayout &DL,882 bool &CanBeNull,883 bool &CanBeFreed) const {884 assert(getType()->isPointerTy() && "must be pointer");885 886 uint64_t DerefBytes = 0;887 CanBeNull = false;888 CanBeFreed = UseDerefAtPointSemantics && canBeFreed();889 if (const Argument *A = dyn_cast<Argument>(this)) {890 DerefBytes = A->getDereferenceableBytes();891 if (DerefBytes == 0) {892 // Handle byval/byref/inalloca/preallocated arguments893 if (Type *ArgMemTy = A->getPointeeInMemoryValueType()) {894 if (ArgMemTy->isSized()) {895 // FIXME: Why isn't this the type alloc size?896 DerefBytes = DL.getTypeStoreSize(ArgMemTy).getKnownMinValue();897 }898 }899 }900 901 if (DerefBytes == 0) {902 DerefBytes = A->getDereferenceableOrNullBytes();903 CanBeNull = true;904 }905 } else if (const auto *Call = dyn_cast<CallBase>(this)) {906 DerefBytes = Call->getRetDereferenceableBytes();907 if (DerefBytes == 0) {908 DerefBytes = Call->getRetDereferenceableOrNullBytes();909 CanBeNull = true;910 }911 } else if (const LoadInst *LI = dyn_cast<LoadInst>(this)) {912 if (MDNode *MD = LI->getMetadata(LLVMContext::MD_dereferenceable)) {913 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));914 DerefBytes = CI->getLimitedValue();915 }916 if (DerefBytes == 0) {917 if (MDNode *MD =918 LI->getMetadata(LLVMContext::MD_dereferenceable_or_null)) {919 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));920 DerefBytes = CI->getLimitedValue();921 }922 CanBeNull = true;923 }924 } else if (auto *IP = dyn_cast<IntToPtrInst>(this)) {925 if (MDNode *MD = IP->getMetadata(LLVMContext::MD_dereferenceable)) {926 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));927 DerefBytes = CI->getLimitedValue();928 }929 if (DerefBytes == 0) {930 if (MDNode *MD =931 IP->getMetadata(LLVMContext::MD_dereferenceable_or_null)) {932 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));933 DerefBytes = CI->getLimitedValue();934 }935 CanBeNull = true;936 }937 } else if (auto *AI = dyn_cast<AllocaInst>(this)) {938 if (!AI->isArrayAllocation()) {939 DerefBytes =940 DL.getTypeStoreSize(AI->getAllocatedType()).getKnownMinValue();941 CanBeNull = false;942 CanBeFreed = false;943 }944 } else if (auto *GV = dyn_cast<GlobalVariable>(this)) {945 if (GV->getValueType()->isSized() && !GV->hasExternalWeakLinkage()) {946 // TODO: Don't outright reject hasExternalWeakLinkage but set the947 // CanBeNull flag.948 DerefBytes = DL.getTypeStoreSize(GV->getValueType()).getFixedValue();949 CanBeNull = false;950 CanBeFreed = false;951 }952 }953 return DerefBytes;954}955 956Align Value::getPointerAlignment(const DataLayout &DL) const {957 assert(getType()->isPointerTy() && "must be pointer");958 if (const Function *F = dyn_cast<Function>(this)) {959 Align FunctionPtrAlign = DL.getFunctionPtrAlign().valueOrOne();960 switch (DL.getFunctionPtrAlignType()) {961 case DataLayout::FunctionPtrAlignType::Independent:962 return FunctionPtrAlign;963 case DataLayout::FunctionPtrAlignType::MultipleOfFunctionAlign:964 return std::max(FunctionPtrAlign, F->getAlign().valueOrOne());965 }966 llvm_unreachable("Unhandled FunctionPtrAlignType");967 } else if (auto *GVar = dyn_cast<GlobalVariable>(this)) {968 const MaybeAlign Alignment(GVar->getAlign());969 if (!Alignment) {970 Type *ObjectType = GVar->getValueType();971 if (ObjectType->isSized()) {972 // If the object is defined in the current Module, we'll be giving973 // it the preferred alignment. Otherwise, we have to assume that it974 // may only have the minimum ABI alignment.975 if (GVar->isStrongDefinitionForLinker())976 return DL.getPreferredAlign(GVar);977 else978 return DL.getABITypeAlign(ObjectType);979 }980 }981 return Alignment.valueOrOne();982 } else if (const Argument *A = dyn_cast<Argument>(this)) {983 const MaybeAlign Alignment = A->getParamAlign();984 if (!Alignment && A->hasStructRetAttr()) {985 // An sret parameter has at least the ABI alignment of the return type.986 Type *EltTy = A->getParamStructRetType();987 if (EltTy->isSized())988 return DL.getABITypeAlign(EltTy);989 }990 return Alignment.valueOrOne();991 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(this)) {992 return AI->getAlign();993 } else if (const auto *Call = dyn_cast<CallBase>(this)) {994 MaybeAlign Alignment = Call->getRetAlign();995 if (!Alignment && Call->getCalledFunction())996 Alignment = Call->getCalledFunction()->getAttributes().getRetAlignment();997 return Alignment.valueOrOne();998 } else if (const LoadInst *LI = dyn_cast<LoadInst>(this)) {999 if (MDNode *MD = LI->getMetadata(LLVMContext::MD_align)) {1000 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));1001 return Align(CI->getLimitedValue());1002 }1003 } else if (auto *CE = dyn_cast<ConstantExpr>(this)) {1004 // Determine the alignment of inttoptr(C).1005 if (CE->getOpcode() == Instruction::IntToPtr &&1006 isa<ConstantInt>(CE->getOperand(0))) {1007 ConstantInt *IntPtr = cast<ConstantInt>(CE->getOperand(0));1008 size_t TrailingZeros = IntPtr->getValue().countr_zero();1009 // While the actual alignment may be large, elsewhere we have1010 // an arbitrary upper alignmet limit, so let's clamp to it.1011 return Align(TrailingZeros < Value::MaxAlignmentExponent1012 ? uint64_t(1) << TrailingZeros1013 : Value::MaximumAlignment);1014 }1015 }1016 return Align(1);1017}1018 1019static std::optional<int64_t>1020getOffsetFromIndex(const GEPOperator *GEP, unsigned Idx, const DataLayout &DL) {1021 // Skip over the first indices.1022 gep_type_iterator GTI = gep_type_begin(GEP);1023 for (unsigned i = 1; i != Idx; ++i, ++GTI)1024 /*skip along*/;1025 1026 // Compute the offset implied by the rest of the indices.1027 int64_t Offset = 0;1028 for (unsigned i = Idx, e = GEP->getNumOperands(); i != e; ++i, ++GTI) {1029 ConstantInt *OpC = dyn_cast<ConstantInt>(GEP->getOperand(i));1030 if (!OpC)1031 return std::nullopt;1032 if (OpC->isZero())1033 continue; // No offset.1034 1035 // Handle struct indices, which add their field offset to the pointer.1036 if (StructType *STy = GTI.getStructTypeOrNull()) {1037 Offset += DL.getStructLayout(STy)->getElementOffset(OpC->getZExtValue());1038 continue;1039 }1040 1041 // Otherwise, we have a sequential type like an array or fixed-length1042 // vector. Multiply the index by the ElementSize.1043 TypeSize Size = GTI.getSequentialElementStride(DL);1044 if (Size.isScalable())1045 return std::nullopt;1046 Offset += Size.getFixedValue() * OpC->getSExtValue();1047 }1048 1049 return Offset;1050}1051 1052std::optional<int64_t> Value::getPointerOffsetFrom(const Value *Other,1053 const DataLayout &DL) const {1054 const Value *Ptr1 = Other;1055 const Value *Ptr2 = this;1056 APInt Offset1(DL.getIndexTypeSizeInBits(Ptr1->getType()), 0);1057 APInt Offset2(DL.getIndexTypeSizeInBits(Ptr2->getType()), 0);1058 Ptr1 = Ptr1->stripAndAccumulateConstantOffsets(DL, Offset1, true);1059 Ptr2 = Ptr2->stripAndAccumulateConstantOffsets(DL, Offset2, true);1060 1061 // Handle the trivial case first.1062 if (Ptr1 == Ptr2)1063 return Offset2.getSExtValue() - Offset1.getSExtValue();1064 1065 const GEPOperator *GEP1 = dyn_cast<GEPOperator>(Ptr1);1066 const GEPOperator *GEP2 = dyn_cast<GEPOperator>(Ptr2);1067 1068 // Right now we handle the case when Ptr1/Ptr2 are both GEPs with an identical1069 // base. After that base, they may have some number of common (and1070 // potentially variable) indices. After that they handle some constant1071 // offset, which determines their offset from each other. At this point, we1072 // handle no other case.1073 if (!GEP1 || !GEP2 || GEP1->getOperand(0) != GEP2->getOperand(0) ||1074 GEP1->getSourceElementType() != GEP2->getSourceElementType())1075 return std::nullopt;1076 1077 // Skip any common indices and track the GEP types.1078 unsigned Idx = 1;1079 for (; Idx != GEP1->getNumOperands() && Idx != GEP2->getNumOperands(); ++Idx)1080 if (GEP1->getOperand(Idx) != GEP2->getOperand(Idx))1081 break;1082 1083 auto IOffset1 = getOffsetFromIndex(GEP1, Idx, DL);1084 auto IOffset2 = getOffsetFromIndex(GEP2, Idx, DL);1085 if (!IOffset1 || !IOffset2)1086 return std::nullopt;1087 return *IOffset2 - *IOffset1 + Offset2.getSExtValue() -1088 Offset1.getSExtValue();1089}1090 1091const Value *Value::DoPHITranslation(const BasicBlock *CurBB,1092 const BasicBlock *PredBB) const {1093 auto *PN = dyn_cast<PHINode>(this);1094 if (PN && PN->getParent() == CurBB)1095 return PN->getIncomingValueForBlock(PredBB);1096 return this;1097}1098 1099LLVMContext &Value::getContext() const { return VTy->getContext(); }1100 1101void Value::reverseUseList() {1102 if (!UseList || !UseList->Next)1103 // No need to reverse 0 or 1 uses.1104 return;1105 1106 Use *Head = UseList;1107 Use *Current = UseList->Next;1108 Head->Next = nullptr;1109 while (Current) {1110 Use *Next = Current->Next;1111 Current->Next = Head;1112 Head->Prev = &Current->Next;1113 Head = Current;1114 Current = Next;1115 }1116 UseList = Head;1117 Head->Prev = &UseList;1118}1119 1120bool Value::isSwiftError() const {1121 auto *Arg = dyn_cast<Argument>(this);1122 if (Arg)1123 return Arg->hasSwiftErrorAttr();1124 auto *Alloca = dyn_cast<AllocaInst>(this);1125 if (!Alloca)1126 return false;1127 return Alloca->isSwiftError();1128}1129 1130//===----------------------------------------------------------------------===//1131// ValueHandleBase Class1132//===----------------------------------------------------------------------===//1133 1134void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) {1135 assert(List && "Handle list is null?");1136 1137 // Splice ourselves into the list.1138 Next = *List;1139 *List = this;1140 setPrevPtr(List);1141 if (Next) {1142 Next->setPrevPtr(&Next);1143 assert(getValPtr() == Next->getValPtr() && "Added to wrong list?");1144 }1145}1146 1147void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) {1148 assert(List && "Must insert after existing node");1149 1150 Next = List->Next;1151 setPrevPtr(&List->Next);1152 List->Next = this;1153 if (Next)1154 Next->setPrevPtr(&Next);1155}1156 1157void ValueHandleBase::AddToUseList() {1158 assert(getValPtr() && "Null pointer doesn't have a use list!");1159 1160 LLVMContextImpl *pImpl = getValPtr()->getContext().pImpl;1161 1162 if (getValPtr()->HasValueHandle) {1163 // If this value already has a ValueHandle, then it must be in the1164 // ValueHandles map already.1165 ValueHandleBase *&Entry = pImpl->ValueHandles[getValPtr()];1166 assert(Entry && "Value doesn't have any handles?");1167 AddToExistingUseList(&Entry);1168 return;1169 }1170 1171 // Ok, it doesn't have any handles yet, so we must insert it into the1172 // DenseMap. However, doing this insertion could cause the DenseMap to1173 // reallocate itself, which would invalidate all of the PrevP pointers that1174 // point into the old table. Handle this by checking for reallocation and1175 // updating the stale pointers only if needed.1176 DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;1177 const void *OldBucketPtr = Handles.getPointerIntoBucketsArray();1178 1179 ValueHandleBase *&Entry = Handles[getValPtr()];1180 assert(!Entry && "Value really did already have handles?");1181 AddToExistingUseList(&Entry);1182 getValPtr()->HasValueHandle = true;1183 1184 // If reallocation didn't happen or if this was the first insertion, don't1185 // walk the table.1186 if (Handles.isPointerIntoBucketsArray(OldBucketPtr) ||1187 Handles.size() == 1) {1188 return;1189 }1190 1191 // Okay, reallocation did happen. Fix the Prev Pointers.1192 for (DenseMap<Value*, ValueHandleBase*>::iterator I = Handles.begin(),1193 E = Handles.end(); I != E; ++I) {1194 assert(I->second && I->first == I->second->getValPtr() &&1195 "List invariant broken!");1196 I->second->setPrevPtr(&I->second);1197 }1198}1199 1200void ValueHandleBase::RemoveFromUseList() {1201 assert(getValPtr() && getValPtr()->HasValueHandle &&1202 "Pointer doesn't have a use list!");1203 1204 // Unlink this from its use list.1205 ValueHandleBase **PrevPtr = getPrevPtr();1206 assert(*PrevPtr == this && "List invariant broken");1207 1208 *PrevPtr = Next;1209 if (Next) {1210 assert(Next->getPrevPtr() == &Next && "List invariant broken");1211 Next->setPrevPtr(PrevPtr);1212 return;1213 }1214 1215 // If the Next pointer was null, then it is possible that this was the last1216 // ValueHandle watching VP. If so, delete its entry from the ValueHandles1217 // map.1218 LLVMContextImpl *pImpl = getValPtr()->getContext().pImpl;1219 DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;1220 if (Handles.isPointerIntoBucketsArray(PrevPtr)) {1221 Handles.erase(getValPtr());1222 getValPtr()->HasValueHandle = false;1223 }1224}1225 1226void ValueHandleBase::ValueIsDeleted(Value *V) {1227 assert(V->HasValueHandle && "Should only be called if ValueHandles present");1228 1229 // Get the linked list base, which is guaranteed to exist since the1230 // HasValueHandle flag is set.1231 LLVMContextImpl *pImpl = V->getContext().pImpl;1232 ValueHandleBase *Entry = pImpl->ValueHandles[V];1233 assert(Entry && "Value bit set but no entries exist");1234 1235 // We use a local ValueHandleBase as an iterator so that ValueHandles can add1236 // and remove themselves from the list without breaking our iteration. This1237 // is not really an AssertingVH; we just have to give ValueHandleBase a kind.1238 // Note that we deliberately do not the support the case when dropping a value1239 // handle results in a new value handle being permanently added to the list1240 // (as might occur in theory for CallbackVH's): the new value handle will not1241 // be processed and the checking code will mete out righteous punishment if1242 // the handle is still present once we have finished processing all the other1243 // value handles (it is fine to momentarily add then remove a value handle).1244 for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {1245 Iterator.RemoveFromUseList();1246 Iterator.AddToExistingUseListAfter(Entry);1247 assert(Entry->Next == &Iterator && "Loop invariant broken.");1248 1249 switch (Entry->getKind()) {1250 case Assert:1251 break;1252 case Weak:1253 case WeakTracking:1254 // WeakTracking and Weak just go to null, which unlinks them1255 // from the list.1256 Entry->operator=(nullptr);1257 break;1258 case Callback:1259 // Forward to the subclass's implementation.1260 static_cast<CallbackVH*>(Entry)->deleted();1261 break;1262 }1263 }1264 1265 // All callbacks, weak references, and assertingVHs should be dropped by now.1266 if (V->HasValueHandle) {1267#ifndef NDEBUG // Only in +Asserts mode...1268 dbgs() << "While deleting: " << *V->getType() << " %" << V->getName()1269 << "\n";1270 if (pImpl->ValueHandles[V]->getKind() == Assert)1271 llvm_unreachable("An asserting value handle still pointed to this"1272 " value!");1273 1274#endif1275 llvm_unreachable("All references to V were not removed?");1276 }1277}1278 1279void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) {1280 assert(Old->HasValueHandle &&"Should only be called if ValueHandles present");1281 assert(Old != New && "Changing value into itself!");1282 assert(Old->getType() == New->getType() &&1283 "replaceAllUses of value with new value of different type!");1284 1285 // Get the linked list base, which is guaranteed to exist since the1286 // HasValueHandle flag is set.1287 LLVMContextImpl *pImpl = Old->getContext().pImpl;1288 ValueHandleBase *Entry = pImpl->ValueHandles[Old];1289 1290 assert(Entry && "Value bit set but no entries exist");1291 1292 // We use a local ValueHandleBase as an iterator so that1293 // ValueHandles can add and remove themselves from the list without1294 // breaking our iteration. This is not really an AssertingVH; we1295 // just have to give ValueHandleBase some kind.1296 for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {1297 Iterator.RemoveFromUseList();1298 Iterator.AddToExistingUseListAfter(Entry);1299 assert(Entry->Next == &Iterator && "Loop invariant broken.");1300 1301 switch (Entry->getKind()) {1302 case Assert:1303 case Weak:1304 // Asserting and Weak handles do not follow RAUW implicitly.1305 break;1306 case WeakTracking:1307 // Weak goes to the new value, which will unlink it from Old's list.1308 Entry->operator=(New);1309 break;1310 case Callback:1311 // Forward to the subclass's implementation.1312 static_cast<CallbackVH*>(Entry)->allUsesReplacedWith(New);1313 break;1314 }1315 }1316 1317#ifndef NDEBUG1318 // If any new weak value handles were added while processing the1319 // list, then complain about it now.1320 if (Old->HasValueHandle)1321 for (Entry = pImpl->ValueHandles[Old]; Entry; Entry = Entry->Next)1322 switch (Entry->getKind()) {1323 case WeakTracking:1324 dbgs() << "After RAUW from " << *Old->getType() << " %"1325 << Old->getName() << " to " << *New->getType() << " %"1326 << New->getName() << "\n";1327 llvm_unreachable(1328 "A weak tracking value handle still pointed to the old value!\n");1329 default:1330 break;1331 }1332#endif1333}1334 1335// Pin the vtable to this file.1336void CallbackVH::anchor() {}1337