1ef860a24SChandler Carruth //===-- Value.cpp - Implement the Value class -----------------------------===// 2ef860a24SChandler Carruth // 32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information. 52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6ef860a24SChandler Carruth // 7ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 8ef860a24SChandler Carruth // 9ef860a24SChandler Carruth // This file implements the Value, ValueHandle, and User classes. 10ef860a24SChandler Carruth // 11ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 12ef860a24SChandler Carruth 139fb823bbSChandler Carruth #include "llvm/IR/Value.h" 14ef860a24SChandler Carruth #include "LLVMContextImpl.h" 15ef860a24SChandler Carruth #include "llvm/ADT/DenseMap.h" 162f2ace98SDmitry Mikulin #include "llvm/ADT/SetVector.h" 174c1a1d3cSReid Kleckner #include "llvm/ADT/SmallString.h" 189fb823bbSChandler Carruth #include "llvm/IR/Constant.h" 199fb823bbSChandler Carruth #include "llvm/IR/Constants.h" 202e42c34dSHal Finkel #include "llvm/IR/DataLayout.h" 219fb823bbSChandler Carruth #include "llvm/IR/DerivedTypes.h" 2296ab8726SReid Kleckner #include "llvm/IR/DerivedUser.h" 2303eb0de9SChandler Carruth #include "llvm/IR/GetElementPtrTypeIterator.h" 249fb823bbSChandler Carruth #include "llvm/IR/InstrTypes.h" 259fb823bbSChandler Carruth #include "llvm/IR/Instructions.h" 26a7343d65SRamkumar Ramachandra #include "llvm/IR/IntrinsicInst.h" 279fb823bbSChandler Carruth #include "llvm/IR/Module.h" 289fb823bbSChandler Carruth #include "llvm/IR/Operator.h" 29a7343d65SRamkumar Ramachandra #include "llvm/IR/Statepoint.h" 304220e9c1SChandler Carruth #include "llvm/IR/ValueHandle.h" 319fb823bbSChandler Carruth #include "llvm/IR/ValueSymbolTable.h" 324c1a1d3cSReid Kleckner #include "llvm/Support/CommandLine.h" 33ef860a24SChandler Carruth #include "llvm/Support/Debug.h" 34ef860a24SChandler Carruth #include "llvm/Support/ErrorHandling.h" 35ef860a24SChandler Carruth #include "llvm/Support/ManagedStatic.h" 36799003bfSBenjamin Kramer #include "llvm/Support/raw_ostream.h" 37ef860a24SChandler Carruth #include <algorithm> 38ecefe5a8SEugene Zelenko 39ef860a24SChandler Carruth using namespace llvm; 40ef860a24SChandler Carruth 414c975578SSerge Guelton static cl::opt<unsigned> NonGlobalValueMaxNameSize( 424c975578SSerge Guelton "non-global-value-max-name-size", cl::Hidden, cl::init(1024), 434c975578SSerge Guelton cl::desc("Maximum size for the name of non-global values.")); 444c975578SSerge Guelton 45ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 46ef860a24SChandler Carruth // Value Class 47ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 48ef860a24SChandler Carruth static inline Type *checkType(Type *Ty) { 49ef860a24SChandler Carruth assert(Ty && "Value defined with a null type: Error!"); 5016bf89ecSReid Kleckner return Ty; 51ef860a24SChandler Carruth } 52ef860a24SChandler Carruth 53ef860a24SChandler Carruth Value::Value(Type *ty, unsigned scid) 54*7975b8c3SSerge Pavlov : VTy(checkType(ty)), UseList(nullptr), SubclassID(scid), HasValueHandle(0), 55*7975b8c3SSerge Pavlov SubclassOptionalData(0), SubclassData(0), NumUserOperands(0), 56*7975b8c3SSerge Pavlov IsUsedByMD(false), HasName(false), HasMetadata(false) { 579fd33f24SSerge Guelton static_assert(ConstantFirstVal == 0, "!(SubclassID < ConstantFirstVal)"); 58ef860a24SChandler Carruth // FIXME: Why isn't this in the subclass gunk?? 59ef860a24SChandler Carruth // Note, we cannot call isa<CallInst> before the CallInst has been 60ef860a24SChandler Carruth // constructed. 61784929d0SCraig Topper if (SubclassID == Instruction::Call || SubclassID == Instruction::Invoke || 62784929d0SCraig Topper SubclassID == Instruction::CallBr) 63ef860a24SChandler Carruth assert((VTy->isFirstClassType() || VTy->isVoidTy() || VTy->isStructTy()) && 64ef860a24SChandler Carruth "invalid CallInst type!"); 65ef860a24SChandler Carruth else if (SubclassID != BasicBlockVal && 669fd33f24SSerge Guelton (/*SubclassID < ConstantFirstVal ||*/ SubclassID > ConstantLastVal)) 67ef860a24SChandler Carruth assert((VTy->isFirstClassType() || VTy->isVoidTy()) && 68ef860a24SChandler Carruth "Cannot create non-first-class values except for constants!"); 6996ab8726SReid Kleckner static_assert(sizeof(Value) == 2 * sizeof(void *) + 2 * sizeof(unsigned), 701762ad3eSReid Kleckner "Value too big"); 71ef860a24SChandler Carruth } 72ef860a24SChandler Carruth 73ef860a24SChandler Carruth Value::~Value() { 74ef860a24SChandler Carruth // Notify all ValueHandles (if present) that this value is going away. 75ef860a24SChandler Carruth if (HasValueHandle) 76ef860a24SChandler Carruth ValueHandleBase::ValueIsDeleted(this); 775bf8fef5SDuncan P. N. Exon Smith if (isUsedByMetadata()) 785bf8fef5SDuncan P. N. Exon Smith ValueAsMetadata::handleDeletion(this); 79ef860a24SChandler Carruth 80*7975b8c3SSerge Pavlov // Remove associated metadata from context. 81*7975b8c3SSerge Pavlov if (HasMetadata) 82*7975b8c3SSerge Pavlov clearMetadata(); 83*7975b8c3SSerge Pavlov 84ef860a24SChandler Carruth #ifndef NDEBUG // Only in -g mode... 85ef860a24SChandler Carruth // Check to make sure that there are no uses of this value that are still 86ef860a24SChandler Carruth // around when the value is destroyed. If there are, then we have a dangling 87b93fb82bSAndrew Kaylor // reference and something is wrong. This code is here to print out where 88b93fb82bSAndrew Kaylor // the value is still being referenced. 89ef860a24SChandler Carruth // 907dd28109SEvgenii Stepanov // Note that use_empty() cannot be called here, as it eventually downcasts 917dd28109SEvgenii Stepanov // 'this' to GlobalValue (derived class of Value), but GlobalValue has already 927dd28109SEvgenii Stepanov // been destructed, so accessing it is UB. 937dd28109SEvgenii Stepanov // 947dd28109SEvgenii Stepanov if (!materialized_use_empty()) { 95ef860a24SChandler Carruth dbgs() << "While deleting: " << *VTy << " %" << getName() << "\n"; 96b93fb82bSAndrew Kaylor for (auto *U : users()) 97b93fb82bSAndrew Kaylor dbgs() << "Use still stuck around after Def is destroyed:" << *U << "\n"; 98ef860a24SChandler Carruth } 99ef860a24SChandler Carruth #endif 1007dd28109SEvgenii Stepanov assert(materialized_use_empty() && "Uses remain when a value is destroyed!"); 101ef860a24SChandler Carruth 102ef860a24SChandler Carruth // If this value is named, destroy the name. This should not be in a symtab 103ef860a24SChandler Carruth // at this point. 1045bf8fef5SDuncan P. N. Exon Smith destroyValueName(); 105ef860a24SChandler Carruth } 106ef860a24SChandler Carruth 10796ab8726SReid Kleckner void Value::deleteValue() { 10896ab8726SReid Kleckner switch (getValueID()) { 10996ab8726SReid Kleckner #define HANDLE_VALUE(Name) \ 11096ab8726SReid Kleckner case Value::Name##Val: \ 11196ab8726SReid Kleckner delete static_cast<Name *>(this); \ 11296ab8726SReid Kleckner break; 11396ab8726SReid Kleckner #define HANDLE_MEMORY_VALUE(Name) \ 11496ab8726SReid Kleckner case Value::Name##Val: \ 11596ab8726SReid Kleckner static_cast<DerivedUser *>(this)->DeleteValue( \ 11696ab8726SReid Kleckner static_cast<DerivedUser *>(this)); \ 11796ab8726SReid Kleckner break; 11815440191SEli Friedman #define HANDLE_CONSTANT(Name) \ 11915440191SEli Friedman case Value::Name##Val: \ 12015440191SEli Friedman llvm_unreachable("constants should be destroyed with destroyConstant"); \ 12115440191SEli Friedman break; 12296ab8726SReid Kleckner #define HANDLE_INSTRUCTION(Name) /* nothing */ 12396ab8726SReid Kleckner #include "llvm/IR/Value.def" 12496ab8726SReid Kleckner 12596ab8726SReid Kleckner #define HANDLE_INST(N, OPC, CLASS) \ 12696ab8726SReid Kleckner case Value::InstructionVal + Instruction::OPC: \ 12796ab8726SReid Kleckner delete static_cast<CLASS *>(this); \ 12896ab8726SReid Kleckner break; 12996ab8726SReid Kleckner #define HANDLE_USER_INST(N, OPC, CLASS) 13096ab8726SReid Kleckner #include "llvm/IR/Instruction.def" 13196ab8726SReid Kleckner 13296ab8726SReid Kleckner default: 13396ab8726SReid Kleckner llvm_unreachable("attempting to delete unknown value kind"); 13496ab8726SReid Kleckner } 13596ab8726SReid Kleckner } 13696ab8726SReid Kleckner 1375bf8fef5SDuncan P. N. Exon Smith void Value::destroyValueName() { 1385bf8fef5SDuncan P. N. Exon Smith ValueName *Name = getValueName(); 13989c8ffd5SChris Lattner if (Name) { 14089c8ffd5SChris Lattner MallocAllocator Allocator; 14189c8ffd5SChris Lattner Name->Destroy(Allocator); 14289c8ffd5SChris Lattner } 1435bf8fef5SDuncan P. N. Exon Smith setValueName(nullptr); 1445bf8fef5SDuncan P. N. Exon Smith } 1455bf8fef5SDuncan P. N. Exon Smith 146ef860a24SChandler Carruth bool Value::hasNUses(unsigned N) const { 1474de31bbaSVedant Kumar return hasNItems(use_begin(), use_end(), N); 148ef860a24SChandler Carruth } 149ef860a24SChandler Carruth 150ef860a24SChandler Carruth bool Value::hasNUsesOrMore(unsigned N) const { 1514de31bbaSVedant Kumar return hasNItemsOrMore(use_begin(), use_end(), N); 152ef860a24SChandler Carruth } 153ef860a24SChandler Carruth 15495848ea1SRoman Lebedev bool Value::hasOneUser() const { 15595848ea1SRoman Lebedev if (use_empty()) 15695848ea1SRoman Lebedev return false; 15795848ea1SRoman Lebedev if (hasOneUse()) 15895848ea1SRoman Lebedev return true; 15995848ea1SRoman Lebedev return std::equal(++user_begin(), user_end(), user_begin()); 16095848ea1SRoman Lebedev } 16195848ea1SRoman Lebedev 162f16f139dSTyker static bool isUnDroppableUser(const User *U) { return !U->isDroppable(); } 163f16f139dSTyker 164f16f139dSTyker Use *Value::getSingleUndroppableUse() { 165f16f139dSTyker Use *Result = nullptr; 166f16f139dSTyker for (Use &U : uses()) { 167f16f139dSTyker if (!U.getUser()->isDroppable()) { 168f16f139dSTyker if (Result) 169f16f139dSTyker return nullptr; 170f16f139dSTyker Result = &U; 171f16f139dSTyker } 172f16f139dSTyker } 173f16f139dSTyker return Result; 174f16f139dSTyker } 175f16f139dSTyker 176f16f139dSTyker bool Value::hasNUndroppableUses(unsigned int N) const { 177f16f139dSTyker return hasNItems(user_begin(), user_end(), N, isUnDroppableUser); 178f16f139dSTyker } 179f16f139dSTyker 180f16f139dSTyker bool Value::hasNUndroppableUsesOrMore(unsigned int N) const { 181f16f139dSTyker return hasNItemsOrMore(user_begin(), user_end(), N, isUnDroppableUser); 182f16f139dSTyker } 183f16f139dSTyker 184f16f139dSTyker void Value::dropDroppableUses( 185f16f139dSTyker llvm::function_ref<bool(const Use *)> ShouldDrop) { 186f16f139dSTyker SmallVector<Use *, 8> ToBeEdited; 187f16f139dSTyker for (Use &U : uses()) 188f16f139dSTyker if (U.getUser()->isDroppable() && ShouldDrop(&U)) 189f16f139dSTyker ToBeEdited.push_back(&U); 190450dc09dSJohannes Doerfert for (Use *U : ToBeEdited) 191450dc09dSJohannes Doerfert dropDroppableUse(*U); 192f16f139dSTyker } 193450dc09dSJohannes Doerfert 194450dc09dSJohannes Doerfert void Value::dropDroppableUsesIn(User &Usr) { 195450dc09dSJohannes Doerfert assert(Usr.isDroppable() && "Expected a droppable user!"); 196450dc09dSJohannes Doerfert for (Use &UsrOp : Usr.operands()) { 197450dc09dSJohannes Doerfert if (UsrOp.get() == this) 198450dc09dSJohannes Doerfert dropDroppableUse(UsrOp); 199f16f139dSTyker } 200f16f139dSTyker } 201f16f139dSTyker 202450dc09dSJohannes Doerfert void Value::dropDroppableUse(Use &U) { 203450dc09dSJohannes Doerfert U.removeFromList(); 204450dc09dSJohannes Doerfert if (auto *Assume = dyn_cast<IntrinsicInst>(U.getUser())) { 205450dc09dSJohannes Doerfert assert(Assume->getIntrinsicID() == Intrinsic::assume); 206450dc09dSJohannes Doerfert unsigned OpNo = U.getOperandNo(); 207450dc09dSJohannes Doerfert if (OpNo == 0) 208450dc09dSJohannes Doerfert U.set(ConstantInt::getTrue(Assume->getContext())); 209450dc09dSJohannes Doerfert else { 210450dc09dSJohannes Doerfert U.set(UndefValue::get(U.get()->getType())); 211450dc09dSJohannes Doerfert CallInst::BundleOpInfo &BOI = Assume->getBundleOpInfoForOperand(OpNo); 2126d365741STyker BOI.Tag = Assume->getContext().pImpl->getOrInsertBundleTag("ignore"); 213450dc09dSJohannes Doerfert } 214450dc09dSJohannes Doerfert return; 215450dc09dSJohannes Doerfert } 216450dc09dSJohannes Doerfert 217450dc09dSJohannes Doerfert llvm_unreachable("unkown droppable use"); 218ce8928f2SJohannes Doerfert } 219ce8928f2SJohannes Doerfert 220ef860a24SChandler Carruth bool Value::isUsedInBasicBlock(const BasicBlock *BB) const { 2210925b24dSJakob Stoklund Olesen // This can be computed either by scanning the instructions in BB, or by 2220925b24dSJakob Stoklund Olesen // scanning the use list of this Value. Both lists can be very long, but 2230925b24dSJakob Stoklund Olesen // usually one is quite short. 2240925b24dSJakob Stoklund Olesen // 2250925b24dSJakob Stoklund Olesen // Scan both lists simultaneously until one is exhausted. This limits the 2260925b24dSJakob Stoklund Olesen // search to the shorter list. 2270925b24dSJakob Stoklund Olesen BasicBlock::const_iterator BI = BB->begin(), BE = BB->end(); 228cdf47884SChandler Carruth const_user_iterator UI = user_begin(), UE = user_end(); 2290925b24dSJakob Stoklund Olesen for (; BI != BE && UI != UE; ++BI, ++UI) { 2300925b24dSJakob Stoklund Olesen // Scan basic block: Check if this Value is used by the instruction at BI. 23142531260SDavid Majnemer if (is_contained(BI->operands(), this)) 232ef860a24SChandler Carruth return true; 2330925b24dSJakob Stoklund Olesen // Scan use list: Check if the use at UI is in BB. 23442531260SDavid Majnemer const auto *User = dyn_cast<Instruction>(*UI); 235ef860a24SChandler Carruth if (User && User->getParent() == BB) 236ef860a24SChandler Carruth return true; 237ef860a24SChandler Carruth } 238ef860a24SChandler Carruth return false; 239ef860a24SChandler Carruth } 240ef860a24SChandler Carruth 2415a0872c2SVedant Kumar unsigned Value::getNumUses() const { 2425a0872c2SVedant Kumar return (unsigned)std::distance(use_begin(), use_end()); 2435a0872c2SVedant Kumar } 244ef860a24SChandler Carruth 245ef860a24SChandler Carruth static bool getSymTab(Value *V, ValueSymbolTable *&ST) { 246c620761cSCraig Topper ST = nullptr; 247ef860a24SChandler Carruth if (Instruction *I = dyn_cast<Instruction>(V)) { 248ef860a24SChandler Carruth if (BasicBlock *P = I->getParent()) 249ef860a24SChandler Carruth if (Function *PP = P->getParent()) 250a53d49e1SMehdi Amini ST = PP->getValueSymbolTable(); 251ef860a24SChandler Carruth } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) { 252ef860a24SChandler Carruth if (Function *P = BB->getParent()) 253a53d49e1SMehdi Amini ST = P->getValueSymbolTable(); 254ef860a24SChandler Carruth } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { 255ef860a24SChandler Carruth if (Module *P = GV->getParent()) 256ef860a24SChandler Carruth ST = &P->getValueSymbolTable(); 257ef860a24SChandler Carruth } else if (Argument *A = dyn_cast<Argument>(V)) { 258ef860a24SChandler Carruth if (Function *P = A->getParent()) 259a53d49e1SMehdi Amini ST = P->getValueSymbolTable(); 2605bf8fef5SDuncan P. N. Exon Smith } else { 261ef860a24SChandler Carruth assert(isa<Constant>(V) && "Unknown value type!"); 262ef860a24SChandler Carruth return true; // no name is setable for this. 263ef860a24SChandler Carruth } 264ef860a24SChandler Carruth return false; 265ef860a24SChandler Carruth } 266ef860a24SChandler Carruth 2677349ab93SOwen Anderson ValueName *Value::getValueName() const { 2687349ab93SOwen Anderson if (!HasName) return nullptr; 2697349ab93SOwen Anderson 2707349ab93SOwen Anderson LLVMContext &Ctx = getContext(); 2717349ab93SOwen Anderson auto I = Ctx.pImpl->ValueNames.find(this); 2727349ab93SOwen Anderson assert(I != Ctx.pImpl->ValueNames.end() && 2737349ab93SOwen Anderson "No name entry found!"); 2747349ab93SOwen Anderson 2757349ab93SOwen Anderson return I->second; 2767349ab93SOwen Anderson } 2777349ab93SOwen Anderson 2787349ab93SOwen Anderson void Value::setValueName(ValueName *VN) { 2797349ab93SOwen Anderson LLVMContext &Ctx = getContext(); 2807349ab93SOwen Anderson 2817349ab93SOwen Anderson assert(HasName == Ctx.pImpl->ValueNames.count(this) && 2827349ab93SOwen Anderson "HasName bit out of sync!"); 2837349ab93SOwen Anderson 2847349ab93SOwen Anderson if (!VN) { 2857349ab93SOwen Anderson if (HasName) 2867349ab93SOwen Anderson Ctx.pImpl->ValueNames.erase(this); 2877349ab93SOwen Anderson HasName = false; 2887349ab93SOwen Anderson return; 2897349ab93SOwen Anderson } 2907349ab93SOwen Anderson 2917349ab93SOwen Anderson HasName = true; 2927349ab93SOwen Anderson Ctx.pImpl->ValueNames[this] = VN; 2937349ab93SOwen Anderson } 2947349ab93SOwen Anderson 295ef860a24SChandler Carruth StringRef Value::getName() const { 296ef860a24SChandler Carruth // Make sure the empty string is still a C string. For historical reasons, 297ef860a24SChandler Carruth // some clients want to call .data() on the result and expect it to be null 298ef860a24SChandler Carruth // terminated. 2997349ab93SOwen Anderson if (!hasName()) 3005bf8fef5SDuncan P. N. Exon Smith return StringRef("", 0); 3015bf8fef5SDuncan P. N. Exon Smith return getValueName()->getKey(); 302ef860a24SChandler Carruth } 303ef860a24SChandler Carruth 304a7c0c18cSPete Cooper void Value::setNameImpl(const Twine &NewName) { 30509b4a8daSMehdi Amini // Fast-path: LLVMContext can be set to strip out non-GlobalValue names 30650af49fcSMehdi Amini if (getContext().shouldDiscardValueNames() && !isa<GlobalValue>(this)) 30709b4a8daSMehdi Amini return; 30809b4a8daSMehdi Amini 309ef860a24SChandler Carruth // Fast path for common IRBuilder case of setName("") when there is no name. 310ef860a24SChandler Carruth if (NewName.isTriviallyEmpty() && !hasName()) 311ef860a24SChandler Carruth return; 312ef860a24SChandler Carruth 313ef860a24SChandler Carruth SmallString<256> NameData; 314ef860a24SChandler Carruth StringRef NameRef = NewName.toStringRef(NameData); 31509d689f9SRafael Espindola assert(NameRef.find_first_of(0) == StringRef::npos && 31609d689f9SRafael Espindola "Null bytes are not allowed in names"); 317ef860a24SChandler Carruth 318ef860a24SChandler Carruth // Name isn't changing? 319ef860a24SChandler Carruth if (getName() == NameRef) 320ef860a24SChandler Carruth return; 321ef860a24SChandler Carruth 3224c975578SSerge Guelton // Cap the size of non-GlobalValue names. 3234c975578SSerge Guelton if (NameRef.size() > NonGlobalValueMaxNameSize && !isa<GlobalValue>(this)) 3244c975578SSerge Guelton NameRef = 3254c975578SSerge Guelton NameRef.substr(0, std::max(1u, (unsigned)NonGlobalValueMaxNameSize)); 3264c975578SSerge Guelton 327ef860a24SChandler Carruth assert(!getType()->isVoidTy() && "Cannot assign a name to void values!"); 328ef860a24SChandler Carruth 329ef860a24SChandler Carruth // Get the symbol table to update for this object. 330ef860a24SChandler Carruth ValueSymbolTable *ST; 331ef860a24SChandler Carruth if (getSymTab(this, ST)) 332ef860a24SChandler Carruth return; // Cannot set a name on this value (e.g. constant). 333ef860a24SChandler Carruth 334ef860a24SChandler Carruth if (!ST) { // No symbol table to update? Just do the change. 335ef860a24SChandler Carruth if (NameRef.empty()) { 336ef860a24SChandler Carruth // Free the name for this value. 3375bf8fef5SDuncan P. N. Exon Smith destroyValueName(); 338ef860a24SChandler Carruth return; 339ef860a24SChandler Carruth } 340ef860a24SChandler Carruth 341ef860a24SChandler Carruth // NOTE: Could optimize for the case the name is shrinking to not deallocate 342ef860a24SChandler Carruth // then reallocated. 3435bf8fef5SDuncan P. N. Exon Smith destroyValueName(); 344ef860a24SChandler Carruth 345ef860a24SChandler Carruth // Create the new name. 34689c8ffd5SChris Lattner MallocAllocator Allocator; 34789c8ffd5SChris Lattner setValueName(ValueName::Create(NameRef, Allocator)); 3485bf8fef5SDuncan P. N. Exon Smith getValueName()->setValue(this); 349ef860a24SChandler Carruth return; 350ef860a24SChandler Carruth } 351ef860a24SChandler Carruth 352ef860a24SChandler Carruth // NOTE: Could optimize for the case the name is shrinking to not deallocate 353ef860a24SChandler Carruth // then reallocated. 354ef860a24SChandler Carruth if (hasName()) { 355ef860a24SChandler Carruth // Remove old name. 3565bf8fef5SDuncan P. N. Exon Smith ST->removeValueName(getValueName()); 3575bf8fef5SDuncan P. N. Exon Smith destroyValueName(); 358ef860a24SChandler Carruth 359ef860a24SChandler Carruth if (NameRef.empty()) 360ef860a24SChandler Carruth return; 361ef860a24SChandler Carruth } 362ef860a24SChandler Carruth 363ef860a24SChandler Carruth // Name is changing to something new. 3645bf8fef5SDuncan P. N. Exon Smith setValueName(ST->createValueName(NameRef, this)); 365ef860a24SChandler Carruth } 366ef860a24SChandler Carruth 367a7c0c18cSPete Cooper void Value::setName(const Twine &NewName) { 368a7c0c18cSPete Cooper setNameImpl(NewName); 369a7c0c18cSPete Cooper if (Function *F = dyn_cast<Function>(this)) 370a7c0c18cSPete Cooper F->recalculateIntrinsicID(); 371a7c0c18cSPete Cooper } 372a7c0c18cSPete Cooper 373ef860a24SChandler Carruth void Value::takeName(Value *V) { 374c620761cSCraig Topper ValueSymbolTable *ST = nullptr; 375ef860a24SChandler Carruth // If this value has a name, drop it. 376ef860a24SChandler Carruth if (hasName()) { 377ef860a24SChandler Carruth // Get the symtab this is in. 378ef860a24SChandler Carruth if (getSymTab(this, ST)) { 379ef860a24SChandler Carruth // We can't set a name on this value, but we need to clear V's name if 380ef860a24SChandler Carruth // it has one. 381ef860a24SChandler Carruth if (V->hasName()) V->setName(""); 382ef860a24SChandler Carruth return; // Cannot set a name on this value (e.g. constant). 383ef860a24SChandler Carruth } 384ef860a24SChandler Carruth 385ef860a24SChandler Carruth // Remove old name. 386ef860a24SChandler Carruth if (ST) 3875bf8fef5SDuncan P. N. Exon Smith ST->removeValueName(getValueName()); 3885bf8fef5SDuncan P. N. Exon Smith destroyValueName(); 389ef860a24SChandler Carruth } 390ef860a24SChandler Carruth 391ef860a24SChandler Carruth // Now we know that this has no name. 392ef860a24SChandler Carruth 393ef860a24SChandler Carruth // If V has no name either, we're done. 394ef860a24SChandler Carruth if (!V->hasName()) return; 395ef860a24SChandler Carruth 396ef860a24SChandler Carruth // Get this's symtab if we didn't before. 397ef860a24SChandler Carruth if (!ST) { 398ef860a24SChandler Carruth if (getSymTab(this, ST)) { 399ef860a24SChandler Carruth // Clear V's name. 400ef860a24SChandler Carruth V->setName(""); 401ef860a24SChandler Carruth return; // Cannot set a name on this value (e.g. constant). 402ef860a24SChandler Carruth } 403ef860a24SChandler Carruth } 404ef860a24SChandler Carruth 405ef860a24SChandler Carruth // Get V's ST, this should always succed, because V has a name. 406ef860a24SChandler Carruth ValueSymbolTable *VST; 407ef860a24SChandler Carruth bool Failure = getSymTab(V, VST); 408ef860a24SChandler Carruth assert(!Failure && "V has a name, so it should have a ST!"); (void)Failure; 409ef860a24SChandler Carruth 410ef860a24SChandler Carruth // If these values are both in the same symtab, we can do this very fast. 411ef860a24SChandler Carruth // This works even if both values have no symtab yet. 412ef860a24SChandler Carruth if (ST == VST) { 413ef860a24SChandler Carruth // Take the name! 4145bf8fef5SDuncan P. N. Exon Smith setValueName(V->getValueName()); 4155bf8fef5SDuncan P. N. Exon Smith V->setValueName(nullptr); 4165bf8fef5SDuncan P. N. Exon Smith getValueName()->setValue(this); 417ef860a24SChandler Carruth return; 418ef860a24SChandler Carruth } 419ef860a24SChandler Carruth 420ef860a24SChandler Carruth // Otherwise, things are slightly more complex. Remove V's name from VST and 421ef860a24SChandler Carruth // then reinsert it into ST. 422ef860a24SChandler Carruth 423ef860a24SChandler Carruth if (VST) 4245bf8fef5SDuncan P. N. Exon Smith VST->removeValueName(V->getValueName()); 4255bf8fef5SDuncan P. N. Exon Smith setValueName(V->getValueName()); 4265bf8fef5SDuncan P. N. Exon Smith V->setValueName(nullptr); 4275bf8fef5SDuncan P. N. Exon Smith getValueName()->setValue(this); 428ef860a24SChandler Carruth 429ef860a24SChandler Carruth if (ST) 430ef860a24SChandler Carruth ST->reinsertValue(this); 431ef860a24SChandler Carruth } 432ef860a24SChandler Carruth 433eec48903SCraig Topper void Value::assertModuleIsMaterializedImpl() const { 434675bdcedSTodd Fiala #ifndef NDEBUG 435257a3536SRafael Espindola const GlobalValue *GV = dyn_cast<GlobalValue>(this); 436257a3536SRafael Espindola if (!GV) 437257a3536SRafael Espindola return; 438257a3536SRafael Espindola const Module *M = GV->getParent(); 439257a3536SRafael Espindola if (!M) 440257a3536SRafael Espindola return; 441257a3536SRafael Espindola assert(M->isMaterialized()); 442675bdcedSTodd Fiala #endif 443257a3536SRafael Espindola } 444257a3536SRafael Espindola 445675bdcedSTodd Fiala #ifndef NDEBUG 44671b7b68bSCraig Topper static bool contains(SmallPtrSetImpl<ConstantExpr *> &Cache, ConstantExpr *Expr, 4477e2b7567SRafael Espindola Constant *C) { 44870573dcdSDavid Blaikie if (!Cache.insert(Expr).second) 4497e2b7567SRafael Espindola return false; 4507e2b7567SRafael Espindola 4517e2b7567SRafael Espindola for (auto &O : Expr->operands()) { 4527e2b7567SRafael Espindola if (O == C) 4537e2b7567SRafael Espindola return true; 4547e2b7567SRafael Espindola auto *CE = dyn_cast<ConstantExpr>(O); 4557e2b7567SRafael Espindola if (!CE) 4567e2b7567SRafael Espindola continue; 4577e2b7567SRafael Espindola if (contains(Cache, CE, C)) 4587e2b7567SRafael Espindola return true; 4597e2b7567SRafael Espindola } 4607e2b7567SRafael Espindola return false; 4617e2b7567SRafael Espindola } 4627e2b7567SRafael Espindola 4637e2b7567SRafael Espindola static bool contains(Value *Expr, Value *V) { 4647e2b7567SRafael Espindola if (Expr == V) 4657e2b7567SRafael Espindola return true; 4667e2b7567SRafael Espindola 4677e2b7567SRafael Espindola auto *C = dyn_cast<Constant>(V); 4687e2b7567SRafael Espindola if (!C) 4697e2b7567SRafael Espindola return false; 4707e2b7567SRafael Espindola 4717e2b7567SRafael Espindola auto *CE = dyn_cast<ConstantExpr>(Expr); 4727e2b7567SRafael Espindola if (!CE) 4737e2b7567SRafael Espindola return false; 4747e2b7567SRafael Espindola 4757e2b7567SRafael Espindola SmallPtrSet<ConstantExpr *, 4> Cache; 4767e2b7567SRafael Espindola return contains(Cache, CE, C); 4777e2b7567SRafael Espindola } 478ecefe5a8SEugene Zelenko #endif // NDEBUG 479ef860a24SChandler Carruth 480e69ae76bSJF Bastien void Value::doRAUW(Value *New, ReplaceMetadataUses ReplaceMetaUses) { 481ef860a24SChandler Carruth assert(New && "Value::replaceAllUsesWith(<null>) is invalid!"); 4827e2b7567SRafael Espindola assert(!contains(New, this) && 4837e2b7567SRafael Espindola "this->replaceAllUsesWith(expr(this)) is NOT valid!"); 484ef860a24SChandler Carruth assert(New->getType() == getType() && 485ef860a24SChandler Carruth "replaceAllUses of value with new value of different type!"); 486ef860a24SChandler Carruth 487ef860a24SChandler Carruth // Notify all ValueHandles (if present) that this value is going away. 488ef860a24SChandler Carruth if (HasValueHandle) 489ef860a24SChandler Carruth ValueHandleBase::ValueIsRAUWd(this, New); 490e69ae76bSJF Bastien if (ReplaceMetaUses == ReplaceMetadataUses::Yes && isUsedByMetadata()) 4915bf8fef5SDuncan P. N. Exon Smith ValueAsMetadata::handleRAUW(this, New); 492ef860a24SChandler Carruth 49381bbf742STeresa Johnson while (!materialized_use_empty()) { 494ef860a24SChandler Carruth Use &U = *UseList; 495ef860a24SChandler Carruth // Must handle Constants specially, we cannot call replaceUsesOfWith on a 496ef860a24SChandler Carruth // constant because they are uniqued. 4976b238633SRafael Espindola if (auto *C = dyn_cast<Constant>(U.getUser())) { 498ef860a24SChandler Carruth if (!isa<GlobalValue>(C)) { 4998914d290SMehdi Amini C->handleOperandChange(this, New); 500ef860a24SChandler Carruth continue; 501ef860a24SChandler Carruth } 502ef860a24SChandler Carruth } 503ef860a24SChandler Carruth 504ef860a24SChandler Carruth U.set(New); 505ef860a24SChandler Carruth } 506ef860a24SChandler Carruth 507ef860a24SChandler Carruth if (BasicBlock *BB = dyn_cast<BasicBlock>(this)) 508ef860a24SChandler Carruth BB->replaceSuccessorsPhiUsesWith(cast<BasicBlock>(New)); 509ef860a24SChandler Carruth } 510ef860a24SChandler Carruth 5111ed53963SDavide Italiano void Value::replaceAllUsesWith(Value *New) { 512e69ae76bSJF Bastien doRAUW(New, ReplaceMetadataUses::Yes); 5131ed53963SDavide Italiano } 5141ed53963SDavide Italiano 5151ed53963SDavide Italiano void Value::replaceNonMetadataUsesWith(Value *New) { 516e69ae76bSJF Bastien doRAUW(New, ReplaceMetadataUses::No); 5171ed53963SDavide Italiano } 5181ed53963SDavide Italiano 519ec6217c9SGerolf Hoflehner // Like replaceAllUsesWith except it does not handle constants or basic blocks. 520ec6217c9SGerolf Hoflehner // This routine leaves uses within BB. 521ec6217c9SGerolf Hoflehner void Value::replaceUsesOutsideBlock(Value *New, BasicBlock *BB) { 522ec6217c9SGerolf Hoflehner assert(New && "Value::replaceUsesOutsideBlock(<null>, BB) is invalid!"); 523ec6217c9SGerolf Hoflehner assert(!contains(New, this) && 524ec6217c9SGerolf Hoflehner "this->replaceUsesOutsideBlock(expr(this), BB) is NOT valid!"); 525ec6217c9SGerolf Hoflehner assert(New->getType() == getType() && 526ec6217c9SGerolf Hoflehner "replaceUses of value with new value of different type!"); 527ec6217c9SGerolf Hoflehner assert(BB && "Basic block that may contain a use of 'New' must be defined\n"); 528ec6217c9SGerolf Hoflehner 529081e990dSRoman Lebedev replaceUsesWithIf(New, [BB](Use &U) { 530081e990dSRoman Lebedev auto *I = dyn_cast<Instruction>(U.getUser()); 531081e990dSRoman Lebedev // Don't replace if it's an instruction in the BB basic block. 532081e990dSRoman Lebedev return !I || I->getParent() != BB; 533081e990dSRoman Lebedev }); 534ec6217c9SGerolf Hoflehner } 535ec6217c9SGerolf Hoflehner 536ef860a24SChandler Carruth namespace { 537ef860a24SChandler Carruth // Various metrics for how much to strip off of pointers. 538ef860a24SChandler Carruth enum PointerStripKind { 539ef860a24SChandler Carruth PSK_ZeroIndices, 5408a410bceSErich Keane PSK_ZeroIndicesAndAliases, 5412452d703SPeter Collingbourne PSK_ZeroIndicesSameRepresentation, 5422452d703SPeter Collingbourne PSK_ZeroIndicesAndInvariantGroups, 543ef860a24SChandler Carruth PSK_InBoundsConstantIndices, 544ef860a24SChandler Carruth PSK_InBounds 545ef860a24SChandler Carruth }; 546ef860a24SChandler Carruth 54767448a8cSTyker template <PointerStripKind StripKind> static void NoopCallback(const Value *) {} 54867448a8cSTyker 549ef860a24SChandler Carruth template <PointerStripKind StripKind> 550b7338fb1STyker static const Value *stripPointerCastsAndOffsets( 551b7338fb1STyker const Value *V, 55267448a8cSTyker function_ref<void(const Value *)> Func = NoopCallback<StripKind>) { 553ef860a24SChandler Carruth if (!V->getType()->isPointerTy()) 554ef860a24SChandler Carruth return V; 555ef860a24SChandler Carruth 556ef860a24SChandler Carruth // Even though we don't look through PHI nodes, we could be called on an 557ef860a24SChandler Carruth // instruction in an unreachable block, which may be on a cycle. 5581dd20e65SCraig Topper SmallPtrSet<const Value *, 4> Visited; 559ef860a24SChandler Carruth 560ef860a24SChandler Carruth Visited.insert(V); 561ef860a24SChandler Carruth do { 562b7338fb1STyker Func(V); 5631dd20e65SCraig Topper if (auto *GEP = dyn_cast<GEPOperator>(V)) { 564ef860a24SChandler Carruth switch (StripKind) { 565ef860a24SChandler Carruth case PSK_ZeroIndices: 5668a410bceSErich Keane case PSK_ZeroIndicesAndAliases: 5672452d703SPeter Collingbourne case PSK_ZeroIndicesSameRepresentation: 5682452d703SPeter Collingbourne case PSK_ZeroIndicesAndInvariantGroups: 569ef860a24SChandler Carruth if (!GEP->hasAllZeroIndices()) 570ef860a24SChandler Carruth return V; 571ef860a24SChandler Carruth break; 572ef860a24SChandler Carruth case PSK_InBoundsConstantIndices: 573ef860a24SChandler Carruth if (!GEP->hasAllConstantIndices()) 574ef860a24SChandler Carruth return V; 575b03fd12cSJustin Bogner LLVM_FALLTHROUGH; 576ef860a24SChandler Carruth case PSK_InBounds: 577ef860a24SChandler Carruth if (!GEP->isInBounds()) 578ef860a24SChandler Carruth return V; 579ef860a24SChandler Carruth break; 580ef860a24SChandler Carruth } 581ef860a24SChandler Carruth V = GEP->getPointerOperand(); 58240107ce7SJohannes Doerfert } else if (Operator::getOpcode(V) == Instruction::BitCast) { 58340107ce7SJohannes Doerfert V = cast<Operator>(V)->getOperand(0); 5842b8d706bSRoman Lebedev if (!V->getType()->isPointerTy()) 5852b8d706bSRoman Lebedev return V; 5862452d703SPeter Collingbourne } else if (StripKind != PSK_ZeroIndicesSameRepresentation && 587b03bd4d9SMatt Arsenault Operator::getOpcode(V) == Instruction::AddrSpaceCast) { 58840107ce7SJohannes Doerfert // TODO: If we know an address space cast will not change the 58940107ce7SJohannes Doerfert // representation we could look through it here as well. 590ef860a24SChandler Carruth V = cast<Operator>(V)->getOperand(0); 5918a410bceSErich Keane } else if (StripKind == PSK_ZeroIndicesAndAliases && isa<GlobalAlias>(V)) { 5928a410bceSErich Keane V = cast<GlobalAlias>(V)->getAliasee(); 593ef860a24SChandler Carruth } else { 59490c09232SChandler Carruth if (const auto *Call = dyn_cast<CallBase>(V)) { 59590c09232SChandler Carruth if (const Value *RV = Call->getReturnedArgOperand()) { 5965c12d8feSHal Finkel V = RV; 5975c12d8feSHal Finkel continue; 5985c12d8feSHal Finkel } 5995dde8094SPiotr Padlewski // The result of launder.invariant.group must alias it's argument, 600610c966aSPiotr Padlewski // but it can't be marked with returned attribute, that's why it needs 601610c966aSPiotr Padlewski // special case. 6022452d703SPeter Collingbourne if (StripKind == PSK_ZeroIndicesAndInvariantGroups && 60390c09232SChandler Carruth (Call->getIntrinsicID() == Intrinsic::launder_invariant_group || 60490c09232SChandler Carruth Call->getIntrinsicID() == Intrinsic::strip_invariant_group)) { 60590c09232SChandler Carruth V = Call->getArgOperand(0); 606610c966aSPiotr Padlewski continue; 607610c966aSPiotr Padlewski } 608610c966aSPiotr Padlewski } 609ef860a24SChandler Carruth return V; 610ef860a24SChandler Carruth } 611ef860a24SChandler Carruth assert(V->getType()->isPointerTy() && "Unexpected operand type!"); 61270573dcdSDavid Blaikie } while (Visited.insert(V).second); 613ef860a24SChandler Carruth 614ef860a24SChandler Carruth return V; 615ef860a24SChandler Carruth } 616ecefe5a8SEugene Zelenko } // end anonymous namespace 617ef860a24SChandler Carruth 6181dd20e65SCraig Topper const Value *Value::stripPointerCasts() const { 6192452d703SPeter Collingbourne return stripPointerCastsAndOffsets<PSK_ZeroIndices>(this); 620c229a4ffSRafael Espindola } 621c229a4ffSRafael Espindola 6228a410bceSErich Keane const Value *Value::stripPointerCastsAndAliases() const { 6238a410bceSErich Keane return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndAliases>(this); 6248a410bceSErich Keane } 6258a410bceSErich Keane 62640107ce7SJohannes Doerfert const Value *Value::stripPointerCastsSameRepresentation() const { 6272452d703SPeter Collingbourne return stripPointerCastsAndOffsets<PSK_ZeroIndicesSameRepresentation>(this); 628ef860a24SChandler Carruth } 629ef860a24SChandler Carruth 6301dd20e65SCraig Topper const Value *Value::stripInBoundsConstantOffsets() const { 631ef860a24SChandler Carruth return stripPointerCastsAndOffsets<PSK_InBoundsConstantIndices>(this); 632ef860a24SChandler Carruth } 633ef860a24SChandler Carruth 6345dde8094SPiotr Padlewski const Value *Value::stripPointerCastsAndInvariantGroups() const { 6352452d703SPeter Collingbourne return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndInvariantGroups>(this); 636610c966aSPiotr Padlewski } 637610c966aSPiotr Padlewski 638e5780776SKuter Dinel const Value *Value::stripAndAccumulateConstantOffsets( 639e5780776SKuter Dinel const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, 640e5780776SKuter Dinel function_ref<bool(Value &, APInt &)> ExternalAnalysis) const { 6413ed286a3SJohannes Doerfert if (!getType()->isPtrOrPtrVectorTy()) 642989e6308SChandler Carruth return this; 643989e6308SChandler Carruth 6443ed286a3SJohannes Doerfert unsigned BitWidth = Offset.getBitWidth(); 6453ed286a3SJohannes Doerfert assert(BitWidth == DL.getIndexTypeSizeInBits(getType()) && 646945b7e5aSElena Demikhovsky "The offset bit width does not match the DL specification."); 647989e6308SChandler Carruth 648989e6308SChandler Carruth // Even though we don't look through PHI nodes, we could be called on an 649989e6308SChandler Carruth // instruction in an unreachable block, which may be on a cycle. 6501dd20e65SCraig Topper SmallPtrSet<const Value *, 4> Visited; 651989e6308SChandler Carruth Visited.insert(this); 6521dd20e65SCraig Topper const Value *V = this; 653989e6308SChandler Carruth do { 6541dd20e65SCraig Topper if (auto *GEP = dyn_cast<GEPOperator>(V)) { 6553ed286a3SJohannes Doerfert // If in-bounds was requested, we do not strip non-in-bounds GEPs. 6563ed286a3SJohannes Doerfert if (!AllowNonInbounds && !GEP->isInBounds()) 657989e6308SChandler Carruth return V; 6583ed286a3SJohannes Doerfert 6593ed286a3SJohannes Doerfert // If one of the values we have visited is an addrspacecast, then 6603ed286a3SJohannes Doerfert // the pointer type of this GEP may be different from the type 6613ed286a3SJohannes Doerfert // of the Ptr parameter which was passed to this function. This 6623ed286a3SJohannes Doerfert // means when we construct GEPOffset, we need to use the size 6633ed286a3SJohannes Doerfert // of GEP's pointer type rather than the size of the original 6643ed286a3SJohannes Doerfert // pointer type. 6653ed286a3SJohannes Doerfert APInt GEPOffset(DL.getIndexTypeSizeInBits(V->getType()), 0); 666e5780776SKuter Dinel if (!GEP->accumulateConstantOffset(DL, GEPOffset, ExternalAnalysis)) 667989e6308SChandler Carruth return V; 6683ed286a3SJohannes Doerfert 6693ed286a3SJohannes Doerfert // Stop traversal if the pointer offset wouldn't fit in the bit-width 6703ed286a3SJohannes Doerfert // provided by the Offset argument. This can happen due to AddrSpaceCast 6713ed286a3SJohannes Doerfert // stripping. 6723ed286a3SJohannes Doerfert if (GEPOffset.getMinSignedBits() > BitWidth) 6733ed286a3SJohannes Doerfert return V; 6743ed286a3SJohannes Doerfert 675e5780776SKuter Dinel // External Analysis can return a result higher/lower than the value 676e5780776SKuter Dinel // represents. We need to detect overflow/underflow. 677e5780776SKuter Dinel APInt GEPOffsetST = GEPOffset.sextOrTrunc(BitWidth); 678e5780776SKuter Dinel if (!ExternalAnalysis) { 679e5780776SKuter Dinel Offset += GEPOffsetST; 680e5780776SKuter Dinel } else { 681e5780776SKuter Dinel bool Overflow = false; 682e5780776SKuter Dinel APInt OldOffset = Offset; 683e5780776SKuter Dinel Offset = Offset.sadd_ov(GEPOffsetST, Overflow); 684e5780776SKuter Dinel if (Overflow) { 685e5780776SKuter Dinel Offset = OldOffset; 686e5780776SKuter Dinel return V; 687e5780776SKuter Dinel } 688e5780776SKuter Dinel } 689989e6308SChandler Carruth V = GEP->getPointerOperand(); 6903ed286a3SJohannes Doerfert } else if (Operator::getOpcode(V) == Instruction::BitCast || 6913ed286a3SJohannes Doerfert Operator::getOpcode(V) == Instruction::AddrSpaceCast) { 692989e6308SChandler Carruth V = cast<Operator>(V)->getOperand(0); 6931dd20e65SCraig Topper } else if (auto *GA = dyn_cast<GlobalAlias>(V)) { 6943ed286a3SJohannes Doerfert if (!GA->isInterposable()) 695989e6308SChandler Carruth V = GA->getAliasee(); 6963ed286a3SJohannes Doerfert } else if (const auto *Call = dyn_cast<CallBase>(V)) { 6973ed286a3SJohannes Doerfert if (const Value *RV = Call->getReturnedArgOperand()) 6985c12d8feSHal Finkel V = RV; 6995c12d8feSHal Finkel } 7003ed286a3SJohannes Doerfert assert(V->getType()->isPtrOrPtrVectorTy() && "Unexpected operand type!"); 70170573dcdSDavid Blaikie } while (Visited.insert(V).second); 702989e6308SChandler Carruth 703989e6308SChandler Carruth return V; 704989e6308SChandler Carruth } 705989e6308SChandler Carruth 706b7338fb1STyker const Value * 707b7338fb1STyker Value::stripInBoundsOffsets(function_ref<void(const Value *)> Func) const { 708b7338fb1STyker return stripPointerCastsAndOffsets<PSK_InBounds>(this, Func); 709ef860a24SChandler Carruth } 710ef860a24SChandler Carruth 7113603de2fSBjorn Steinbrink uint64_t Value::getPointerDereferenceableBytes(const DataLayout &DL, 71200953cbeSSanjoy Das bool &CanBeNull) const { 713345f0148SArtur Pilipenko assert(getType()->isPointerTy() && "must be pointer"); 714345f0148SArtur Pilipenko 7153603de2fSBjorn Steinbrink uint64_t DerefBytes = 0; 716345f0148SArtur Pilipenko CanBeNull = false; 717345f0148SArtur Pilipenko if (const Argument *A = dyn_cast<Argument>(this)) { 718345f0148SArtur Pilipenko DerefBytes = A->getDereferenceableBytes(); 719dc08185cSMatt Arsenault if (DerefBytes == 0) { 720dc08185cSMatt Arsenault // Handle byval/byref/inalloca/preallocated arguments 721dc08185cSMatt Arsenault if (Type *ArgMemTy = A->getPointeeInMemoryValueType()) { 722dc08185cSMatt Arsenault if (ArgMemTy->isSized()) { 723dc08185cSMatt Arsenault // FIXME: Why isn't this the type alloc size? 724dc08185cSMatt Arsenault DerefBytes = DL.getTypeStoreSize(ArgMemTy).getKnownMinSize(); 72500953cbeSSanjoy Das } 726dc08185cSMatt Arsenault } 727dc08185cSMatt Arsenault } 728dc08185cSMatt Arsenault 729345f0148SArtur Pilipenko if (DerefBytes == 0) { 730345f0148SArtur Pilipenko DerefBytes = A->getDereferenceableOrNullBytes(); 731345f0148SArtur Pilipenko CanBeNull = true; 732345f0148SArtur Pilipenko } 73390c09232SChandler Carruth } else if (const auto *Call = dyn_cast<CallBase>(this)) { 73490c09232SChandler Carruth DerefBytes = Call->getDereferenceableBytes(AttributeList::ReturnIndex); 735345f0148SArtur Pilipenko if (DerefBytes == 0) { 73690c09232SChandler Carruth DerefBytes = 73790c09232SChandler Carruth Call->getDereferenceableOrNullBytes(AttributeList::ReturnIndex); 738345f0148SArtur Pilipenko CanBeNull = true; 739345f0148SArtur Pilipenko } 740345f0148SArtur Pilipenko } else if (const LoadInst *LI = dyn_cast<LoadInst>(this)) { 741345f0148SArtur Pilipenko if (MDNode *MD = LI->getMetadata(LLVMContext::MD_dereferenceable)) { 742345f0148SArtur Pilipenko ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0)); 743345f0148SArtur Pilipenko DerefBytes = CI->getLimitedValue(); 744345f0148SArtur Pilipenko } 745345f0148SArtur Pilipenko if (DerefBytes == 0) { 746345f0148SArtur Pilipenko if (MDNode *MD = 747345f0148SArtur Pilipenko LI->getMetadata(LLVMContext::MD_dereferenceable_or_null)) { 748345f0148SArtur Pilipenko ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0)); 749345f0148SArtur Pilipenko DerefBytes = CI->getLimitedValue(); 750345f0148SArtur Pilipenko } 751345f0148SArtur Pilipenko CanBeNull = true; 752345f0148SArtur Pilipenko } 7536f13637aSRyan Taylor } else if (auto *IP = dyn_cast<IntToPtrInst>(this)) { 7546f13637aSRyan Taylor if (MDNode *MD = IP->getMetadata(LLVMContext::MD_dereferenceable)) { 7556f13637aSRyan Taylor ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0)); 7566f13637aSRyan Taylor DerefBytes = CI->getLimitedValue(); 7576f13637aSRyan Taylor } 7586f13637aSRyan Taylor if (DerefBytes == 0) { 7596f13637aSRyan Taylor if (MDNode *MD = 7606f13637aSRyan Taylor IP->getMetadata(LLVMContext::MD_dereferenceable_or_null)) { 7616f13637aSRyan Taylor ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0)); 7626f13637aSRyan Taylor DerefBytes = CI->getLimitedValue(); 7636f13637aSRyan Taylor } 7646f13637aSRyan Taylor CanBeNull = true; 7656f13637aSRyan Taylor } 76600953cbeSSanjoy Das } else if (auto *AI = dyn_cast<AllocaInst>(this)) { 767030123e8SBjorn Steinbrink if (!AI->isArrayAllocation()) { 768ebec984eSEli Friedman DerefBytes = 769ebec984eSEli Friedman DL.getTypeStoreSize(AI->getAllocatedType()).getKnownMinSize(); 77000953cbeSSanjoy Das CanBeNull = false; 77100953cbeSSanjoy Das } 77200953cbeSSanjoy Das } else if (auto *GV = dyn_cast<GlobalVariable>(this)) { 77300953cbeSSanjoy Das if (GV->getValueType()->isSized() && !GV->hasExternalWeakLinkage()) { 77400953cbeSSanjoy Das // TODO: Don't outright reject hasExternalWeakLinkage but set the 77500953cbeSSanjoy Das // CanBeNull flag. 776ebec984eSEli Friedman DerefBytes = DL.getTypeStoreSize(GV->getValueType()).getFixedSize(); 77700953cbeSSanjoy Das CanBeNull = false; 77800953cbeSSanjoy Das } 779345f0148SArtur Pilipenko } 780345f0148SArtur Pilipenko return DerefBytes; 781345f0148SArtur Pilipenko } 782345f0148SArtur Pilipenko 783f26bdb53SEli Friedman Align Value::getPointerAlignment(const DataLayout &DL) const { 784ae51afc5SArtur Pilipenko assert(getType()->isPointerTy() && "must be pointer"); 785ae51afc5SArtur Pilipenko if (auto *GO = dyn_cast<GlobalObject>(this)) { 786308e82ecSMichael Platings if (isa<Function>(GO)) { 787f26bdb53SEli Friedman Align FunctionPtrAlign = DL.getFunctionPtrAlign().valueOrOne(); 788308e82ecSMichael Platings switch (DL.getFunctionPtrAlignType()) { 789308e82ecSMichael Platings case DataLayout::FunctionPtrAlignType::Independent: 790bae629b9SGuillaume Chatelet return FunctionPtrAlign; 791308e82ecSMichael Platings case DataLayout::FunctionPtrAlignType::MultipleOfFunctionAlign: 792f26bdb53SEli Friedman return std::max(FunctionPtrAlign, GO->getAlign().valueOrOne()); 793308e82ecSMichael Platings } 7946200a568SGuillaume Chatelet llvm_unreachable("Unhandled FunctionPtrAlignType"); 795308e82ecSMichael Platings } 796bae629b9SGuillaume Chatelet const MaybeAlign Alignment(GO->getAlignment()); 797bae629b9SGuillaume Chatelet if (!Alignment) { 798ae51afc5SArtur Pilipenko if (auto *GVar = dyn_cast<GlobalVariable>(GO)) { 799ae51afc5SArtur Pilipenko Type *ObjectType = GVar->getValueType(); 800ae51afc5SArtur Pilipenko if (ObjectType->isSized()) { 801ae51afc5SArtur Pilipenko // If the object is defined in the current Module, we'll be giving 802ae51afc5SArtur Pilipenko // it the preferred alignment. Otherwise, we have to assume that it 803ae51afc5SArtur Pilipenko // may only have the minimum ABI alignment. 804ae51afc5SArtur Pilipenko if (GVar->isStrongDefinitionForLinker()) 805368a5e3aSGuillaume Chatelet return DL.getPreferredAlign(GVar); 806ae51afc5SArtur Pilipenko else 8079068bccbSGuillaume Chatelet return DL.getABITypeAlign(ObjectType); 808ae51afc5SArtur Pilipenko } 809ae51afc5SArtur Pilipenko } 810ae51afc5SArtur Pilipenko } 811f26bdb53SEli Friedman return Alignment.valueOrOne(); 812ae51afc5SArtur Pilipenko } else if (const Argument *A = dyn_cast<Argument>(this)) { 8139068bccbSGuillaume Chatelet const MaybeAlign Alignment = A->getParamAlign(); 814bae629b9SGuillaume Chatelet if (!Alignment && A->hasStructRetAttr()) { 815ae51afc5SArtur Pilipenko // An sret parameter has at least the ABI alignment of the return type. 816d65a7003SMatt Arsenault Type *EltTy = A->getParamStructRetType(); 817ae51afc5SArtur Pilipenko if (EltTy->isSized()) 8189068bccbSGuillaume Chatelet return DL.getABITypeAlign(EltTy); 819ae51afc5SArtur Pilipenko } 820f26bdb53SEli Friedman return Alignment.valueOrOne(); 821c97eac65SArtur Pilipenko } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(this)) { 822f26bdb53SEli Friedman return AI->getAlign(); 823afd4a37bSHideto Ueno } else if (const auto *Call = dyn_cast<CallBase>(this)) { 824f26bdb53SEli Friedman MaybeAlign Alignment = Call->getRetAlign(); 825bae629b9SGuillaume Chatelet if (!Alignment && Call->getCalledFunction()) 826f26bdb53SEli Friedman Alignment = Call->getCalledFunction()->getAttributes().getRetAlignment(); 827f26bdb53SEli Friedman return Alignment.valueOrOne(); 8286200a568SGuillaume Chatelet } else if (const LoadInst *LI = dyn_cast<LoadInst>(this)) { 829ae51afc5SArtur Pilipenko if (MDNode *MD = LI->getMetadata(LLVMContext::MD_align)) { 830ae51afc5SArtur Pilipenko ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0)); 831f26bdb53SEli Friedman return Align(CI->getLimitedValue()); 832ae51afc5SArtur Pilipenko } 833a6492e22SRoman Lebedev } else if (auto *CstPtr = dyn_cast<Constant>(this)) { 834a6492e22SRoman Lebedev if (auto *CstInt = dyn_cast_or_null<ConstantInt>(ConstantExpr::getPtrToInt( 835a6492e22SRoman Lebedev const_cast<Constant *>(CstPtr), DL.getIntPtrType(getType()), 836a6492e22SRoman Lebedev /*OnlyIfReduced=*/true))) { 837a6492e22SRoman Lebedev size_t TrailingZeros = CstInt->getValue().countTrailingZeros(); 838a6492e22SRoman Lebedev // While the actual alignment may be large, elsewhere we have 839a6492e22SRoman Lebedev // an arbitrary upper alignmet limit, so let's clamp to it. 840a6492e22SRoman Lebedev return Align(TrailingZeros < Value::MaxAlignmentExponent 841a6492e22SRoman Lebedev ? uint64_t(1) << TrailingZeros 842a6492e22SRoman Lebedev : Value::MaximumAlignment); 843a6492e22SRoman Lebedev } 8446200a568SGuillaume Chatelet } 845f26bdb53SEli Friedman return Align(1); 846ae51afc5SArtur Pilipenko } 847ae51afc5SArtur Pilipenko 8481dd20e65SCraig Topper const Value *Value::DoPHITranslation(const BasicBlock *CurBB, 8491dd20e65SCraig Topper const BasicBlock *PredBB) const { 8501dd20e65SCraig Topper auto *PN = dyn_cast<PHINode>(this); 851ef860a24SChandler Carruth if (PN && PN->getParent() == CurBB) 852ef860a24SChandler Carruth return PN->getIncomingValueForBlock(PredBB); 853ef860a24SChandler Carruth return this; 854ef860a24SChandler Carruth } 855ef860a24SChandler Carruth 856ef860a24SChandler Carruth LLVMContext &Value::getContext() const { return VTy->getContext(); } 857ef860a24SChandler Carruth 8583441ffe9SDuncan P. N. Exon Smith void Value::reverseUseList() { 8593441ffe9SDuncan P. N. Exon Smith if (!UseList || !UseList->Next) 8603441ffe9SDuncan P. N. Exon Smith // No need to reverse 0 or 1 uses. 8613441ffe9SDuncan P. N. Exon Smith return; 8623441ffe9SDuncan P. N. Exon Smith 8633441ffe9SDuncan P. N. Exon Smith Use *Head = UseList; 8643441ffe9SDuncan P. N. Exon Smith Use *Current = UseList->Next; 8653441ffe9SDuncan P. N. Exon Smith Head->Next = nullptr; 8663441ffe9SDuncan P. N. Exon Smith while (Current) { 8673441ffe9SDuncan P. N. Exon Smith Use *Next = Current->Next; 8683441ffe9SDuncan P. N. Exon Smith Current->Next = Head; 86991ef7cb5SJay Foad Head->Prev = &Current->Next; 8703441ffe9SDuncan P. N. Exon Smith Head = Current; 8713441ffe9SDuncan P. N. Exon Smith Current = Next; 8723441ffe9SDuncan P. N. Exon Smith } 8733441ffe9SDuncan P. N. Exon Smith UseList = Head; 87491ef7cb5SJay Foad Head->Prev = &UseList; 8753441ffe9SDuncan P. N. Exon Smith } 8763441ffe9SDuncan P. N. Exon Smith 877ad830022SArnold Schwaighofer bool Value::isSwiftError() const { 878ad830022SArnold Schwaighofer auto *Arg = dyn_cast<Argument>(this); 879ad830022SArnold Schwaighofer if (Arg) 880ad830022SArnold Schwaighofer return Arg->hasSwiftErrorAttr(); 881ad830022SArnold Schwaighofer auto *Alloca = dyn_cast<AllocaInst>(this); 882ad830022SArnold Schwaighofer if (!Alloca) 883ad830022SArnold Schwaighofer return false; 884ad830022SArnold Schwaighofer return Alloca->isSwiftError(); 885ad830022SArnold Schwaighofer } 886ad830022SArnold Schwaighofer 887ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 888ef860a24SChandler Carruth // ValueHandleBase Class 889ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 890ef860a24SChandler Carruth 891ef860a24SChandler Carruth void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) { 892ef860a24SChandler Carruth assert(List && "Handle list is null?"); 893ef860a24SChandler Carruth 894ef860a24SChandler Carruth // Splice ourselves into the list. 895ef860a24SChandler Carruth Next = *List; 896ef860a24SChandler Carruth *List = this; 897ef860a24SChandler Carruth setPrevPtr(List); 898ef860a24SChandler Carruth if (Next) { 899ef860a24SChandler Carruth Next->setPrevPtr(&Next); 9006f08789dSSanjoy Das assert(getValPtr() == Next->getValPtr() && "Added to wrong list?"); 901ef860a24SChandler Carruth } 902ef860a24SChandler Carruth } 903ef860a24SChandler Carruth 904ef860a24SChandler Carruth void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) { 905ef860a24SChandler Carruth assert(List && "Must insert after existing node"); 906ef860a24SChandler Carruth 907ef860a24SChandler Carruth Next = List->Next; 908ef860a24SChandler Carruth setPrevPtr(&List->Next); 909ef860a24SChandler Carruth List->Next = this; 910ef860a24SChandler Carruth if (Next) 911ef860a24SChandler Carruth Next->setPrevPtr(&Next); 912ef860a24SChandler Carruth } 913ef860a24SChandler Carruth 914ef860a24SChandler Carruth void ValueHandleBase::AddToUseList() { 9156f08789dSSanjoy Das assert(getValPtr() && "Null pointer doesn't have a use list!"); 916ef860a24SChandler Carruth 9176f08789dSSanjoy Das LLVMContextImpl *pImpl = getValPtr()->getContext().pImpl; 918ef860a24SChandler Carruth 9196f08789dSSanjoy Das if (getValPtr()->HasValueHandle) { 920ef860a24SChandler Carruth // If this value already has a ValueHandle, then it must be in the 921ef860a24SChandler Carruth // ValueHandles map already. 9226f08789dSSanjoy Das ValueHandleBase *&Entry = pImpl->ValueHandles[getValPtr()]; 9232617dcceSCraig Topper assert(Entry && "Value doesn't have any handles?"); 924ef860a24SChandler Carruth AddToExistingUseList(&Entry); 925ef860a24SChandler Carruth return; 926ef860a24SChandler Carruth } 927ef860a24SChandler Carruth 928ef860a24SChandler Carruth // Ok, it doesn't have any handles yet, so we must insert it into the 929ef860a24SChandler Carruth // DenseMap. However, doing this insertion could cause the DenseMap to 930ef860a24SChandler Carruth // reallocate itself, which would invalidate all of the PrevP pointers that 931ef860a24SChandler Carruth // point into the old table. Handle this by checking for reallocation and 932ef860a24SChandler Carruth // updating the stale pointers only if needed. 933ef860a24SChandler Carruth DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles; 934ef860a24SChandler Carruth const void *OldBucketPtr = Handles.getPointerIntoBucketsArray(); 935ef860a24SChandler Carruth 9366f08789dSSanjoy Das ValueHandleBase *&Entry = Handles[getValPtr()]; 9372617dcceSCraig Topper assert(!Entry && "Value really did already have handles?"); 938ef860a24SChandler Carruth AddToExistingUseList(&Entry); 9396f08789dSSanjoy Das getValPtr()->HasValueHandle = true; 940ef860a24SChandler Carruth 941ef860a24SChandler Carruth // If reallocation didn't happen or if this was the first insertion, don't 942ef860a24SChandler Carruth // walk the table. 943ef860a24SChandler Carruth if (Handles.isPointerIntoBucketsArray(OldBucketPtr) || 944ef860a24SChandler Carruth Handles.size() == 1) { 945ef860a24SChandler Carruth return; 946ef860a24SChandler Carruth } 947ef860a24SChandler Carruth 948ef860a24SChandler Carruth // Okay, reallocation did happen. Fix the Prev Pointers. 949ef860a24SChandler Carruth for (DenseMap<Value*, ValueHandleBase*>::iterator I = Handles.begin(), 950ef860a24SChandler Carruth E = Handles.end(); I != E; ++I) { 9516f08789dSSanjoy Das assert(I->second && I->first == I->second->getValPtr() && 952ef860a24SChandler Carruth "List invariant broken!"); 953ef860a24SChandler Carruth I->second->setPrevPtr(&I->second); 954ef860a24SChandler Carruth } 955ef860a24SChandler Carruth } 956ef860a24SChandler Carruth 957ef860a24SChandler Carruth void ValueHandleBase::RemoveFromUseList() { 9586f08789dSSanjoy Das assert(getValPtr() && getValPtr()->HasValueHandle && 959ef860a24SChandler Carruth "Pointer doesn't have a use list!"); 960ef860a24SChandler Carruth 961ef860a24SChandler Carruth // Unlink this from its use list. 962ef860a24SChandler Carruth ValueHandleBase **PrevPtr = getPrevPtr(); 963ef860a24SChandler Carruth assert(*PrevPtr == this && "List invariant broken"); 964ef860a24SChandler Carruth 965ef860a24SChandler Carruth *PrevPtr = Next; 966ef860a24SChandler Carruth if (Next) { 967ef860a24SChandler Carruth assert(Next->getPrevPtr() == &Next && "List invariant broken"); 968ef860a24SChandler Carruth Next->setPrevPtr(PrevPtr); 969ef860a24SChandler Carruth return; 970ef860a24SChandler Carruth } 971ef860a24SChandler Carruth 972ef860a24SChandler Carruth // If the Next pointer was null, then it is possible that this was the last 973ef860a24SChandler Carruth // ValueHandle watching VP. If so, delete its entry from the ValueHandles 974ef860a24SChandler Carruth // map. 9756f08789dSSanjoy Das LLVMContextImpl *pImpl = getValPtr()->getContext().pImpl; 976ef860a24SChandler Carruth DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles; 977ef860a24SChandler Carruth if (Handles.isPointerIntoBucketsArray(PrevPtr)) { 9786f08789dSSanjoy Das Handles.erase(getValPtr()); 9796f08789dSSanjoy Das getValPtr()->HasValueHandle = false; 980ef860a24SChandler Carruth } 981ef860a24SChandler Carruth } 982ef860a24SChandler Carruth 983ef860a24SChandler Carruth void ValueHandleBase::ValueIsDeleted(Value *V) { 984ef860a24SChandler Carruth assert(V->HasValueHandle && "Should only be called if ValueHandles present"); 985ef860a24SChandler Carruth 986ef860a24SChandler Carruth // Get the linked list base, which is guaranteed to exist since the 987ef860a24SChandler Carruth // HasValueHandle flag is set. 988ef860a24SChandler Carruth LLVMContextImpl *pImpl = V->getContext().pImpl; 989ef860a24SChandler Carruth ValueHandleBase *Entry = pImpl->ValueHandles[V]; 990ef860a24SChandler Carruth assert(Entry && "Value bit set but no entries exist"); 991ef860a24SChandler Carruth 992ef860a24SChandler Carruth // We use a local ValueHandleBase as an iterator so that ValueHandles can add 993ef860a24SChandler Carruth // and remove themselves from the list without breaking our iteration. This 994ef860a24SChandler Carruth // is not really an AssertingVH; we just have to give ValueHandleBase a kind. 995ef860a24SChandler Carruth // Note that we deliberately do not the support the case when dropping a value 996ef860a24SChandler Carruth // handle results in a new value handle being permanently added to the list 997ef860a24SChandler Carruth // (as might occur in theory for CallbackVH's): the new value handle will not 998ef860a24SChandler Carruth // be processed and the checking code will mete out righteous punishment if 999ef860a24SChandler Carruth // the handle is still present once we have finished processing all the other 1000ef860a24SChandler Carruth // value handles (it is fine to momentarily add then remove a value handle). 1001ef860a24SChandler Carruth for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) { 1002ef860a24SChandler Carruth Iterator.RemoveFromUseList(); 1003ef860a24SChandler Carruth Iterator.AddToExistingUseListAfter(Entry); 1004ef860a24SChandler Carruth assert(Entry->Next == &Iterator && "Loop invariant broken."); 1005ef860a24SChandler Carruth 1006ef860a24SChandler Carruth switch (Entry->getKind()) { 1007ef860a24SChandler Carruth case Assert: 1008ef860a24SChandler Carruth break; 1009f1c0eafdSSanjoy Das case Weak: 1010e6bca0eeSSanjoy Das case WeakTracking: 1011f1c0eafdSSanjoy Das // WeakTracking and Weak just go to null, which unlinks them 1012f1c0eafdSSanjoy Das // from the list. 1013c620761cSCraig Topper Entry->operator=(nullptr); 1014ef860a24SChandler Carruth break; 1015ef860a24SChandler Carruth case Callback: 1016ef860a24SChandler Carruth // Forward to the subclass's implementation. 1017ef860a24SChandler Carruth static_cast<CallbackVH*>(Entry)->deleted(); 1018ef860a24SChandler Carruth break; 1019ef860a24SChandler Carruth } 1020ef860a24SChandler Carruth } 1021ef860a24SChandler Carruth 1022ef860a24SChandler Carruth // All callbacks, weak references, and assertingVHs should be dropped by now. 1023ef860a24SChandler Carruth if (V->HasValueHandle) { 1024ef860a24SChandler Carruth #ifndef NDEBUG // Only in +Asserts mode... 1025ef860a24SChandler Carruth dbgs() << "While deleting: " << *V->getType() << " %" << V->getName() 1026ef860a24SChandler Carruth << "\n"; 1027ef860a24SChandler Carruth if (pImpl->ValueHandles[V]->getKind() == Assert) 1028ef860a24SChandler Carruth llvm_unreachable("An asserting value handle still pointed to this" 1029ef860a24SChandler Carruth " value!"); 1030ef860a24SChandler Carruth 1031ef860a24SChandler Carruth #endif 1032ef860a24SChandler Carruth llvm_unreachable("All references to V were not removed?"); 1033ef860a24SChandler Carruth } 1034ef860a24SChandler Carruth } 1035ef860a24SChandler Carruth 1036ef860a24SChandler Carruth void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) { 1037ef860a24SChandler Carruth assert(Old->HasValueHandle &&"Should only be called if ValueHandles present"); 1038ef860a24SChandler Carruth assert(Old != New && "Changing value into itself!"); 1039c1892e2dSFrederic Riss assert(Old->getType() == New->getType() && 1040c1892e2dSFrederic Riss "replaceAllUses of value with new value of different type!"); 1041ef860a24SChandler Carruth 1042ef860a24SChandler Carruth // Get the linked list base, which is guaranteed to exist since the 1043ef860a24SChandler Carruth // HasValueHandle flag is set. 1044ef860a24SChandler Carruth LLVMContextImpl *pImpl = Old->getContext().pImpl; 1045ef860a24SChandler Carruth ValueHandleBase *Entry = pImpl->ValueHandles[Old]; 1046ef860a24SChandler Carruth 1047ef860a24SChandler Carruth assert(Entry && "Value bit set but no entries exist"); 1048ef860a24SChandler Carruth 1049ef860a24SChandler Carruth // We use a local ValueHandleBase as an iterator so that 1050ef860a24SChandler Carruth // ValueHandles can add and remove themselves from the list without 1051ef860a24SChandler Carruth // breaking our iteration. This is not really an AssertingVH; we 1052ef860a24SChandler Carruth // just have to give ValueHandleBase some kind. 1053ef860a24SChandler Carruth for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) { 1054ef860a24SChandler Carruth Iterator.RemoveFromUseList(); 1055ef860a24SChandler Carruth Iterator.AddToExistingUseListAfter(Entry); 1056ef860a24SChandler Carruth assert(Entry->Next == &Iterator && "Loop invariant broken."); 1057ef860a24SChandler Carruth 1058ef860a24SChandler Carruth switch (Entry->getKind()) { 1059ef860a24SChandler Carruth case Assert: 1060f1c0eafdSSanjoy Das case Weak: 1061f1c0eafdSSanjoy Das // Asserting and Weak handles do not follow RAUW implicitly. 1062ef860a24SChandler Carruth break; 1063e6bca0eeSSanjoy Das case WeakTracking: 10642cbeb00fSSanjoy Das // Weak goes to the new value, which will unlink it from Old's list. 1065ef860a24SChandler Carruth Entry->operator=(New); 1066ef860a24SChandler Carruth break; 1067ef860a24SChandler Carruth case Callback: 1068ef860a24SChandler Carruth // Forward to the subclass's implementation. 1069ef860a24SChandler Carruth static_cast<CallbackVH*>(Entry)->allUsesReplacedWith(New); 1070ef860a24SChandler Carruth break; 1071ef860a24SChandler Carruth } 1072ef860a24SChandler Carruth } 1073ef860a24SChandler Carruth 1074ef860a24SChandler Carruth #ifndef NDEBUG 10758a623820SSanjoy Das // If any new weak value handles were added while processing the 1076ef860a24SChandler Carruth // list, then complain about it now. 1077ef860a24SChandler Carruth if (Old->HasValueHandle) 1078ef860a24SChandler Carruth for (Entry = pImpl->ValueHandles[Old]; Entry; Entry = Entry->Next) 1079ef860a24SChandler Carruth switch (Entry->getKind()) { 1080e6bca0eeSSanjoy Das case WeakTracking: 1081ef860a24SChandler Carruth dbgs() << "After RAUW from " << *Old->getType() << " %" 1082ef860a24SChandler Carruth << Old->getName() << " to " << *New->getType() << " %" 1083ef860a24SChandler Carruth << New->getName() << "\n"; 10848a623820SSanjoy Das llvm_unreachable( 1085e6bca0eeSSanjoy Das "A weak tracking value handle still pointed to the old value!\n"); 1086ef860a24SChandler Carruth default: 1087ef860a24SChandler Carruth break; 1088ef860a24SChandler Carruth } 1089ef860a24SChandler Carruth #endif 1090ef860a24SChandler Carruth } 1091ef860a24SChandler Carruth 1092d12ccbd3SJuergen Ributzka // Pin the vtable to this file. 1093d12ccbd3SJuergen Ributzka void CallbackVH::anchor() {} 1094