1ef860a24SChandler Carruth //===-- Value.cpp - Implement the Value class -----------------------------===// 2ef860a24SChandler Carruth // 3ef860a24SChandler Carruth // The LLVM Compiler Infrastructure 4ef860a24SChandler Carruth // 5ef860a24SChandler Carruth // This file is distributed under the University of Illinois Open Source 6ef860a24SChandler Carruth // License. See LICENSE.TXT for details. 7ef860a24SChandler Carruth // 8ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 9ef860a24SChandler Carruth // 10ef860a24SChandler Carruth // This file implements the Value, ValueHandle, and User classes. 11ef860a24SChandler Carruth // 12ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 13ef860a24SChandler Carruth 149fb823bbSChandler Carruth #include "llvm/IR/Value.h" 15ef860a24SChandler Carruth #include "LLVMContextImpl.h" 16ef860a24SChandler Carruth #include "llvm/ADT/DenseMap.h" 17ef860a24SChandler Carruth #include "llvm/ADT/SmallString.h" 18b0407ba0SHal Finkel #include "llvm/IR/CallSite.h" 199fb823bbSChandler Carruth #include "llvm/IR/Constant.h" 209fb823bbSChandler Carruth #include "llvm/IR/Constants.h" 212e42c34dSHal Finkel #include "llvm/IR/DataLayout.h" 229fb823bbSChandler Carruth #include "llvm/IR/DerivedTypes.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" 32ef860a24SChandler Carruth #include "llvm/Support/Debug.h" 33ef860a24SChandler Carruth #include "llvm/Support/ErrorHandling.h" 34ef860a24SChandler Carruth #include "llvm/Support/ManagedStatic.h" 35799003bfSBenjamin Kramer #include "llvm/Support/raw_ostream.h" 36ef860a24SChandler Carruth #include <algorithm> 37ecefe5a8SEugene Zelenko 38ef860a24SChandler Carruth using namespace llvm; 39ef860a24SChandler Carruth 40ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 41ef860a24SChandler Carruth // Value Class 42ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 43ef860a24SChandler Carruth static inline Type *checkType(Type *Ty) { 44ef860a24SChandler Carruth assert(Ty && "Value defined with a null type: Error!"); 4516bf89ecSReid Kleckner return Ty; 46ef860a24SChandler Carruth } 47ef860a24SChandler Carruth 48ef860a24SChandler Carruth Value::Value(Type *ty, unsigned scid) 497349ab93SOwen Anderson : VTy(checkType(ty)), UseList(nullptr), SubclassID(scid), 507349ab93SOwen Anderson HasValueHandle(0), SubclassOptionalData(0), SubclassData(0), 51b4eede2cSPete Cooper NumUserOperands(0), IsUsedByMD(false), HasName(false) { 52ef860a24SChandler Carruth // FIXME: Why isn't this in the subclass gunk?? 53ef860a24SChandler Carruth // Note, we cannot call isa<CallInst> before the CallInst has been 54ef860a24SChandler Carruth // constructed. 55ef860a24SChandler Carruth if (SubclassID == Instruction::Call || SubclassID == Instruction::Invoke) 56ef860a24SChandler Carruth assert((VTy->isFirstClassType() || VTy->isVoidTy() || VTy->isStructTy()) && 57ef860a24SChandler Carruth "invalid CallInst type!"); 58ef860a24SChandler Carruth else if (SubclassID != BasicBlockVal && 59ef860a24SChandler Carruth (SubclassID < ConstantFirstVal || SubclassID > ConstantLastVal)) 60ef860a24SChandler Carruth assert((VTy->isFirstClassType() || VTy->isVoidTy()) && 61ef860a24SChandler Carruth "Cannot create non-first-class values except for constants!"); 62ef860a24SChandler Carruth } 63ef860a24SChandler Carruth 64ef860a24SChandler Carruth Value::~Value() { 65ef860a24SChandler Carruth // Notify all ValueHandles (if present) that this value is going away. 66ef860a24SChandler Carruth if (HasValueHandle) 67ef860a24SChandler Carruth ValueHandleBase::ValueIsDeleted(this); 685bf8fef5SDuncan P. N. Exon Smith if (isUsedByMetadata()) 695bf8fef5SDuncan P. N. Exon Smith ValueAsMetadata::handleDeletion(this); 70ef860a24SChandler Carruth 71ef860a24SChandler Carruth #ifndef NDEBUG // Only in -g mode... 72ef860a24SChandler Carruth // Check to make sure that there are no uses of this value that are still 73ef860a24SChandler Carruth // around when the value is destroyed. If there are, then we have a dangling 74b93fb82bSAndrew Kaylor // reference and something is wrong. This code is here to print out where 75b93fb82bSAndrew Kaylor // the value is still being referenced. 76ef860a24SChandler Carruth // 77ef860a24SChandler Carruth if (!use_empty()) { 78ef860a24SChandler Carruth dbgs() << "While deleting: " << *VTy << " %" << getName() << "\n"; 79b93fb82bSAndrew Kaylor for (auto *U : users()) 80b93fb82bSAndrew Kaylor dbgs() << "Use still stuck around after Def is destroyed:" << *U << "\n"; 81ef860a24SChandler Carruth } 82ef860a24SChandler Carruth #endif 83ef860a24SChandler Carruth assert(use_empty() && "Uses remain when a value is destroyed!"); 84ef860a24SChandler Carruth 85ef860a24SChandler Carruth // If this value is named, destroy the name. This should not be in a symtab 86ef860a24SChandler Carruth // at this point. 875bf8fef5SDuncan P. N. Exon Smith destroyValueName(); 88ef860a24SChandler Carruth } 89ef860a24SChandler Carruth 905bf8fef5SDuncan P. N. Exon Smith void Value::destroyValueName() { 915bf8fef5SDuncan P. N. Exon Smith ValueName *Name = getValueName(); 925bf8fef5SDuncan P. N. Exon Smith if (Name) 935bf8fef5SDuncan P. N. Exon Smith Name->Destroy(); 945bf8fef5SDuncan P. N. Exon Smith setValueName(nullptr); 955bf8fef5SDuncan P. N. Exon Smith } 965bf8fef5SDuncan P. N. Exon Smith 97ef860a24SChandler Carruth bool Value::hasNUses(unsigned N) const { 98ef860a24SChandler Carruth const_use_iterator UI = use_begin(), E = use_end(); 99ef860a24SChandler Carruth 100ef860a24SChandler Carruth for (; N; --N, ++UI) 101ef860a24SChandler Carruth if (UI == E) return false; // Too few. 102ef860a24SChandler Carruth return UI == E; 103ef860a24SChandler Carruth } 104ef860a24SChandler Carruth 105ef860a24SChandler Carruth bool Value::hasNUsesOrMore(unsigned N) const { 106ef860a24SChandler Carruth const_use_iterator UI = use_begin(), E = use_end(); 107ef860a24SChandler Carruth 108ef860a24SChandler Carruth for (; N; --N, ++UI) 109ef860a24SChandler Carruth if (UI == E) return false; // Too few. 110ef860a24SChandler Carruth 111ef860a24SChandler Carruth return true; 112ef860a24SChandler Carruth } 113ef860a24SChandler Carruth 114ef860a24SChandler Carruth bool Value::isUsedInBasicBlock(const BasicBlock *BB) const { 1150925b24dSJakob Stoklund Olesen // This can be computed either by scanning the instructions in BB, or by 1160925b24dSJakob Stoklund Olesen // scanning the use list of this Value. Both lists can be very long, but 1170925b24dSJakob Stoklund Olesen // usually one is quite short. 1180925b24dSJakob Stoklund Olesen // 1190925b24dSJakob Stoklund Olesen // Scan both lists simultaneously until one is exhausted. This limits the 1200925b24dSJakob Stoklund Olesen // search to the shorter list. 1210925b24dSJakob Stoklund Olesen BasicBlock::const_iterator BI = BB->begin(), BE = BB->end(); 122cdf47884SChandler Carruth const_user_iterator UI = user_begin(), UE = user_end(); 1230925b24dSJakob Stoklund Olesen for (; BI != BE && UI != UE; ++BI, ++UI) { 1240925b24dSJakob Stoklund Olesen // Scan basic block: Check if this Value is used by the instruction at BI. 1250925b24dSJakob Stoklund Olesen if (std::find(BI->op_begin(), BI->op_end(), this) != BI->op_end()) 126ef860a24SChandler Carruth return true; 1270925b24dSJakob Stoklund Olesen // Scan use list: Check if the use at UI is in BB. 1280925b24dSJakob Stoklund Olesen const Instruction *User = dyn_cast<Instruction>(*UI); 129ef860a24SChandler Carruth if (User && User->getParent() == BB) 130ef860a24SChandler Carruth return true; 131ef860a24SChandler Carruth } 132ef860a24SChandler Carruth return false; 133ef860a24SChandler Carruth } 134ef860a24SChandler Carruth 135ef860a24SChandler Carruth unsigned Value::getNumUses() const { 136ef860a24SChandler Carruth return (unsigned)std::distance(use_begin(), use_end()); 137ef860a24SChandler Carruth } 138ef860a24SChandler Carruth 139ef860a24SChandler Carruth static bool getSymTab(Value *V, ValueSymbolTable *&ST) { 140c620761cSCraig Topper ST = nullptr; 141ef860a24SChandler Carruth if (Instruction *I = dyn_cast<Instruction>(V)) { 142ef860a24SChandler Carruth if (BasicBlock *P = I->getParent()) 143ef860a24SChandler Carruth if (Function *PP = P->getParent()) 144ef860a24SChandler Carruth ST = &PP->getValueSymbolTable(); 145ef860a24SChandler Carruth } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) { 146ef860a24SChandler Carruth if (Function *P = BB->getParent()) 147ef860a24SChandler Carruth ST = &P->getValueSymbolTable(); 148ef860a24SChandler Carruth } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { 149ef860a24SChandler Carruth if (Module *P = GV->getParent()) 150ef860a24SChandler Carruth ST = &P->getValueSymbolTable(); 151ef860a24SChandler Carruth } else if (Argument *A = dyn_cast<Argument>(V)) { 152ef860a24SChandler Carruth if (Function *P = A->getParent()) 153ef860a24SChandler Carruth ST = &P->getValueSymbolTable(); 1545bf8fef5SDuncan P. N. Exon Smith } else { 155ef860a24SChandler Carruth assert(isa<Constant>(V) && "Unknown value type!"); 156ef860a24SChandler Carruth return true; // no name is setable for this. 157ef860a24SChandler Carruth } 158ef860a24SChandler Carruth return false; 159ef860a24SChandler Carruth } 160ef860a24SChandler Carruth 1617349ab93SOwen Anderson ValueName *Value::getValueName() const { 1627349ab93SOwen Anderson if (!HasName) return nullptr; 1637349ab93SOwen Anderson 1647349ab93SOwen Anderson LLVMContext &Ctx = getContext(); 1657349ab93SOwen Anderson auto I = Ctx.pImpl->ValueNames.find(this); 1667349ab93SOwen Anderson assert(I != Ctx.pImpl->ValueNames.end() && 1677349ab93SOwen Anderson "No name entry found!"); 1687349ab93SOwen Anderson 1697349ab93SOwen Anderson return I->second; 1707349ab93SOwen Anderson } 1717349ab93SOwen Anderson 1727349ab93SOwen Anderson void Value::setValueName(ValueName *VN) { 1737349ab93SOwen Anderson LLVMContext &Ctx = getContext(); 1747349ab93SOwen Anderson 1757349ab93SOwen Anderson assert(HasName == Ctx.pImpl->ValueNames.count(this) && 1767349ab93SOwen Anderson "HasName bit out of sync!"); 1777349ab93SOwen Anderson 1787349ab93SOwen Anderson if (!VN) { 1797349ab93SOwen Anderson if (HasName) 1807349ab93SOwen Anderson Ctx.pImpl->ValueNames.erase(this); 1817349ab93SOwen Anderson HasName = false; 1827349ab93SOwen Anderson return; 1837349ab93SOwen Anderson } 1847349ab93SOwen Anderson 1857349ab93SOwen Anderson HasName = true; 1867349ab93SOwen Anderson Ctx.pImpl->ValueNames[this] = VN; 1877349ab93SOwen Anderson } 1887349ab93SOwen Anderson 189ef860a24SChandler Carruth StringRef Value::getName() const { 190ef860a24SChandler Carruth // Make sure the empty string is still a C string. For historical reasons, 191ef860a24SChandler Carruth // some clients want to call .data() on the result and expect it to be null 192ef860a24SChandler Carruth // terminated. 1937349ab93SOwen Anderson if (!hasName()) 1945bf8fef5SDuncan P. N. Exon Smith return StringRef("", 0); 1955bf8fef5SDuncan P. N. Exon Smith return getValueName()->getKey(); 196ef860a24SChandler Carruth } 197ef860a24SChandler Carruth 198a7c0c18cSPete Cooper void Value::setNameImpl(const Twine &NewName) { 199ef860a24SChandler Carruth // Fast path for common IRBuilder case of setName("") when there is no name. 200ef860a24SChandler Carruth if (NewName.isTriviallyEmpty() && !hasName()) 201ef860a24SChandler Carruth return; 202ef860a24SChandler Carruth 203ef860a24SChandler Carruth SmallString<256> NameData; 204ef860a24SChandler Carruth StringRef NameRef = NewName.toStringRef(NameData); 20509d689f9SRafael Espindola assert(NameRef.find_first_of(0) == StringRef::npos && 20609d689f9SRafael Espindola "Null bytes are not allowed in names"); 207ef860a24SChandler Carruth 208ef860a24SChandler Carruth // Name isn't changing? 209ef860a24SChandler Carruth if (getName() == NameRef) 210ef860a24SChandler Carruth return; 211ef860a24SChandler Carruth 212ef860a24SChandler Carruth assert(!getType()->isVoidTy() && "Cannot assign a name to void values!"); 213ef860a24SChandler Carruth 214ef860a24SChandler Carruth // Get the symbol table to update for this object. 215ef860a24SChandler Carruth ValueSymbolTable *ST; 216ef860a24SChandler Carruth if (getSymTab(this, ST)) 217ef860a24SChandler Carruth return; // Cannot set a name on this value (e.g. constant). 218ef860a24SChandler Carruth 219ef860a24SChandler Carruth if (!ST) { // No symbol table to update? Just do the change. 220ef860a24SChandler Carruth if (NameRef.empty()) { 221ef860a24SChandler Carruth // Free the name for this value. 2225bf8fef5SDuncan P. N. Exon Smith destroyValueName(); 223ef860a24SChandler Carruth return; 224ef860a24SChandler Carruth } 225ef860a24SChandler Carruth 226ef860a24SChandler Carruth // NOTE: Could optimize for the case the name is shrinking to not deallocate 227ef860a24SChandler Carruth // then reallocated. 2285bf8fef5SDuncan P. N. Exon Smith destroyValueName(); 229ef860a24SChandler Carruth 230ef860a24SChandler Carruth // Create the new name. 2315bf8fef5SDuncan P. N. Exon Smith setValueName(ValueName::Create(NameRef)); 2325bf8fef5SDuncan P. N. Exon Smith getValueName()->setValue(this); 233ef860a24SChandler Carruth return; 234ef860a24SChandler Carruth } 235ef860a24SChandler Carruth 236ef860a24SChandler Carruth // NOTE: Could optimize for the case the name is shrinking to not deallocate 237ef860a24SChandler Carruth // then reallocated. 238ef860a24SChandler Carruth if (hasName()) { 239ef860a24SChandler Carruth // Remove old name. 2405bf8fef5SDuncan P. N. Exon Smith ST->removeValueName(getValueName()); 2415bf8fef5SDuncan P. N. Exon Smith destroyValueName(); 242ef860a24SChandler Carruth 243ef860a24SChandler Carruth if (NameRef.empty()) 244ef860a24SChandler Carruth return; 245ef860a24SChandler Carruth } 246ef860a24SChandler Carruth 247ef860a24SChandler Carruth // Name is changing to something new. 2485bf8fef5SDuncan P. N. Exon Smith setValueName(ST->createValueName(NameRef, this)); 249ef860a24SChandler Carruth } 250ef860a24SChandler Carruth 251a7c0c18cSPete Cooper void Value::setName(const Twine &NewName) { 252a7c0c18cSPete Cooper setNameImpl(NewName); 253a7c0c18cSPete Cooper if (Function *F = dyn_cast<Function>(this)) 254a7c0c18cSPete Cooper F->recalculateIntrinsicID(); 255a7c0c18cSPete Cooper } 256a7c0c18cSPete Cooper 257ef860a24SChandler Carruth void Value::takeName(Value *V) { 258c620761cSCraig Topper ValueSymbolTable *ST = nullptr; 259ef860a24SChandler Carruth // If this value has a name, drop it. 260ef860a24SChandler Carruth if (hasName()) { 261ef860a24SChandler Carruth // Get the symtab this is in. 262ef860a24SChandler Carruth if (getSymTab(this, ST)) { 263ef860a24SChandler Carruth // We can't set a name on this value, but we need to clear V's name if 264ef860a24SChandler Carruth // it has one. 265ef860a24SChandler Carruth if (V->hasName()) V->setName(""); 266ef860a24SChandler Carruth return; // Cannot set a name on this value (e.g. constant). 267ef860a24SChandler Carruth } 268ef860a24SChandler Carruth 269ef860a24SChandler Carruth // Remove old name. 270ef860a24SChandler Carruth if (ST) 2715bf8fef5SDuncan P. N. Exon Smith ST->removeValueName(getValueName()); 2725bf8fef5SDuncan P. N. Exon Smith destroyValueName(); 273ef860a24SChandler Carruth } 274ef860a24SChandler Carruth 275ef860a24SChandler Carruth // Now we know that this has no name. 276ef860a24SChandler Carruth 277ef860a24SChandler Carruth // If V has no name either, we're done. 278ef860a24SChandler Carruth if (!V->hasName()) return; 279ef860a24SChandler Carruth 280ef860a24SChandler Carruth // Get this's symtab if we didn't before. 281ef860a24SChandler Carruth if (!ST) { 282ef860a24SChandler Carruth if (getSymTab(this, ST)) { 283ef860a24SChandler Carruth // Clear V's name. 284ef860a24SChandler Carruth V->setName(""); 285ef860a24SChandler Carruth return; // Cannot set a name on this value (e.g. constant). 286ef860a24SChandler Carruth } 287ef860a24SChandler Carruth } 288ef860a24SChandler Carruth 289ef860a24SChandler Carruth // Get V's ST, this should always succed, because V has a name. 290ef860a24SChandler Carruth ValueSymbolTable *VST; 291ef860a24SChandler Carruth bool Failure = getSymTab(V, VST); 292ef860a24SChandler Carruth assert(!Failure && "V has a name, so it should have a ST!"); (void)Failure; 293ef860a24SChandler Carruth 294ef860a24SChandler Carruth // If these values are both in the same symtab, we can do this very fast. 295ef860a24SChandler Carruth // This works even if both values have no symtab yet. 296ef860a24SChandler Carruth if (ST == VST) { 297ef860a24SChandler Carruth // Take the name! 2985bf8fef5SDuncan P. N. Exon Smith setValueName(V->getValueName()); 2995bf8fef5SDuncan P. N. Exon Smith V->setValueName(nullptr); 3005bf8fef5SDuncan P. N. Exon Smith getValueName()->setValue(this); 301ef860a24SChandler Carruth return; 302ef860a24SChandler Carruth } 303ef860a24SChandler Carruth 304ef860a24SChandler Carruth // Otherwise, things are slightly more complex. Remove V's name from VST and 305ef860a24SChandler Carruth // then reinsert it into ST. 306ef860a24SChandler Carruth 307ef860a24SChandler Carruth if (VST) 3085bf8fef5SDuncan P. N. Exon Smith VST->removeValueName(V->getValueName()); 3095bf8fef5SDuncan P. N. Exon Smith setValueName(V->getValueName()); 3105bf8fef5SDuncan P. N. Exon Smith V->setValueName(nullptr); 3115bf8fef5SDuncan P. N. Exon Smith getValueName()->setValue(this); 312ef860a24SChandler Carruth 313ef860a24SChandler Carruth if (ST) 314ef860a24SChandler Carruth ST->reinsertValue(this); 315ef860a24SChandler Carruth } 316ef860a24SChandler Carruth 317257a3536SRafael Espindola void Value::assertModuleIsMaterialized() const { 318675bdcedSTodd Fiala #ifndef NDEBUG 319257a3536SRafael Espindola const GlobalValue *GV = dyn_cast<GlobalValue>(this); 320257a3536SRafael Espindola if (!GV) 321257a3536SRafael Espindola return; 322257a3536SRafael Espindola const Module *M = GV->getParent(); 323257a3536SRafael Espindola if (!M) 324257a3536SRafael Espindola return; 325257a3536SRafael Espindola assert(M->isMaterialized()); 326675bdcedSTodd Fiala #endif 327257a3536SRafael Espindola } 328257a3536SRafael Espindola 329675bdcedSTodd Fiala #ifndef NDEBUG 33071b7b68bSCraig Topper static bool contains(SmallPtrSetImpl<ConstantExpr *> &Cache, ConstantExpr *Expr, 3317e2b7567SRafael Espindola Constant *C) { 33270573dcdSDavid Blaikie if (!Cache.insert(Expr).second) 3337e2b7567SRafael Espindola return false; 3347e2b7567SRafael Espindola 3357e2b7567SRafael Espindola for (auto &O : Expr->operands()) { 3367e2b7567SRafael Espindola if (O == C) 3377e2b7567SRafael Espindola return true; 3387e2b7567SRafael Espindola auto *CE = dyn_cast<ConstantExpr>(O); 3397e2b7567SRafael Espindola if (!CE) 3407e2b7567SRafael Espindola continue; 3417e2b7567SRafael Espindola if (contains(Cache, CE, C)) 3427e2b7567SRafael Espindola return true; 3437e2b7567SRafael Espindola } 3447e2b7567SRafael Espindola return false; 3457e2b7567SRafael Espindola } 3467e2b7567SRafael Espindola 3477e2b7567SRafael Espindola static bool contains(Value *Expr, Value *V) { 3487e2b7567SRafael Espindola if (Expr == V) 3497e2b7567SRafael Espindola return true; 3507e2b7567SRafael Espindola 3517e2b7567SRafael Espindola auto *C = dyn_cast<Constant>(V); 3527e2b7567SRafael Espindola if (!C) 3537e2b7567SRafael Espindola return false; 3547e2b7567SRafael Espindola 3557e2b7567SRafael Espindola auto *CE = dyn_cast<ConstantExpr>(Expr); 3567e2b7567SRafael Espindola if (!CE) 3577e2b7567SRafael Espindola return false; 3587e2b7567SRafael Espindola 3597e2b7567SRafael Espindola SmallPtrSet<ConstantExpr *, 4> Cache; 3607e2b7567SRafael Espindola return contains(Cache, CE, C); 3617e2b7567SRafael Espindola } 362ecefe5a8SEugene Zelenko #endif // NDEBUG 363ef860a24SChandler Carruth 364ef860a24SChandler Carruth void Value::replaceAllUsesWith(Value *New) { 365ef860a24SChandler Carruth assert(New && "Value::replaceAllUsesWith(<null>) is invalid!"); 3667e2b7567SRafael Espindola assert(!contains(New, this) && 3677e2b7567SRafael Espindola "this->replaceAllUsesWith(expr(this)) is NOT valid!"); 368ef860a24SChandler Carruth assert(New->getType() == getType() && 369ef860a24SChandler Carruth "replaceAllUses of value with new value of different type!"); 370ef860a24SChandler Carruth 371ef860a24SChandler Carruth // Notify all ValueHandles (if present) that this value is going away. 372ef860a24SChandler Carruth if (HasValueHandle) 373ef860a24SChandler Carruth ValueHandleBase::ValueIsRAUWd(this, New); 3745bf8fef5SDuncan P. N. Exon Smith if (isUsedByMetadata()) 3755bf8fef5SDuncan P. N. Exon Smith ValueAsMetadata::handleRAUW(this, New); 376ef860a24SChandler Carruth 377ef860a24SChandler Carruth while (!use_empty()) { 378ef860a24SChandler Carruth Use &U = *UseList; 379ef860a24SChandler Carruth // Must handle Constants specially, we cannot call replaceUsesOfWith on a 380ef860a24SChandler Carruth // constant because they are uniqued. 3816b238633SRafael Espindola if (auto *C = dyn_cast<Constant>(U.getUser())) { 382ef860a24SChandler Carruth if (!isa<GlobalValue>(C)) { 3838914d290SMehdi Amini C->handleOperandChange(this, New); 384ef860a24SChandler Carruth continue; 385ef860a24SChandler Carruth } 386ef860a24SChandler Carruth } 387ef860a24SChandler Carruth 388ef860a24SChandler Carruth U.set(New); 389ef860a24SChandler Carruth } 390ef860a24SChandler Carruth 391ef860a24SChandler Carruth if (BasicBlock *BB = dyn_cast<BasicBlock>(this)) 392ef860a24SChandler Carruth BB->replaceSuccessorsPhiUsesWith(cast<BasicBlock>(New)); 393ef860a24SChandler Carruth } 394ef860a24SChandler Carruth 395ec6217c9SGerolf Hoflehner // Like replaceAllUsesWith except it does not handle constants or basic blocks. 396ec6217c9SGerolf Hoflehner // This routine leaves uses within BB. 397ec6217c9SGerolf Hoflehner void Value::replaceUsesOutsideBlock(Value *New, BasicBlock *BB) { 398ec6217c9SGerolf Hoflehner assert(New && "Value::replaceUsesOutsideBlock(<null>, BB) is invalid!"); 399ec6217c9SGerolf Hoflehner assert(!contains(New, this) && 400ec6217c9SGerolf Hoflehner "this->replaceUsesOutsideBlock(expr(this), BB) is NOT valid!"); 401ec6217c9SGerolf Hoflehner assert(New->getType() == getType() && 402ec6217c9SGerolf Hoflehner "replaceUses of value with new value of different type!"); 403ec6217c9SGerolf Hoflehner assert(BB && "Basic block that may contain a use of 'New' must be defined\n"); 404ec6217c9SGerolf Hoflehner 405ec6217c9SGerolf Hoflehner use_iterator UI = use_begin(), E = use_end(); 406ec6217c9SGerolf Hoflehner for (; UI != E;) { 407ec6217c9SGerolf Hoflehner Use &U = *UI; 408ec6217c9SGerolf Hoflehner ++UI; 409ec6217c9SGerolf Hoflehner auto *Usr = dyn_cast<Instruction>(U.getUser()); 410ec6217c9SGerolf Hoflehner if (Usr && Usr->getParent() == BB) 411ec6217c9SGerolf Hoflehner continue; 412ec6217c9SGerolf Hoflehner U.set(New); 413ec6217c9SGerolf Hoflehner } 414ec6217c9SGerolf Hoflehner } 415ec6217c9SGerolf Hoflehner 416ef860a24SChandler Carruth namespace { 417ef860a24SChandler Carruth // Various metrics for how much to strip off of pointers. 418ef860a24SChandler Carruth enum PointerStripKind { 419ef860a24SChandler Carruth PSK_ZeroIndices, 420c229a4ffSRafael Espindola PSK_ZeroIndicesAndAliases, 421ef860a24SChandler Carruth PSK_InBoundsConstantIndices, 422ef860a24SChandler Carruth PSK_InBounds 423ef860a24SChandler Carruth }; 424ef860a24SChandler Carruth 425ef860a24SChandler Carruth template <PointerStripKind StripKind> 426ef860a24SChandler Carruth static Value *stripPointerCastsAndOffsets(Value *V) { 427ef860a24SChandler Carruth if (!V->getType()->isPointerTy()) 428ef860a24SChandler Carruth return V; 429ef860a24SChandler Carruth 430ef860a24SChandler Carruth // Even though we don't look through PHI nodes, we could be called on an 431ef860a24SChandler Carruth // instruction in an unreachable block, which may be on a cycle. 432ef860a24SChandler Carruth SmallPtrSet<Value *, 4> Visited; 433ef860a24SChandler Carruth 434ef860a24SChandler Carruth Visited.insert(V); 435ef860a24SChandler Carruth do { 436ef860a24SChandler Carruth if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { 437ef860a24SChandler Carruth switch (StripKind) { 438c229a4ffSRafael Espindola case PSK_ZeroIndicesAndAliases: 439ef860a24SChandler Carruth case PSK_ZeroIndices: 440ef860a24SChandler Carruth if (!GEP->hasAllZeroIndices()) 441ef860a24SChandler Carruth return V; 442ef860a24SChandler Carruth break; 443ef860a24SChandler Carruth case PSK_InBoundsConstantIndices: 444ef860a24SChandler Carruth if (!GEP->hasAllConstantIndices()) 445ef860a24SChandler Carruth return V; 446ef860a24SChandler Carruth // fallthrough 447ef860a24SChandler Carruth case PSK_InBounds: 448ef860a24SChandler Carruth if (!GEP->isInBounds()) 449ef860a24SChandler Carruth return V; 450ef860a24SChandler Carruth break; 451ef860a24SChandler Carruth } 452ef860a24SChandler Carruth V = GEP->getPointerOperand(); 453b03bd4d9SMatt Arsenault } else if (Operator::getOpcode(V) == Instruction::BitCast || 454b03bd4d9SMatt Arsenault Operator::getOpcode(V) == Instruction::AddrSpaceCast) { 455ef860a24SChandler Carruth V = cast<Operator>(V)->getOperand(0); 456ef860a24SChandler Carruth } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { 457c229a4ffSRafael Espindola if (StripKind == PSK_ZeroIndices || GA->mayBeOverridden()) 458ef860a24SChandler Carruth return V; 459ef860a24SChandler Carruth V = GA->getAliasee(); 460ef860a24SChandler Carruth } else { 461ef860a24SChandler Carruth return V; 462ef860a24SChandler Carruth } 463ef860a24SChandler Carruth assert(V->getType()->isPointerTy() && "Unexpected operand type!"); 46470573dcdSDavid Blaikie } while (Visited.insert(V).second); 465ef860a24SChandler Carruth 466ef860a24SChandler Carruth return V; 467ef860a24SChandler Carruth } 468ecefe5a8SEugene Zelenko } // end anonymous namespace 469ef860a24SChandler Carruth 470ef860a24SChandler Carruth Value *Value::stripPointerCasts() { 471c229a4ffSRafael Espindola return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndAliases>(this); 472c229a4ffSRafael Espindola } 473c229a4ffSRafael Espindola 474c229a4ffSRafael Espindola Value *Value::stripPointerCastsNoFollowAliases() { 475ef860a24SChandler Carruth return stripPointerCastsAndOffsets<PSK_ZeroIndices>(this); 476ef860a24SChandler Carruth } 477ef860a24SChandler Carruth 478ef860a24SChandler Carruth Value *Value::stripInBoundsConstantOffsets() { 479ef860a24SChandler Carruth return stripPointerCastsAndOffsets<PSK_InBoundsConstantIndices>(this); 480ef860a24SChandler Carruth } 481ef860a24SChandler Carruth 482989e6308SChandler Carruth Value *Value::stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, 483989e6308SChandler Carruth APInt &Offset) { 484989e6308SChandler Carruth if (!getType()->isPointerTy()) 485989e6308SChandler Carruth return this; 486989e6308SChandler Carruth 487989e6308SChandler Carruth assert(Offset.getBitWidth() == DL.getPointerSizeInBits(cast<PointerType>( 488989e6308SChandler Carruth getType())->getAddressSpace()) && 489989e6308SChandler Carruth "The offset must have exactly as many bits as our pointer."); 490989e6308SChandler Carruth 491989e6308SChandler Carruth // Even though we don't look through PHI nodes, we could be called on an 492989e6308SChandler Carruth // instruction in an unreachable block, which may be on a cycle. 493989e6308SChandler Carruth SmallPtrSet<Value *, 4> Visited; 494989e6308SChandler Carruth Visited.insert(this); 495989e6308SChandler Carruth Value *V = this; 496989e6308SChandler Carruth do { 497989e6308SChandler Carruth if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { 498989e6308SChandler Carruth if (!GEP->isInBounds()) 499989e6308SChandler Carruth return V; 500ba689b33SChandler Carruth APInt GEPOffset(Offset); 501ba689b33SChandler Carruth if (!GEP->accumulateConstantOffset(DL, GEPOffset)) 502989e6308SChandler Carruth return V; 503ba689b33SChandler Carruth Offset = GEPOffset; 504989e6308SChandler Carruth V = GEP->getPointerOperand(); 505d63df510SPhilip Reames } else if (Operator::getOpcode(V) == Instruction::BitCast) { 506989e6308SChandler Carruth V = cast<Operator>(V)->getOperand(0); 507989e6308SChandler Carruth } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { 508989e6308SChandler Carruth V = GA->getAliasee(); 509989e6308SChandler Carruth } else { 510989e6308SChandler Carruth return V; 511989e6308SChandler Carruth } 512989e6308SChandler Carruth assert(V->getType()->isPointerTy() && "Unexpected operand type!"); 51370573dcdSDavid Blaikie } while (Visited.insert(V).second); 514989e6308SChandler Carruth 515989e6308SChandler Carruth return V; 516989e6308SChandler Carruth } 517989e6308SChandler Carruth 518ef860a24SChandler Carruth Value *Value::stripInBoundsOffsets() { 519ef860a24SChandler Carruth return stripPointerCastsAndOffsets<PSK_InBounds>(this); 520ef860a24SChandler Carruth } 521ef860a24SChandler Carruth 522*ae51afc5SArtur Pilipenko unsigned Value::getPointerAlignment(const DataLayout &DL) const { 523*ae51afc5SArtur Pilipenko assert(getType()->isPointerTy() && "must be pointer"); 524*ae51afc5SArtur Pilipenko 525*ae51afc5SArtur Pilipenko unsigned Align = 0; 526*ae51afc5SArtur Pilipenko if (auto *GO = dyn_cast<GlobalObject>(this)) { 527*ae51afc5SArtur Pilipenko Align = GO->getAlignment(); 528*ae51afc5SArtur Pilipenko if (Align == 0) { 529*ae51afc5SArtur Pilipenko if (auto *GVar = dyn_cast<GlobalVariable>(GO)) { 530*ae51afc5SArtur Pilipenko Type *ObjectType = GVar->getValueType(); 531*ae51afc5SArtur Pilipenko if (ObjectType->isSized()) { 532*ae51afc5SArtur Pilipenko // If the object is defined in the current Module, we'll be giving 533*ae51afc5SArtur Pilipenko // it the preferred alignment. Otherwise, we have to assume that it 534*ae51afc5SArtur Pilipenko // may only have the minimum ABI alignment. 535*ae51afc5SArtur Pilipenko if (GVar->isStrongDefinitionForLinker()) 536*ae51afc5SArtur Pilipenko Align = DL.getPreferredAlignment(GVar); 537*ae51afc5SArtur Pilipenko else 538*ae51afc5SArtur Pilipenko Align = DL.getABITypeAlignment(ObjectType); 539*ae51afc5SArtur Pilipenko } 540*ae51afc5SArtur Pilipenko } 541*ae51afc5SArtur Pilipenko } 542*ae51afc5SArtur Pilipenko } else if (const Argument *A = dyn_cast<Argument>(this)) { 543*ae51afc5SArtur Pilipenko Align = A->getParamAlignment(); 544*ae51afc5SArtur Pilipenko 545*ae51afc5SArtur Pilipenko if (!Align && A->hasStructRetAttr()) { 546*ae51afc5SArtur Pilipenko // An sret parameter has at least the ABI alignment of the return type. 547*ae51afc5SArtur Pilipenko Type *EltTy = cast<PointerType>(A->getType())->getElementType(); 548*ae51afc5SArtur Pilipenko if (EltTy->isSized()) 549*ae51afc5SArtur Pilipenko Align = DL.getABITypeAlignment(EltTy); 550*ae51afc5SArtur Pilipenko } 551*ae51afc5SArtur Pilipenko } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(this)) 552*ae51afc5SArtur Pilipenko Align = AI->getAlignment(); 553*ae51afc5SArtur Pilipenko else if (auto CS = ImmutableCallSite(this)) 554*ae51afc5SArtur Pilipenko Align = CS.getAttributes().getParamAlignment(AttributeSet::ReturnIndex); 555*ae51afc5SArtur Pilipenko else if (const LoadInst *LI = dyn_cast<LoadInst>(this)) 556*ae51afc5SArtur Pilipenko if (MDNode *MD = LI->getMetadata(LLVMContext::MD_align)) { 557*ae51afc5SArtur Pilipenko ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0)); 558*ae51afc5SArtur Pilipenko Align = CI->getLimitedValue(); 559*ae51afc5SArtur Pilipenko } 560*ae51afc5SArtur Pilipenko 561*ae51afc5SArtur Pilipenko return Align; 562*ae51afc5SArtur Pilipenko } 563*ae51afc5SArtur Pilipenko 564ef860a24SChandler Carruth Value *Value::DoPHITranslation(const BasicBlock *CurBB, 565ef860a24SChandler Carruth const BasicBlock *PredBB) { 566ef860a24SChandler Carruth PHINode *PN = dyn_cast<PHINode>(this); 567ef860a24SChandler Carruth if (PN && PN->getParent() == CurBB) 568ef860a24SChandler Carruth return PN->getIncomingValueForBlock(PredBB); 569ef860a24SChandler Carruth return this; 570ef860a24SChandler Carruth } 571ef860a24SChandler Carruth 572ef860a24SChandler Carruth LLVMContext &Value::getContext() const { return VTy->getContext(); } 573ef860a24SChandler Carruth 5743441ffe9SDuncan P. N. Exon Smith void Value::reverseUseList() { 5753441ffe9SDuncan P. N. Exon Smith if (!UseList || !UseList->Next) 5763441ffe9SDuncan P. N. Exon Smith // No need to reverse 0 or 1 uses. 5773441ffe9SDuncan P. N. Exon Smith return; 5783441ffe9SDuncan P. N. Exon Smith 5793441ffe9SDuncan P. N. Exon Smith Use *Head = UseList; 5803441ffe9SDuncan P. N. Exon Smith Use *Current = UseList->Next; 5813441ffe9SDuncan P. N. Exon Smith Head->Next = nullptr; 5823441ffe9SDuncan P. N. Exon Smith while (Current) { 5833441ffe9SDuncan P. N. Exon Smith Use *Next = Current->Next; 5843441ffe9SDuncan P. N. Exon Smith Current->Next = Head; 5853441ffe9SDuncan P. N. Exon Smith Head->setPrev(&Current->Next); 5863441ffe9SDuncan P. N. Exon Smith Head = Current; 5873441ffe9SDuncan P. N. Exon Smith Current = Next; 5883441ffe9SDuncan P. N. Exon Smith } 5893441ffe9SDuncan P. N. Exon Smith UseList = Head; 5903441ffe9SDuncan P. N. Exon Smith Head->setPrev(&UseList); 5913441ffe9SDuncan P. N. Exon Smith } 5923441ffe9SDuncan P. N. Exon Smith 593ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 594ef860a24SChandler Carruth // ValueHandleBase Class 595ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 596ef860a24SChandler Carruth 597ef860a24SChandler Carruth void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) { 598ef860a24SChandler Carruth assert(List && "Handle list is null?"); 599ef860a24SChandler Carruth 600ef860a24SChandler Carruth // Splice ourselves into the list. 601ef860a24SChandler Carruth Next = *List; 602ef860a24SChandler Carruth *List = this; 603ef860a24SChandler Carruth setPrevPtr(List); 604ef860a24SChandler Carruth if (Next) { 605ef860a24SChandler Carruth Next->setPrevPtr(&Next); 606f4ea3d3dSChandler Carruth assert(V == Next->V && "Added to wrong list?"); 607ef860a24SChandler Carruth } 608ef860a24SChandler Carruth } 609ef860a24SChandler Carruth 610ef860a24SChandler Carruth void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) { 611ef860a24SChandler Carruth assert(List && "Must insert after existing node"); 612ef860a24SChandler Carruth 613ef860a24SChandler Carruth Next = List->Next; 614ef860a24SChandler Carruth setPrevPtr(&List->Next); 615ef860a24SChandler Carruth List->Next = this; 616ef860a24SChandler Carruth if (Next) 617ef860a24SChandler Carruth Next->setPrevPtr(&Next); 618ef860a24SChandler Carruth } 619ef860a24SChandler Carruth 620ef860a24SChandler Carruth void ValueHandleBase::AddToUseList() { 621f4ea3d3dSChandler Carruth assert(V && "Null pointer doesn't have a use list!"); 622ef860a24SChandler Carruth 623f4ea3d3dSChandler Carruth LLVMContextImpl *pImpl = V->getContext().pImpl; 624ef860a24SChandler Carruth 625f4ea3d3dSChandler Carruth if (V->HasValueHandle) { 626ef860a24SChandler Carruth // If this value already has a ValueHandle, then it must be in the 627ef860a24SChandler Carruth // ValueHandles map already. 628f4ea3d3dSChandler Carruth ValueHandleBase *&Entry = pImpl->ValueHandles[V]; 6292617dcceSCraig Topper assert(Entry && "Value doesn't have any handles?"); 630ef860a24SChandler Carruth AddToExistingUseList(&Entry); 631ef860a24SChandler Carruth return; 632ef860a24SChandler Carruth } 633ef860a24SChandler Carruth 634ef860a24SChandler Carruth // Ok, it doesn't have any handles yet, so we must insert it into the 635ef860a24SChandler Carruth // DenseMap. However, doing this insertion could cause the DenseMap to 636ef860a24SChandler Carruth // reallocate itself, which would invalidate all of the PrevP pointers that 637ef860a24SChandler Carruth // point into the old table. Handle this by checking for reallocation and 638ef860a24SChandler Carruth // updating the stale pointers only if needed. 639ef860a24SChandler Carruth DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles; 640ef860a24SChandler Carruth const void *OldBucketPtr = Handles.getPointerIntoBucketsArray(); 641ef860a24SChandler Carruth 642f4ea3d3dSChandler Carruth ValueHandleBase *&Entry = Handles[V]; 6432617dcceSCraig Topper assert(!Entry && "Value really did already have handles?"); 644ef860a24SChandler Carruth AddToExistingUseList(&Entry); 645f4ea3d3dSChandler Carruth V->HasValueHandle = true; 646ef860a24SChandler Carruth 647ef860a24SChandler Carruth // If reallocation didn't happen or if this was the first insertion, don't 648ef860a24SChandler Carruth // walk the table. 649ef860a24SChandler Carruth if (Handles.isPointerIntoBucketsArray(OldBucketPtr) || 650ef860a24SChandler Carruth Handles.size() == 1) { 651ef860a24SChandler Carruth return; 652ef860a24SChandler Carruth } 653ef860a24SChandler Carruth 654ef860a24SChandler Carruth // Okay, reallocation did happen. Fix the Prev Pointers. 655ef860a24SChandler Carruth for (DenseMap<Value*, ValueHandleBase*>::iterator I = Handles.begin(), 656ef860a24SChandler Carruth E = Handles.end(); I != E; ++I) { 657f4ea3d3dSChandler Carruth assert(I->second && I->first == I->second->V && 658ef860a24SChandler Carruth "List invariant broken!"); 659ef860a24SChandler Carruth I->second->setPrevPtr(&I->second); 660ef860a24SChandler Carruth } 661ef860a24SChandler Carruth } 662ef860a24SChandler Carruth 663ef860a24SChandler Carruth void ValueHandleBase::RemoveFromUseList() { 664f4ea3d3dSChandler Carruth assert(V && V->HasValueHandle && 665ef860a24SChandler Carruth "Pointer doesn't have a use list!"); 666ef860a24SChandler Carruth 667ef860a24SChandler Carruth // Unlink this from its use list. 668ef860a24SChandler Carruth ValueHandleBase **PrevPtr = getPrevPtr(); 669ef860a24SChandler Carruth assert(*PrevPtr == this && "List invariant broken"); 670ef860a24SChandler Carruth 671ef860a24SChandler Carruth *PrevPtr = Next; 672ef860a24SChandler Carruth if (Next) { 673ef860a24SChandler Carruth assert(Next->getPrevPtr() == &Next && "List invariant broken"); 674ef860a24SChandler Carruth Next->setPrevPtr(PrevPtr); 675ef860a24SChandler Carruth return; 676ef860a24SChandler Carruth } 677ef860a24SChandler Carruth 678ef860a24SChandler Carruth // If the Next pointer was null, then it is possible that this was the last 679ef860a24SChandler Carruth // ValueHandle watching VP. If so, delete its entry from the ValueHandles 680ef860a24SChandler Carruth // map. 681f4ea3d3dSChandler Carruth LLVMContextImpl *pImpl = V->getContext().pImpl; 682ef860a24SChandler Carruth DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles; 683ef860a24SChandler Carruth if (Handles.isPointerIntoBucketsArray(PrevPtr)) { 684f4ea3d3dSChandler Carruth Handles.erase(V); 685f4ea3d3dSChandler Carruth V->HasValueHandle = false; 686ef860a24SChandler Carruth } 687ef860a24SChandler Carruth } 688ef860a24SChandler Carruth 689ef860a24SChandler Carruth void ValueHandleBase::ValueIsDeleted(Value *V) { 690ef860a24SChandler Carruth assert(V->HasValueHandle && "Should only be called if ValueHandles present"); 691ef860a24SChandler Carruth 692ef860a24SChandler Carruth // Get the linked list base, which is guaranteed to exist since the 693ef860a24SChandler Carruth // HasValueHandle flag is set. 694ef860a24SChandler Carruth LLVMContextImpl *pImpl = V->getContext().pImpl; 695ef860a24SChandler Carruth ValueHandleBase *Entry = pImpl->ValueHandles[V]; 696ef860a24SChandler Carruth assert(Entry && "Value bit set but no entries exist"); 697ef860a24SChandler Carruth 698ef860a24SChandler Carruth // We use a local ValueHandleBase as an iterator so that ValueHandles can add 699ef860a24SChandler Carruth // and remove themselves from the list without breaking our iteration. This 700ef860a24SChandler Carruth // is not really an AssertingVH; we just have to give ValueHandleBase a kind. 701ef860a24SChandler Carruth // Note that we deliberately do not the support the case when dropping a value 702ef860a24SChandler Carruth // handle results in a new value handle being permanently added to the list 703ef860a24SChandler Carruth // (as might occur in theory for CallbackVH's): the new value handle will not 704ef860a24SChandler Carruth // be processed and the checking code will mete out righteous punishment if 705ef860a24SChandler Carruth // the handle is still present once we have finished processing all the other 706ef860a24SChandler Carruth // value handles (it is fine to momentarily add then remove a value handle). 707ef860a24SChandler Carruth for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) { 708ef860a24SChandler Carruth Iterator.RemoveFromUseList(); 709ef860a24SChandler Carruth Iterator.AddToExistingUseListAfter(Entry); 710ef860a24SChandler Carruth assert(Entry->Next == &Iterator && "Loop invariant broken."); 711ef860a24SChandler Carruth 712ef860a24SChandler Carruth switch (Entry->getKind()) { 713ef860a24SChandler Carruth case Assert: 714ef860a24SChandler Carruth break; 715ef860a24SChandler Carruth case Tracking: 716ef860a24SChandler Carruth // Mark that this value has been deleted by setting it to an invalid Value 717ef860a24SChandler Carruth // pointer. 718ef860a24SChandler Carruth Entry->operator=(DenseMapInfo<Value *>::getTombstoneKey()); 719ef860a24SChandler Carruth break; 720ef860a24SChandler Carruth case Weak: 721ef860a24SChandler Carruth // Weak just goes to null, which will unlink it from the list. 722c620761cSCraig Topper Entry->operator=(nullptr); 723ef860a24SChandler Carruth break; 724ef860a24SChandler Carruth case Callback: 725ef860a24SChandler Carruth // Forward to the subclass's implementation. 726ef860a24SChandler Carruth static_cast<CallbackVH*>(Entry)->deleted(); 727ef860a24SChandler Carruth break; 728ef860a24SChandler Carruth } 729ef860a24SChandler Carruth } 730ef860a24SChandler Carruth 731ef860a24SChandler Carruth // All callbacks, weak references, and assertingVHs should be dropped by now. 732ef860a24SChandler Carruth if (V->HasValueHandle) { 733ef860a24SChandler Carruth #ifndef NDEBUG // Only in +Asserts mode... 734ef860a24SChandler Carruth dbgs() << "While deleting: " << *V->getType() << " %" << V->getName() 735ef860a24SChandler Carruth << "\n"; 736ef860a24SChandler Carruth if (pImpl->ValueHandles[V]->getKind() == Assert) 737ef860a24SChandler Carruth llvm_unreachable("An asserting value handle still pointed to this" 738ef860a24SChandler Carruth " value!"); 739ef860a24SChandler Carruth 740ef860a24SChandler Carruth #endif 741ef860a24SChandler Carruth llvm_unreachable("All references to V were not removed?"); 742ef860a24SChandler Carruth } 743ef860a24SChandler Carruth } 744ef860a24SChandler Carruth 745ef860a24SChandler Carruth void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) { 746ef860a24SChandler Carruth assert(Old->HasValueHandle &&"Should only be called if ValueHandles present"); 747ef860a24SChandler Carruth assert(Old != New && "Changing value into itself!"); 748c1892e2dSFrederic Riss assert(Old->getType() == New->getType() && 749c1892e2dSFrederic Riss "replaceAllUses of value with new value of different type!"); 750ef860a24SChandler Carruth 751ef860a24SChandler Carruth // Get the linked list base, which is guaranteed to exist since the 752ef860a24SChandler Carruth // HasValueHandle flag is set. 753ef860a24SChandler Carruth LLVMContextImpl *pImpl = Old->getContext().pImpl; 754ef860a24SChandler Carruth ValueHandleBase *Entry = pImpl->ValueHandles[Old]; 755ef860a24SChandler Carruth 756ef860a24SChandler Carruth assert(Entry && "Value bit set but no entries exist"); 757ef860a24SChandler Carruth 758ef860a24SChandler Carruth // We use a local ValueHandleBase as an iterator so that 759ef860a24SChandler Carruth // ValueHandles can add and remove themselves from the list without 760ef860a24SChandler Carruth // breaking our iteration. This is not really an AssertingVH; we 761ef860a24SChandler Carruth // just have to give ValueHandleBase some kind. 762ef860a24SChandler Carruth for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) { 763ef860a24SChandler Carruth Iterator.RemoveFromUseList(); 764ef860a24SChandler Carruth Iterator.AddToExistingUseListAfter(Entry); 765ef860a24SChandler Carruth assert(Entry->Next == &Iterator && "Loop invariant broken."); 766ef860a24SChandler Carruth 767ef860a24SChandler Carruth switch (Entry->getKind()) { 768ef860a24SChandler Carruth case Assert: 769ef860a24SChandler Carruth // Asserting handle does not follow RAUW implicitly. 770ef860a24SChandler Carruth break; 771ef860a24SChandler Carruth case Tracking: 772ef860a24SChandler Carruth // Tracking goes to new value like a WeakVH. Note that this may make it 773ef860a24SChandler Carruth // something incompatible with its templated type. We don't want to have a 774ef860a24SChandler Carruth // virtual (or inline) interface to handle this though, so instead we make 775ef860a24SChandler Carruth // the TrackingVH accessors guarantee that a client never sees this value. 776ef860a24SChandler Carruth 777ef860a24SChandler Carruth // FALLTHROUGH 778ef860a24SChandler Carruth case Weak: 779ef860a24SChandler Carruth // Weak goes to the new value, which will unlink it from Old's list. 780ef860a24SChandler Carruth Entry->operator=(New); 781ef860a24SChandler Carruth break; 782ef860a24SChandler Carruth case Callback: 783ef860a24SChandler Carruth // Forward to the subclass's implementation. 784ef860a24SChandler Carruth static_cast<CallbackVH*>(Entry)->allUsesReplacedWith(New); 785ef860a24SChandler Carruth break; 786ef860a24SChandler Carruth } 787ef860a24SChandler Carruth } 788ef860a24SChandler Carruth 789ef860a24SChandler Carruth #ifndef NDEBUG 790ef860a24SChandler Carruth // If any new tracking or weak value handles were added while processing the 791ef860a24SChandler Carruth // list, then complain about it now. 792ef860a24SChandler Carruth if (Old->HasValueHandle) 793ef860a24SChandler Carruth for (Entry = pImpl->ValueHandles[Old]; Entry; Entry = Entry->Next) 794ef860a24SChandler Carruth switch (Entry->getKind()) { 795ef860a24SChandler Carruth case Tracking: 796ef860a24SChandler Carruth case Weak: 797ef860a24SChandler Carruth dbgs() << "After RAUW from " << *Old->getType() << " %" 798ef860a24SChandler Carruth << Old->getName() << " to " << *New->getType() << " %" 799ef860a24SChandler Carruth << New->getName() << "\n"; 800ef860a24SChandler Carruth llvm_unreachable("A tracking or weak value handle still pointed to the" 801ef860a24SChandler Carruth " old value!\n"); 802ef860a24SChandler Carruth default: 803ef860a24SChandler Carruth break; 804ef860a24SChandler Carruth } 805ef860a24SChandler Carruth #endif 806ef860a24SChandler Carruth } 807ef860a24SChandler Carruth 808d12ccbd3SJuergen Ributzka // Pin the vtable to this file. 809d12ccbd3SJuergen Ributzka void CallbackVH::anchor() {} 810