10b57cec5SDimitry Andric //===-- Value.cpp - Implement the Value class -----------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file implements the Value, ValueHandle, and User classes. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "llvm/IR/Value.h" 140b57cec5SDimitry Andric #include "LLVMContextImpl.h" 150b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 160b57cec5SDimitry Andric #include "llvm/ADT/SetVector.h" 17480093f4SDimitry Andric #include "llvm/ADT/SmallString.h" 180b57cec5SDimitry Andric #include "llvm/IR/Constant.h" 190b57cec5SDimitry Andric #include "llvm/IR/Constants.h" 200b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h" 210b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h" 220b57cec5SDimitry Andric #include "llvm/IR/DerivedUser.h" 230b57cec5SDimitry Andric #include "llvm/IR/GetElementPtrTypeIterator.h" 240b57cec5SDimitry Andric #include "llvm/IR/InstrTypes.h" 250b57cec5SDimitry Andric #include "llvm/IR/Instructions.h" 260b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h" 270b57cec5SDimitry Andric #include "llvm/IR/Module.h" 280b57cec5SDimitry Andric #include "llvm/IR/Operator.h" 290b57cec5SDimitry Andric #include "llvm/IR/Statepoint.h" 300b57cec5SDimitry Andric #include "llvm/IR/ValueHandle.h" 310b57cec5SDimitry Andric #include "llvm/IR/ValueSymbolTable.h" 32480093f4SDimitry Andric #include "llvm/Support/CommandLine.h" 330b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 340b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 350b57cec5SDimitry Andric #include "llvm/Support/ManagedStatic.h" 360b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 370b57cec5SDimitry Andric #include <algorithm> 380b57cec5SDimitry Andric 390b57cec5SDimitry Andric using namespace llvm; 400b57cec5SDimitry Andric 410b57cec5SDimitry Andric static cl::opt<unsigned> NonGlobalValueMaxNameSize( 420b57cec5SDimitry Andric "non-global-value-max-name-size", cl::Hidden, cl::init(1024), 430b57cec5SDimitry Andric cl::desc("Maximum size for the name of non-global values.")); 440b57cec5SDimitry Andric 450b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 460b57cec5SDimitry Andric // Value Class 470b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 480b57cec5SDimitry Andric static inline Type *checkType(Type *Ty) { 490b57cec5SDimitry Andric assert(Ty && "Value defined with a null type: Error!"); 500b57cec5SDimitry Andric return Ty; 510b57cec5SDimitry Andric } 520b57cec5SDimitry Andric 530b57cec5SDimitry Andric Value::Value(Type *ty, unsigned scid) 540b57cec5SDimitry Andric : VTy(checkType(ty)), UseList(nullptr), SubclassID(scid), 550b57cec5SDimitry Andric HasValueHandle(0), SubclassOptionalData(0), SubclassData(0), 560b57cec5SDimitry Andric NumUserOperands(0), IsUsedByMD(false), HasName(false) { 570b57cec5SDimitry Andric static_assert(ConstantFirstVal == 0, "!(SubclassID < ConstantFirstVal)"); 580b57cec5SDimitry Andric // FIXME: Why isn't this in the subclass gunk?? 590b57cec5SDimitry Andric // Note, we cannot call isa<CallInst> before the CallInst has been 600b57cec5SDimitry Andric // constructed. 610b57cec5SDimitry Andric if (SubclassID == Instruction::Call || SubclassID == Instruction::Invoke || 620b57cec5SDimitry Andric SubclassID == Instruction::CallBr) 630b57cec5SDimitry Andric assert((VTy->isFirstClassType() || VTy->isVoidTy() || VTy->isStructTy()) && 640b57cec5SDimitry Andric "invalid CallInst type!"); 650b57cec5SDimitry Andric else if (SubclassID != BasicBlockVal && 660b57cec5SDimitry Andric (/*SubclassID < ConstantFirstVal ||*/ SubclassID > ConstantLastVal)) 670b57cec5SDimitry Andric assert((VTy->isFirstClassType() || VTy->isVoidTy()) && 680b57cec5SDimitry Andric "Cannot create non-first-class values except for constants!"); 690b57cec5SDimitry Andric static_assert(sizeof(Value) == 2 * sizeof(void *) + 2 * sizeof(unsigned), 700b57cec5SDimitry Andric "Value too big"); 710b57cec5SDimitry Andric } 720b57cec5SDimitry Andric 730b57cec5SDimitry Andric Value::~Value() { 740b57cec5SDimitry Andric // Notify all ValueHandles (if present) that this value is going away. 750b57cec5SDimitry Andric if (HasValueHandle) 760b57cec5SDimitry Andric ValueHandleBase::ValueIsDeleted(this); 770b57cec5SDimitry Andric if (isUsedByMetadata()) 780b57cec5SDimitry Andric ValueAsMetadata::handleDeletion(this); 790b57cec5SDimitry Andric 800b57cec5SDimitry Andric #ifndef NDEBUG // Only in -g mode... 810b57cec5SDimitry Andric // Check to make sure that there are no uses of this value that are still 820b57cec5SDimitry Andric // around when the value is destroyed. If there are, then we have a dangling 830b57cec5SDimitry Andric // reference and something is wrong. This code is here to print out where 840b57cec5SDimitry Andric // the value is still being referenced. 850b57cec5SDimitry Andric // 86*5ffd83dbSDimitry Andric // Note that use_empty() cannot be called here, as it eventually downcasts 87*5ffd83dbSDimitry Andric // 'this' to GlobalValue (derived class of Value), but GlobalValue has already 88*5ffd83dbSDimitry Andric // been destructed, so accessing it is UB. 89*5ffd83dbSDimitry Andric // 90*5ffd83dbSDimitry Andric if (!materialized_use_empty()) { 910b57cec5SDimitry Andric dbgs() << "While deleting: " << *VTy << " %" << getName() << "\n"; 920b57cec5SDimitry Andric for (auto *U : users()) 930b57cec5SDimitry Andric dbgs() << "Use still stuck around after Def is destroyed:" << *U << "\n"; 940b57cec5SDimitry Andric } 950b57cec5SDimitry Andric #endif 96*5ffd83dbSDimitry Andric assert(materialized_use_empty() && "Uses remain when a value is destroyed!"); 970b57cec5SDimitry Andric 980b57cec5SDimitry Andric // If this value is named, destroy the name. This should not be in a symtab 990b57cec5SDimitry Andric // at this point. 1000b57cec5SDimitry Andric destroyValueName(); 1010b57cec5SDimitry Andric } 1020b57cec5SDimitry Andric 1030b57cec5SDimitry Andric void Value::deleteValue() { 1040b57cec5SDimitry Andric switch (getValueID()) { 1050b57cec5SDimitry Andric #define HANDLE_VALUE(Name) \ 1060b57cec5SDimitry Andric case Value::Name##Val: \ 1070b57cec5SDimitry Andric delete static_cast<Name *>(this); \ 1080b57cec5SDimitry Andric break; 1090b57cec5SDimitry Andric #define HANDLE_MEMORY_VALUE(Name) \ 1100b57cec5SDimitry Andric case Value::Name##Val: \ 1110b57cec5SDimitry Andric static_cast<DerivedUser *>(this)->DeleteValue( \ 1120b57cec5SDimitry Andric static_cast<DerivedUser *>(this)); \ 1130b57cec5SDimitry Andric break; 114*5ffd83dbSDimitry Andric #define HANDLE_CONSTANT(Name) \ 115*5ffd83dbSDimitry Andric case Value::Name##Val: \ 116*5ffd83dbSDimitry Andric llvm_unreachable("constants should be destroyed with destroyConstant"); \ 117*5ffd83dbSDimitry Andric break; 1180b57cec5SDimitry Andric #define HANDLE_INSTRUCTION(Name) /* nothing */ 1190b57cec5SDimitry Andric #include "llvm/IR/Value.def" 1200b57cec5SDimitry Andric 1210b57cec5SDimitry Andric #define HANDLE_INST(N, OPC, CLASS) \ 1220b57cec5SDimitry Andric case Value::InstructionVal + Instruction::OPC: \ 1230b57cec5SDimitry Andric delete static_cast<CLASS *>(this); \ 1240b57cec5SDimitry Andric break; 1250b57cec5SDimitry Andric #define HANDLE_USER_INST(N, OPC, CLASS) 1260b57cec5SDimitry Andric #include "llvm/IR/Instruction.def" 1270b57cec5SDimitry Andric 1280b57cec5SDimitry Andric default: 1290b57cec5SDimitry Andric llvm_unreachable("attempting to delete unknown value kind"); 1300b57cec5SDimitry Andric } 1310b57cec5SDimitry Andric } 1320b57cec5SDimitry Andric 1330b57cec5SDimitry Andric void Value::destroyValueName() { 1340b57cec5SDimitry Andric ValueName *Name = getValueName(); 135*5ffd83dbSDimitry Andric if (Name) { 136*5ffd83dbSDimitry Andric MallocAllocator Allocator; 137*5ffd83dbSDimitry Andric Name->Destroy(Allocator); 138*5ffd83dbSDimitry Andric } 1390b57cec5SDimitry Andric setValueName(nullptr); 1400b57cec5SDimitry Andric } 1410b57cec5SDimitry Andric 1420b57cec5SDimitry Andric bool Value::hasNUses(unsigned N) const { 1430b57cec5SDimitry Andric return hasNItems(use_begin(), use_end(), N); 1440b57cec5SDimitry Andric } 1450b57cec5SDimitry Andric 1460b57cec5SDimitry Andric bool Value::hasNUsesOrMore(unsigned N) const { 1470b57cec5SDimitry Andric return hasNItemsOrMore(use_begin(), use_end(), N); 1480b57cec5SDimitry Andric } 1490b57cec5SDimitry Andric 150*5ffd83dbSDimitry Andric static bool isUnDroppableUser(const User *U) { return !U->isDroppable(); } 151*5ffd83dbSDimitry Andric 152*5ffd83dbSDimitry Andric Use *Value::getSingleUndroppableUse() { 153*5ffd83dbSDimitry Andric Use *Result = nullptr; 154*5ffd83dbSDimitry Andric for (Use &U : uses()) { 155*5ffd83dbSDimitry Andric if (!U.getUser()->isDroppable()) { 156*5ffd83dbSDimitry Andric if (Result) 157*5ffd83dbSDimitry Andric return nullptr; 158*5ffd83dbSDimitry Andric Result = &U; 159*5ffd83dbSDimitry Andric } 160*5ffd83dbSDimitry Andric } 161*5ffd83dbSDimitry Andric return Result; 162*5ffd83dbSDimitry Andric } 163*5ffd83dbSDimitry Andric 164*5ffd83dbSDimitry Andric bool Value::hasNUndroppableUses(unsigned int N) const { 165*5ffd83dbSDimitry Andric return hasNItems(user_begin(), user_end(), N, isUnDroppableUser); 166*5ffd83dbSDimitry Andric } 167*5ffd83dbSDimitry Andric 168*5ffd83dbSDimitry Andric bool Value::hasNUndroppableUsesOrMore(unsigned int N) const { 169*5ffd83dbSDimitry Andric return hasNItemsOrMore(user_begin(), user_end(), N, isUnDroppableUser); 170*5ffd83dbSDimitry Andric } 171*5ffd83dbSDimitry Andric 172*5ffd83dbSDimitry Andric void Value::dropDroppableUses( 173*5ffd83dbSDimitry Andric llvm::function_ref<bool(const Use *)> ShouldDrop) { 174*5ffd83dbSDimitry Andric SmallVector<Use *, 8> ToBeEdited; 175*5ffd83dbSDimitry Andric for (Use &U : uses()) 176*5ffd83dbSDimitry Andric if (U.getUser()->isDroppable() && ShouldDrop(&U)) 177*5ffd83dbSDimitry Andric ToBeEdited.push_back(&U); 178*5ffd83dbSDimitry Andric for (Use *U : ToBeEdited) { 179*5ffd83dbSDimitry Andric U->removeFromList(); 180*5ffd83dbSDimitry Andric if (auto *Assume = dyn_cast<IntrinsicInst>(U->getUser())) { 181*5ffd83dbSDimitry Andric assert(Assume->getIntrinsicID() == Intrinsic::assume); 182*5ffd83dbSDimitry Andric unsigned OpNo = U->getOperandNo(); 183*5ffd83dbSDimitry Andric if (OpNo == 0) 184*5ffd83dbSDimitry Andric Assume->setOperand(0, ConstantInt::getTrue(Assume->getContext())); 185*5ffd83dbSDimitry Andric else { 186*5ffd83dbSDimitry Andric Assume->setOperand(OpNo, UndefValue::get(U->get()->getType())); 187*5ffd83dbSDimitry Andric CallInst::BundleOpInfo &BOI = Assume->getBundleOpInfoForOperand(OpNo); 188*5ffd83dbSDimitry Andric BOI.Tag = getContext().pImpl->getOrInsertBundleTag("ignore"); 189*5ffd83dbSDimitry Andric } 190*5ffd83dbSDimitry Andric } else 191*5ffd83dbSDimitry Andric llvm_unreachable("unkown droppable use"); 192*5ffd83dbSDimitry Andric } 193*5ffd83dbSDimitry Andric } 194*5ffd83dbSDimitry Andric 1950b57cec5SDimitry Andric bool Value::isUsedInBasicBlock(const BasicBlock *BB) const { 1960b57cec5SDimitry Andric // This can be computed either by scanning the instructions in BB, or by 1970b57cec5SDimitry Andric // scanning the use list of this Value. Both lists can be very long, but 1980b57cec5SDimitry Andric // usually one is quite short. 1990b57cec5SDimitry Andric // 2000b57cec5SDimitry Andric // Scan both lists simultaneously until one is exhausted. This limits the 2010b57cec5SDimitry Andric // search to the shorter list. 2020b57cec5SDimitry Andric BasicBlock::const_iterator BI = BB->begin(), BE = BB->end(); 2030b57cec5SDimitry Andric const_user_iterator UI = user_begin(), UE = user_end(); 2040b57cec5SDimitry Andric for (; BI != BE && UI != UE; ++BI, ++UI) { 2050b57cec5SDimitry Andric // Scan basic block: Check if this Value is used by the instruction at BI. 2060b57cec5SDimitry Andric if (is_contained(BI->operands(), this)) 2070b57cec5SDimitry Andric return true; 2080b57cec5SDimitry Andric // Scan use list: Check if the use at UI is in BB. 2090b57cec5SDimitry Andric const auto *User = dyn_cast<Instruction>(*UI); 2100b57cec5SDimitry Andric if (User && User->getParent() == BB) 2110b57cec5SDimitry Andric return true; 2120b57cec5SDimitry Andric } 2130b57cec5SDimitry Andric return false; 2140b57cec5SDimitry Andric } 2150b57cec5SDimitry Andric 2160b57cec5SDimitry Andric unsigned Value::getNumUses() const { 2170b57cec5SDimitry Andric return (unsigned)std::distance(use_begin(), use_end()); 2180b57cec5SDimitry Andric } 2190b57cec5SDimitry Andric 2200b57cec5SDimitry Andric static bool getSymTab(Value *V, ValueSymbolTable *&ST) { 2210b57cec5SDimitry Andric ST = nullptr; 2220b57cec5SDimitry Andric if (Instruction *I = dyn_cast<Instruction>(V)) { 2230b57cec5SDimitry Andric if (BasicBlock *P = I->getParent()) 2240b57cec5SDimitry Andric if (Function *PP = P->getParent()) 2250b57cec5SDimitry Andric ST = PP->getValueSymbolTable(); 2260b57cec5SDimitry Andric } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) { 2270b57cec5SDimitry Andric if (Function *P = BB->getParent()) 2280b57cec5SDimitry Andric ST = P->getValueSymbolTable(); 2290b57cec5SDimitry Andric } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { 2300b57cec5SDimitry Andric if (Module *P = GV->getParent()) 2310b57cec5SDimitry Andric ST = &P->getValueSymbolTable(); 2320b57cec5SDimitry Andric } else if (Argument *A = dyn_cast<Argument>(V)) { 2330b57cec5SDimitry Andric if (Function *P = A->getParent()) 2340b57cec5SDimitry Andric ST = P->getValueSymbolTable(); 2350b57cec5SDimitry Andric } else { 2360b57cec5SDimitry Andric assert(isa<Constant>(V) && "Unknown value type!"); 2370b57cec5SDimitry Andric return true; // no name is setable for this. 2380b57cec5SDimitry Andric } 2390b57cec5SDimitry Andric return false; 2400b57cec5SDimitry Andric } 2410b57cec5SDimitry Andric 2420b57cec5SDimitry Andric ValueName *Value::getValueName() const { 2430b57cec5SDimitry Andric if (!HasName) return nullptr; 2440b57cec5SDimitry Andric 2450b57cec5SDimitry Andric LLVMContext &Ctx = getContext(); 2460b57cec5SDimitry Andric auto I = Ctx.pImpl->ValueNames.find(this); 2470b57cec5SDimitry Andric assert(I != Ctx.pImpl->ValueNames.end() && 2480b57cec5SDimitry Andric "No name entry found!"); 2490b57cec5SDimitry Andric 2500b57cec5SDimitry Andric return I->second; 2510b57cec5SDimitry Andric } 2520b57cec5SDimitry Andric 2530b57cec5SDimitry Andric void Value::setValueName(ValueName *VN) { 2540b57cec5SDimitry Andric LLVMContext &Ctx = getContext(); 2550b57cec5SDimitry Andric 2560b57cec5SDimitry Andric assert(HasName == Ctx.pImpl->ValueNames.count(this) && 2570b57cec5SDimitry Andric "HasName bit out of sync!"); 2580b57cec5SDimitry Andric 2590b57cec5SDimitry Andric if (!VN) { 2600b57cec5SDimitry Andric if (HasName) 2610b57cec5SDimitry Andric Ctx.pImpl->ValueNames.erase(this); 2620b57cec5SDimitry Andric HasName = false; 2630b57cec5SDimitry Andric return; 2640b57cec5SDimitry Andric } 2650b57cec5SDimitry Andric 2660b57cec5SDimitry Andric HasName = true; 2670b57cec5SDimitry Andric Ctx.pImpl->ValueNames[this] = VN; 2680b57cec5SDimitry Andric } 2690b57cec5SDimitry Andric 2700b57cec5SDimitry Andric StringRef Value::getName() const { 2710b57cec5SDimitry Andric // Make sure the empty string is still a C string. For historical reasons, 2720b57cec5SDimitry Andric // some clients want to call .data() on the result and expect it to be null 2730b57cec5SDimitry Andric // terminated. 2740b57cec5SDimitry Andric if (!hasName()) 2750b57cec5SDimitry Andric return StringRef("", 0); 2760b57cec5SDimitry Andric return getValueName()->getKey(); 2770b57cec5SDimitry Andric } 2780b57cec5SDimitry Andric 2790b57cec5SDimitry Andric void Value::setNameImpl(const Twine &NewName) { 2800b57cec5SDimitry Andric // Fast-path: LLVMContext can be set to strip out non-GlobalValue names 2810b57cec5SDimitry Andric if (getContext().shouldDiscardValueNames() && !isa<GlobalValue>(this)) 2820b57cec5SDimitry Andric return; 2830b57cec5SDimitry Andric 2840b57cec5SDimitry Andric // Fast path for common IRBuilder case of setName("") when there is no name. 2850b57cec5SDimitry Andric if (NewName.isTriviallyEmpty() && !hasName()) 2860b57cec5SDimitry Andric return; 2870b57cec5SDimitry Andric 2880b57cec5SDimitry Andric SmallString<256> NameData; 2890b57cec5SDimitry Andric StringRef NameRef = NewName.toStringRef(NameData); 2900b57cec5SDimitry Andric assert(NameRef.find_first_of(0) == StringRef::npos && 2910b57cec5SDimitry Andric "Null bytes are not allowed in names"); 2920b57cec5SDimitry Andric 2930b57cec5SDimitry Andric // Name isn't changing? 2940b57cec5SDimitry Andric if (getName() == NameRef) 2950b57cec5SDimitry Andric return; 2960b57cec5SDimitry Andric 2970b57cec5SDimitry Andric // Cap the size of non-GlobalValue names. 2980b57cec5SDimitry Andric if (NameRef.size() > NonGlobalValueMaxNameSize && !isa<GlobalValue>(this)) 2990b57cec5SDimitry Andric NameRef = 3000b57cec5SDimitry Andric NameRef.substr(0, std::max(1u, (unsigned)NonGlobalValueMaxNameSize)); 3010b57cec5SDimitry Andric 3020b57cec5SDimitry Andric assert(!getType()->isVoidTy() && "Cannot assign a name to void values!"); 3030b57cec5SDimitry Andric 3040b57cec5SDimitry Andric // Get the symbol table to update for this object. 3050b57cec5SDimitry Andric ValueSymbolTable *ST; 3060b57cec5SDimitry Andric if (getSymTab(this, ST)) 3070b57cec5SDimitry Andric return; // Cannot set a name on this value (e.g. constant). 3080b57cec5SDimitry Andric 3090b57cec5SDimitry Andric if (!ST) { // No symbol table to update? Just do the change. 3100b57cec5SDimitry Andric if (NameRef.empty()) { 3110b57cec5SDimitry Andric // Free the name for this value. 3120b57cec5SDimitry Andric destroyValueName(); 3130b57cec5SDimitry Andric return; 3140b57cec5SDimitry Andric } 3150b57cec5SDimitry Andric 3160b57cec5SDimitry Andric // NOTE: Could optimize for the case the name is shrinking to not deallocate 3170b57cec5SDimitry Andric // then reallocated. 3180b57cec5SDimitry Andric destroyValueName(); 3190b57cec5SDimitry Andric 3200b57cec5SDimitry Andric // Create the new name. 321*5ffd83dbSDimitry Andric MallocAllocator Allocator; 322*5ffd83dbSDimitry Andric setValueName(ValueName::Create(NameRef, Allocator)); 3230b57cec5SDimitry Andric getValueName()->setValue(this); 3240b57cec5SDimitry Andric return; 3250b57cec5SDimitry Andric } 3260b57cec5SDimitry Andric 3270b57cec5SDimitry Andric // NOTE: Could optimize for the case the name is shrinking to not deallocate 3280b57cec5SDimitry Andric // then reallocated. 3290b57cec5SDimitry Andric if (hasName()) { 3300b57cec5SDimitry Andric // Remove old name. 3310b57cec5SDimitry Andric ST->removeValueName(getValueName()); 3320b57cec5SDimitry Andric destroyValueName(); 3330b57cec5SDimitry Andric 3340b57cec5SDimitry Andric if (NameRef.empty()) 3350b57cec5SDimitry Andric return; 3360b57cec5SDimitry Andric } 3370b57cec5SDimitry Andric 3380b57cec5SDimitry Andric // Name is changing to something new. 3390b57cec5SDimitry Andric setValueName(ST->createValueName(NameRef, this)); 3400b57cec5SDimitry Andric } 3410b57cec5SDimitry Andric 3420b57cec5SDimitry Andric void Value::setName(const Twine &NewName) { 3430b57cec5SDimitry Andric setNameImpl(NewName); 3440b57cec5SDimitry Andric if (Function *F = dyn_cast<Function>(this)) 3450b57cec5SDimitry Andric F->recalculateIntrinsicID(); 3460b57cec5SDimitry Andric } 3470b57cec5SDimitry Andric 3480b57cec5SDimitry Andric void Value::takeName(Value *V) { 3490b57cec5SDimitry Andric ValueSymbolTable *ST = nullptr; 3500b57cec5SDimitry Andric // If this value has a name, drop it. 3510b57cec5SDimitry Andric if (hasName()) { 3520b57cec5SDimitry Andric // Get the symtab this is in. 3530b57cec5SDimitry Andric if (getSymTab(this, ST)) { 3540b57cec5SDimitry Andric // We can't set a name on this value, but we need to clear V's name if 3550b57cec5SDimitry Andric // it has one. 3560b57cec5SDimitry Andric if (V->hasName()) V->setName(""); 3570b57cec5SDimitry Andric return; // Cannot set a name on this value (e.g. constant). 3580b57cec5SDimitry Andric } 3590b57cec5SDimitry Andric 3600b57cec5SDimitry Andric // Remove old name. 3610b57cec5SDimitry Andric if (ST) 3620b57cec5SDimitry Andric ST->removeValueName(getValueName()); 3630b57cec5SDimitry Andric destroyValueName(); 3640b57cec5SDimitry Andric } 3650b57cec5SDimitry Andric 3660b57cec5SDimitry Andric // Now we know that this has no name. 3670b57cec5SDimitry Andric 3680b57cec5SDimitry Andric // If V has no name either, we're done. 3690b57cec5SDimitry Andric if (!V->hasName()) return; 3700b57cec5SDimitry Andric 3710b57cec5SDimitry Andric // Get this's symtab if we didn't before. 3720b57cec5SDimitry Andric if (!ST) { 3730b57cec5SDimitry Andric if (getSymTab(this, ST)) { 3740b57cec5SDimitry Andric // Clear V's name. 3750b57cec5SDimitry Andric V->setName(""); 3760b57cec5SDimitry Andric return; // Cannot set a name on this value (e.g. constant). 3770b57cec5SDimitry Andric } 3780b57cec5SDimitry Andric } 3790b57cec5SDimitry Andric 3800b57cec5SDimitry Andric // Get V's ST, this should always succed, because V has a name. 3810b57cec5SDimitry Andric ValueSymbolTable *VST; 3820b57cec5SDimitry Andric bool Failure = getSymTab(V, VST); 3830b57cec5SDimitry Andric assert(!Failure && "V has a name, so it should have a ST!"); (void)Failure; 3840b57cec5SDimitry Andric 3850b57cec5SDimitry Andric // If these values are both in the same symtab, we can do this very fast. 3860b57cec5SDimitry Andric // This works even if both values have no symtab yet. 3870b57cec5SDimitry Andric if (ST == VST) { 3880b57cec5SDimitry Andric // Take the name! 3890b57cec5SDimitry Andric setValueName(V->getValueName()); 3900b57cec5SDimitry Andric V->setValueName(nullptr); 3910b57cec5SDimitry Andric getValueName()->setValue(this); 3920b57cec5SDimitry Andric return; 3930b57cec5SDimitry Andric } 3940b57cec5SDimitry Andric 3950b57cec5SDimitry Andric // Otherwise, things are slightly more complex. Remove V's name from VST and 3960b57cec5SDimitry Andric // then reinsert it into ST. 3970b57cec5SDimitry Andric 3980b57cec5SDimitry Andric if (VST) 3990b57cec5SDimitry Andric VST->removeValueName(V->getValueName()); 4000b57cec5SDimitry Andric setValueName(V->getValueName()); 4010b57cec5SDimitry Andric V->setValueName(nullptr); 4020b57cec5SDimitry Andric getValueName()->setValue(this); 4030b57cec5SDimitry Andric 4040b57cec5SDimitry Andric if (ST) 4050b57cec5SDimitry Andric ST->reinsertValue(this); 4060b57cec5SDimitry Andric } 4070b57cec5SDimitry Andric 4080b57cec5SDimitry Andric void Value::assertModuleIsMaterializedImpl() const { 4090b57cec5SDimitry Andric #ifndef NDEBUG 4100b57cec5SDimitry Andric const GlobalValue *GV = dyn_cast<GlobalValue>(this); 4110b57cec5SDimitry Andric if (!GV) 4120b57cec5SDimitry Andric return; 4130b57cec5SDimitry Andric const Module *M = GV->getParent(); 4140b57cec5SDimitry Andric if (!M) 4150b57cec5SDimitry Andric return; 4160b57cec5SDimitry Andric assert(M->isMaterialized()); 4170b57cec5SDimitry Andric #endif 4180b57cec5SDimitry Andric } 4190b57cec5SDimitry Andric 4200b57cec5SDimitry Andric #ifndef NDEBUG 4210b57cec5SDimitry Andric static bool contains(SmallPtrSetImpl<ConstantExpr *> &Cache, ConstantExpr *Expr, 4220b57cec5SDimitry Andric Constant *C) { 4230b57cec5SDimitry Andric if (!Cache.insert(Expr).second) 4240b57cec5SDimitry Andric return false; 4250b57cec5SDimitry Andric 4260b57cec5SDimitry Andric for (auto &O : Expr->operands()) { 4270b57cec5SDimitry Andric if (O == C) 4280b57cec5SDimitry Andric return true; 4290b57cec5SDimitry Andric auto *CE = dyn_cast<ConstantExpr>(O); 4300b57cec5SDimitry Andric if (!CE) 4310b57cec5SDimitry Andric continue; 4320b57cec5SDimitry Andric if (contains(Cache, CE, C)) 4330b57cec5SDimitry Andric return true; 4340b57cec5SDimitry Andric } 4350b57cec5SDimitry Andric return false; 4360b57cec5SDimitry Andric } 4370b57cec5SDimitry Andric 4380b57cec5SDimitry Andric static bool contains(Value *Expr, Value *V) { 4390b57cec5SDimitry Andric if (Expr == V) 4400b57cec5SDimitry Andric return true; 4410b57cec5SDimitry Andric 4420b57cec5SDimitry Andric auto *C = dyn_cast<Constant>(V); 4430b57cec5SDimitry Andric if (!C) 4440b57cec5SDimitry Andric return false; 4450b57cec5SDimitry Andric 4460b57cec5SDimitry Andric auto *CE = dyn_cast<ConstantExpr>(Expr); 4470b57cec5SDimitry Andric if (!CE) 4480b57cec5SDimitry Andric return false; 4490b57cec5SDimitry Andric 4500b57cec5SDimitry Andric SmallPtrSet<ConstantExpr *, 4> Cache; 4510b57cec5SDimitry Andric return contains(Cache, CE, C); 4520b57cec5SDimitry Andric } 4530b57cec5SDimitry Andric #endif // NDEBUG 4540b57cec5SDimitry Andric 4550b57cec5SDimitry Andric void Value::doRAUW(Value *New, ReplaceMetadataUses ReplaceMetaUses) { 4560b57cec5SDimitry Andric assert(New && "Value::replaceAllUsesWith(<null>) is invalid!"); 4570b57cec5SDimitry Andric assert(!contains(New, this) && 4580b57cec5SDimitry Andric "this->replaceAllUsesWith(expr(this)) is NOT valid!"); 4590b57cec5SDimitry Andric assert(New->getType() == getType() && 4600b57cec5SDimitry Andric "replaceAllUses of value with new value of different type!"); 4610b57cec5SDimitry Andric 4620b57cec5SDimitry Andric // Notify all ValueHandles (if present) that this value is going away. 4630b57cec5SDimitry Andric if (HasValueHandle) 4640b57cec5SDimitry Andric ValueHandleBase::ValueIsRAUWd(this, New); 4650b57cec5SDimitry Andric if (ReplaceMetaUses == ReplaceMetadataUses::Yes && isUsedByMetadata()) 4660b57cec5SDimitry Andric ValueAsMetadata::handleRAUW(this, New); 4670b57cec5SDimitry Andric 4680b57cec5SDimitry Andric while (!materialized_use_empty()) { 4690b57cec5SDimitry Andric Use &U = *UseList; 4700b57cec5SDimitry Andric // Must handle Constants specially, we cannot call replaceUsesOfWith on a 4710b57cec5SDimitry Andric // constant because they are uniqued. 4720b57cec5SDimitry Andric if (auto *C = dyn_cast<Constant>(U.getUser())) { 4730b57cec5SDimitry Andric if (!isa<GlobalValue>(C)) { 4740b57cec5SDimitry Andric C->handleOperandChange(this, New); 4750b57cec5SDimitry Andric continue; 4760b57cec5SDimitry Andric } 4770b57cec5SDimitry Andric } 4780b57cec5SDimitry Andric 4790b57cec5SDimitry Andric U.set(New); 4800b57cec5SDimitry Andric } 4810b57cec5SDimitry Andric 4820b57cec5SDimitry Andric if (BasicBlock *BB = dyn_cast<BasicBlock>(this)) 4830b57cec5SDimitry Andric BB->replaceSuccessorsPhiUsesWith(cast<BasicBlock>(New)); 4840b57cec5SDimitry Andric } 4850b57cec5SDimitry Andric 4860b57cec5SDimitry Andric void Value::replaceAllUsesWith(Value *New) { 4870b57cec5SDimitry Andric doRAUW(New, ReplaceMetadataUses::Yes); 4880b57cec5SDimitry Andric } 4890b57cec5SDimitry Andric 4900b57cec5SDimitry Andric void Value::replaceNonMetadataUsesWith(Value *New) { 4910b57cec5SDimitry Andric doRAUW(New, ReplaceMetadataUses::No); 4920b57cec5SDimitry Andric } 4930b57cec5SDimitry Andric 4940b57cec5SDimitry Andric // Like replaceAllUsesWith except it does not handle constants or basic blocks. 4950b57cec5SDimitry Andric // This routine leaves uses within BB. 4960b57cec5SDimitry Andric void Value::replaceUsesOutsideBlock(Value *New, BasicBlock *BB) { 4970b57cec5SDimitry Andric assert(New && "Value::replaceUsesOutsideBlock(<null>, BB) is invalid!"); 4980b57cec5SDimitry Andric assert(!contains(New, this) && 4990b57cec5SDimitry Andric "this->replaceUsesOutsideBlock(expr(this), BB) is NOT valid!"); 5000b57cec5SDimitry Andric assert(New->getType() == getType() && 5010b57cec5SDimitry Andric "replaceUses of value with new value of different type!"); 5020b57cec5SDimitry Andric assert(BB && "Basic block that may contain a use of 'New' must be defined\n"); 5030b57cec5SDimitry Andric 5048bcb0991SDimitry Andric replaceUsesWithIf(New, [BB](Use &U) { 5058bcb0991SDimitry Andric auto *I = dyn_cast<Instruction>(U.getUser()); 5068bcb0991SDimitry Andric // Don't replace if it's an instruction in the BB basic block. 5078bcb0991SDimitry Andric return !I || I->getParent() != BB; 5088bcb0991SDimitry Andric }); 5090b57cec5SDimitry Andric } 5100b57cec5SDimitry Andric 5110b57cec5SDimitry Andric namespace { 5120b57cec5SDimitry Andric // Various metrics for how much to strip off of pointers. 5130b57cec5SDimitry Andric enum PointerStripKind { 5140b57cec5SDimitry Andric PSK_ZeroIndices, 5150b57cec5SDimitry Andric PSK_ZeroIndicesAndAliases, 5168bcb0991SDimitry Andric PSK_ZeroIndicesSameRepresentation, 5178bcb0991SDimitry Andric PSK_ZeroIndicesAndInvariantGroups, 5180b57cec5SDimitry Andric PSK_InBoundsConstantIndices, 5190b57cec5SDimitry Andric PSK_InBounds 5200b57cec5SDimitry Andric }; 5210b57cec5SDimitry Andric 522*5ffd83dbSDimitry Andric template <PointerStripKind StripKind> static void NoopCallback(const Value *) {} 523*5ffd83dbSDimitry Andric 5240b57cec5SDimitry Andric template <PointerStripKind StripKind> 525*5ffd83dbSDimitry Andric static const Value *stripPointerCastsAndOffsets( 526*5ffd83dbSDimitry Andric const Value *V, 527*5ffd83dbSDimitry Andric function_ref<void(const Value *)> Func = NoopCallback<StripKind>) { 5280b57cec5SDimitry Andric if (!V->getType()->isPointerTy()) 5290b57cec5SDimitry Andric return V; 5300b57cec5SDimitry Andric 5310b57cec5SDimitry Andric // Even though we don't look through PHI nodes, we could be called on an 5320b57cec5SDimitry Andric // instruction in an unreachable block, which may be on a cycle. 5330b57cec5SDimitry Andric SmallPtrSet<const Value *, 4> Visited; 5340b57cec5SDimitry Andric 5350b57cec5SDimitry Andric Visited.insert(V); 5360b57cec5SDimitry Andric do { 537*5ffd83dbSDimitry Andric Func(V); 5380b57cec5SDimitry Andric if (auto *GEP = dyn_cast<GEPOperator>(V)) { 5390b57cec5SDimitry Andric switch (StripKind) { 5400b57cec5SDimitry Andric case PSK_ZeroIndices: 5418bcb0991SDimitry Andric case PSK_ZeroIndicesAndAliases: 5428bcb0991SDimitry Andric case PSK_ZeroIndicesSameRepresentation: 5438bcb0991SDimitry Andric case PSK_ZeroIndicesAndInvariantGroups: 5440b57cec5SDimitry Andric if (!GEP->hasAllZeroIndices()) 5450b57cec5SDimitry Andric return V; 5460b57cec5SDimitry Andric break; 5470b57cec5SDimitry Andric case PSK_InBoundsConstantIndices: 5480b57cec5SDimitry Andric if (!GEP->hasAllConstantIndices()) 5490b57cec5SDimitry Andric return V; 5500b57cec5SDimitry Andric LLVM_FALLTHROUGH; 5510b57cec5SDimitry Andric case PSK_InBounds: 5520b57cec5SDimitry Andric if (!GEP->isInBounds()) 5530b57cec5SDimitry Andric return V; 5540b57cec5SDimitry Andric break; 5550b57cec5SDimitry Andric } 5560b57cec5SDimitry Andric V = GEP->getPointerOperand(); 5570b57cec5SDimitry Andric } else if (Operator::getOpcode(V) == Instruction::BitCast) { 5580b57cec5SDimitry Andric V = cast<Operator>(V)->getOperand(0); 559*5ffd83dbSDimitry Andric if (!V->getType()->isPointerTy()) 560*5ffd83dbSDimitry Andric return V; 5618bcb0991SDimitry Andric } else if (StripKind != PSK_ZeroIndicesSameRepresentation && 5620b57cec5SDimitry Andric Operator::getOpcode(V) == Instruction::AddrSpaceCast) { 5630b57cec5SDimitry Andric // TODO: If we know an address space cast will not change the 5640b57cec5SDimitry Andric // representation we could look through it here as well. 5650b57cec5SDimitry Andric V = cast<Operator>(V)->getOperand(0); 5668bcb0991SDimitry Andric } else if (StripKind == PSK_ZeroIndicesAndAliases && isa<GlobalAlias>(V)) { 5678bcb0991SDimitry Andric V = cast<GlobalAlias>(V)->getAliasee(); 5680b57cec5SDimitry Andric } else { 5690b57cec5SDimitry Andric if (const auto *Call = dyn_cast<CallBase>(V)) { 5700b57cec5SDimitry Andric if (const Value *RV = Call->getReturnedArgOperand()) { 5710b57cec5SDimitry Andric V = RV; 5720b57cec5SDimitry Andric continue; 5730b57cec5SDimitry Andric } 5740b57cec5SDimitry Andric // The result of launder.invariant.group must alias it's argument, 5750b57cec5SDimitry Andric // but it can't be marked with returned attribute, that's why it needs 5760b57cec5SDimitry Andric // special case. 5778bcb0991SDimitry Andric if (StripKind == PSK_ZeroIndicesAndInvariantGroups && 5780b57cec5SDimitry Andric (Call->getIntrinsicID() == Intrinsic::launder_invariant_group || 5790b57cec5SDimitry Andric Call->getIntrinsicID() == Intrinsic::strip_invariant_group)) { 5800b57cec5SDimitry Andric V = Call->getArgOperand(0); 5810b57cec5SDimitry Andric continue; 5820b57cec5SDimitry Andric } 5830b57cec5SDimitry Andric } 5840b57cec5SDimitry Andric return V; 5850b57cec5SDimitry Andric } 5860b57cec5SDimitry Andric assert(V->getType()->isPointerTy() && "Unexpected operand type!"); 5870b57cec5SDimitry Andric } while (Visited.insert(V).second); 5880b57cec5SDimitry Andric 5890b57cec5SDimitry Andric return V; 5900b57cec5SDimitry Andric } 5910b57cec5SDimitry Andric } // end anonymous namespace 5920b57cec5SDimitry Andric 5930b57cec5SDimitry Andric const Value *Value::stripPointerCasts() const { 5948bcb0991SDimitry Andric return stripPointerCastsAndOffsets<PSK_ZeroIndices>(this); 5958bcb0991SDimitry Andric } 5968bcb0991SDimitry Andric 5978bcb0991SDimitry Andric const Value *Value::stripPointerCastsAndAliases() const { 5980b57cec5SDimitry Andric return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndAliases>(this); 5990b57cec5SDimitry Andric } 6000b57cec5SDimitry Andric 6010b57cec5SDimitry Andric const Value *Value::stripPointerCastsSameRepresentation() const { 6028bcb0991SDimitry Andric return stripPointerCastsAndOffsets<PSK_ZeroIndicesSameRepresentation>(this); 6030b57cec5SDimitry Andric } 6040b57cec5SDimitry Andric 6050b57cec5SDimitry Andric const Value *Value::stripInBoundsConstantOffsets() const { 6060b57cec5SDimitry Andric return stripPointerCastsAndOffsets<PSK_InBoundsConstantIndices>(this); 6070b57cec5SDimitry Andric } 6080b57cec5SDimitry Andric 6090b57cec5SDimitry Andric const Value *Value::stripPointerCastsAndInvariantGroups() const { 6108bcb0991SDimitry Andric return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndInvariantGroups>(this); 6110b57cec5SDimitry Andric } 6120b57cec5SDimitry Andric 613*5ffd83dbSDimitry Andric const Value *Value::stripAndAccumulateConstantOffsets( 614*5ffd83dbSDimitry Andric const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, 615*5ffd83dbSDimitry Andric function_ref<bool(Value &, APInt &)> ExternalAnalysis) const { 6160b57cec5SDimitry Andric if (!getType()->isPtrOrPtrVectorTy()) 6170b57cec5SDimitry Andric return this; 6180b57cec5SDimitry Andric 6190b57cec5SDimitry Andric unsigned BitWidth = Offset.getBitWidth(); 6200b57cec5SDimitry Andric assert(BitWidth == DL.getIndexTypeSizeInBits(getType()) && 6210b57cec5SDimitry Andric "The offset bit width does not match the DL specification."); 6220b57cec5SDimitry Andric 6230b57cec5SDimitry Andric // Even though we don't look through PHI nodes, we could be called on an 6240b57cec5SDimitry Andric // instruction in an unreachable block, which may be on a cycle. 6250b57cec5SDimitry Andric SmallPtrSet<const Value *, 4> Visited; 6260b57cec5SDimitry Andric Visited.insert(this); 6270b57cec5SDimitry Andric const Value *V = this; 6280b57cec5SDimitry Andric do { 6290b57cec5SDimitry Andric if (auto *GEP = dyn_cast<GEPOperator>(V)) { 6300b57cec5SDimitry Andric // If in-bounds was requested, we do not strip non-in-bounds GEPs. 6310b57cec5SDimitry Andric if (!AllowNonInbounds && !GEP->isInBounds()) 6320b57cec5SDimitry Andric return V; 6330b57cec5SDimitry Andric 6340b57cec5SDimitry Andric // If one of the values we have visited is an addrspacecast, then 6350b57cec5SDimitry Andric // the pointer type of this GEP may be different from the type 6360b57cec5SDimitry Andric // of the Ptr parameter which was passed to this function. This 6370b57cec5SDimitry Andric // means when we construct GEPOffset, we need to use the size 6380b57cec5SDimitry Andric // of GEP's pointer type rather than the size of the original 6390b57cec5SDimitry Andric // pointer type. 6400b57cec5SDimitry Andric APInt GEPOffset(DL.getIndexTypeSizeInBits(V->getType()), 0); 641*5ffd83dbSDimitry Andric if (!GEP->accumulateConstantOffset(DL, GEPOffset, ExternalAnalysis)) 6420b57cec5SDimitry Andric return V; 6430b57cec5SDimitry Andric 6440b57cec5SDimitry Andric // Stop traversal if the pointer offset wouldn't fit in the bit-width 6450b57cec5SDimitry Andric // provided by the Offset argument. This can happen due to AddrSpaceCast 6460b57cec5SDimitry Andric // stripping. 6470b57cec5SDimitry Andric if (GEPOffset.getMinSignedBits() > BitWidth) 6480b57cec5SDimitry Andric return V; 6490b57cec5SDimitry Andric 650*5ffd83dbSDimitry Andric // External Analysis can return a result higher/lower than the value 651*5ffd83dbSDimitry Andric // represents. We need to detect overflow/underflow. 652*5ffd83dbSDimitry Andric APInt GEPOffsetST = GEPOffset.sextOrTrunc(BitWidth); 653*5ffd83dbSDimitry Andric if (!ExternalAnalysis) { 654*5ffd83dbSDimitry Andric Offset += GEPOffsetST; 655*5ffd83dbSDimitry Andric } else { 656*5ffd83dbSDimitry Andric bool Overflow = false; 657*5ffd83dbSDimitry Andric APInt OldOffset = Offset; 658*5ffd83dbSDimitry Andric Offset = Offset.sadd_ov(GEPOffsetST, Overflow); 659*5ffd83dbSDimitry Andric if (Overflow) { 660*5ffd83dbSDimitry Andric Offset = OldOffset; 661*5ffd83dbSDimitry Andric return V; 662*5ffd83dbSDimitry Andric } 663*5ffd83dbSDimitry Andric } 6640b57cec5SDimitry Andric V = GEP->getPointerOperand(); 6650b57cec5SDimitry Andric } else if (Operator::getOpcode(V) == Instruction::BitCast || 6660b57cec5SDimitry Andric Operator::getOpcode(V) == Instruction::AddrSpaceCast) { 6670b57cec5SDimitry Andric V = cast<Operator>(V)->getOperand(0); 6680b57cec5SDimitry Andric } else if (auto *GA = dyn_cast<GlobalAlias>(V)) { 6690b57cec5SDimitry Andric if (!GA->isInterposable()) 6700b57cec5SDimitry Andric V = GA->getAliasee(); 6710b57cec5SDimitry Andric } else if (const auto *Call = dyn_cast<CallBase>(V)) { 6720b57cec5SDimitry Andric if (const Value *RV = Call->getReturnedArgOperand()) 6730b57cec5SDimitry Andric V = RV; 6740b57cec5SDimitry Andric } 6750b57cec5SDimitry Andric assert(V->getType()->isPtrOrPtrVectorTy() && "Unexpected operand type!"); 6760b57cec5SDimitry Andric } while (Visited.insert(V).second); 6770b57cec5SDimitry Andric 6780b57cec5SDimitry Andric return V; 6790b57cec5SDimitry Andric } 6800b57cec5SDimitry Andric 681*5ffd83dbSDimitry Andric const Value * 682*5ffd83dbSDimitry Andric Value::stripInBoundsOffsets(function_ref<void(const Value *)> Func) const { 683*5ffd83dbSDimitry Andric return stripPointerCastsAndOffsets<PSK_InBounds>(this, Func); 6840b57cec5SDimitry Andric } 6850b57cec5SDimitry Andric 6860b57cec5SDimitry Andric uint64_t Value::getPointerDereferenceableBytes(const DataLayout &DL, 6870b57cec5SDimitry Andric bool &CanBeNull) const { 6880b57cec5SDimitry Andric assert(getType()->isPointerTy() && "must be pointer"); 6890b57cec5SDimitry Andric 6900b57cec5SDimitry Andric uint64_t DerefBytes = 0; 6910b57cec5SDimitry Andric CanBeNull = false; 6920b57cec5SDimitry Andric if (const Argument *A = dyn_cast<Argument>(this)) { 6930b57cec5SDimitry Andric DerefBytes = A->getDereferenceableBytes(); 6940b57cec5SDimitry Andric if (DerefBytes == 0 && (A->hasByValAttr() || A->hasStructRetAttr())) { 6950b57cec5SDimitry Andric Type *PT = cast<PointerType>(A->getType())->getElementType(); 6960b57cec5SDimitry Andric if (PT->isSized()) 697*5ffd83dbSDimitry Andric DerefBytes = DL.getTypeStoreSize(PT).getKnownMinSize(); 6980b57cec5SDimitry Andric } 6990b57cec5SDimitry Andric if (DerefBytes == 0) { 7000b57cec5SDimitry Andric DerefBytes = A->getDereferenceableOrNullBytes(); 7010b57cec5SDimitry Andric CanBeNull = true; 7020b57cec5SDimitry Andric } 7030b57cec5SDimitry Andric } else if (const auto *Call = dyn_cast<CallBase>(this)) { 7040b57cec5SDimitry Andric DerefBytes = Call->getDereferenceableBytes(AttributeList::ReturnIndex); 7050b57cec5SDimitry Andric if (DerefBytes == 0) { 7060b57cec5SDimitry Andric DerefBytes = 7070b57cec5SDimitry Andric Call->getDereferenceableOrNullBytes(AttributeList::ReturnIndex); 7080b57cec5SDimitry Andric CanBeNull = true; 7090b57cec5SDimitry Andric } 7100b57cec5SDimitry Andric } else if (const LoadInst *LI = dyn_cast<LoadInst>(this)) { 7110b57cec5SDimitry Andric if (MDNode *MD = LI->getMetadata(LLVMContext::MD_dereferenceable)) { 7120b57cec5SDimitry Andric ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0)); 7130b57cec5SDimitry Andric DerefBytes = CI->getLimitedValue(); 7140b57cec5SDimitry Andric } 7150b57cec5SDimitry Andric if (DerefBytes == 0) { 7160b57cec5SDimitry Andric if (MDNode *MD = 7170b57cec5SDimitry Andric LI->getMetadata(LLVMContext::MD_dereferenceable_or_null)) { 7180b57cec5SDimitry Andric ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0)); 7190b57cec5SDimitry Andric DerefBytes = CI->getLimitedValue(); 7200b57cec5SDimitry Andric } 7210b57cec5SDimitry Andric CanBeNull = true; 7220b57cec5SDimitry Andric } 7238bcb0991SDimitry Andric } else if (auto *IP = dyn_cast<IntToPtrInst>(this)) { 7248bcb0991SDimitry Andric if (MDNode *MD = IP->getMetadata(LLVMContext::MD_dereferenceable)) { 7258bcb0991SDimitry Andric ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0)); 7268bcb0991SDimitry Andric DerefBytes = CI->getLimitedValue(); 7278bcb0991SDimitry Andric } 7288bcb0991SDimitry Andric if (DerefBytes == 0) { 7298bcb0991SDimitry Andric if (MDNode *MD = 7308bcb0991SDimitry Andric IP->getMetadata(LLVMContext::MD_dereferenceable_or_null)) { 7318bcb0991SDimitry Andric ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0)); 7328bcb0991SDimitry Andric DerefBytes = CI->getLimitedValue(); 7338bcb0991SDimitry Andric } 7348bcb0991SDimitry Andric CanBeNull = true; 7358bcb0991SDimitry Andric } 7360b57cec5SDimitry Andric } else if (auto *AI = dyn_cast<AllocaInst>(this)) { 7370b57cec5SDimitry Andric if (!AI->isArrayAllocation()) { 738*5ffd83dbSDimitry Andric DerefBytes = 739*5ffd83dbSDimitry Andric DL.getTypeStoreSize(AI->getAllocatedType()).getKnownMinSize(); 7400b57cec5SDimitry Andric CanBeNull = false; 7410b57cec5SDimitry Andric } 7420b57cec5SDimitry Andric } else if (auto *GV = dyn_cast<GlobalVariable>(this)) { 7430b57cec5SDimitry Andric if (GV->getValueType()->isSized() && !GV->hasExternalWeakLinkage()) { 7440b57cec5SDimitry Andric // TODO: Don't outright reject hasExternalWeakLinkage but set the 7450b57cec5SDimitry Andric // CanBeNull flag. 746*5ffd83dbSDimitry Andric DerefBytes = DL.getTypeStoreSize(GV->getValueType()).getFixedSize(); 7470b57cec5SDimitry Andric CanBeNull = false; 7480b57cec5SDimitry Andric } 7490b57cec5SDimitry Andric } 7500b57cec5SDimitry Andric return DerefBytes; 7510b57cec5SDimitry Andric } 7520b57cec5SDimitry Andric 753*5ffd83dbSDimitry Andric Align Value::getPointerAlignment(const DataLayout &DL) const { 7540b57cec5SDimitry Andric assert(getType()->isPointerTy() && "must be pointer"); 7550b57cec5SDimitry Andric if (auto *GO = dyn_cast<GlobalObject>(this)) { 7560b57cec5SDimitry Andric if (isa<Function>(GO)) { 757*5ffd83dbSDimitry Andric Align FunctionPtrAlign = DL.getFunctionPtrAlign().valueOrOne(); 7580b57cec5SDimitry Andric switch (DL.getFunctionPtrAlignType()) { 7590b57cec5SDimitry Andric case DataLayout::FunctionPtrAlignType::Independent: 7608bcb0991SDimitry Andric return FunctionPtrAlign; 7610b57cec5SDimitry Andric case DataLayout::FunctionPtrAlignType::MultipleOfFunctionAlign: 762*5ffd83dbSDimitry Andric return std::max(FunctionPtrAlign, GO->getAlign().valueOrOne()); 7630b57cec5SDimitry Andric } 7648bcb0991SDimitry Andric llvm_unreachable("Unhandled FunctionPtrAlignType"); 7650b57cec5SDimitry Andric } 7668bcb0991SDimitry Andric const MaybeAlign Alignment(GO->getAlignment()); 7678bcb0991SDimitry Andric if (!Alignment) { 7680b57cec5SDimitry Andric if (auto *GVar = dyn_cast<GlobalVariable>(GO)) { 7690b57cec5SDimitry Andric Type *ObjectType = GVar->getValueType(); 7700b57cec5SDimitry Andric if (ObjectType->isSized()) { 7710b57cec5SDimitry Andric // If the object is defined in the current Module, we'll be giving 7720b57cec5SDimitry Andric // it the preferred alignment. Otherwise, we have to assume that it 7730b57cec5SDimitry Andric // may only have the minimum ABI alignment. 7740b57cec5SDimitry Andric if (GVar->isStrongDefinitionForLinker()) 775*5ffd83dbSDimitry Andric return DL.getPreferredAlign(GVar); 7760b57cec5SDimitry Andric else 777*5ffd83dbSDimitry Andric return DL.getABITypeAlign(ObjectType); 7780b57cec5SDimitry Andric } 7790b57cec5SDimitry Andric } 7800b57cec5SDimitry Andric } 781*5ffd83dbSDimitry Andric return Alignment.valueOrOne(); 7820b57cec5SDimitry Andric } else if (const Argument *A = dyn_cast<Argument>(this)) { 783*5ffd83dbSDimitry Andric const MaybeAlign Alignment = A->getParamAlign(); 7848bcb0991SDimitry Andric if (!Alignment && A->hasStructRetAttr()) { 7850b57cec5SDimitry Andric // An sret parameter has at least the ABI alignment of the return type. 7860b57cec5SDimitry Andric Type *EltTy = cast<PointerType>(A->getType())->getElementType(); 7870b57cec5SDimitry Andric if (EltTy->isSized()) 788*5ffd83dbSDimitry Andric return DL.getABITypeAlign(EltTy); 7890b57cec5SDimitry Andric } 790*5ffd83dbSDimitry Andric return Alignment.valueOrOne(); 7910b57cec5SDimitry Andric } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(this)) { 792*5ffd83dbSDimitry Andric return AI->getAlign(); 7938bcb0991SDimitry Andric } else if (const auto *Call = dyn_cast<CallBase>(this)) { 794*5ffd83dbSDimitry Andric MaybeAlign Alignment = Call->getRetAlign(); 7958bcb0991SDimitry Andric if (!Alignment && Call->getCalledFunction()) 796*5ffd83dbSDimitry Andric Alignment = Call->getCalledFunction()->getAttributes().getRetAlignment(); 797*5ffd83dbSDimitry Andric return Alignment.valueOrOne(); 7988bcb0991SDimitry Andric } else if (const LoadInst *LI = dyn_cast<LoadInst>(this)) { 7990b57cec5SDimitry Andric if (MDNode *MD = LI->getMetadata(LLVMContext::MD_align)) { 8000b57cec5SDimitry Andric ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0)); 801*5ffd83dbSDimitry Andric return Align(CI->getLimitedValue()); 802*5ffd83dbSDimitry Andric } 803*5ffd83dbSDimitry Andric } else if (auto *CstPtr = dyn_cast<Constant>(this)) { 804*5ffd83dbSDimitry Andric if (auto *CstInt = dyn_cast_or_null<ConstantInt>(ConstantExpr::getPtrToInt( 805*5ffd83dbSDimitry Andric const_cast<Constant *>(CstPtr), DL.getIntPtrType(getType()), 806*5ffd83dbSDimitry Andric /*OnlyIfReduced=*/true))) { 807*5ffd83dbSDimitry Andric size_t TrailingZeros = CstInt->getValue().countTrailingZeros(); 808*5ffd83dbSDimitry Andric // While the actual alignment may be large, elsewhere we have 809*5ffd83dbSDimitry Andric // an arbitrary upper alignmet limit, so let's clamp to it. 810*5ffd83dbSDimitry Andric return Align(TrailingZeros < Value::MaxAlignmentExponent 811*5ffd83dbSDimitry Andric ? uint64_t(1) << TrailingZeros 812*5ffd83dbSDimitry Andric : Value::MaximumAlignment); 8130b57cec5SDimitry Andric } 8148bcb0991SDimitry Andric } 815*5ffd83dbSDimitry Andric return Align(1); 8160b57cec5SDimitry Andric } 8170b57cec5SDimitry Andric 8180b57cec5SDimitry Andric const Value *Value::DoPHITranslation(const BasicBlock *CurBB, 8190b57cec5SDimitry Andric const BasicBlock *PredBB) const { 8200b57cec5SDimitry Andric auto *PN = dyn_cast<PHINode>(this); 8210b57cec5SDimitry Andric if (PN && PN->getParent() == CurBB) 8220b57cec5SDimitry Andric return PN->getIncomingValueForBlock(PredBB); 8230b57cec5SDimitry Andric return this; 8240b57cec5SDimitry Andric } 8250b57cec5SDimitry Andric 8260b57cec5SDimitry Andric LLVMContext &Value::getContext() const { return VTy->getContext(); } 8270b57cec5SDimitry Andric 8280b57cec5SDimitry Andric void Value::reverseUseList() { 8290b57cec5SDimitry Andric if (!UseList || !UseList->Next) 8300b57cec5SDimitry Andric // No need to reverse 0 or 1 uses. 8310b57cec5SDimitry Andric return; 8320b57cec5SDimitry Andric 8330b57cec5SDimitry Andric Use *Head = UseList; 8340b57cec5SDimitry Andric Use *Current = UseList->Next; 8350b57cec5SDimitry Andric Head->Next = nullptr; 8360b57cec5SDimitry Andric while (Current) { 8370b57cec5SDimitry Andric Use *Next = Current->Next; 8380b57cec5SDimitry Andric Current->Next = Head; 839*5ffd83dbSDimitry Andric Head->Prev = &Current->Next; 8400b57cec5SDimitry Andric Head = Current; 8410b57cec5SDimitry Andric Current = Next; 8420b57cec5SDimitry Andric } 8430b57cec5SDimitry Andric UseList = Head; 844*5ffd83dbSDimitry Andric Head->Prev = &UseList; 8450b57cec5SDimitry Andric } 8460b57cec5SDimitry Andric 8470b57cec5SDimitry Andric bool Value::isSwiftError() const { 8480b57cec5SDimitry Andric auto *Arg = dyn_cast<Argument>(this); 8490b57cec5SDimitry Andric if (Arg) 8500b57cec5SDimitry Andric return Arg->hasSwiftErrorAttr(); 8510b57cec5SDimitry Andric auto *Alloca = dyn_cast<AllocaInst>(this); 8520b57cec5SDimitry Andric if (!Alloca) 8530b57cec5SDimitry Andric return false; 8540b57cec5SDimitry Andric return Alloca->isSwiftError(); 8550b57cec5SDimitry Andric } 8560b57cec5SDimitry Andric 8570b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8580b57cec5SDimitry Andric // ValueHandleBase Class 8590b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8600b57cec5SDimitry Andric 8610b57cec5SDimitry Andric void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) { 8620b57cec5SDimitry Andric assert(List && "Handle list is null?"); 8630b57cec5SDimitry Andric 8640b57cec5SDimitry Andric // Splice ourselves into the list. 8650b57cec5SDimitry Andric Next = *List; 8660b57cec5SDimitry Andric *List = this; 8670b57cec5SDimitry Andric setPrevPtr(List); 8680b57cec5SDimitry Andric if (Next) { 8690b57cec5SDimitry Andric Next->setPrevPtr(&Next); 8700b57cec5SDimitry Andric assert(getValPtr() == Next->getValPtr() && "Added to wrong list?"); 8710b57cec5SDimitry Andric } 8720b57cec5SDimitry Andric } 8730b57cec5SDimitry Andric 8740b57cec5SDimitry Andric void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) { 8750b57cec5SDimitry Andric assert(List && "Must insert after existing node"); 8760b57cec5SDimitry Andric 8770b57cec5SDimitry Andric Next = List->Next; 8780b57cec5SDimitry Andric setPrevPtr(&List->Next); 8790b57cec5SDimitry Andric List->Next = this; 8800b57cec5SDimitry Andric if (Next) 8810b57cec5SDimitry Andric Next->setPrevPtr(&Next); 8820b57cec5SDimitry Andric } 8830b57cec5SDimitry Andric 8840b57cec5SDimitry Andric void ValueHandleBase::AddToUseList() { 8850b57cec5SDimitry Andric assert(getValPtr() && "Null pointer doesn't have a use list!"); 8860b57cec5SDimitry Andric 8870b57cec5SDimitry Andric LLVMContextImpl *pImpl = getValPtr()->getContext().pImpl; 8880b57cec5SDimitry Andric 8890b57cec5SDimitry Andric if (getValPtr()->HasValueHandle) { 8900b57cec5SDimitry Andric // If this value already has a ValueHandle, then it must be in the 8910b57cec5SDimitry Andric // ValueHandles map already. 8920b57cec5SDimitry Andric ValueHandleBase *&Entry = pImpl->ValueHandles[getValPtr()]; 8930b57cec5SDimitry Andric assert(Entry && "Value doesn't have any handles?"); 8940b57cec5SDimitry Andric AddToExistingUseList(&Entry); 8950b57cec5SDimitry Andric return; 8960b57cec5SDimitry Andric } 8970b57cec5SDimitry Andric 8980b57cec5SDimitry Andric // Ok, it doesn't have any handles yet, so we must insert it into the 8990b57cec5SDimitry Andric // DenseMap. However, doing this insertion could cause the DenseMap to 9000b57cec5SDimitry Andric // reallocate itself, which would invalidate all of the PrevP pointers that 9010b57cec5SDimitry Andric // point into the old table. Handle this by checking for reallocation and 9020b57cec5SDimitry Andric // updating the stale pointers only if needed. 9030b57cec5SDimitry Andric DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles; 9040b57cec5SDimitry Andric const void *OldBucketPtr = Handles.getPointerIntoBucketsArray(); 9050b57cec5SDimitry Andric 9060b57cec5SDimitry Andric ValueHandleBase *&Entry = Handles[getValPtr()]; 9070b57cec5SDimitry Andric assert(!Entry && "Value really did already have handles?"); 9080b57cec5SDimitry Andric AddToExistingUseList(&Entry); 9090b57cec5SDimitry Andric getValPtr()->HasValueHandle = true; 9100b57cec5SDimitry Andric 9110b57cec5SDimitry Andric // If reallocation didn't happen or if this was the first insertion, don't 9120b57cec5SDimitry Andric // walk the table. 9130b57cec5SDimitry Andric if (Handles.isPointerIntoBucketsArray(OldBucketPtr) || 9140b57cec5SDimitry Andric Handles.size() == 1) { 9150b57cec5SDimitry Andric return; 9160b57cec5SDimitry Andric } 9170b57cec5SDimitry Andric 9180b57cec5SDimitry Andric // Okay, reallocation did happen. Fix the Prev Pointers. 9190b57cec5SDimitry Andric for (DenseMap<Value*, ValueHandleBase*>::iterator I = Handles.begin(), 9200b57cec5SDimitry Andric E = Handles.end(); I != E; ++I) { 9210b57cec5SDimitry Andric assert(I->second && I->first == I->second->getValPtr() && 9220b57cec5SDimitry Andric "List invariant broken!"); 9230b57cec5SDimitry Andric I->second->setPrevPtr(&I->second); 9240b57cec5SDimitry Andric } 9250b57cec5SDimitry Andric } 9260b57cec5SDimitry Andric 9270b57cec5SDimitry Andric void ValueHandleBase::RemoveFromUseList() { 9280b57cec5SDimitry Andric assert(getValPtr() && getValPtr()->HasValueHandle && 9290b57cec5SDimitry Andric "Pointer doesn't have a use list!"); 9300b57cec5SDimitry Andric 9310b57cec5SDimitry Andric // Unlink this from its use list. 9320b57cec5SDimitry Andric ValueHandleBase **PrevPtr = getPrevPtr(); 9330b57cec5SDimitry Andric assert(*PrevPtr == this && "List invariant broken"); 9340b57cec5SDimitry Andric 9350b57cec5SDimitry Andric *PrevPtr = Next; 9360b57cec5SDimitry Andric if (Next) { 9370b57cec5SDimitry Andric assert(Next->getPrevPtr() == &Next && "List invariant broken"); 9380b57cec5SDimitry Andric Next->setPrevPtr(PrevPtr); 9390b57cec5SDimitry Andric return; 9400b57cec5SDimitry Andric } 9410b57cec5SDimitry Andric 9420b57cec5SDimitry Andric // If the Next pointer was null, then it is possible that this was the last 9430b57cec5SDimitry Andric // ValueHandle watching VP. If so, delete its entry from the ValueHandles 9440b57cec5SDimitry Andric // map. 9450b57cec5SDimitry Andric LLVMContextImpl *pImpl = getValPtr()->getContext().pImpl; 9460b57cec5SDimitry Andric DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles; 9470b57cec5SDimitry Andric if (Handles.isPointerIntoBucketsArray(PrevPtr)) { 9480b57cec5SDimitry Andric Handles.erase(getValPtr()); 9490b57cec5SDimitry Andric getValPtr()->HasValueHandle = false; 9500b57cec5SDimitry Andric } 9510b57cec5SDimitry Andric } 9520b57cec5SDimitry Andric 9530b57cec5SDimitry Andric void ValueHandleBase::ValueIsDeleted(Value *V) { 9540b57cec5SDimitry Andric assert(V->HasValueHandle && "Should only be called if ValueHandles present"); 9550b57cec5SDimitry Andric 9560b57cec5SDimitry Andric // Get the linked list base, which is guaranteed to exist since the 9570b57cec5SDimitry Andric // HasValueHandle flag is set. 9580b57cec5SDimitry Andric LLVMContextImpl *pImpl = V->getContext().pImpl; 9590b57cec5SDimitry Andric ValueHandleBase *Entry = pImpl->ValueHandles[V]; 9600b57cec5SDimitry Andric assert(Entry && "Value bit set but no entries exist"); 9610b57cec5SDimitry Andric 9620b57cec5SDimitry Andric // We use a local ValueHandleBase as an iterator so that ValueHandles can add 9630b57cec5SDimitry Andric // and remove themselves from the list without breaking our iteration. This 9640b57cec5SDimitry Andric // is not really an AssertingVH; we just have to give ValueHandleBase a kind. 9650b57cec5SDimitry Andric // Note that we deliberately do not the support the case when dropping a value 9660b57cec5SDimitry Andric // handle results in a new value handle being permanently added to the list 9670b57cec5SDimitry Andric // (as might occur in theory for CallbackVH's): the new value handle will not 9680b57cec5SDimitry Andric // be processed and the checking code will mete out righteous punishment if 9690b57cec5SDimitry Andric // the handle is still present once we have finished processing all the other 9700b57cec5SDimitry Andric // value handles (it is fine to momentarily add then remove a value handle). 9710b57cec5SDimitry Andric for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) { 9720b57cec5SDimitry Andric Iterator.RemoveFromUseList(); 9730b57cec5SDimitry Andric Iterator.AddToExistingUseListAfter(Entry); 9740b57cec5SDimitry Andric assert(Entry->Next == &Iterator && "Loop invariant broken."); 9750b57cec5SDimitry Andric 9760b57cec5SDimitry Andric switch (Entry->getKind()) { 9770b57cec5SDimitry Andric case Assert: 9780b57cec5SDimitry Andric break; 9790b57cec5SDimitry Andric case Weak: 9800b57cec5SDimitry Andric case WeakTracking: 9810b57cec5SDimitry Andric // WeakTracking and Weak just go to null, which unlinks them 9820b57cec5SDimitry Andric // from the list. 9830b57cec5SDimitry Andric Entry->operator=(nullptr); 9840b57cec5SDimitry Andric break; 9850b57cec5SDimitry Andric case Callback: 9860b57cec5SDimitry Andric // Forward to the subclass's implementation. 9870b57cec5SDimitry Andric static_cast<CallbackVH*>(Entry)->deleted(); 9880b57cec5SDimitry Andric break; 9890b57cec5SDimitry Andric } 9900b57cec5SDimitry Andric } 9910b57cec5SDimitry Andric 9920b57cec5SDimitry Andric // All callbacks, weak references, and assertingVHs should be dropped by now. 9930b57cec5SDimitry Andric if (V->HasValueHandle) { 9940b57cec5SDimitry Andric #ifndef NDEBUG // Only in +Asserts mode... 9950b57cec5SDimitry Andric dbgs() << "While deleting: " << *V->getType() << " %" << V->getName() 9960b57cec5SDimitry Andric << "\n"; 9970b57cec5SDimitry Andric if (pImpl->ValueHandles[V]->getKind() == Assert) 9980b57cec5SDimitry Andric llvm_unreachable("An asserting value handle still pointed to this" 9990b57cec5SDimitry Andric " value!"); 10000b57cec5SDimitry Andric 10010b57cec5SDimitry Andric #endif 10020b57cec5SDimitry Andric llvm_unreachable("All references to V were not removed?"); 10030b57cec5SDimitry Andric } 10040b57cec5SDimitry Andric } 10050b57cec5SDimitry Andric 10060b57cec5SDimitry Andric void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) { 10070b57cec5SDimitry Andric assert(Old->HasValueHandle &&"Should only be called if ValueHandles present"); 10080b57cec5SDimitry Andric assert(Old != New && "Changing value into itself!"); 10090b57cec5SDimitry Andric assert(Old->getType() == New->getType() && 10100b57cec5SDimitry Andric "replaceAllUses of value with new value of different type!"); 10110b57cec5SDimitry Andric 10120b57cec5SDimitry Andric // Get the linked list base, which is guaranteed to exist since the 10130b57cec5SDimitry Andric // HasValueHandle flag is set. 10140b57cec5SDimitry Andric LLVMContextImpl *pImpl = Old->getContext().pImpl; 10150b57cec5SDimitry Andric ValueHandleBase *Entry = pImpl->ValueHandles[Old]; 10160b57cec5SDimitry Andric 10170b57cec5SDimitry Andric assert(Entry && "Value bit set but no entries exist"); 10180b57cec5SDimitry Andric 10190b57cec5SDimitry Andric // We use a local ValueHandleBase as an iterator so that 10200b57cec5SDimitry Andric // ValueHandles can add and remove themselves from the list without 10210b57cec5SDimitry Andric // breaking our iteration. This is not really an AssertingVH; we 10220b57cec5SDimitry Andric // just have to give ValueHandleBase some kind. 10230b57cec5SDimitry Andric for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) { 10240b57cec5SDimitry Andric Iterator.RemoveFromUseList(); 10250b57cec5SDimitry Andric Iterator.AddToExistingUseListAfter(Entry); 10260b57cec5SDimitry Andric assert(Entry->Next == &Iterator && "Loop invariant broken."); 10270b57cec5SDimitry Andric 10280b57cec5SDimitry Andric switch (Entry->getKind()) { 10290b57cec5SDimitry Andric case Assert: 10300b57cec5SDimitry Andric case Weak: 10310b57cec5SDimitry Andric // Asserting and Weak handles do not follow RAUW implicitly. 10320b57cec5SDimitry Andric break; 10330b57cec5SDimitry Andric case WeakTracking: 10340b57cec5SDimitry Andric // Weak goes to the new value, which will unlink it from Old's list. 10350b57cec5SDimitry Andric Entry->operator=(New); 10360b57cec5SDimitry Andric break; 10370b57cec5SDimitry Andric case Callback: 10380b57cec5SDimitry Andric // Forward to the subclass's implementation. 10390b57cec5SDimitry Andric static_cast<CallbackVH*>(Entry)->allUsesReplacedWith(New); 10400b57cec5SDimitry Andric break; 10410b57cec5SDimitry Andric } 10420b57cec5SDimitry Andric } 10430b57cec5SDimitry Andric 10440b57cec5SDimitry Andric #ifndef NDEBUG 10450b57cec5SDimitry Andric // If any new weak value handles were added while processing the 10460b57cec5SDimitry Andric // list, then complain about it now. 10470b57cec5SDimitry Andric if (Old->HasValueHandle) 10480b57cec5SDimitry Andric for (Entry = pImpl->ValueHandles[Old]; Entry; Entry = Entry->Next) 10490b57cec5SDimitry Andric switch (Entry->getKind()) { 10500b57cec5SDimitry Andric case WeakTracking: 10510b57cec5SDimitry Andric dbgs() << "After RAUW from " << *Old->getType() << " %" 10520b57cec5SDimitry Andric << Old->getName() << " to " << *New->getType() << " %" 10530b57cec5SDimitry Andric << New->getName() << "\n"; 10540b57cec5SDimitry Andric llvm_unreachable( 10550b57cec5SDimitry Andric "A weak tracking value handle still pointed to the old value!\n"); 10560b57cec5SDimitry Andric default: 10570b57cec5SDimitry Andric break; 10580b57cec5SDimitry Andric } 10590b57cec5SDimitry Andric #endif 10600b57cec5SDimitry Andric } 10610b57cec5SDimitry Andric 10620b57cec5SDimitry Andric // Pin the vtable to this file. 10630b57cec5SDimitry Andric void CallbackVH::anchor() {} 1064