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!"); 621762ad3eSReid Kleckner static_assert(sizeof(Value) == 3 * sizeof(void *) + 2 * sizeof(unsigned), 631762ad3eSReid Kleckner "Value too big"); 64ef860a24SChandler Carruth } 65ef860a24SChandler Carruth 66ef860a24SChandler Carruth Value::~Value() { 67ef860a24SChandler Carruth // Notify all ValueHandles (if present) that this value is going away. 68ef860a24SChandler Carruth if (HasValueHandle) 69ef860a24SChandler Carruth ValueHandleBase::ValueIsDeleted(this); 705bf8fef5SDuncan P. N. Exon Smith if (isUsedByMetadata()) 715bf8fef5SDuncan P. N. Exon Smith ValueAsMetadata::handleDeletion(this); 72ef860a24SChandler Carruth 73ef860a24SChandler Carruth #ifndef NDEBUG // Only in -g mode... 74ef860a24SChandler Carruth // Check to make sure that there are no uses of this value that are still 75ef860a24SChandler Carruth // around when the value is destroyed. If there are, then we have a dangling 76b93fb82bSAndrew Kaylor // reference and something is wrong. This code is here to print out where 77b93fb82bSAndrew Kaylor // the value is still being referenced. 78ef860a24SChandler Carruth // 79ef860a24SChandler Carruth if (!use_empty()) { 80ef860a24SChandler Carruth dbgs() << "While deleting: " << *VTy << " %" << getName() << "\n"; 81b93fb82bSAndrew Kaylor for (auto *U : users()) 82b93fb82bSAndrew Kaylor dbgs() << "Use still stuck around after Def is destroyed:" << *U << "\n"; 83ef860a24SChandler Carruth } 84ef860a24SChandler Carruth #endif 85ef860a24SChandler Carruth assert(use_empty() && "Uses remain when a value is destroyed!"); 86ef860a24SChandler Carruth 87ef860a24SChandler Carruth // If this value is named, destroy the name. This should not be in a symtab 88ef860a24SChandler Carruth // at this point. 895bf8fef5SDuncan P. N. Exon Smith destroyValueName(); 90ef860a24SChandler Carruth } 91ef860a24SChandler Carruth 925bf8fef5SDuncan P. N. Exon Smith void Value::destroyValueName() { 935bf8fef5SDuncan P. N. Exon Smith ValueName *Name = getValueName(); 945bf8fef5SDuncan P. N. Exon Smith if (Name) 955bf8fef5SDuncan P. N. Exon Smith Name->Destroy(); 965bf8fef5SDuncan P. N. Exon Smith setValueName(nullptr); 975bf8fef5SDuncan P. N. Exon Smith } 985bf8fef5SDuncan P. N. Exon Smith 99ef860a24SChandler Carruth bool Value::hasNUses(unsigned N) const { 100ef860a24SChandler Carruth const_use_iterator UI = use_begin(), E = use_end(); 101ef860a24SChandler Carruth 102ef860a24SChandler Carruth for (; N; --N, ++UI) 103ef860a24SChandler Carruth if (UI == E) return false; // Too few. 104ef860a24SChandler Carruth return UI == E; 105ef860a24SChandler Carruth } 106ef860a24SChandler Carruth 107ef860a24SChandler Carruth bool Value::hasNUsesOrMore(unsigned N) const { 108ef860a24SChandler Carruth const_use_iterator UI = use_begin(), E = use_end(); 109ef860a24SChandler Carruth 110ef860a24SChandler Carruth for (; N; --N, ++UI) 111ef860a24SChandler Carruth if (UI == E) return false; // Too few. 112ef860a24SChandler Carruth 113ef860a24SChandler Carruth return true; 114ef860a24SChandler Carruth } 115ef860a24SChandler Carruth 116ef860a24SChandler Carruth bool Value::isUsedInBasicBlock(const BasicBlock *BB) const { 1170925b24dSJakob Stoklund Olesen // This can be computed either by scanning the instructions in BB, or by 1180925b24dSJakob Stoklund Olesen // scanning the use list of this Value. Both lists can be very long, but 1190925b24dSJakob Stoklund Olesen // usually one is quite short. 1200925b24dSJakob Stoklund Olesen // 1210925b24dSJakob Stoklund Olesen // Scan both lists simultaneously until one is exhausted. This limits the 1220925b24dSJakob Stoklund Olesen // search to the shorter list. 1230925b24dSJakob Stoklund Olesen BasicBlock::const_iterator BI = BB->begin(), BE = BB->end(); 124cdf47884SChandler Carruth const_user_iterator UI = user_begin(), UE = user_end(); 1250925b24dSJakob Stoklund Olesen for (; BI != BE && UI != UE; ++BI, ++UI) { 1260925b24dSJakob Stoklund Olesen // Scan basic block: Check if this Value is used by the instruction at BI. 127*42531260SDavid Majnemer if (is_contained(BI->operands(), this)) 128ef860a24SChandler Carruth return true; 1290925b24dSJakob Stoklund Olesen // Scan use list: Check if the use at UI is in BB. 130*42531260SDavid Majnemer const auto *User = dyn_cast<Instruction>(*UI); 131ef860a24SChandler Carruth if (User && User->getParent() == BB) 132ef860a24SChandler Carruth return true; 133ef860a24SChandler Carruth } 134ef860a24SChandler Carruth return false; 135ef860a24SChandler Carruth } 136ef860a24SChandler Carruth 137ef860a24SChandler Carruth unsigned Value::getNumUses() const { 138ef860a24SChandler Carruth return (unsigned)std::distance(use_begin(), use_end()); 139ef860a24SChandler Carruth } 140ef860a24SChandler Carruth 141ef860a24SChandler Carruth static bool getSymTab(Value *V, ValueSymbolTable *&ST) { 142c620761cSCraig Topper ST = nullptr; 143ef860a24SChandler Carruth if (Instruction *I = dyn_cast<Instruction>(V)) { 144ef860a24SChandler Carruth if (BasicBlock *P = I->getParent()) 145ef860a24SChandler Carruth if (Function *PP = P->getParent()) 146ef860a24SChandler Carruth ST = &PP->getValueSymbolTable(); 147ef860a24SChandler Carruth } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) { 148ef860a24SChandler Carruth if (Function *P = BB->getParent()) 149ef860a24SChandler Carruth ST = &P->getValueSymbolTable(); 150ef860a24SChandler Carruth } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { 151ef860a24SChandler Carruth if (Module *P = GV->getParent()) 152ef860a24SChandler Carruth ST = &P->getValueSymbolTable(); 153ef860a24SChandler Carruth } else if (Argument *A = dyn_cast<Argument>(V)) { 154ef860a24SChandler Carruth if (Function *P = A->getParent()) 155ef860a24SChandler Carruth ST = &P->getValueSymbolTable(); 1565bf8fef5SDuncan P. N. Exon Smith } else { 157ef860a24SChandler Carruth assert(isa<Constant>(V) && "Unknown value type!"); 158ef860a24SChandler Carruth return true; // no name is setable for this. 159ef860a24SChandler Carruth } 160ef860a24SChandler Carruth return false; 161ef860a24SChandler Carruth } 162ef860a24SChandler Carruth 1637349ab93SOwen Anderson ValueName *Value::getValueName() const { 1647349ab93SOwen Anderson if (!HasName) return nullptr; 1657349ab93SOwen Anderson 1667349ab93SOwen Anderson LLVMContext &Ctx = getContext(); 1677349ab93SOwen Anderson auto I = Ctx.pImpl->ValueNames.find(this); 1687349ab93SOwen Anderson assert(I != Ctx.pImpl->ValueNames.end() && 1697349ab93SOwen Anderson "No name entry found!"); 1707349ab93SOwen Anderson 1717349ab93SOwen Anderson return I->second; 1727349ab93SOwen Anderson } 1737349ab93SOwen Anderson 1747349ab93SOwen Anderson void Value::setValueName(ValueName *VN) { 1757349ab93SOwen Anderson LLVMContext &Ctx = getContext(); 1767349ab93SOwen Anderson 1777349ab93SOwen Anderson assert(HasName == Ctx.pImpl->ValueNames.count(this) && 1787349ab93SOwen Anderson "HasName bit out of sync!"); 1797349ab93SOwen Anderson 1807349ab93SOwen Anderson if (!VN) { 1817349ab93SOwen Anderson if (HasName) 1827349ab93SOwen Anderson Ctx.pImpl->ValueNames.erase(this); 1837349ab93SOwen Anderson HasName = false; 1847349ab93SOwen Anderson return; 1857349ab93SOwen Anderson } 1867349ab93SOwen Anderson 1877349ab93SOwen Anderson HasName = true; 1887349ab93SOwen Anderson Ctx.pImpl->ValueNames[this] = VN; 1897349ab93SOwen Anderson } 1907349ab93SOwen Anderson 191ef860a24SChandler Carruth StringRef Value::getName() const { 192ef860a24SChandler Carruth // Make sure the empty string is still a C string. For historical reasons, 193ef860a24SChandler Carruth // some clients want to call .data() on the result and expect it to be null 194ef860a24SChandler Carruth // terminated. 1957349ab93SOwen Anderson if (!hasName()) 1965bf8fef5SDuncan P. N. Exon Smith return StringRef("", 0); 1975bf8fef5SDuncan P. N. Exon Smith return getValueName()->getKey(); 198ef860a24SChandler Carruth } 199ef860a24SChandler Carruth 200a7c0c18cSPete Cooper void Value::setNameImpl(const Twine &NewName) { 20109b4a8daSMehdi Amini // Fast-path: LLVMContext can be set to strip out non-GlobalValue names 20250af49fcSMehdi Amini if (getContext().shouldDiscardValueNames() && !isa<GlobalValue>(this)) 20309b4a8daSMehdi Amini return; 20409b4a8daSMehdi Amini 205ef860a24SChandler Carruth // Fast path for common IRBuilder case of setName("") when there is no name. 206ef860a24SChandler Carruth if (NewName.isTriviallyEmpty() && !hasName()) 207ef860a24SChandler Carruth return; 208ef860a24SChandler Carruth 209ef860a24SChandler Carruth SmallString<256> NameData; 210ef860a24SChandler Carruth StringRef NameRef = NewName.toStringRef(NameData); 21109d689f9SRafael Espindola assert(NameRef.find_first_of(0) == StringRef::npos && 21209d689f9SRafael Espindola "Null bytes are not allowed in names"); 213ef860a24SChandler Carruth 214ef860a24SChandler Carruth // Name isn't changing? 215ef860a24SChandler Carruth if (getName() == NameRef) 216ef860a24SChandler Carruth return; 217ef860a24SChandler Carruth 218ef860a24SChandler Carruth assert(!getType()->isVoidTy() && "Cannot assign a name to void values!"); 219ef860a24SChandler Carruth 220ef860a24SChandler Carruth // Get the symbol table to update for this object. 221ef860a24SChandler Carruth ValueSymbolTable *ST; 222ef860a24SChandler Carruth if (getSymTab(this, ST)) 223ef860a24SChandler Carruth return; // Cannot set a name on this value (e.g. constant). 224ef860a24SChandler Carruth 225ef860a24SChandler Carruth if (!ST) { // No symbol table to update? Just do the change. 226ef860a24SChandler Carruth if (NameRef.empty()) { 227ef860a24SChandler Carruth // Free the name for this value. 2285bf8fef5SDuncan P. N. Exon Smith destroyValueName(); 229ef860a24SChandler Carruth return; 230ef860a24SChandler Carruth } 231ef860a24SChandler Carruth 232ef860a24SChandler Carruth // NOTE: Could optimize for the case the name is shrinking to not deallocate 233ef860a24SChandler Carruth // then reallocated. 2345bf8fef5SDuncan P. N. Exon Smith destroyValueName(); 235ef860a24SChandler Carruth 236ef860a24SChandler Carruth // Create the new name. 2375bf8fef5SDuncan P. N. Exon Smith setValueName(ValueName::Create(NameRef)); 2385bf8fef5SDuncan P. N. Exon Smith getValueName()->setValue(this); 239ef860a24SChandler Carruth return; 240ef860a24SChandler Carruth } 241ef860a24SChandler Carruth 242ef860a24SChandler Carruth // NOTE: Could optimize for the case the name is shrinking to not deallocate 243ef860a24SChandler Carruth // then reallocated. 244ef860a24SChandler Carruth if (hasName()) { 245ef860a24SChandler Carruth // Remove old name. 2465bf8fef5SDuncan P. N. Exon Smith ST->removeValueName(getValueName()); 2475bf8fef5SDuncan P. N. Exon Smith destroyValueName(); 248ef860a24SChandler Carruth 249ef860a24SChandler Carruth if (NameRef.empty()) 250ef860a24SChandler Carruth return; 251ef860a24SChandler Carruth } 252ef860a24SChandler Carruth 253ef860a24SChandler Carruth // Name is changing to something new. 2545bf8fef5SDuncan P. N. Exon Smith setValueName(ST->createValueName(NameRef, this)); 255ef860a24SChandler Carruth } 256ef860a24SChandler Carruth 257a7c0c18cSPete Cooper void Value::setName(const Twine &NewName) { 258a7c0c18cSPete Cooper setNameImpl(NewName); 259a7c0c18cSPete Cooper if (Function *F = dyn_cast<Function>(this)) 260a7c0c18cSPete Cooper F->recalculateIntrinsicID(); 261a7c0c18cSPete Cooper } 262a7c0c18cSPete Cooper 263ef860a24SChandler Carruth void Value::takeName(Value *V) { 264c620761cSCraig Topper ValueSymbolTable *ST = nullptr; 265ef860a24SChandler Carruth // If this value has a name, drop it. 266ef860a24SChandler Carruth if (hasName()) { 267ef860a24SChandler Carruth // Get the symtab this is in. 268ef860a24SChandler Carruth if (getSymTab(this, ST)) { 269ef860a24SChandler Carruth // We can't set a name on this value, but we need to clear V's name if 270ef860a24SChandler Carruth // it has one. 271ef860a24SChandler Carruth if (V->hasName()) V->setName(""); 272ef860a24SChandler Carruth return; // Cannot set a name on this value (e.g. constant). 273ef860a24SChandler Carruth } 274ef860a24SChandler Carruth 275ef860a24SChandler Carruth // Remove old name. 276ef860a24SChandler Carruth if (ST) 2775bf8fef5SDuncan P. N. Exon Smith ST->removeValueName(getValueName()); 2785bf8fef5SDuncan P. N. Exon Smith destroyValueName(); 279ef860a24SChandler Carruth } 280ef860a24SChandler Carruth 281ef860a24SChandler Carruth // Now we know that this has no name. 282ef860a24SChandler Carruth 283ef860a24SChandler Carruth // If V has no name either, we're done. 284ef860a24SChandler Carruth if (!V->hasName()) return; 285ef860a24SChandler Carruth 286ef860a24SChandler Carruth // Get this's symtab if we didn't before. 287ef860a24SChandler Carruth if (!ST) { 288ef860a24SChandler Carruth if (getSymTab(this, ST)) { 289ef860a24SChandler Carruth // Clear V's name. 290ef860a24SChandler Carruth V->setName(""); 291ef860a24SChandler Carruth return; // Cannot set a name on this value (e.g. constant). 292ef860a24SChandler Carruth } 293ef860a24SChandler Carruth } 294ef860a24SChandler Carruth 295ef860a24SChandler Carruth // Get V's ST, this should always succed, because V has a name. 296ef860a24SChandler Carruth ValueSymbolTable *VST; 297ef860a24SChandler Carruth bool Failure = getSymTab(V, VST); 298ef860a24SChandler Carruth assert(!Failure && "V has a name, so it should have a ST!"); (void)Failure; 299ef860a24SChandler Carruth 300ef860a24SChandler Carruth // If these values are both in the same symtab, we can do this very fast. 301ef860a24SChandler Carruth // This works even if both values have no symtab yet. 302ef860a24SChandler Carruth if (ST == VST) { 303ef860a24SChandler Carruth // Take the name! 3045bf8fef5SDuncan P. N. Exon Smith setValueName(V->getValueName()); 3055bf8fef5SDuncan P. N. Exon Smith V->setValueName(nullptr); 3065bf8fef5SDuncan P. N. Exon Smith getValueName()->setValue(this); 307ef860a24SChandler Carruth return; 308ef860a24SChandler Carruth } 309ef860a24SChandler Carruth 310ef860a24SChandler Carruth // Otherwise, things are slightly more complex. Remove V's name from VST and 311ef860a24SChandler Carruth // then reinsert it into ST. 312ef860a24SChandler Carruth 313ef860a24SChandler Carruth if (VST) 3145bf8fef5SDuncan P. N. Exon Smith VST->removeValueName(V->getValueName()); 3155bf8fef5SDuncan P. N. Exon Smith setValueName(V->getValueName()); 3165bf8fef5SDuncan P. N. Exon Smith V->setValueName(nullptr); 3175bf8fef5SDuncan P. N. Exon Smith getValueName()->setValue(this); 318ef860a24SChandler Carruth 319ef860a24SChandler Carruth if (ST) 320ef860a24SChandler Carruth ST->reinsertValue(this); 321ef860a24SChandler Carruth } 322ef860a24SChandler Carruth 323257a3536SRafael Espindola void Value::assertModuleIsMaterialized() const { 324675bdcedSTodd Fiala #ifndef NDEBUG 325257a3536SRafael Espindola const GlobalValue *GV = dyn_cast<GlobalValue>(this); 326257a3536SRafael Espindola if (!GV) 327257a3536SRafael Espindola return; 328257a3536SRafael Espindola const Module *M = GV->getParent(); 329257a3536SRafael Espindola if (!M) 330257a3536SRafael Espindola return; 331257a3536SRafael Espindola assert(M->isMaterialized()); 332675bdcedSTodd Fiala #endif 333257a3536SRafael Espindola } 334257a3536SRafael Espindola 335675bdcedSTodd Fiala #ifndef NDEBUG 33671b7b68bSCraig Topper static bool contains(SmallPtrSetImpl<ConstantExpr *> &Cache, ConstantExpr *Expr, 3377e2b7567SRafael Espindola Constant *C) { 33870573dcdSDavid Blaikie if (!Cache.insert(Expr).second) 3397e2b7567SRafael Espindola return false; 3407e2b7567SRafael Espindola 3417e2b7567SRafael Espindola for (auto &O : Expr->operands()) { 3427e2b7567SRafael Espindola if (O == C) 3437e2b7567SRafael Espindola return true; 3447e2b7567SRafael Espindola auto *CE = dyn_cast<ConstantExpr>(O); 3457e2b7567SRafael Espindola if (!CE) 3467e2b7567SRafael Espindola continue; 3477e2b7567SRafael Espindola if (contains(Cache, CE, C)) 3487e2b7567SRafael Espindola return true; 3497e2b7567SRafael Espindola } 3507e2b7567SRafael Espindola return false; 3517e2b7567SRafael Espindola } 3527e2b7567SRafael Espindola 3537e2b7567SRafael Espindola static bool contains(Value *Expr, Value *V) { 3547e2b7567SRafael Espindola if (Expr == V) 3557e2b7567SRafael Espindola return true; 3567e2b7567SRafael Espindola 3577e2b7567SRafael Espindola auto *C = dyn_cast<Constant>(V); 3587e2b7567SRafael Espindola if (!C) 3597e2b7567SRafael Espindola return false; 3607e2b7567SRafael Espindola 3617e2b7567SRafael Espindola auto *CE = dyn_cast<ConstantExpr>(Expr); 3627e2b7567SRafael Espindola if (!CE) 3637e2b7567SRafael Espindola return false; 3647e2b7567SRafael Espindola 3657e2b7567SRafael Espindola SmallPtrSet<ConstantExpr *, 4> Cache; 3667e2b7567SRafael Espindola return contains(Cache, CE, C); 3677e2b7567SRafael Espindola } 368ecefe5a8SEugene Zelenko #endif // NDEBUG 369ef860a24SChandler Carruth 370ef860a24SChandler Carruth void Value::replaceAllUsesWith(Value *New) { 371ef860a24SChandler Carruth assert(New && "Value::replaceAllUsesWith(<null>) is invalid!"); 3727e2b7567SRafael Espindola assert(!contains(New, this) && 3737e2b7567SRafael Espindola "this->replaceAllUsesWith(expr(this)) is NOT valid!"); 374ef860a24SChandler Carruth assert(New->getType() == getType() && 375ef860a24SChandler Carruth "replaceAllUses of value with new value of different type!"); 376ef860a24SChandler Carruth 377ef860a24SChandler Carruth // Notify all ValueHandles (if present) that this value is going away. 378ef860a24SChandler Carruth if (HasValueHandle) 379ef860a24SChandler Carruth ValueHandleBase::ValueIsRAUWd(this, New); 3805bf8fef5SDuncan P. N. Exon Smith if (isUsedByMetadata()) 3815bf8fef5SDuncan P. N. Exon Smith ValueAsMetadata::handleRAUW(this, New); 382ef860a24SChandler Carruth 383ef860a24SChandler Carruth while (!use_empty()) { 384ef860a24SChandler Carruth Use &U = *UseList; 385ef860a24SChandler Carruth // Must handle Constants specially, we cannot call replaceUsesOfWith on a 386ef860a24SChandler Carruth // constant because they are uniqued. 3876b238633SRafael Espindola if (auto *C = dyn_cast<Constant>(U.getUser())) { 388ef860a24SChandler Carruth if (!isa<GlobalValue>(C)) { 3898914d290SMehdi Amini C->handleOperandChange(this, New); 390ef860a24SChandler Carruth continue; 391ef860a24SChandler Carruth } 392ef860a24SChandler Carruth } 393ef860a24SChandler Carruth 394ef860a24SChandler Carruth U.set(New); 395ef860a24SChandler Carruth } 396ef860a24SChandler Carruth 397ef860a24SChandler Carruth if (BasicBlock *BB = dyn_cast<BasicBlock>(this)) 398ef860a24SChandler Carruth BB->replaceSuccessorsPhiUsesWith(cast<BasicBlock>(New)); 399ef860a24SChandler Carruth } 400ef860a24SChandler Carruth 401ec6217c9SGerolf Hoflehner // Like replaceAllUsesWith except it does not handle constants or basic blocks. 402ec6217c9SGerolf Hoflehner // This routine leaves uses within BB. 403ec6217c9SGerolf Hoflehner void Value::replaceUsesOutsideBlock(Value *New, BasicBlock *BB) { 404ec6217c9SGerolf Hoflehner assert(New && "Value::replaceUsesOutsideBlock(<null>, BB) is invalid!"); 405ec6217c9SGerolf Hoflehner assert(!contains(New, this) && 406ec6217c9SGerolf Hoflehner "this->replaceUsesOutsideBlock(expr(this), BB) is NOT valid!"); 407ec6217c9SGerolf Hoflehner assert(New->getType() == getType() && 408ec6217c9SGerolf Hoflehner "replaceUses of value with new value of different type!"); 409ec6217c9SGerolf Hoflehner assert(BB && "Basic block that may contain a use of 'New' must be defined\n"); 410ec6217c9SGerolf Hoflehner 411ec6217c9SGerolf Hoflehner use_iterator UI = use_begin(), E = use_end(); 412ec6217c9SGerolf Hoflehner for (; UI != E;) { 413ec6217c9SGerolf Hoflehner Use &U = *UI; 414ec6217c9SGerolf Hoflehner ++UI; 415ec6217c9SGerolf Hoflehner auto *Usr = dyn_cast<Instruction>(U.getUser()); 416ec6217c9SGerolf Hoflehner if (Usr && Usr->getParent() == BB) 417ec6217c9SGerolf Hoflehner continue; 418ec6217c9SGerolf Hoflehner U.set(New); 419ec6217c9SGerolf Hoflehner } 420ec6217c9SGerolf Hoflehner } 421ec6217c9SGerolf Hoflehner 422ef860a24SChandler Carruth namespace { 423ef860a24SChandler Carruth // Various metrics for how much to strip off of pointers. 424ef860a24SChandler Carruth enum PointerStripKind { 425ef860a24SChandler Carruth PSK_ZeroIndices, 426c229a4ffSRafael Espindola PSK_ZeroIndicesAndAliases, 427ef860a24SChandler Carruth PSK_InBoundsConstantIndices, 428ef860a24SChandler Carruth PSK_InBounds 429ef860a24SChandler Carruth }; 430ef860a24SChandler Carruth 431ef860a24SChandler Carruth template <PointerStripKind StripKind> 432ef860a24SChandler Carruth static Value *stripPointerCastsAndOffsets(Value *V) { 433ef860a24SChandler Carruth if (!V->getType()->isPointerTy()) 434ef860a24SChandler Carruth return V; 435ef860a24SChandler Carruth 436ef860a24SChandler Carruth // Even though we don't look through PHI nodes, we could be called on an 437ef860a24SChandler Carruth // instruction in an unreachable block, which may be on a cycle. 438ef860a24SChandler Carruth SmallPtrSet<Value *, 4> Visited; 439ef860a24SChandler Carruth 440ef860a24SChandler Carruth Visited.insert(V); 441ef860a24SChandler Carruth do { 442ef860a24SChandler Carruth if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { 443ef860a24SChandler Carruth switch (StripKind) { 444c229a4ffSRafael Espindola case PSK_ZeroIndicesAndAliases: 445ef860a24SChandler Carruth case PSK_ZeroIndices: 446ef860a24SChandler Carruth if (!GEP->hasAllZeroIndices()) 447ef860a24SChandler Carruth return V; 448ef860a24SChandler Carruth break; 449ef860a24SChandler Carruth case PSK_InBoundsConstantIndices: 450ef860a24SChandler Carruth if (!GEP->hasAllConstantIndices()) 451ef860a24SChandler Carruth return V; 452ef860a24SChandler Carruth // fallthrough 453ef860a24SChandler Carruth case PSK_InBounds: 454ef860a24SChandler Carruth if (!GEP->isInBounds()) 455ef860a24SChandler Carruth return V; 456ef860a24SChandler Carruth break; 457ef860a24SChandler Carruth } 458ef860a24SChandler Carruth V = GEP->getPointerOperand(); 459b03bd4d9SMatt Arsenault } else if (Operator::getOpcode(V) == Instruction::BitCast || 460b03bd4d9SMatt Arsenault Operator::getOpcode(V) == Instruction::AddrSpaceCast) { 461ef860a24SChandler Carruth V = cast<Operator>(V)->getOperand(0); 462ef860a24SChandler Carruth } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { 4635ce32728SSanjoy Das if (StripKind == PSK_ZeroIndices || GA->isInterposable()) 464ef860a24SChandler Carruth return V; 465ef860a24SChandler Carruth V = GA->getAliasee(); 466ef860a24SChandler Carruth } else { 4675c12d8feSHal Finkel if (auto CS = CallSite(V)) 4685c12d8feSHal Finkel if (Value *RV = CS.getReturnedArgOperand()) { 4695c12d8feSHal Finkel V = RV; 4705c12d8feSHal Finkel continue; 4715c12d8feSHal Finkel } 4725c12d8feSHal Finkel 473ef860a24SChandler Carruth return V; 474ef860a24SChandler Carruth } 475ef860a24SChandler Carruth assert(V->getType()->isPointerTy() && "Unexpected operand type!"); 47670573dcdSDavid Blaikie } while (Visited.insert(V).second); 477ef860a24SChandler Carruth 478ef860a24SChandler Carruth return V; 479ef860a24SChandler Carruth } 480ecefe5a8SEugene Zelenko } // end anonymous namespace 481ef860a24SChandler Carruth 482ef860a24SChandler Carruth Value *Value::stripPointerCasts() { 483c229a4ffSRafael Espindola return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndAliases>(this); 484c229a4ffSRafael Espindola } 485c229a4ffSRafael Espindola 486c229a4ffSRafael Espindola Value *Value::stripPointerCastsNoFollowAliases() { 487ef860a24SChandler Carruth return stripPointerCastsAndOffsets<PSK_ZeroIndices>(this); 488ef860a24SChandler Carruth } 489ef860a24SChandler Carruth 490ef860a24SChandler Carruth Value *Value::stripInBoundsConstantOffsets() { 491ef860a24SChandler Carruth return stripPointerCastsAndOffsets<PSK_InBoundsConstantIndices>(this); 492ef860a24SChandler Carruth } 493ef860a24SChandler Carruth 494989e6308SChandler Carruth Value *Value::stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, 495989e6308SChandler Carruth APInt &Offset) { 496989e6308SChandler Carruth if (!getType()->isPointerTy()) 497989e6308SChandler Carruth return this; 498989e6308SChandler Carruth 499989e6308SChandler Carruth assert(Offset.getBitWidth() == DL.getPointerSizeInBits(cast<PointerType>( 500989e6308SChandler Carruth getType())->getAddressSpace()) && 501989e6308SChandler Carruth "The offset must have exactly as many bits as our pointer."); 502989e6308SChandler Carruth 503989e6308SChandler Carruth // Even though we don't look through PHI nodes, we could be called on an 504989e6308SChandler Carruth // instruction in an unreachable block, which may be on a cycle. 505989e6308SChandler Carruth SmallPtrSet<Value *, 4> Visited; 506989e6308SChandler Carruth Visited.insert(this); 507989e6308SChandler Carruth Value *V = this; 508989e6308SChandler Carruth do { 509989e6308SChandler Carruth if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { 510989e6308SChandler Carruth if (!GEP->isInBounds()) 511989e6308SChandler Carruth return V; 512ba689b33SChandler Carruth APInt GEPOffset(Offset); 513ba689b33SChandler Carruth if (!GEP->accumulateConstantOffset(DL, GEPOffset)) 514989e6308SChandler Carruth return V; 515ba689b33SChandler Carruth Offset = GEPOffset; 516989e6308SChandler Carruth V = GEP->getPointerOperand(); 517d63df510SPhilip Reames } else if (Operator::getOpcode(V) == Instruction::BitCast) { 518989e6308SChandler Carruth V = cast<Operator>(V)->getOperand(0); 519989e6308SChandler Carruth } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { 520989e6308SChandler Carruth V = GA->getAliasee(); 521989e6308SChandler Carruth } else { 5225c12d8feSHal Finkel if (auto CS = CallSite(V)) 5235c12d8feSHal Finkel if (Value *RV = CS.getReturnedArgOperand()) { 5245c12d8feSHal Finkel V = RV; 5255c12d8feSHal Finkel continue; 5265c12d8feSHal Finkel } 5275c12d8feSHal Finkel 528989e6308SChandler Carruth return V; 529989e6308SChandler Carruth } 530989e6308SChandler Carruth assert(V->getType()->isPointerTy() && "Unexpected operand type!"); 53170573dcdSDavid Blaikie } while (Visited.insert(V).second); 532989e6308SChandler Carruth 533989e6308SChandler Carruth return V; 534989e6308SChandler Carruth } 535989e6308SChandler Carruth 536ef860a24SChandler Carruth Value *Value::stripInBoundsOffsets() { 537ef860a24SChandler Carruth return stripPointerCastsAndOffsets<PSK_InBounds>(this); 538ef860a24SChandler Carruth } 539ef860a24SChandler Carruth 54000953cbeSSanjoy Das unsigned Value::getPointerDereferenceableBytes(const DataLayout &DL, 54100953cbeSSanjoy Das bool &CanBeNull) const { 542345f0148SArtur Pilipenko assert(getType()->isPointerTy() && "must be pointer"); 543345f0148SArtur Pilipenko 544345f0148SArtur Pilipenko unsigned DerefBytes = 0; 545345f0148SArtur Pilipenko CanBeNull = false; 546345f0148SArtur Pilipenko if (const Argument *A = dyn_cast<Argument>(this)) { 547345f0148SArtur Pilipenko DerefBytes = A->getDereferenceableBytes(); 54800953cbeSSanjoy Das if (DerefBytes == 0 && A->hasByValAttr() && A->getType()->isSized()) { 54900953cbeSSanjoy Das DerefBytes = DL.getTypeStoreSize(A->getType()); 55000953cbeSSanjoy Das CanBeNull = false; 55100953cbeSSanjoy Das } 552345f0148SArtur Pilipenko if (DerefBytes == 0) { 553345f0148SArtur Pilipenko DerefBytes = A->getDereferenceableOrNullBytes(); 554345f0148SArtur Pilipenko CanBeNull = true; 555345f0148SArtur Pilipenko } 556345f0148SArtur Pilipenko } else if (auto CS = ImmutableCallSite(this)) { 557345f0148SArtur Pilipenko DerefBytes = CS.getDereferenceableBytes(0); 558345f0148SArtur Pilipenko if (DerefBytes == 0) { 559345f0148SArtur Pilipenko DerefBytes = CS.getDereferenceableOrNullBytes(0); 560345f0148SArtur Pilipenko CanBeNull = true; 561345f0148SArtur Pilipenko } 562345f0148SArtur Pilipenko } else if (const LoadInst *LI = dyn_cast<LoadInst>(this)) { 563345f0148SArtur Pilipenko if (MDNode *MD = LI->getMetadata(LLVMContext::MD_dereferenceable)) { 564345f0148SArtur Pilipenko ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0)); 565345f0148SArtur Pilipenko DerefBytes = CI->getLimitedValue(); 566345f0148SArtur Pilipenko } 567345f0148SArtur Pilipenko if (DerefBytes == 0) { 568345f0148SArtur Pilipenko if (MDNode *MD = 569345f0148SArtur Pilipenko LI->getMetadata(LLVMContext::MD_dereferenceable_or_null)) { 570345f0148SArtur Pilipenko ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0)); 571345f0148SArtur Pilipenko DerefBytes = CI->getLimitedValue(); 572345f0148SArtur Pilipenko } 573345f0148SArtur Pilipenko CanBeNull = true; 574345f0148SArtur Pilipenko } 57500953cbeSSanjoy Das } else if (auto *AI = dyn_cast<AllocaInst>(this)) { 57600953cbeSSanjoy Das if (AI->getAllocatedType()->isSized()) { 57700953cbeSSanjoy Das DerefBytes = DL.getTypeStoreSize(AI->getAllocatedType()); 57800953cbeSSanjoy Das CanBeNull = false; 57900953cbeSSanjoy Das } 58000953cbeSSanjoy Das } else if (auto *GV = dyn_cast<GlobalVariable>(this)) { 58100953cbeSSanjoy Das if (GV->getValueType()->isSized() && !GV->hasExternalWeakLinkage()) { 58200953cbeSSanjoy Das // TODO: Don't outright reject hasExternalWeakLinkage but set the 58300953cbeSSanjoy Das // CanBeNull flag. 58400953cbeSSanjoy Das DerefBytes = DL.getTypeStoreSize(GV->getValueType()); 58500953cbeSSanjoy Das CanBeNull = false; 58600953cbeSSanjoy Das } 587345f0148SArtur Pilipenko } 588345f0148SArtur Pilipenko return DerefBytes; 589345f0148SArtur Pilipenko } 590345f0148SArtur Pilipenko 591ae51afc5SArtur Pilipenko unsigned Value::getPointerAlignment(const DataLayout &DL) const { 592ae51afc5SArtur Pilipenko assert(getType()->isPointerTy() && "must be pointer"); 593ae51afc5SArtur Pilipenko 594ae51afc5SArtur Pilipenko unsigned Align = 0; 595ae51afc5SArtur Pilipenko if (auto *GO = dyn_cast<GlobalObject>(this)) { 596ae51afc5SArtur Pilipenko Align = GO->getAlignment(); 597ae51afc5SArtur Pilipenko if (Align == 0) { 598ae51afc5SArtur Pilipenko if (auto *GVar = dyn_cast<GlobalVariable>(GO)) { 599ae51afc5SArtur Pilipenko Type *ObjectType = GVar->getValueType(); 600ae51afc5SArtur Pilipenko if (ObjectType->isSized()) { 601ae51afc5SArtur Pilipenko // If the object is defined in the current Module, we'll be giving 602ae51afc5SArtur Pilipenko // it the preferred alignment. Otherwise, we have to assume that it 603ae51afc5SArtur Pilipenko // may only have the minimum ABI alignment. 604ae51afc5SArtur Pilipenko if (GVar->isStrongDefinitionForLinker()) 605ae51afc5SArtur Pilipenko Align = DL.getPreferredAlignment(GVar); 606ae51afc5SArtur Pilipenko else 607ae51afc5SArtur Pilipenko Align = DL.getABITypeAlignment(ObjectType); 608ae51afc5SArtur Pilipenko } 609ae51afc5SArtur Pilipenko } 610ae51afc5SArtur Pilipenko } 611ae51afc5SArtur Pilipenko } else if (const Argument *A = dyn_cast<Argument>(this)) { 612ae51afc5SArtur Pilipenko Align = A->getParamAlignment(); 613ae51afc5SArtur Pilipenko 614ae51afc5SArtur Pilipenko if (!Align && A->hasStructRetAttr()) { 615ae51afc5SArtur Pilipenko // An sret parameter has at least the ABI alignment of the return type. 616ae51afc5SArtur Pilipenko Type *EltTy = cast<PointerType>(A->getType())->getElementType(); 617ae51afc5SArtur Pilipenko if (EltTy->isSized()) 618ae51afc5SArtur Pilipenko Align = DL.getABITypeAlignment(EltTy); 619ae51afc5SArtur Pilipenko } 620c97eac65SArtur Pilipenko } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(this)) { 621ae51afc5SArtur Pilipenko Align = AI->getAlignment(); 622c97eac65SArtur Pilipenko if (Align == 0) { 623c97eac65SArtur Pilipenko Type *AllocatedType = AI->getAllocatedType(); 624c97eac65SArtur Pilipenko if (AllocatedType->isSized()) 625c97eac65SArtur Pilipenko Align = DL.getPrefTypeAlignment(AllocatedType); 626c97eac65SArtur Pilipenko } 627c97eac65SArtur Pilipenko } else if (auto CS = ImmutableCallSite(this)) 628ae51afc5SArtur Pilipenko Align = CS.getAttributes().getParamAlignment(AttributeSet::ReturnIndex); 629ae51afc5SArtur Pilipenko else if (const LoadInst *LI = dyn_cast<LoadInst>(this)) 630ae51afc5SArtur Pilipenko if (MDNode *MD = LI->getMetadata(LLVMContext::MD_align)) { 631ae51afc5SArtur Pilipenko ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0)); 632ae51afc5SArtur Pilipenko Align = CI->getLimitedValue(); 633ae51afc5SArtur Pilipenko } 634ae51afc5SArtur Pilipenko 635ae51afc5SArtur Pilipenko return Align; 636ae51afc5SArtur Pilipenko } 637ae51afc5SArtur Pilipenko 638ef860a24SChandler Carruth Value *Value::DoPHITranslation(const BasicBlock *CurBB, 639ef860a24SChandler Carruth const BasicBlock *PredBB) { 640ef860a24SChandler Carruth PHINode *PN = dyn_cast<PHINode>(this); 641ef860a24SChandler Carruth if (PN && PN->getParent() == CurBB) 642ef860a24SChandler Carruth return PN->getIncomingValueForBlock(PredBB); 643ef860a24SChandler Carruth return this; 644ef860a24SChandler Carruth } 645ef860a24SChandler Carruth 646ef860a24SChandler Carruth LLVMContext &Value::getContext() const { return VTy->getContext(); } 647ef860a24SChandler Carruth 6483441ffe9SDuncan P. N. Exon Smith void Value::reverseUseList() { 6493441ffe9SDuncan P. N. Exon Smith if (!UseList || !UseList->Next) 6503441ffe9SDuncan P. N. Exon Smith // No need to reverse 0 or 1 uses. 6513441ffe9SDuncan P. N. Exon Smith return; 6523441ffe9SDuncan P. N. Exon Smith 6533441ffe9SDuncan P. N. Exon Smith Use *Head = UseList; 6543441ffe9SDuncan P. N. Exon Smith Use *Current = UseList->Next; 6553441ffe9SDuncan P. N. Exon Smith Head->Next = nullptr; 6563441ffe9SDuncan P. N. Exon Smith while (Current) { 6573441ffe9SDuncan P. N. Exon Smith Use *Next = Current->Next; 6583441ffe9SDuncan P. N. Exon Smith Current->Next = Head; 6593441ffe9SDuncan P. N. Exon Smith Head->setPrev(&Current->Next); 6603441ffe9SDuncan P. N. Exon Smith Head = Current; 6613441ffe9SDuncan P. N. Exon Smith Current = Next; 6623441ffe9SDuncan P. N. Exon Smith } 6633441ffe9SDuncan P. N. Exon Smith UseList = Head; 6643441ffe9SDuncan P. N. Exon Smith Head->setPrev(&UseList); 6653441ffe9SDuncan P. N. Exon Smith } 6663441ffe9SDuncan P. N. Exon Smith 667ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 668ef860a24SChandler Carruth // ValueHandleBase Class 669ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 670ef860a24SChandler Carruth 671ef860a24SChandler Carruth void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) { 672ef860a24SChandler Carruth assert(List && "Handle list is null?"); 673ef860a24SChandler Carruth 674ef860a24SChandler Carruth // Splice ourselves into the list. 675ef860a24SChandler Carruth Next = *List; 676ef860a24SChandler Carruth *List = this; 677ef860a24SChandler Carruth setPrevPtr(List); 678ef860a24SChandler Carruth if (Next) { 679ef860a24SChandler Carruth Next->setPrevPtr(&Next); 680f4ea3d3dSChandler Carruth assert(V == Next->V && "Added to wrong list?"); 681ef860a24SChandler Carruth } 682ef860a24SChandler Carruth } 683ef860a24SChandler Carruth 684ef860a24SChandler Carruth void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) { 685ef860a24SChandler Carruth assert(List && "Must insert after existing node"); 686ef860a24SChandler Carruth 687ef860a24SChandler Carruth Next = List->Next; 688ef860a24SChandler Carruth setPrevPtr(&List->Next); 689ef860a24SChandler Carruth List->Next = this; 690ef860a24SChandler Carruth if (Next) 691ef860a24SChandler Carruth Next->setPrevPtr(&Next); 692ef860a24SChandler Carruth } 693ef860a24SChandler Carruth 694ef860a24SChandler Carruth void ValueHandleBase::AddToUseList() { 695f4ea3d3dSChandler Carruth assert(V && "Null pointer doesn't have a use list!"); 696ef860a24SChandler Carruth 697f4ea3d3dSChandler Carruth LLVMContextImpl *pImpl = V->getContext().pImpl; 698ef860a24SChandler Carruth 699f4ea3d3dSChandler Carruth if (V->HasValueHandle) { 700ef860a24SChandler Carruth // If this value already has a ValueHandle, then it must be in the 701ef860a24SChandler Carruth // ValueHandles map already. 702f4ea3d3dSChandler Carruth ValueHandleBase *&Entry = pImpl->ValueHandles[V]; 7032617dcceSCraig Topper assert(Entry && "Value doesn't have any handles?"); 704ef860a24SChandler Carruth AddToExistingUseList(&Entry); 705ef860a24SChandler Carruth return; 706ef860a24SChandler Carruth } 707ef860a24SChandler Carruth 708ef860a24SChandler Carruth // Ok, it doesn't have any handles yet, so we must insert it into the 709ef860a24SChandler Carruth // DenseMap. However, doing this insertion could cause the DenseMap to 710ef860a24SChandler Carruth // reallocate itself, which would invalidate all of the PrevP pointers that 711ef860a24SChandler Carruth // point into the old table. Handle this by checking for reallocation and 712ef860a24SChandler Carruth // updating the stale pointers only if needed. 713ef860a24SChandler Carruth DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles; 714ef860a24SChandler Carruth const void *OldBucketPtr = Handles.getPointerIntoBucketsArray(); 715ef860a24SChandler Carruth 716f4ea3d3dSChandler Carruth ValueHandleBase *&Entry = Handles[V]; 7172617dcceSCraig Topper assert(!Entry && "Value really did already have handles?"); 718ef860a24SChandler Carruth AddToExistingUseList(&Entry); 719f4ea3d3dSChandler Carruth V->HasValueHandle = true; 720ef860a24SChandler Carruth 721ef860a24SChandler Carruth // If reallocation didn't happen or if this was the first insertion, don't 722ef860a24SChandler Carruth // walk the table. 723ef860a24SChandler Carruth if (Handles.isPointerIntoBucketsArray(OldBucketPtr) || 724ef860a24SChandler Carruth Handles.size() == 1) { 725ef860a24SChandler Carruth return; 726ef860a24SChandler Carruth } 727ef860a24SChandler Carruth 728ef860a24SChandler Carruth // Okay, reallocation did happen. Fix the Prev Pointers. 729ef860a24SChandler Carruth for (DenseMap<Value*, ValueHandleBase*>::iterator I = Handles.begin(), 730ef860a24SChandler Carruth E = Handles.end(); I != E; ++I) { 731f4ea3d3dSChandler Carruth assert(I->second && I->first == I->second->V && 732ef860a24SChandler Carruth "List invariant broken!"); 733ef860a24SChandler Carruth I->second->setPrevPtr(&I->second); 734ef860a24SChandler Carruth } 735ef860a24SChandler Carruth } 736ef860a24SChandler Carruth 737ef860a24SChandler Carruth void ValueHandleBase::RemoveFromUseList() { 738f4ea3d3dSChandler Carruth assert(V && V->HasValueHandle && 739ef860a24SChandler Carruth "Pointer doesn't have a use list!"); 740ef860a24SChandler Carruth 741ef860a24SChandler Carruth // Unlink this from its use list. 742ef860a24SChandler Carruth ValueHandleBase **PrevPtr = getPrevPtr(); 743ef860a24SChandler Carruth assert(*PrevPtr == this && "List invariant broken"); 744ef860a24SChandler Carruth 745ef860a24SChandler Carruth *PrevPtr = Next; 746ef860a24SChandler Carruth if (Next) { 747ef860a24SChandler Carruth assert(Next->getPrevPtr() == &Next && "List invariant broken"); 748ef860a24SChandler Carruth Next->setPrevPtr(PrevPtr); 749ef860a24SChandler Carruth return; 750ef860a24SChandler Carruth } 751ef860a24SChandler Carruth 752ef860a24SChandler Carruth // If the Next pointer was null, then it is possible that this was the last 753ef860a24SChandler Carruth // ValueHandle watching VP. If so, delete its entry from the ValueHandles 754ef860a24SChandler Carruth // map. 755f4ea3d3dSChandler Carruth LLVMContextImpl *pImpl = V->getContext().pImpl; 756ef860a24SChandler Carruth DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles; 757ef860a24SChandler Carruth if (Handles.isPointerIntoBucketsArray(PrevPtr)) { 758f4ea3d3dSChandler Carruth Handles.erase(V); 759f4ea3d3dSChandler Carruth V->HasValueHandle = false; 760ef860a24SChandler Carruth } 761ef860a24SChandler Carruth } 762ef860a24SChandler Carruth 763ef860a24SChandler Carruth void ValueHandleBase::ValueIsDeleted(Value *V) { 764ef860a24SChandler Carruth assert(V->HasValueHandle && "Should only be called if ValueHandles present"); 765ef860a24SChandler Carruth 766ef860a24SChandler Carruth // Get the linked list base, which is guaranteed to exist since the 767ef860a24SChandler Carruth // HasValueHandle flag is set. 768ef860a24SChandler Carruth LLVMContextImpl *pImpl = V->getContext().pImpl; 769ef860a24SChandler Carruth ValueHandleBase *Entry = pImpl->ValueHandles[V]; 770ef860a24SChandler Carruth assert(Entry && "Value bit set but no entries exist"); 771ef860a24SChandler Carruth 772ef860a24SChandler Carruth // We use a local ValueHandleBase as an iterator so that ValueHandles can add 773ef860a24SChandler Carruth // and remove themselves from the list without breaking our iteration. This 774ef860a24SChandler Carruth // is not really an AssertingVH; we just have to give ValueHandleBase a kind. 775ef860a24SChandler Carruth // Note that we deliberately do not the support the case when dropping a value 776ef860a24SChandler Carruth // handle results in a new value handle being permanently added to the list 777ef860a24SChandler Carruth // (as might occur in theory for CallbackVH's): the new value handle will not 778ef860a24SChandler Carruth // be processed and the checking code will mete out righteous punishment if 779ef860a24SChandler Carruth // the handle is still present once we have finished processing all the other 780ef860a24SChandler Carruth // value handles (it is fine to momentarily add then remove a value handle). 781ef860a24SChandler Carruth for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) { 782ef860a24SChandler Carruth Iterator.RemoveFromUseList(); 783ef860a24SChandler Carruth Iterator.AddToExistingUseListAfter(Entry); 784ef860a24SChandler Carruth assert(Entry->Next == &Iterator && "Loop invariant broken."); 785ef860a24SChandler Carruth 786ef860a24SChandler Carruth switch (Entry->getKind()) { 787ef860a24SChandler Carruth case Assert: 788ef860a24SChandler Carruth break; 789ef860a24SChandler Carruth case Tracking: 790ef860a24SChandler Carruth // Mark that this value has been deleted by setting it to an invalid Value 791ef860a24SChandler Carruth // pointer. 792ef860a24SChandler Carruth Entry->operator=(DenseMapInfo<Value *>::getTombstoneKey()); 793ef860a24SChandler Carruth break; 794ef860a24SChandler Carruth case Weak: 795ef860a24SChandler Carruth // Weak just goes to null, which will unlink it from the list. 796c620761cSCraig Topper Entry->operator=(nullptr); 797ef860a24SChandler Carruth break; 798ef860a24SChandler Carruth case Callback: 799ef860a24SChandler Carruth // Forward to the subclass's implementation. 800ef860a24SChandler Carruth static_cast<CallbackVH*>(Entry)->deleted(); 801ef860a24SChandler Carruth break; 802ef860a24SChandler Carruth } 803ef860a24SChandler Carruth } 804ef860a24SChandler Carruth 805ef860a24SChandler Carruth // All callbacks, weak references, and assertingVHs should be dropped by now. 806ef860a24SChandler Carruth if (V->HasValueHandle) { 807ef860a24SChandler Carruth #ifndef NDEBUG // Only in +Asserts mode... 808ef860a24SChandler Carruth dbgs() << "While deleting: " << *V->getType() << " %" << V->getName() 809ef860a24SChandler Carruth << "\n"; 810ef860a24SChandler Carruth if (pImpl->ValueHandles[V]->getKind() == Assert) 811ef860a24SChandler Carruth llvm_unreachable("An asserting value handle still pointed to this" 812ef860a24SChandler Carruth " value!"); 813ef860a24SChandler Carruth 814ef860a24SChandler Carruth #endif 815ef860a24SChandler Carruth llvm_unreachable("All references to V were not removed?"); 816ef860a24SChandler Carruth } 817ef860a24SChandler Carruth } 818ef860a24SChandler Carruth 819ef860a24SChandler Carruth void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) { 820ef860a24SChandler Carruth assert(Old->HasValueHandle &&"Should only be called if ValueHandles present"); 821ef860a24SChandler Carruth assert(Old != New && "Changing value into itself!"); 822c1892e2dSFrederic Riss assert(Old->getType() == New->getType() && 823c1892e2dSFrederic Riss "replaceAllUses of value with new value of different type!"); 824ef860a24SChandler Carruth 825ef860a24SChandler Carruth // Get the linked list base, which is guaranteed to exist since the 826ef860a24SChandler Carruth // HasValueHandle flag is set. 827ef860a24SChandler Carruth LLVMContextImpl *pImpl = Old->getContext().pImpl; 828ef860a24SChandler Carruth ValueHandleBase *Entry = pImpl->ValueHandles[Old]; 829ef860a24SChandler Carruth 830ef860a24SChandler Carruth assert(Entry && "Value bit set but no entries exist"); 831ef860a24SChandler Carruth 832ef860a24SChandler Carruth // We use a local ValueHandleBase as an iterator so that 833ef860a24SChandler Carruth // ValueHandles can add and remove themselves from the list without 834ef860a24SChandler Carruth // breaking our iteration. This is not really an AssertingVH; we 835ef860a24SChandler Carruth // just have to give ValueHandleBase some kind. 836ef860a24SChandler Carruth for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) { 837ef860a24SChandler Carruth Iterator.RemoveFromUseList(); 838ef860a24SChandler Carruth Iterator.AddToExistingUseListAfter(Entry); 839ef860a24SChandler Carruth assert(Entry->Next == &Iterator && "Loop invariant broken."); 840ef860a24SChandler Carruth 841ef860a24SChandler Carruth switch (Entry->getKind()) { 842ef860a24SChandler Carruth case Assert: 843ef860a24SChandler Carruth // Asserting handle does not follow RAUW implicitly. 844ef860a24SChandler Carruth break; 845ef860a24SChandler Carruth case Tracking: 846ef860a24SChandler Carruth // Tracking goes to new value like a WeakVH. Note that this may make it 847ef860a24SChandler Carruth // something incompatible with its templated type. We don't want to have a 848ef860a24SChandler Carruth // virtual (or inline) interface to handle this though, so instead we make 849ef860a24SChandler Carruth // the TrackingVH accessors guarantee that a client never sees this value. 850ef860a24SChandler Carruth 851ef860a24SChandler Carruth // FALLTHROUGH 852ef860a24SChandler Carruth case Weak: 853ef860a24SChandler Carruth // Weak goes to the new value, which will unlink it from Old's list. 854ef860a24SChandler Carruth Entry->operator=(New); 855ef860a24SChandler Carruth break; 856ef860a24SChandler Carruth case Callback: 857ef860a24SChandler Carruth // Forward to the subclass's implementation. 858ef860a24SChandler Carruth static_cast<CallbackVH*>(Entry)->allUsesReplacedWith(New); 859ef860a24SChandler Carruth break; 860ef860a24SChandler Carruth } 861ef860a24SChandler Carruth } 862ef860a24SChandler Carruth 863ef860a24SChandler Carruth #ifndef NDEBUG 864ef860a24SChandler Carruth // If any new tracking or weak value handles were added while processing the 865ef860a24SChandler Carruth // list, then complain about it now. 866ef860a24SChandler Carruth if (Old->HasValueHandle) 867ef860a24SChandler Carruth for (Entry = pImpl->ValueHandles[Old]; Entry; Entry = Entry->Next) 868ef860a24SChandler Carruth switch (Entry->getKind()) { 869ef860a24SChandler Carruth case Tracking: 870ef860a24SChandler Carruth case Weak: 871ef860a24SChandler Carruth dbgs() << "After RAUW from " << *Old->getType() << " %" 872ef860a24SChandler Carruth << Old->getName() << " to " << *New->getType() << " %" 873ef860a24SChandler Carruth << New->getName() << "\n"; 874ef860a24SChandler Carruth llvm_unreachable("A tracking or weak value handle still pointed to the" 875ef860a24SChandler Carruth " old value!\n"); 876ef860a24SChandler Carruth default: 877ef860a24SChandler Carruth break; 878ef860a24SChandler Carruth } 879ef860a24SChandler Carruth #endif 880ef860a24SChandler Carruth } 881ef860a24SChandler Carruth 882d12ccbd3SJuergen Ributzka // Pin the vtable to this file. 883d12ccbd3SJuergen Ributzka void CallbackVH::anchor() {} 884