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" 35ef860a24SChandler Carruth #include <algorithm> 36ef860a24SChandler Carruth using namespace llvm; 37ef860a24SChandler Carruth 38ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 39ef860a24SChandler Carruth // Value Class 40ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 41ef860a24SChandler Carruth 42ef860a24SChandler Carruth static inline Type *checkType(Type *Ty) { 43ef860a24SChandler Carruth assert(Ty && "Value defined with a null type: Error!"); 4416bf89ecSReid Kleckner return Ty; 45ef860a24SChandler Carruth } 46ef860a24SChandler Carruth 47ef860a24SChandler Carruth Value::Value(Type *ty, unsigned scid) 485bf8fef5SDuncan P. N. Exon Smith : VTy(checkType(ty)), UseList(nullptr), SubclassID(scid), HasValueHandle(0), 495bf8fef5SDuncan P. N. Exon Smith SubclassOptionalData(0), SubclassData(0), NumOperands(0) { 50ef860a24SChandler Carruth // FIXME: Why isn't this in the subclass gunk?? 51ef860a24SChandler Carruth // Note, we cannot call isa<CallInst> before the CallInst has been 52ef860a24SChandler Carruth // constructed. 53ef860a24SChandler Carruth if (SubclassID == Instruction::Call || SubclassID == Instruction::Invoke) 54ef860a24SChandler Carruth assert((VTy->isFirstClassType() || VTy->isVoidTy() || VTy->isStructTy()) && 55ef860a24SChandler Carruth "invalid CallInst type!"); 56ef860a24SChandler Carruth else if (SubclassID != BasicBlockVal && 57ef860a24SChandler Carruth (SubclassID < ConstantFirstVal || SubclassID > ConstantLastVal)) 58ef860a24SChandler Carruth assert((VTy->isFirstClassType() || VTy->isVoidTy()) && 59ef860a24SChandler Carruth "Cannot create non-first-class values except for constants!"); 60ef860a24SChandler Carruth } 61ef860a24SChandler Carruth 62ef860a24SChandler Carruth Value::~Value() { 63ef860a24SChandler Carruth // Notify all ValueHandles (if present) that this value is going away. 64ef860a24SChandler Carruth if (HasValueHandle) 65ef860a24SChandler Carruth ValueHandleBase::ValueIsDeleted(this); 665bf8fef5SDuncan P. N. Exon Smith if (isUsedByMetadata()) 675bf8fef5SDuncan P. N. Exon Smith ValueAsMetadata::handleDeletion(this); 68ef860a24SChandler Carruth 69ef860a24SChandler Carruth #ifndef NDEBUG // Only in -g mode... 70ef860a24SChandler Carruth // Check to make sure that there are no uses of this value that are still 71ef860a24SChandler Carruth // around when the value is destroyed. If there are, then we have a dangling 72*b93fb82bSAndrew Kaylor // reference and something is wrong. This code is here to print out where 73*b93fb82bSAndrew Kaylor // the value is still being referenced. 74ef860a24SChandler Carruth // 75ef860a24SChandler Carruth if (!use_empty()) { 76ef860a24SChandler Carruth dbgs() << "While deleting: " << *VTy << " %" << getName() << "\n"; 77*b93fb82bSAndrew Kaylor for (auto *U : users()) 78*b93fb82bSAndrew Kaylor dbgs() << "Use still stuck around after Def is destroyed:" << *U << "\n"; 79ef860a24SChandler Carruth } 80ef860a24SChandler Carruth #endif 81ef860a24SChandler Carruth assert(use_empty() && "Uses remain when a value is destroyed!"); 82ef860a24SChandler Carruth 83ef860a24SChandler Carruth // If this value is named, destroy the name. This should not be in a symtab 84ef860a24SChandler Carruth // at this point. 855bf8fef5SDuncan P. N. Exon Smith destroyValueName(); 86ef860a24SChandler Carruth } 87ef860a24SChandler Carruth 885bf8fef5SDuncan P. N. Exon Smith void Value::destroyValueName() { 895bf8fef5SDuncan P. N. Exon Smith ValueName *Name = getValueName(); 905bf8fef5SDuncan P. N. Exon Smith if (Name) 915bf8fef5SDuncan P. N. Exon Smith Name->Destroy(); 925bf8fef5SDuncan P. N. Exon Smith setValueName(nullptr); 935bf8fef5SDuncan P. N. Exon Smith } 945bf8fef5SDuncan P. N. Exon Smith 95ef860a24SChandler Carruth bool Value::hasNUses(unsigned N) const { 96ef860a24SChandler Carruth const_use_iterator UI = use_begin(), E = use_end(); 97ef860a24SChandler Carruth 98ef860a24SChandler Carruth for (; N; --N, ++UI) 99ef860a24SChandler Carruth if (UI == E) return false; // Too few. 100ef860a24SChandler Carruth return UI == E; 101ef860a24SChandler Carruth } 102ef860a24SChandler Carruth 103ef860a24SChandler Carruth bool Value::hasNUsesOrMore(unsigned N) const { 104ef860a24SChandler Carruth const_use_iterator UI = use_begin(), E = use_end(); 105ef860a24SChandler Carruth 106ef860a24SChandler Carruth for (; N; --N, ++UI) 107ef860a24SChandler Carruth if (UI == E) return false; // Too few. 108ef860a24SChandler Carruth 109ef860a24SChandler Carruth return true; 110ef860a24SChandler Carruth } 111ef860a24SChandler Carruth 112ef860a24SChandler Carruth bool Value::isUsedInBasicBlock(const BasicBlock *BB) const { 1130925b24dSJakob Stoklund Olesen // This can be computed either by scanning the instructions in BB, or by 1140925b24dSJakob Stoklund Olesen // scanning the use list of this Value. Both lists can be very long, but 1150925b24dSJakob Stoklund Olesen // usually one is quite short. 1160925b24dSJakob Stoklund Olesen // 1170925b24dSJakob Stoklund Olesen // Scan both lists simultaneously until one is exhausted. This limits the 1180925b24dSJakob Stoklund Olesen // search to the shorter list. 1190925b24dSJakob Stoklund Olesen BasicBlock::const_iterator BI = BB->begin(), BE = BB->end(); 120cdf47884SChandler Carruth const_user_iterator UI = user_begin(), UE = user_end(); 1210925b24dSJakob Stoklund Olesen for (; BI != BE && UI != UE; ++BI, ++UI) { 1220925b24dSJakob Stoklund Olesen // Scan basic block: Check if this Value is used by the instruction at BI. 1230925b24dSJakob Stoklund Olesen if (std::find(BI->op_begin(), BI->op_end(), this) != BI->op_end()) 124ef860a24SChandler Carruth return true; 1250925b24dSJakob Stoklund Olesen // Scan use list: Check if the use at UI is in BB. 1260925b24dSJakob Stoklund Olesen const Instruction *User = dyn_cast<Instruction>(*UI); 127ef860a24SChandler Carruth if (User && User->getParent() == BB) 128ef860a24SChandler Carruth return true; 129ef860a24SChandler Carruth } 130ef860a24SChandler Carruth return false; 131ef860a24SChandler Carruth } 132ef860a24SChandler Carruth 133ef860a24SChandler Carruth unsigned Value::getNumUses() const { 134ef860a24SChandler Carruth return (unsigned)std::distance(use_begin(), use_end()); 135ef860a24SChandler Carruth } 136ef860a24SChandler Carruth 137ef860a24SChandler Carruth static bool getSymTab(Value *V, ValueSymbolTable *&ST) { 138c620761cSCraig Topper ST = nullptr; 139ef860a24SChandler Carruth if (Instruction *I = dyn_cast<Instruction>(V)) { 140ef860a24SChandler Carruth if (BasicBlock *P = I->getParent()) 141ef860a24SChandler Carruth if (Function *PP = P->getParent()) 142ef860a24SChandler Carruth ST = &PP->getValueSymbolTable(); 143ef860a24SChandler Carruth } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) { 144ef860a24SChandler Carruth if (Function *P = BB->getParent()) 145ef860a24SChandler Carruth ST = &P->getValueSymbolTable(); 146ef860a24SChandler Carruth } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { 147ef860a24SChandler Carruth if (Module *P = GV->getParent()) 148ef860a24SChandler Carruth ST = &P->getValueSymbolTable(); 149ef860a24SChandler Carruth } else if (Argument *A = dyn_cast<Argument>(V)) { 150ef860a24SChandler Carruth if (Function *P = A->getParent()) 151ef860a24SChandler Carruth ST = &P->getValueSymbolTable(); 1525bf8fef5SDuncan P. N. Exon Smith } else { 153ef860a24SChandler Carruth assert(isa<Constant>(V) && "Unknown value type!"); 154ef860a24SChandler Carruth return true; // no name is setable for this. 155ef860a24SChandler Carruth } 156ef860a24SChandler Carruth return false; 157ef860a24SChandler Carruth } 158ef860a24SChandler Carruth 159ef860a24SChandler Carruth StringRef Value::getName() const { 160ef860a24SChandler Carruth // Make sure the empty string is still a C string. For historical reasons, 161ef860a24SChandler Carruth // some clients want to call .data() on the result and expect it to be null 162ef860a24SChandler Carruth // terminated. 1635bf8fef5SDuncan P. N. Exon Smith if (!getValueName()) 1645bf8fef5SDuncan P. N. Exon Smith return StringRef("", 0); 1655bf8fef5SDuncan P. N. Exon Smith return getValueName()->getKey(); 166ef860a24SChandler Carruth } 167ef860a24SChandler Carruth 168ef860a24SChandler Carruth void Value::setName(const Twine &NewName) { 169ef860a24SChandler Carruth // Fast path for common IRBuilder case of setName("") when there is no name. 170ef860a24SChandler Carruth if (NewName.isTriviallyEmpty() && !hasName()) 171ef860a24SChandler Carruth return; 172ef860a24SChandler Carruth 173ef860a24SChandler Carruth SmallString<256> NameData; 174ef860a24SChandler Carruth StringRef NameRef = NewName.toStringRef(NameData); 17509d689f9SRafael Espindola assert(NameRef.find_first_of(0) == StringRef::npos && 17609d689f9SRafael Espindola "Null bytes are not allowed in names"); 177ef860a24SChandler Carruth 178ef860a24SChandler Carruth // Name isn't changing? 179ef860a24SChandler Carruth if (getName() == NameRef) 180ef860a24SChandler Carruth return; 181ef860a24SChandler Carruth 182ef860a24SChandler Carruth assert(!getType()->isVoidTy() && "Cannot assign a name to void values!"); 183ef860a24SChandler Carruth 184ef860a24SChandler Carruth // Get the symbol table to update for this object. 185ef860a24SChandler Carruth ValueSymbolTable *ST; 186ef860a24SChandler Carruth if (getSymTab(this, ST)) 187ef860a24SChandler Carruth return; // Cannot set a name on this value (e.g. constant). 188ef860a24SChandler Carruth 189516d7039SMichael Ilseman if (Function *F = dyn_cast<Function>(this)) 190516d7039SMichael Ilseman getContext().pImpl->IntrinsicIDCache.erase(F); 191516d7039SMichael Ilseman 192ef860a24SChandler Carruth if (!ST) { // No symbol table to update? Just do the change. 193ef860a24SChandler Carruth if (NameRef.empty()) { 194ef860a24SChandler Carruth // Free the name for this value. 1955bf8fef5SDuncan P. N. Exon Smith destroyValueName(); 196ef860a24SChandler Carruth return; 197ef860a24SChandler Carruth } 198ef860a24SChandler Carruth 199ef860a24SChandler Carruth // NOTE: Could optimize for the case the name is shrinking to not deallocate 200ef860a24SChandler Carruth // then reallocated. 2015bf8fef5SDuncan P. N. Exon Smith destroyValueName(); 202ef860a24SChandler Carruth 203ef860a24SChandler Carruth // Create the new name. 2045bf8fef5SDuncan P. N. Exon Smith setValueName(ValueName::Create(NameRef)); 2055bf8fef5SDuncan P. N. Exon Smith getValueName()->setValue(this); 206ef860a24SChandler Carruth return; 207ef860a24SChandler Carruth } 208ef860a24SChandler Carruth 209ef860a24SChandler Carruth // NOTE: Could optimize for the case the name is shrinking to not deallocate 210ef860a24SChandler Carruth // then reallocated. 211ef860a24SChandler Carruth if (hasName()) { 212ef860a24SChandler Carruth // Remove old name. 2135bf8fef5SDuncan P. N. Exon Smith ST->removeValueName(getValueName()); 2145bf8fef5SDuncan P. N. Exon Smith destroyValueName(); 215ef860a24SChandler Carruth 216ef860a24SChandler Carruth if (NameRef.empty()) 217ef860a24SChandler Carruth return; 218ef860a24SChandler Carruth } 219ef860a24SChandler Carruth 220ef860a24SChandler Carruth // Name is changing to something new. 2215bf8fef5SDuncan P. N. Exon Smith setValueName(ST->createValueName(NameRef, this)); 222ef860a24SChandler Carruth } 223ef860a24SChandler Carruth 224ef860a24SChandler Carruth void Value::takeName(Value *V) { 225c620761cSCraig Topper ValueSymbolTable *ST = nullptr; 226ef860a24SChandler Carruth // If this value has a name, drop it. 227ef860a24SChandler Carruth if (hasName()) { 228ef860a24SChandler Carruth // Get the symtab this is in. 229ef860a24SChandler Carruth if (getSymTab(this, ST)) { 230ef860a24SChandler Carruth // We can't set a name on this value, but we need to clear V's name if 231ef860a24SChandler Carruth // it has one. 232ef860a24SChandler Carruth if (V->hasName()) V->setName(""); 233ef860a24SChandler Carruth return; // Cannot set a name on this value (e.g. constant). 234ef860a24SChandler Carruth } 235ef860a24SChandler Carruth 236ef860a24SChandler Carruth // Remove old name. 237ef860a24SChandler Carruth if (ST) 2385bf8fef5SDuncan P. N. Exon Smith ST->removeValueName(getValueName()); 2395bf8fef5SDuncan P. N. Exon Smith destroyValueName(); 240ef860a24SChandler Carruth } 241ef860a24SChandler Carruth 242ef860a24SChandler Carruth // Now we know that this has no name. 243ef860a24SChandler Carruth 244ef860a24SChandler Carruth // If V has no name either, we're done. 245ef860a24SChandler Carruth if (!V->hasName()) return; 246ef860a24SChandler Carruth 247ef860a24SChandler Carruth // Get this's symtab if we didn't before. 248ef860a24SChandler Carruth if (!ST) { 249ef860a24SChandler Carruth if (getSymTab(this, ST)) { 250ef860a24SChandler Carruth // Clear V's name. 251ef860a24SChandler Carruth V->setName(""); 252ef860a24SChandler Carruth return; // Cannot set a name on this value (e.g. constant). 253ef860a24SChandler Carruth } 254ef860a24SChandler Carruth } 255ef860a24SChandler Carruth 256ef860a24SChandler Carruth // Get V's ST, this should always succed, because V has a name. 257ef860a24SChandler Carruth ValueSymbolTable *VST; 258ef860a24SChandler Carruth bool Failure = getSymTab(V, VST); 259ef860a24SChandler Carruth assert(!Failure && "V has a name, so it should have a ST!"); (void)Failure; 260ef860a24SChandler Carruth 261ef860a24SChandler Carruth // If these values are both in the same symtab, we can do this very fast. 262ef860a24SChandler Carruth // This works even if both values have no symtab yet. 263ef860a24SChandler Carruth if (ST == VST) { 264ef860a24SChandler Carruth // Take the name! 2655bf8fef5SDuncan P. N. Exon Smith setValueName(V->getValueName()); 2665bf8fef5SDuncan P. N. Exon Smith V->setValueName(nullptr); 2675bf8fef5SDuncan P. N. Exon Smith getValueName()->setValue(this); 268ef860a24SChandler Carruth return; 269ef860a24SChandler Carruth } 270ef860a24SChandler Carruth 271ef860a24SChandler Carruth // Otherwise, things are slightly more complex. Remove V's name from VST and 272ef860a24SChandler Carruth // then reinsert it into ST. 273ef860a24SChandler Carruth 274ef860a24SChandler Carruth if (VST) 2755bf8fef5SDuncan P. N. Exon Smith VST->removeValueName(V->getValueName()); 2765bf8fef5SDuncan P. N. Exon Smith setValueName(V->getValueName()); 2775bf8fef5SDuncan P. N. Exon Smith V->setValueName(nullptr); 2785bf8fef5SDuncan P. N. Exon Smith getValueName()->setValue(this); 279ef860a24SChandler Carruth 280ef860a24SChandler Carruth if (ST) 281ef860a24SChandler Carruth ST->reinsertValue(this); 282ef860a24SChandler Carruth } 283ef860a24SChandler Carruth 2847e2b7567SRafael Espindola #ifndef NDEBUG 28571b7b68bSCraig Topper static bool contains(SmallPtrSetImpl<ConstantExpr *> &Cache, ConstantExpr *Expr, 2867e2b7567SRafael Espindola Constant *C) { 28770573dcdSDavid Blaikie if (!Cache.insert(Expr).second) 2887e2b7567SRafael Espindola return false; 2897e2b7567SRafael Espindola 2907e2b7567SRafael Espindola for (auto &O : Expr->operands()) { 2917e2b7567SRafael Espindola if (O == C) 2927e2b7567SRafael Espindola return true; 2937e2b7567SRafael Espindola auto *CE = dyn_cast<ConstantExpr>(O); 2947e2b7567SRafael Espindola if (!CE) 2957e2b7567SRafael Espindola continue; 2967e2b7567SRafael Espindola if (contains(Cache, CE, C)) 2977e2b7567SRafael Espindola return true; 2987e2b7567SRafael Espindola } 2997e2b7567SRafael Espindola return false; 3007e2b7567SRafael Espindola } 3017e2b7567SRafael Espindola 3027e2b7567SRafael Espindola static bool contains(Value *Expr, Value *V) { 3037e2b7567SRafael Espindola if (Expr == V) 3047e2b7567SRafael Espindola return true; 3057e2b7567SRafael Espindola 3067e2b7567SRafael Espindola auto *C = dyn_cast<Constant>(V); 3077e2b7567SRafael Espindola if (!C) 3087e2b7567SRafael Espindola return false; 3097e2b7567SRafael Espindola 3107e2b7567SRafael Espindola auto *CE = dyn_cast<ConstantExpr>(Expr); 3117e2b7567SRafael Espindola if (!CE) 3127e2b7567SRafael Espindola return false; 3137e2b7567SRafael Espindola 3147e2b7567SRafael Espindola SmallPtrSet<ConstantExpr *, 4> Cache; 3157e2b7567SRafael Espindola return contains(Cache, CE, C); 3167e2b7567SRafael Espindola } 3177e2b7567SRafael Espindola #endif 318ef860a24SChandler Carruth 319ef860a24SChandler Carruth void Value::replaceAllUsesWith(Value *New) { 320ef860a24SChandler Carruth assert(New && "Value::replaceAllUsesWith(<null>) is invalid!"); 3217e2b7567SRafael Espindola assert(!contains(New, this) && 3227e2b7567SRafael Espindola "this->replaceAllUsesWith(expr(this)) is NOT valid!"); 323ef860a24SChandler Carruth assert(New->getType() == getType() && 324ef860a24SChandler Carruth "replaceAllUses of value with new value of different type!"); 325ef860a24SChandler Carruth 326ef860a24SChandler Carruth // Notify all ValueHandles (if present) that this value is going away. 327ef860a24SChandler Carruth if (HasValueHandle) 328ef860a24SChandler Carruth ValueHandleBase::ValueIsRAUWd(this, New); 3295bf8fef5SDuncan P. N. Exon Smith if (isUsedByMetadata()) 3305bf8fef5SDuncan P. N. Exon Smith ValueAsMetadata::handleRAUW(this, New); 331ef860a24SChandler Carruth 332ef860a24SChandler Carruth while (!use_empty()) { 333ef860a24SChandler Carruth Use &U = *UseList; 334ef860a24SChandler Carruth // Must handle Constants specially, we cannot call replaceUsesOfWith on a 335ef860a24SChandler Carruth // constant because they are uniqued. 3366b238633SRafael Espindola if (auto *C = dyn_cast<Constant>(U.getUser())) { 337ef860a24SChandler Carruth if (!isa<GlobalValue>(C)) { 338ef860a24SChandler Carruth C->replaceUsesOfWithOnConstant(this, New, &U); 339ef860a24SChandler Carruth continue; 340ef860a24SChandler Carruth } 341ef860a24SChandler Carruth } 342ef860a24SChandler Carruth 343ef860a24SChandler Carruth U.set(New); 344ef860a24SChandler Carruth } 345ef860a24SChandler Carruth 346ef860a24SChandler Carruth if (BasicBlock *BB = dyn_cast<BasicBlock>(this)) 347ef860a24SChandler Carruth BB->replaceSuccessorsPhiUsesWith(cast<BasicBlock>(New)); 348ef860a24SChandler Carruth } 349ef860a24SChandler Carruth 350ec6217c9SGerolf Hoflehner // Like replaceAllUsesWith except it does not handle constants or basic blocks. 351ec6217c9SGerolf Hoflehner // This routine leaves uses within BB. 352ec6217c9SGerolf Hoflehner void Value::replaceUsesOutsideBlock(Value *New, BasicBlock *BB) { 353ec6217c9SGerolf Hoflehner assert(New && "Value::replaceUsesOutsideBlock(<null>, BB) is invalid!"); 354ec6217c9SGerolf Hoflehner assert(!contains(New, this) && 355ec6217c9SGerolf Hoflehner "this->replaceUsesOutsideBlock(expr(this), BB) is NOT valid!"); 356ec6217c9SGerolf Hoflehner assert(New->getType() == getType() && 357ec6217c9SGerolf Hoflehner "replaceUses of value with new value of different type!"); 358ec6217c9SGerolf Hoflehner assert(BB && "Basic block that may contain a use of 'New' must be defined\n"); 359ec6217c9SGerolf Hoflehner 360ec6217c9SGerolf Hoflehner use_iterator UI = use_begin(), E = use_end(); 361ec6217c9SGerolf Hoflehner for (; UI != E;) { 362ec6217c9SGerolf Hoflehner Use &U = *UI; 363ec6217c9SGerolf Hoflehner ++UI; 364ec6217c9SGerolf Hoflehner auto *Usr = dyn_cast<Instruction>(U.getUser()); 365ec6217c9SGerolf Hoflehner if (Usr && Usr->getParent() == BB) 366ec6217c9SGerolf Hoflehner continue; 367ec6217c9SGerolf Hoflehner U.set(New); 368ec6217c9SGerolf Hoflehner } 369ec6217c9SGerolf Hoflehner return; 370ec6217c9SGerolf Hoflehner } 371ec6217c9SGerolf Hoflehner 372ef860a24SChandler Carruth namespace { 373ef860a24SChandler Carruth // Various metrics for how much to strip off of pointers. 374ef860a24SChandler Carruth enum PointerStripKind { 375ef860a24SChandler Carruth PSK_ZeroIndices, 376c229a4ffSRafael Espindola PSK_ZeroIndicesAndAliases, 377ef860a24SChandler Carruth PSK_InBoundsConstantIndices, 378ef860a24SChandler Carruth PSK_InBounds 379ef860a24SChandler Carruth }; 380ef860a24SChandler Carruth 381ef860a24SChandler Carruth template <PointerStripKind StripKind> 382ef860a24SChandler Carruth static Value *stripPointerCastsAndOffsets(Value *V) { 383ef860a24SChandler Carruth if (!V->getType()->isPointerTy()) 384ef860a24SChandler Carruth return V; 385ef860a24SChandler Carruth 386ef860a24SChandler Carruth // Even though we don't look through PHI nodes, we could be called on an 387ef860a24SChandler Carruth // instruction in an unreachable block, which may be on a cycle. 388ef860a24SChandler Carruth SmallPtrSet<Value *, 4> Visited; 389ef860a24SChandler Carruth 390ef860a24SChandler Carruth Visited.insert(V); 391ef860a24SChandler Carruth do { 392ef860a24SChandler Carruth if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { 393ef860a24SChandler Carruth switch (StripKind) { 394c229a4ffSRafael Espindola case PSK_ZeroIndicesAndAliases: 395ef860a24SChandler Carruth case PSK_ZeroIndices: 396ef860a24SChandler Carruth if (!GEP->hasAllZeroIndices()) 397ef860a24SChandler Carruth return V; 398ef860a24SChandler Carruth break; 399ef860a24SChandler Carruth case PSK_InBoundsConstantIndices: 400ef860a24SChandler Carruth if (!GEP->hasAllConstantIndices()) 401ef860a24SChandler Carruth return V; 402ef860a24SChandler Carruth // fallthrough 403ef860a24SChandler Carruth case PSK_InBounds: 404ef860a24SChandler Carruth if (!GEP->isInBounds()) 405ef860a24SChandler Carruth return V; 406ef860a24SChandler Carruth break; 407ef860a24SChandler Carruth } 408ef860a24SChandler Carruth V = GEP->getPointerOperand(); 409b03bd4d9SMatt Arsenault } else if (Operator::getOpcode(V) == Instruction::BitCast || 410b03bd4d9SMatt Arsenault Operator::getOpcode(V) == Instruction::AddrSpaceCast) { 411ef860a24SChandler Carruth V = cast<Operator>(V)->getOperand(0); 412ef860a24SChandler Carruth } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { 413c229a4ffSRafael Espindola if (StripKind == PSK_ZeroIndices || GA->mayBeOverridden()) 414ef860a24SChandler Carruth return V; 415ef860a24SChandler Carruth V = GA->getAliasee(); 416ef860a24SChandler Carruth } else { 417ef860a24SChandler Carruth return V; 418ef860a24SChandler Carruth } 419ef860a24SChandler Carruth assert(V->getType()->isPointerTy() && "Unexpected operand type!"); 42070573dcdSDavid Blaikie } while (Visited.insert(V).second); 421ef860a24SChandler Carruth 422ef860a24SChandler Carruth return V; 423ef860a24SChandler Carruth } 424ef860a24SChandler Carruth } // namespace 425ef860a24SChandler Carruth 426ef860a24SChandler Carruth Value *Value::stripPointerCasts() { 427c229a4ffSRafael Espindola return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndAliases>(this); 428c229a4ffSRafael Espindola } 429c229a4ffSRafael Espindola 430c229a4ffSRafael Espindola Value *Value::stripPointerCastsNoFollowAliases() { 431ef860a24SChandler Carruth return stripPointerCastsAndOffsets<PSK_ZeroIndices>(this); 432ef860a24SChandler Carruth } 433ef860a24SChandler Carruth 434ef860a24SChandler Carruth Value *Value::stripInBoundsConstantOffsets() { 435ef860a24SChandler Carruth return stripPointerCastsAndOffsets<PSK_InBoundsConstantIndices>(this); 436ef860a24SChandler Carruth } 437ef860a24SChandler Carruth 438989e6308SChandler Carruth Value *Value::stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, 439989e6308SChandler Carruth APInt &Offset) { 440989e6308SChandler Carruth if (!getType()->isPointerTy()) 441989e6308SChandler Carruth return this; 442989e6308SChandler Carruth 443989e6308SChandler Carruth assert(Offset.getBitWidth() == DL.getPointerSizeInBits(cast<PointerType>( 444989e6308SChandler Carruth getType())->getAddressSpace()) && 445989e6308SChandler Carruth "The offset must have exactly as many bits as our pointer."); 446989e6308SChandler Carruth 447989e6308SChandler Carruth // Even though we don't look through PHI nodes, we could be called on an 448989e6308SChandler Carruth // instruction in an unreachable block, which may be on a cycle. 449989e6308SChandler Carruth SmallPtrSet<Value *, 4> Visited; 450989e6308SChandler Carruth Visited.insert(this); 451989e6308SChandler Carruth Value *V = this; 452989e6308SChandler Carruth do { 453989e6308SChandler Carruth if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { 454989e6308SChandler Carruth if (!GEP->isInBounds()) 455989e6308SChandler Carruth return V; 456ba689b33SChandler Carruth APInt GEPOffset(Offset); 457ba689b33SChandler Carruth if (!GEP->accumulateConstantOffset(DL, GEPOffset)) 458989e6308SChandler Carruth return V; 459ba689b33SChandler Carruth Offset = GEPOffset; 460989e6308SChandler Carruth V = GEP->getPointerOperand(); 461b8e7c736SHal Finkel } else if (Operator::getOpcode(V) == Instruction::BitCast || 462b8e7c736SHal Finkel Operator::getOpcode(V) == Instruction::AddrSpaceCast) { 463989e6308SChandler Carruth V = cast<Operator>(V)->getOperand(0); 464989e6308SChandler Carruth } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { 465989e6308SChandler Carruth V = GA->getAliasee(); 466989e6308SChandler Carruth } else { 467989e6308SChandler Carruth return V; 468989e6308SChandler Carruth } 469989e6308SChandler Carruth assert(V->getType()->isPointerTy() && "Unexpected operand type!"); 47070573dcdSDavid Blaikie } while (Visited.insert(V).second); 471989e6308SChandler Carruth 472989e6308SChandler Carruth return V; 473989e6308SChandler Carruth } 474989e6308SChandler Carruth 475ef860a24SChandler Carruth Value *Value::stripInBoundsOffsets() { 476ef860a24SChandler Carruth return stripPointerCastsAndOffsets<PSK_InBounds>(this); 477ef860a24SChandler Carruth } 478ef860a24SChandler Carruth 479fcece4d2SDuncan P. N. Exon Smith /// \brief Check if Value is always a dereferenceable pointer. 480fcece4d2SDuncan P. N. Exon Smith /// 481fcece4d2SDuncan P. N. Exon Smith /// Test if V is always a pointer to allocated and suitably aligned memory for 482fcece4d2SDuncan P. N. Exon Smith /// a simple load or store. 483a28d91d8SMehdi Amini static bool isDereferenceablePointer(const Value *V, const DataLayout &DL, 48471b7b68bSCraig Topper SmallPtrSetImpl<const Value *> &Visited) { 485ef860a24SChandler Carruth // Note that it is not safe to speculate into a malloc'd region because 486ef860a24SChandler Carruth // malloc may return null. 487ef860a24SChandler Carruth 488ef860a24SChandler Carruth // These are obviously ok. 489ef860a24SChandler Carruth if (isa<AllocaInst>(V)) return true; 490ef860a24SChandler Carruth 4912e42c34dSHal Finkel // It's not always safe to follow a bitcast, for example: 4922e42c34dSHal Finkel // bitcast i8* (alloca i8) to i32* 4932e42c34dSHal Finkel // would result in a 4-byte load from a 1-byte alloca. However, 4942e42c34dSHal Finkel // if we're casting from a pointer from a type of larger size 4952e42c34dSHal Finkel // to a type of smaller size (or the same size), and the alignment 4962e42c34dSHal Finkel // is at least as large as for the resulting pointer type, then 4972e42c34dSHal Finkel // we can look through the bitcast. 498d2b6fdbcSMichael Kuperstein if (const BitCastOperator *BC = dyn_cast<BitCastOperator>(V)) { 4992e42c34dSHal Finkel Type *STy = BC->getSrcTy()->getPointerElementType(), 5002e42c34dSHal Finkel *DTy = BC->getDestTy()->getPointerElementType(); 50166e23f12SHal Finkel if (STy->isSized() && DTy->isSized() && 502a28d91d8SMehdi Amini (DL.getTypeStoreSize(STy) >= DL.getTypeStoreSize(DTy)) && 503a28d91d8SMehdi Amini (DL.getABITypeAlignment(STy) >= DL.getABITypeAlignment(DTy))) 5042e42c34dSHal Finkel return isDereferenceablePointer(BC->getOperand(0), DL, Visited); 5052e42c34dSHal Finkel } 5062e42c34dSHal Finkel 507ef860a24SChandler Carruth // Global variables which can't collapse to null are ok. 508ef860a24SChandler Carruth if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) 509ef860a24SChandler Carruth return !GV->hasExternalWeakLinkage(); 510ef860a24SChandler Carruth 511b0407ba0SHal Finkel // byval arguments are okay. Arguments specifically marked as 512b0407ba0SHal Finkel // dereferenceable are okay too. 513b0407ba0SHal Finkel if (const Argument *A = dyn_cast<Argument>(V)) { 514b0407ba0SHal Finkel if (A->hasByValAttr()) 515b0407ba0SHal Finkel return true; 516b0407ba0SHal Finkel else if (uint64_t Bytes = A->getDereferenceableBytes()) { 517b0407ba0SHal Finkel Type *Ty = V->getType()->getPointerElementType(); 518a28d91d8SMehdi Amini if (Ty->isSized() && DL.getTypeStoreSize(Ty) <= Bytes) 519b0407ba0SHal Finkel return true; 520b0407ba0SHal Finkel } 521b0407ba0SHal Finkel 522b0407ba0SHal Finkel return false; 523b0407ba0SHal Finkel } 524b0407ba0SHal Finkel 525b0407ba0SHal Finkel // Return values from call sites specifically marked as dereferenceable are 526b0407ba0SHal Finkel // also okay. 527b0407ba0SHal Finkel if (ImmutableCallSite CS = V) { 528b0407ba0SHal Finkel if (uint64_t Bytes = CS.getDereferenceableBytes(0)) { 529b0407ba0SHal Finkel Type *Ty = V->getType()->getPointerElementType(); 530a28d91d8SMehdi Amini if (Ty->isSized() && DL.getTypeStoreSize(Ty) <= Bytes) 531b0407ba0SHal Finkel return true; 532b0407ba0SHal Finkel } 533b0407ba0SHal Finkel } 534ef860a24SChandler Carruth 535ef860a24SChandler Carruth // For GEPs, determine if the indexing lands within the allocated object. 536ef860a24SChandler Carruth if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { 537ef860a24SChandler Carruth // Conservatively require that the base pointer be fully dereferenceable. 53870573dcdSDavid Blaikie if (!Visited.insert(GEP->getOperand(0)).second) 539ef860a24SChandler Carruth return false; 5402e42c34dSHal Finkel if (!isDereferenceablePointer(GEP->getOperand(0), DL, Visited)) 541ef860a24SChandler Carruth return false; 542ef860a24SChandler Carruth // Check the indices. 543ef860a24SChandler Carruth gep_type_iterator GTI = gep_type_begin(GEP); 544ef860a24SChandler Carruth for (User::const_op_iterator I = GEP->op_begin()+1, 545ef860a24SChandler Carruth E = GEP->op_end(); I != E; ++I) { 546ef860a24SChandler Carruth Value *Index = *I; 547ef860a24SChandler Carruth Type *Ty = *GTI++; 548ef860a24SChandler Carruth // Struct indices can't be out of bounds. 549ef860a24SChandler Carruth if (isa<StructType>(Ty)) 550ef860a24SChandler Carruth continue; 551ef860a24SChandler Carruth ConstantInt *CI = dyn_cast<ConstantInt>(Index); 552ef860a24SChandler Carruth if (!CI) 553ef860a24SChandler Carruth return false; 554ef860a24SChandler Carruth // Zero is always ok. 555ef860a24SChandler Carruth if (CI->isZero()) 556ef860a24SChandler Carruth continue; 557ef860a24SChandler Carruth // Check to see that it's within the bounds of an array. 558ef860a24SChandler Carruth ArrayType *ATy = dyn_cast<ArrayType>(Ty); 559ef860a24SChandler Carruth if (!ATy) 560ef860a24SChandler Carruth return false; 561ef860a24SChandler Carruth if (CI->getValue().getActiveBits() > 64) 562ef860a24SChandler Carruth return false; 563ef860a24SChandler Carruth if (CI->getZExtValue() >= ATy->getNumElements()) 564ef860a24SChandler Carruth return false; 565ef860a24SChandler Carruth } 566ef860a24SChandler Carruth // Indices check out; this is dereferenceable. 567ef860a24SChandler Carruth return true; 568ef860a24SChandler Carruth } 569ef860a24SChandler Carruth 570a7343d65SRamkumar Ramachandra // For gc.relocate, look through relocations 571a7343d65SRamkumar Ramachandra if (const IntrinsicInst *I = dyn_cast<IntrinsicInst>(V)) 572a7343d65SRamkumar Ramachandra if (I->getIntrinsicID() == Intrinsic::experimental_gc_relocate) { 573a7343d65SRamkumar Ramachandra GCRelocateOperands RelocateInst(I); 574a7343d65SRamkumar Ramachandra return isDereferenceablePointer(RelocateInst.derivedPtr(), DL, Visited); 575a7343d65SRamkumar Ramachandra } 576a7343d65SRamkumar Ramachandra 577199b39e0SMatt Arsenault if (const AddrSpaceCastInst *ASC = dyn_cast<AddrSpaceCastInst>(V)) 578199b39e0SMatt Arsenault return isDereferenceablePointer(ASC->getOperand(0), DL, Visited); 579199b39e0SMatt Arsenault 580ef860a24SChandler Carruth // If we don't know, assume the worst. 581ef860a24SChandler Carruth return false; 582ef860a24SChandler Carruth } 583ef860a24SChandler Carruth 584a28d91d8SMehdi Amini bool Value::isDereferenceablePointer(const DataLayout &DL) const { 5859e440c08SHal Finkel // When dereferenceability information is provided by a dereferenceable 5869e440c08SHal Finkel // attribute, we know exactly how many bytes are dereferenceable. If we can 5879e440c08SHal Finkel // determine the exact offset to the attributed variable, we can use that 5889e440c08SHal Finkel // information here. 5899e440c08SHal Finkel Type *Ty = getType()->getPointerElementType(); 590a28d91d8SMehdi Amini if (Ty->isSized()) { 591a28d91d8SMehdi Amini APInt Offset(DL.getTypeStoreSizeInBits(getType()), 0); 592a28d91d8SMehdi Amini const Value *BV = stripAndAccumulateInBoundsConstantOffsets(DL, Offset); 5939e440c08SHal Finkel 5949e440c08SHal Finkel APInt DerefBytes(Offset.getBitWidth(), 0); 5959e440c08SHal Finkel if (const Argument *A = dyn_cast<Argument>(BV)) 5969e440c08SHal Finkel DerefBytes = A->getDereferenceableBytes(); 5979e440c08SHal Finkel else if (ImmutableCallSite CS = BV) 5989e440c08SHal Finkel DerefBytes = CS.getDereferenceableBytes(0); 5999e440c08SHal Finkel 6009e440c08SHal Finkel if (DerefBytes.getBoolValue() && Offset.isNonNegative()) { 601a28d91d8SMehdi Amini if (DerefBytes.uge(Offset + DL.getTypeStoreSize(Ty))) 6029e440c08SHal Finkel return true; 6039e440c08SHal Finkel } 6049e440c08SHal Finkel } 6059e440c08SHal Finkel 606ef860a24SChandler Carruth SmallPtrSet<const Value *, 32> Visited; 6072e42c34dSHal Finkel return ::isDereferenceablePointer(this, DL, Visited); 608ef860a24SChandler Carruth } 609ef860a24SChandler Carruth 610ef860a24SChandler Carruth Value *Value::DoPHITranslation(const BasicBlock *CurBB, 611ef860a24SChandler Carruth const BasicBlock *PredBB) { 612ef860a24SChandler Carruth PHINode *PN = dyn_cast<PHINode>(this); 613ef860a24SChandler Carruth if (PN && PN->getParent() == CurBB) 614ef860a24SChandler Carruth return PN->getIncomingValueForBlock(PredBB); 615ef860a24SChandler Carruth return this; 616ef860a24SChandler Carruth } 617ef860a24SChandler Carruth 618ef860a24SChandler Carruth LLVMContext &Value::getContext() const { return VTy->getContext(); } 619ef860a24SChandler Carruth 6203441ffe9SDuncan P. N. Exon Smith void Value::reverseUseList() { 6213441ffe9SDuncan P. N. Exon Smith if (!UseList || !UseList->Next) 6223441ffe9SDuncan P. N. Exon Smith // No need to reverse 0 or 1 uses. 6233441ffe9SDuncan P. N. Exon Smith return; 6243441ffe9SDuncan P. N. Exon Smith 6253441ffe9SDuncan P. N. Exon Smith Use *Head = UseList; 6263441ffe9SDuncan P. N. Exon Smith Use *Current = UseList->Next; 6273441ffe9SDuncan P. N. Exon Smith Head->Next = nullptr; 6283441ffe9SDuncan P. N. Exon Smith while (Current) { 6293441ffe9SDuncan P. N. Exon Smith Use *Next = Current->Next; 6303441ffe9SDuncan P. N. Exon Smith Current->Next = Head; 6313441ffe9SDuncan P. N. Exon Smith Head->setPrev(&Current->Next); 6323441ffe9SDuncan P. N. Exon Smith Head = Current; 6333441ffe9SDuncan P. N. Exon Smith Current = Next; 6343441ffe9SDuncan P. N. Exon Smith } 6353441ffe9SDuncan P. N. Exon Smith UseList = Head; 6363441ffe9SDuncan P. N. Exon Smith Head->setPrev(&UseList); 6373441ffe9SDuncan P. N. Exon Smith } 6383441ffe9SDuncan P. N. Exon Smith 639ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 640ef860a24SChandler Carruth // ValueHandleBase Class 641ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 642ef860a24SChandler Carruth 643ef860a24SChandler Carruth void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) { 644ef860a24SChandler Carruth assert(List && "Handle list is null?"); 645ef860a24SChandler Carruth 646ef860a24SChandler Carruth // Splice ourselves into the list. 647ef860a24SChandler Carruth Next = *List; 648ef860a24SChandler Carruth *List = this; 649ef860a24SChandler Carruth setPrevPtr(List); 650ef860a24SChandler Carruth if (Next) { 651ef860a24SChandler Carruth Next->setPrevPtr(&Next); 652f4ea3d3dSChandler Carruth assert(V == Next->V && "Added to wrong list?"); 653ef860a24SChandler Carruth } 654ef860a24SChandler Carruth } 655ef860a24SChandler Carruth 656ef860a24SChandler Carruth void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) { 657ef860a24SChandler Carruth assert(List && "Must insert after existing node"); 658ef860a24SChandler Carruth 659ef860a24SChandler Carruth Next = List->Next; 660ef860a24SChandler Carruth setPrevPtr(&List->Next); 661ef860a24SChandler Carruth List->Next = this; 662ef860a24SChandler Carruth if (Next) 663ef860a24SChandler Carruth Next->setPrevPtr(&Next); 664ef860a24SChandler Carruth } 665ef860a24SChandler Carruth 666ef860a24SChandler Carruth void ValueHandleBase::AddToUseList() { 667f4ea3d3dSChandler Carruth assert(V && "Null pointer doesn't have a use list!"); 668ef860a24SChandler Carruth 669f4ea3d3dSChandler Carruth LLVMContextImpl *pImpl = V->getContext().pImpl; 670ef860a24SChandler Carruth 671f4ea3d3dSChandler Carruth if (V->HasValueHandle) { 672ef860a24SChandler Carruth // If this value already has a ValueHandle, then it must be in the 673ef860a24SChandler Carruth // ValueHandles map already. 674f4ea3d3dSChandler Carruth ValueHandleBase *&Entry = pImpl->ValueHandles[V]; 6752617dcceSCraig Topper assert(Entry && "Value doesn't have any handles?"); 676ef860a24SChandler Carruth AddToExistingUseList(&Entry); 677ef860a24SChandler Carruth return; 678ef860a24SChandler Carruth } 679ef860a24SChandler Carruth 680ef860a24SChandler Carruth // Ok, it doesn't have any handles yet, so we must insert it into the 681ef860a24SChandler Carruth // DenseMap. However, doing this insertion could cause the DenseMap to 682ef860a24SChandler Carruth // reallocate itself, which would invalidate all of the PrevP pointers that 683ef860a24SChandler Carruth // point into the old table. Handle this by checking for reallocation and 684ef860a24SChandler Carruth // updating the stale pointers only if needed. 685ef860a24SChandler Carruth DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles; 686ef860a24SChandler Carruth const void *OldBucketPtr = Handles.getPointerIntoBucketsArray(); 687ef860a24SChandler Carruth 688f4ea3d3dSChandler Carruth ValueHandleBase *&Entry = Handles[V]; 6892617dcceSCraig Topper assert(!Entry && "Value really did already have handles?"); 690ef860a24SChandler Carruth AddToExistingUseList(&Entry); 691f4ea3d3dSChandler Carruth V->HasValueHandle = true; 692ef860a24SChandler Carruth 693ef860a24SChandler Carruth // If reallocation didn't happen or if this was the first insertion, don't 694ef860a24SChandler Carruth // walk the table. 695ef860a24SChandler Carruth if (Handles.isPointerIntoBucketsArray(OldBucketPtr) || 696ef860a24SChandler Carruth Handles.size() == 1) { 697ef860a24SChandler Carruth return; 698ef860a24SChandler Carruth } 699ef860a24SChandler Carruth 700ef860a24SChandler Carruth // Okay, reallocation did happen. Fix the Prev Pointers. 701ef860a24SChandler Carruth for (DenseMap<Value*, ValueHandleBase*>::iterator I = Handles.begin(), 702ef860a24SChandler Carruth E = Handles.end(); I != E; ++I) { 703f4ea3d3dSChandler Carruth assert(I->second && I->first == I->second->V && 704ef860a24SChandler Carruth "List invariant broken!"); 705ef860a24SChandler Carruth I->second->setPrevPtr(&I->second); 706ef860a24SChandler Carruth } 707ef860a24SChandler Carruth } 708ef860a24SChandler Carruth 709ef860a24SChandler Carruth void ValueHandleBase::RemoveFromUseList() { 710f4ea3d3dSChandler Carruth assert(V && V->HasValueHandle && 711ef860a24SChandler Carruth "Pointer doesn't have a use list!"); 712ef860a24SChandler Carruth 713ef860a24SChandler Carruth // Unlink this from its use list. 714ef860a24SChandler Carruth ValueHandleBase **PrevPtr = getPrevPtr(); 715ef860a24SChandler Carruth assert(*PrevPtr == this && "List invariant broken"); 716ef860a24SChandler Carruth 717ef860a24SChandler Carruth *PrevPtr = Next; 718ef860a24SChandler Carruth if (Next) { 719ef860a24SChandler Carruth assert(Next->getPrevPtr() == &Next && "List invariant broken"); 720ef860a24SChandler Carruth Next->setPrevPtr(PrevPtr); 721ef860a24SChandler Carruth return; 722ef860a24SChandler Carruth } 723ef860a24SChandler Carruth 724ef860a24SChandler Carruth // If the Next pointer was null, then it is possible that this was the last 725ef860a24SChandler Carruth // ValueHandle watching VP. If so, delete its entry from the ValueHandles 726ef860a24SChandler Carruth // map. 727f4ea3d3dSChandler Carruth LLVMContextImpl *pImpl = V->getContext().pImpl; 728ef860a24SChandler Carruth DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles; 729ef860a24SChandler Carruth if (Handles.isPointerIntoBucketsArray(PrevPtr)) { 730f4ea3d3dSChandler Carruth Handles.erase(V); 731f4ea3d3dSChandler Carruth V->HasValueHandle = false; 732ef860a24SChandler Carruth } 733ef860a24SChandler Carruth } 734ef860a24SChandler Carruth 735ef860a24SChandler Carruth 736ef860a24SChandler Carruth void ValueHandleBase::ValueIsDeleted(Value *V) { 737ef860a24SChandler Carruth assert(V->HasValueHandle && "Should only be called if ValueHandles present"); 738ef860a24SChandler Carruth 739ef860a24SChandler Carruth // Get the linked list base, which is guaranteed to exist since the 740ef860a24SChandler Carruth // HasValueHandle flag is set. 741ef860a24SChandler Carruth LLVMContextImpl *pImpl = V->getContext().pImpl; 742ef860a24SChandler Carruth ValueHandleBase *Entry = pImpl->ValueHandles[V]; 743ef860a24SChandler Carruth assert(Entry && "Value bit set but no entries exist"); 744ef860a24SChandler Carruth 745ef860a24SChandler Carruth // We use a local ValueHandleBase as an iterator so that ValueHandles can add 746ef860a24SChandler Carruth // and remove themselves from the list without breaking our iteration. This 747ef860a24SChandler Carruth // is not really an AssertingVH; we just have to give ValueHandleBase a kind. 748ef860a24SChandler Carruth // Note that we deliberately do not the support the case when dropping a value 749ef860a24SChandler Carruth // handle results in a new value handle being permanently added to the list 750ef860a24SChandler Carruth // (as might occur in theory for CallbackVH's): the new value handle will not 751ef860a24SChandler Carruth // be processed and the checking code will mete out righteous punishment if 752ef860a24SChandler Carruth // the handle is still present once we have finished processing all the other 753ef860a24SChandler Carruth // value handles (it is fine to momentarily add then remove a value handle). 754ef860a24SChandler Carruth for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) { 755ef860a24SChandler Carruth Iterator.RemoveFromUseList(); 756ef860a24SChandler Carruth Iterator.AddToExistingUseListAfter(Entry); 757ef860a24SChandler Carruth assert(Entry->Next == &Iterator && "Loop invariant broken."); 758ef860a24SChandler Carruth 759ef860a24SChandler Carruth switch (Entry->getKind()) { 760ef860a24SChandler Carruth case Assert: 761ef860a24SChandler Carruth break; 762ef860a24SChandler Carruth case Tracking: 763ef860a24SChandler Carruth // Mark that this value has been deleted by setting it to an invalid Value 764ef860a24SChandler Carruth // pointer. 765ef860a24SChandler Carruth Entry->operator=(DenseMapInfo<Value *>::getTombstoneKey()); 766ef860a24SChandler Carruth break; 767ef860a24SChandler Carruth case Weak: 768ef860a24SChandler Carruth // Weak just goes to null, which will unlink it from the list. 769c620761cSCraig Topper Entry->operator=(nullptr); 770ef860a24SChandler Carruth break; 771ef860a24SChandler Carruth case Callback: 772ef860a24SChandler Carruth // Forward to the subclass's implementation. 773ef860a24SChandler Carruth static_cast<CallbackVH*>(Entry)->deleted(); 774ef860a24SChandler Carruth break; 775ef860a24SChandler Carruth } 776ef860a24SChandler Carruth } 777ef860a24SChandler Carruth 778ef860a24SChandler Carruth // All callbacks, weak references, and assertingVHs should be dropped by now. 779ef860a24SChandler Carruth if (V->HasValueHandle) { 780ef860a24SChandler Carruth #ifndef NDEBUG // Only in +Asserts mode... 781ef860a24SChandler Carruth dbgs() << "While deleting: " << *V->getType() << " %" << V->getName() 782ef860a24SChandler Carruth << "\n"; 783ef860a24SChandler Carruth if (pImpl->ValueHandles[V]->getKind() == Assert) 784ef860a24SChandler Carruth llvm_unreachable("An asserting value handle still pointed to this" 785ef860a24SChandler Carruth " value!"); 786ef860a24SChandler Carruth 787ef860a24SChandler Carruth #endif 788ef860a24SChandler Carruth llvm_unreachable("All references to V were not removed?"); 789ef860a24SChandler Carruth } 790ef860a24SChandler Carruth } 791ef860a24SChandler Carruth 792ef860a24SChandler Carruth 793ef860a24SChandler Carruth void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) { 794ef860a24SChandler Carruth assert(Old->HasValueHandle &&"Should only be called if ValueHandles present"); 795ef860a24SChandler Carruth assert(Old != New && "Changing value into itself!"); 796c1892e2dSFrederic Riss assert(Old->getType() == New->getType() && 797c1892e2dSFrederic Riss "replaceAllUses of value with new value of different type!"); 798ef860a24SChandler Carruth 799ef860a24SChandler Carruth // Get the linked list base, which is guaranteed to exist since the 800ef860a24SChandler Carruth // HasValueHandle flag is set. 801ef860a24SChandler Carruth LLVMContextImpl *pImpl = Old->getContext().pImpl; 802ef860a24SChandler Carruth ValueHandleBase *Entry = pImpl->ValueHandles[Old]; 803ef860a24SChandler Carruth 804ef860a24SChandler Carruth assert(Entry && "Value bit set but no entries exist"); 805ef860a24SChandler Carruth 806ef860a24SChandler Carruth // We use a local ValueHandleBase as an iterator so that 807ef860a24SChandler Carruth // ValueHandles can add and remove themselves from the list without 808ef860a24SChandler Carruth // breaking our iteration. This is not really an AssertingVH; we 809ef860a24SChandler Carruth // just have to give ValueHandleBase some kind. 810ef860a24SChandler Carruth for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) { 811ef860a24SChandler Carruth Iterator.RemoveFromUseList(); 812ef860a24SChandler Carruth Iterator.AddToExistingUseListAfter(Entry); 813ef860a24SChandler Carruth assert(Entry->Next == &Iterator && "Loop invariant broken."); 814ef860a24SChandler Carruth 815ef860a24SChandler Carruth switch (Entry->getKind()) { 816ef860a24SChandler Carruth case Assert: 817ef860a24SChandler Carruth // Asserting handle does not follow RAUW implicitly. 818ef860a24SChandler Carruth break; 819ef860a24SChandler Carruth case Tracking: 820ef860a24SChandler Carruth // Tracking goes to new value like a WeakVH. Note that this may make it 821ef860a24SChandler Carruth // something incompatible with its templated type. We don't want to have a 822ef860a24SChandler Carruth // virtual (or inline) interface to handle this though, so instead we make 823ef860a24SChandler Carruth // the TrackingVH accessors guarantee that a client never sees this value. 824ef860a24SChandler Carruth 825ef860a24SChandler Carruth // FALLTHROUGH 826ef860a24SChandler Carruth case Weak: 827ef860a24SChandler Carruth // Weak goes to the new value, which will unlink it from Old's list. 828ef860a24SChandler Carruth Entry->operator=(New); 829ef860a24SChandler Carruth break; 830ef860a24SChandler Carruth case Callback: 831ef860a24SChandler Carruth // Forward to the subclass's implementation. 832ef860a24SChandler Carruth static_cast<CallbackVH*>(Entry)->allUsesReplacedWith(New); 833ef860a24SChandler Carruth break; 834ef860a24SChandler Carruth } 835ef860a24SChandler Carruth } 836ef860a24SChandler Carruth 837ef860a24SChandler Carruth #ifndef NDEBUG 838ef860a24SChandler Carruth // If any new tracking or weak value handles were added while processing the 839ef860a24SChandler Carruth // list, then complain about it now. 840ef860a24SChandler Carruth if (Old->HasValueHandle) 841ef860a24SChandler Carruth for (Entry = pImpl->ValueHandles[Old]; Entry; Entry = Entry->Next) 842ef860a24SChandler Carruth switch (Entry->getKind()) { 843ef860a24SChandler Carruth case Tracking: 844ef860a24SChandler Carruth case Weak: 845ef860a24SChandler Carruth dbgs() << "After RAUW from " << *Old->getType() << " %" 846ef860a24SChandler Carruth << Old->getName() << " to " << *New->getType() << " %" 847ef860a24SChandler Carruth << New->getName() << "\n"; 848ef860a24SChandler Carruth llvm_unreachable("A tracking or weak value handle still pointed to the" 849ef860a24SChandler Carruth " old value!\n"); 850ef860a24SChandler Carruth default: 851ef860a24SChandler Carruth break; 852ef860a24SChandler Carruth } 853ef860a24SChandler Carruth #endif 854ef860a24SChandler Carruth } 855ef860a24SChandler Carruth 856d12ccbd3SJuergen Ributzka // Pin the vtable to this file. 857d12ccbd3SJuergen Ributzka void CallbackVH::anchor() {} 858