1*ef860a24SChandler Carruth //===-- Value.cpp - Implement the Value class -----------------------------===// 2*ef860a24SChandler Carruth // 3*ef860a24SChandler Carruth // The LLVM Compiler Infrastructure 4*ef860a24SChandler Carruth // 5*ef860a24SChandler Carruth // This file is distributed under the University of Illinois Open Source 6*ef860a24SChandler Carruth // License. See LICENSE.TXT for details. 7*ef860a24SChandler Carruth // 8*ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 9*ef860a24SChandler Carruth // 10*ef860a24SChandler Carruth // This file implements the Value, ValueHandle, and User classes. 11*ef860a24SChandler Carruth // 12*ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 13*ef860a24SChandler Carruth 14*ef860a24SChandler Carruth #include "llvm/Value.h" 15*ef860a24SChandler Carruth #include "LLVMContextImpl.h" 16*ef860a24SChandler Carruth #include "llvm/ADT/DenseMap.h" 17*ef860a24SChandler Carruth #include "llvm/ADT/SmallString.h" 18*ef860a24SChandler Carruth #include "llvm/Constant.h" 19*ef860a24SChandler Carruth #include "llvm/Constants.h" 20*ef860a24SChandler Carruth #include "llvm/DerivedTypes.h" 21*ef860a24SChandler Carruth #include "llvm/InstrTypes.h" 22*ef860a24SChandler Carruth #include "llvm/Instructions.h" 23*ef860a24SChandler Carruth #include "llvm/Module.h" 24*ef860a24SChandler Carruth #include "llvm/Operator.h" 25*ef860a24SChandler Carruth #include "llvm/Support/Debug.h" 26*ef860a24SChandler Carruth #include "llvm/Support/ErrorHandling.h" 27*ef860a24SChandler Carruth #include "llvm/Support/GetElementPtrTypeIterator.h" 28*ef860a24SChandler Carruth #include "llvm/Support/LeakDetector.h" 29*ef860a24SChandler Carruth #include "llvm/Support/ManagedStatic.h" 30*ef860a24SChandler Carruth #include "llvm/Support/ValueHandle.h" 31*ef860a24SChandler Carruth #include "llvm/ValueSymbolTable.h" 32*ef860a24SChandler Carruth #include <algorithm> 33*ef860a24SChandler Carruth using namespace llvm; 34*ef860a24SChandler Carruth 35*ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 36*ef860a24SChandler Carruth // Value Class 37*ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 38*ef860a24SChandler Carruth 39*ef860a24SChandler Carruth static inline Type *checkType(Type *Ty) { 40*ef860a24SChandler Carruth assert(Ty && "Value defined with a null type: Error!"); 41*ef860a24SChandler Carruth return const_cast<Type*>(Ty); 42*ef860a24SChandler Carruth } 43*ef860a24SChandler Carruth 44*ef860a24SChandler Carruth Value::Value(Type *ty, unsigned scid) 45*ef860a24SChandler Carruth : SubclassID(scid), HasValueHandle(0), 46*ef860a24SChandler Carruth SubclassOptionalData(0), SubclassData(0), VTy((Type*)checkType(ty)), 47*ef860a24SChandler Carruth UseList(0), Name(0) { 48*ef860a24SChandler Carruth // FIXME: Why isn't this in the subclass gunk?? 49*ef860a24SChandler Carruth // Note, we cannot call isa<CallInst> before the CallInst has been 50*ef860a24SChandler Carruth // constructed. 51*ef860a24SChandler Carruth if (SubclassID == Instruction::Call || SubclassID == Instruction::Invoke) 52*ef860a24SChandler Carruth assert((VTy->isFirstClassType() || VTy->isVoidTy() || VTy->isStructTy()) && 53*ef860a24SChandler Carruth "invalid CallInst type!"); 54*ef860a24SChandler Carruth else if (SubclassID != BasicBlockVal && 55*ef860a24SChandler Carruth (SubclassID < ConstantFirstVal || SubclassID > ConstantLastVal)) 56*ef860a24SChandler Carruth assert((VTy->isFirstClassType() || VTy->isVoidTy()) && 57*ef860a24SChandler Carruth "Cannot create non-first-class values except for constants!"); 58*ef860a24SChandler Carruth } 59*ef860a24SChandler Carruth 60*ef860a24SChandler Carruth Value::~Value() { 61*ef860a24SChandler Carruth // Notify all ValueHandles (if present) that this value is going away. 62*ef860a24SChandler Carruth if (HasValueHandle) 63*ef860a24SChandler Carruth ValueHandleBase::ValueIsDeleted(this); 64*ef860a24SChandler Carruth 65*ef860a24SChandler Carruth #ifndef NDEBUG // Only in -g mode... 66*ef860a24SChandler Carruth // Check to make sure that there are no uses of this value that are still 67*ef860a24SChandler Carruth // around when the value is destroyed. If there are, then we have a dangling 68*ef860a24SChandler Carruth // reference and something is wrong. This code is here to print out what is 69*ef860a24SChandler Carruth // still being referenced. The value in question should be printed as 70*ef860a24SChandler Carruth // a <badref> 71*ef860a24SChandler Carruth // 72*ef860a24SChandler Carruth if (!use_empty()) { 73*ef860a24SChandler Carruth dbgs() << "While deleting: " << *VTy << " %" << getName() << "\n"; 74*ef860a24SChandler Carruth for (use_iterator I = use_begin(), E = use_end(); I != E; ++I) 75*ef860a24SChandler Carruth dbgs() << "Use still stuck around after Def is destroyed:" 76*ef860a24SChandler Carruth << **I << "\n"; 77*ef860a24SChandler Carruth } 78*ef860a24SChandler Carruth #endif 79*ef860a24SChandler Carruth assert(use_empty() && "Uses remain when a value is destroyed!"); 80*ef860a24SChandler Carruth 81*ef860a24SChandler Carruth // If this value is named, destroy the name. This should not be in a symtab 82*ef860a24SChandler Carruth // at this point. 83*ef860a24SChandler Carruth if (Name && SubclassID != MDStringVal) 84*ef860a24SChandler Carruth Name->Destroy(); 85*ef860a24SChandler Carruth 86*ef860a24SChandler Carruth // There should be no uses of this object anymore, remove it. 87*ef860a24SChandler Carruth LeakDetector::removeGarbageObject(this); 88*ef860a24SChandler Carruth } 89*ef860a24SChandler Carruth 90*ef860a24SChandler Carruth /// hasNUses - Return true if this Value has exactly N users. 91*ef860a24SChandler Carruth /// 92*ef860a24SChandler Carruth bool Value::hasNUses(unsigned N) const { 93*ef860a24SChandler Carruth const_use_iterator UI = use_begin(), E = use_end(); 94*ef860a24SChandler Carruth 95*ef860a24SChandler Carruth for (; N; --N, ++UI) 96*ef860a24SChandler Carruth if (UI == E) return false; // Too few. 97*ef860a24SChandler Carruth return UI == E; 98*ef860a24SChandler Carruth } 99*ef860a24SChandler Carruth 100*ef860a24SChandler Carruth /// hasNUsesOrMore - Return true if this value has N users or more. This is 101*ef860a24SChandler Carruth /// logically equivalent to getNumUses() >= N. 102*ef860a24SChandler Carruth /// 103*ef860a24SChandler Carruth bool Value::hasNUsesOrMore(unsigned N) const { 104*ef860a24SChandler Carruth const_use_iterator UI = use_begin(), E = use_end(); 105*ef860a24SChandler Carruth 106*ef860a24SChandler Carruth for (; N; --N, ++UI) 107*ef860a24SChandler Carruth if (UI == E) return false; // Too few. 108*ef860a24SChandler Carruth 109*ef860a24SChandler Carruth return true; 110*ef860a24SChandler Carruth } 111*ef860a24SChandler Carruth 112*ef860a24SChandler Carruth /// isUsedInBasicBlock - Return true if this value is used in the specified 113*ef860a24SChandler Carruth /// basic block. 114*ef860a24SChandler Carruth bool Value::isUsedInBasicBlock(const BasicBlock *BB) const { 115*ef860a24SChandler Carruth // Start by scanning over the instructions looking for a use before we start 116*ef860a24SChandler Carruth // the expensive use iteration. 117*ef860a24SChandler Carruth unsigned MaxBlockSize = 3; 118*ef860a24SChandler Carruth for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I != E; ++I) { 119*ef860a24SChandler Carruth if (std::find(I->op_begin(), I->op_end(), this) != I->op_end()) 120*ef860a24SChandler Carruth return true; 121*ef860a24SChandler Carruth if (MaxBlockSize-- == 0) // If the block is larger fall back to use_iterator 122*ef860a24SChandler Carruth break; 123*ef860a24SChandler Carruth } 124*ef860a24SChandler Carruth 125*ef860a24SChandler Carruth if (MaxBlockSize != 0) // We scanned the entire block and found no use. 126*ef860a24SChandler Carruth return false; 127*ef860a24SChandler Carruth 128*ef860a24SChandler Carruth for (const_use_iterator I = use_begin(), E = use_end(); I != E; ++I) { 129*ef860a24SChandler Carruth const Instruction *User = dyn_cast<Instruction>(*I); 130*ef860a24SChandler Carruth if (User && User->getParent() == BB) 131*ef860a24SChandler Carruth return true; 132*ef860a24SChandler Carruth } 133*ef860a24SChandler Carruth return false; 134*ef860a24SChandler Carruth } 135*ef860a24SChandler Carruth 136*ef860a24SChandler Carruth 137*ef860a24SChandler Carruth /// getNumUses - This method computes the number of uses of this Value. This 138*ef860a24SChandler Carruth /// is a linear time operation. Use hasOneUse or hasNUses to check for specific 139*ef860a24SChandler Carruth /// values. 140*ef860a24SChandler Carruth unsigned Value::getNumUses() const { 141*ef860a24SChandler Carruth return (unsigned)std::distance(use_begin(), use_end()); 142*ef860a24SChandler Carruth } 143*ef860a24SChandler Carruth 144*ef860a24SChandler Carruth static bool getSymTab(Value *V, ValueSymbolTable *&ST) { 145*ef860a24SChandler Carruth ST = 0; 146*ef860a24SChandler Carruth if (Instruction *I = dyn_cast<Instruction>(V)) { 147*ef860a24SChandler Carruth if (BasicBlock *P = I->getParent()) 148*ef860a24SChandler Carruth if (Function *PP = P->getParent()) 149*ef860a24SChandler Carruth ST = &PP->getValueSymbolTable(); 150*ef860a24SChandler Carruth } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) { 151*ef860a24SChandler Carruth if (Function *P = BB->getParent()) 152*ef860a24SChandler Carruth ST = &P->getValueSymbolTable(); 153*ef860a24SChandler Carruth } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { 154*ef860a24SChandler Carruth if (Module *P = GV->getParent()) 155*ef860a24SChandler Carruth ST = &P->getValueSymbolTable(); 156*ef860a24SChandler Carruth } else if (Argument *A = dyn_cast<Argument>(V)) { 157*ef860a24SChandler Carruth if (Function *P = A->getParent()) 158*ef860a24SChandler Carruth ST = &P->getValueSymbolTable(); 159*ef860a24SChandler Carruth } else if (isa<MDString>(V)) 160*ef860a24SChandler Carruth return true; 161*ef860a24SChandler Carruth else { 162*ef860a24SChandler Carruth assert(isa<Constant>(V) && "Unknown value type!"); 163*ef860a24SChandler Carruth return true; // no name is setable for this. 164*ef860a24SChandler Carruth } 165*ef860a24SChandler Carruth return false; 166*ef860a24SChandler Carruth } 167*ef860a24SChandler Carruth 168*ef860a24SChandler Carruth StringRef Value::getName() const { 169*ef860a24SChandler Carruth // Make sure the empty string is still a C string. For historical reasons, 170*ef860a24SChandler Carruth // some clients want to call .data() on the result and expect it to be null 171*ef860a24SChandler Carruth // terminated. 172*ef860a24SChandler Carruth if (!Name) return StringRef("", 0); 173*ef860a24SChandler Carruth return Name->getKey(); 174*ef860a24SChandler Carruth } 175*ef860a24SChandler Carruth 176*ef860a24SChandler Carruth void Value::setName(const Twine &NewName) { 177*ef860a24SChandler Carruth assert(SubclassID != MDStringVal && 178*ef860a24SChandler Carruth "Cannot set the name of MDString with this method!"); 179*ef860a24SChandler Carruth 180*ef860a24SChandler Carruth // Fast path for common IRBuilder case of setName("") when there is no name. 181*ef860a24SChandler Carruth if (NewName.isTriviallyEmpty() && !hasName()) 182*ef860a24SChandler Carruth return; 183*ef860a24SChandler Carruth 184*ef860a24SChandler Carruth SmallString<256> NameData; 185*ef860a24SChandler Carruth StringRef NameRef = NewName.toStringRef(NameData); 186*ef860a24SChandler Carruth 187*ef860a24SChandler Carruth // Name isn't changing? 188*ef860a24SChandler Carruth if (getName() == NameRef) 189*ef860a24SChandler Carruth return; 190*ef860a24SChandler Carruth 191*ef860a24SChandler Carruth assert(!getType()->isVoidTy() && "Cannot assign a name to void values!"); 192*ef860a24SChandler Carruth 193*ef860a24SChandler Carruth // Get the symbol table to update for this object. 194*ef860a24SChandler Carruth ValueSymbolTable *ST; 195*ef860a24SChandler Carruth if (getSymTab(this, ST)) 196*ef860a24SChandler Carruth return; // Cannot set a name on this value (e.g. constant). 197*ef860a24SChandler Carruth 198*ef860a24SChandler Carruth if (!ST) { // No symbol table to update? Just do the change. 199*ef860a24SChandler Carruth if (NameRef.empty()) { 200*ef860a24SChandler Carruth // Free the name for this value. 201*ef860a24SChandler Carruth Name->Destroy(); 202*ef860a24SChandler Carruth Name = 0; 203*ef860a24SChandler Carruth return; 204*ef860a24SChandler Carruth } 205*ef860a24SChandler Carruth 206*ef860a24SChandler Carruth if (Name) 207*ef860a24SChandler Carruth Name->Destroy(); 208*ef860a24SChandler Carruth 209*ef860a24SChandler Carruth // NOTE: Could optimize for the case the name is shrinking to not deallocate 210*ef860a24SChandler Carruth // then reallocated. 211*ef860a24SChandler Carruth 212*ef860a24SChandler Carruth // Create the new name. 213*ef860a24SChandler Carruth Name = ValueName::Create(NameRef.begin(), NameRef.end()); 214*ef860a24SChandler Carruth Name->setValue(this); 215*ef860a24SChandler Carruth return; 216*ef860a24SChandler Carruth } 217*ef860a24SChandler Carruth 218*ef860a24SChandler Carruth // NOTE: Could optimize for the case the name is shrinking to not deallocate 219*ef860a24SChandler Carruth // then reallocated. 220*ef860a24SChandler Carruth if (hasName()) { 221*ef860a24SChandler Carruth // Remove old name. 222*ef860a24SChandler Carruth ST->removeValueName(Name); 223*ef860a24SChandler Carruth Name->Destroy(); 224*ef860a24SChandler Carruth Name = 0; 225*ef860a24SChandler Carruth 226*ef860a24SChandler Carruth if (NameRef.empty()) 227*ef860a24SChandler Carruth return; 228*ef860a24SChandler Carruth } 229*ef860a24SChandler Carruth 230*ef860a24SChandler Carruth // Name is changing to something new. 231*ef860a24SChandler Carruth Name = ST->createValueName(NameRef, this); 232*ef860a24SChandler Carruth } 233*ef860a24SChandler Carruth 234*ef860a24SChandler Carruth 235*ef860a24SChandler Carruth /// takeName - transfer the name from V to this value, setting V's name to 236*ef860a24SChandler Carruth /// empty. It is an error to call V->takeName(V). 237*ef860a24SChandler Carruth void Value::takeName(Value *V) { 238*ef860a24SChandler Carruth assert(SubclassID != MDStringVal && "Cannot take the name of an MDString!"); 239*ef860a24SChandler Carruth 240*ef860a24SChandler Carruth ValueSymbolTable *ST = 0; 241*ef860a24SChandler Carruth // If this value has a name, drop it. 242*ef860a24SChandler Carruth if (hasName()) { 243*ef860a24SChandler Carruth // Get the symtab this is in. 244*ef860a24SChandler Carruth if (getSymTab(this, ST)) { 245*ef860a24SChandler Carruth // We can't set a name on this value, but we need to clear V's name if 246*ef860a24SChandler Carruth // it has one. 247*ef860a24SChandler Carruth if (V->hasName()) V->setName(""); 248*ef860a24SChandler Carruth return; // Cannot set a name on this value (e.g. constant). 249*ef860a24SChandler Carruth } 250*ef860a24SChandler Carruth 251*ef860a24SChandler Carruth // Remove old name. 252*ef860a24SChandler Carruth if (ST) 253*ef860a24SChandler Carruth ST->removeValueName(Name); 254*ef860a24SChandler Carruth Name->Destroy(); 255*ef860a24SChandler Carruth Name = 0; 256*ef860a24SChandler Carruth } 257*ef860a24SChandler Carruth 258*ef860a24SChandler Carruth // Now we know that this has no name. 259*ef860a24SChandler Carruth 260*ef860a24SChandler Carruth // If V has no name either, we're done. 261*ef860a24SChandler Carruth if (!V->hasName()) return; 262*ef860a24SChandler Carruth 263*ef860a24SChandler Carruth // Get this's symtab if we didn't before. 264*ef860a24SChandler Carruth if (!ST) { 265*ef860a24SChandler Carruth if (getSymTab(this, ST)) { 266*ef860a24SChandler Carruth // Clear V's name. 267*ef860a24SChandler Carruth V->setName(""); 268*ef860a24SChandler Carruth return; // Cannot set a name on this value (e.g. constant). 269*ef860a24SChandler Carruth } 270*ef860a24SChandler Carruth } 271*ef860a24SChandler Carruth 272*ef860a24SChandler Carruth // Get V's ST, this should always succed, because V has a name. 273*ef860a24SChandler Carruth ValueSymbolTable *VST; 274*ef860a24SChandler Carruth bool Failure = getSymTab(V, VST); 275*ef860a24SChandler Carruth assert(!Failure && "V has a name, so it should have a ST!"); (void)Failure; 276*ef860a24SChandler Carruth 277*ef860a24SChandler Carruth // If these values are both in the same symtab, we can do this very fast. 278*ef860a24SChandler Carruth // This works even if both values have no symtab yet. 279*ef860a24SChandler Carruth if (ST == VST) { 280*ef860a24SChandler Carruth // Take the name! 281*ef860a24SChandler Carruth Name = V->Name; 282*ef860a24SChandler Carruth V->Name = 0; 283*ef860a24SChandler Carruth Name->setValue(this); 284*ef860a24SChandler Carruth return; 285*ef860a24SChandler Carruth } 286*ef860a24SChandler Carruth 287*ef860a24SChandler Carruth // Otherwise, things are slightly more complex. Remove V's name from VST and 288*ef860a24SChandler Carruth // then reinsert it into ST. 289*ef860a24SChandler Carruth 290*ef860a24SChandler Carruth if (VST) 291*ef860a24SChandler Carruth VST->removeValueName(V->Name); 292*ef860a24SChandler Carruth Name = V->Name; 293*ef860a24SChandler Carruth V->Name = 0; 294*ef860a24SChandler Carruth Name->setValue(this); 295*ef860a24SChandler Carruth 296*ef860a24SChandler Carruth if (ST) 297*ef860a24SChandler Carruth ST->reinsertValue(this); 298*ef860a24SChandler Carruth } 299*ef860a24SChandler Carruth 300*ef860a24SChandler Carruth 301*ef860a24SChandler Carruth void Value::replaceAllUsesWith(Value *New) { 302*ef860a24SChandler Carruth assert(New && "Value::replaceAllUsesWith(<null>) is invalid!"); 303*ef860a24SChandler Carruth assert(New != this && "this->replaceAllUsesWith(this) is NOT valid!"); 304*ef860a24SChandler Carruth assert(New->getType() == getType() && 305*ef860a24SChandler Carruth "replaceAllUses of value with new value of different type!"); 306*ef860a24SChandler Carruth 307*ef860a24SChandler Carruth // Notify all ValueHandles (if present) that this value is going away. 308*ef860a24SChandler Carruth if (HasValueHandle) 309*ef860a24SChandler Carruth ValueHandleBase::ValueIsRAUWd(this, New); 310*ef860a24SChandler Carruth 311*ef860a24SChandler Carruth while (!use_empty()) { 312*ef860a24SChandler Carruth Use &U = *UseList; 313*ef860a24SChandler Carruth // Must handle Constants specially, we cannot call replaceUsesOfWith on a 314*ef860a24SChandler Carruth // constant because they are uniqued. 315*ef860a24SChandler Carruth if (Constant *C = dyn_cast<Constant>(U.getUser())) { 316*ef860a24SChandler Carruth if (!isa<GlobalValue>(C)) { 317*ef860a24SChandler Carruth C->replaceUsesOfWithOnConstant(this, New, &U); 318*ef860a24SChandler Carruth continue; 319*ef860a24SChandler Carruth } 320*ef860a24SChandler Carruth } 321*ef860a24SChandler Carruth 322*ef860a24SChandler Carruth U.set(New); 323*ef860a24SChandler Carruth } 324*ef860a24SChandler Carruth 325*ef860a24SChandler Carruth if (BasicBlock *BB = dyn_cast<BasicBlock>(this)) 326*ef860a24SChandler Carruth BB->replaceSuccessorsPhiUsesWith(cast<BasicBlock>(New)); 327*ef860a24SChandler Carruth } 328*ef860a24SChandler Carruth 329*ef860a24SChandler Carruth namespace { 330*ef860a24SChandler Carruth // Various metrics for how much to strip off of pointers. 331*ef860a24SChandler Carruth enum PointerStripKind { 332*ef860a24SChandler Carruth PSK_ZeroIndices, 333*ef860a24SChandler Carruth PSK_InBoundsConstantIndices, 334*ef860a24SChandler Carruth PSK_InBounds 335*ef860a24SChandler Carruth }; 336*ef860a24SChandler Carruth 337*ef860a24SChandler Carruth template <PointerStripKind StripKind> 338*ef860a24SChandler Carruth static Value *stripPointerCastsAndOffsets(Value *V) { 339*ef860a24SChandler Carruth if (!V->getType()->isPointerTy()) 340*ef860a24SChandler Carruth return V; 341*ef860a24SChandler Carruth 342*ef860a24SChandler Carruth // Even though we don't look through PHI nodes, we could be called on an 343*ef860a24SChandler Carruth // instruction in an unreachable block, which may be on a cycle. 344*ef860a24SChandler Carruth SmallPtrSet<Value *, 4> Visited; 345*ef860a24SChandler Carruth 346*ef860a24SChandler Carruth Visited.insert(V); 347*ef860a24SChandler Carruth do { 348*ef860a24SChandler Carruth if (GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { 349*ef860a24SChandler Carruth switch (StripKind) { 350*ef860a24SChandler Carruth case PSK_ZeroIndices: 351*ef860a24SChandler Carruth if (!GEP->hasAllZeroIndices()) 352*ef860a24SChandler Carruth return V; 353*ef860a24SChandler Carruth break; 354*ef860a24SChandler Carruth case PSK_InBoundsConstantIndices: 355*ef860a24SChandler Carruth if (!GEP->hasAllConstantIndices()) 356*ef860a24SChandler Carruth return V; 357*ef860a24SChandler Carruth // fallthrough 358*ef860a24SChandler Carruth case PSK_InBounds: 359*ef860a24SChandler Carruth if (!GEP->isInBounds()) 360*ef860a24SChandler Carruth return V; 361*ef860a24SChandler Carruth break; 362*ef860a24SChandler Carruth } 363*ef860a24SChandler Carruth V = GEP->getPointerOperand(); 364*ef860a24SChandler Carruth } else if (Operator::getOpcode(V) == Instruction::BitCast) { 365*ef860a24SChandler Carruth V = cast<Operator>(V)->getOperand(0); 366*ef860a24SChandler Carruth } else if (GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) { 367*ef860a24SChandler Carruth if (GA->mayBeOverridden()) 368*ef860a24SChandler Carruth return V; 369*ef860a24SChandler Carruth V = GA->getAliasee(); 370*ef860a24SChandler Carruth } else { 371*ef860a24SChandler Carruth return V; 372*ef860a24SChandler Carruth } 373*ef860a24SChandler Carruth assert(V->getType()->isPointerTy() && "Unexpected operand type!"); 374*ef860a24SChandler Carruth } while (Visited.insert(V)); 375*ef860a24SChandler Carruth 376*ef860a24SChandler Carruth return V; 377*ef860a24SChandler Carruth } 378*ef860a24SChandler Carruth } // namespace 379*ef860a24SChandler Carruth 380*ef860a24SChandler Carruth Value *Value::stripPointerCasts() { 381*ef860a24SChandler Carruth return stripPointerCastsAndOffsets<PSK_ZeroIndices>(this); 382*ef860a24SChandler Carruth } 383*ef860a24SChandler Carruth 384*ef860a24SChandler Carruth Value *Value::stripInBoundsConstantOffsets() { 385*ef860a24SChandler Carruth return stripPointerCastsAndOffsets<PSK_InBoundsConstantIndices>(this); 386*ef860a24SChandler Carruth } 387*ef860a24SChandler Carruth 388*ef860a24SChandler Carruth Value *Value::stripInBoundsOffsets() { 389*ef860a24SChandler Carruth return stripPointerCastsAndOffsets<PSK_InBounds>(this); 390*ef860a24SChandler Carruth } 391*ef860a24SChandler Carruth 392*ef860a24SChandler Carruth /// isDereferenceablePointer - Test if this value is always a pointer to 393*ef860a24SChandler Carruth /// allocated and suitably aligned memory for a simple load or store. 394*ef860a24SChandler Carruth static bool isDereferenceablePointer(const Value *V, 395*ef860a24SChandler Carruth SmallPtrSet<const Value *, 32> &Visited) { 396*ef860a24SChandler Carruth // Note that it is not safe to speculate into a malloc'd region because 397*ef860a24SChandler Carruth // malloc may return null. 398*ef860a24SChandler Carruth // It's also not always safe to follow a bitcast, for example: 399*ef860a24SChandler Carruth // bitcast i8* (alloca i8) to i32* 400*ef860a24SChandler Carruth // would result in a 4-byte load from a 1-byte alloca. Some cases could 401*ef860a24SChandler Carruth // be handled using DataLayout to check sizes and alignments though. 402*ef860a24SChandler Carruth 403*ef860a24SChandler Carruth // These are obviously ok. 404*ef860a24SChandler Carruth if (isa<AllocaInst>(V)) return true; 405*ef860a24SChandler Carruth 406*ef860a24SChandler Carruth // Global variables which can't collapse to null are ok. 407*ef860a24SChandler Carruth if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) 408*ef860a24SChandler Carruth return !GV->hasExternalWeakLinkage(); 409*ef860a24SChandler Carruth 410*ef860a24SChandler Carruth // byval arguments are ok. 411*ef860a24SChandler Carruth if (const Argument *A = dyn_cast<Argument>(V)) 412*ef860a24SChandler Carruth return A->hasByValAttr(); 413*ef860a24SChandler Carruth 414*ef860a24SChandler Carruth // For GEPs, determine if the indexing lands within the allocated object. 415*ef860a24SChandler Carruth if (const GEPOperator *GEP = dyn_cast<GEPOperator>(V)) { 416*ef860a24SChandler Carruth // Conservatively require that the base pointer be fully dereferenceable. 417*ef860a24SChandler Carruth if (!Visited.insert(GEP->getOperand(0))) 418*ef860a24SChandler Carruth return false; 419*ef860a24SChandler Carruth if (!isDereferenceablePointer(GEP->getOperand(0), Visited)) 420*ef860a24SChandler Carruth return false; 421*ef860a24SChandler Carruth // Check the indices. 422*ef860a24SChandler Carruth gep_type_iterator GTI = gep_type_begin(GEP); 423*ef860a24SChandler Carruth for (User::const_op_iterator I = GEP->op_begin()+1, 424*ef860a24SChandler Carruth E = GEP->op_end(); I != E; ++I) { 425*ef860a24SChandler Carruth Value *Index = *I; 426*ef860a24SChandler Carruth Type *Ty = *GTI++; 427*ef860a24SChandler Carruth // Struct indices can't be out of bounds. 428*ef860a24SChandler Carruth if (isa<StructType>(Ty)) 429*ef860a24SChandler Carruth continue; 430*ef860a24SChandler Carruth ConstantInt *CI = dyn_cast<ConstantInt>(Index); 431*ef860a24SChandler Carruth if (!CI) 432*ef860a24SChandler Carruth return false; 433*ef860a24SChandler Carruth // Zero is always ok. 434*ef860a24SChandler Carruth if (CI->isZero()) 435*ef860a24SChandler Carruth continue; 436*ef860a24SChandler Carruth // Check to see that it's within the bounds of an array. 437*ef860a24SChandler Carruth ArrayType *ATy = dyn_cast<ArrayType>(Ty); 438*ef860a24SChandler Carruth if (!ATy) 439*ef860a24SChandler Carruth return false; 440*ef860a24SChandler Carruth if (CI->getValue().getActiveBits() > 64) 441*ef860a24SChandler Carruth return false; 442*ef860a24SChandler Carruth if (CI->getZExtValue() >= ATy->getNumElements()) 443*ef860a24SChandler Carruth return false; 444*ef860a24SChandler Carruth } 445*ef860a24SChandler Carruth // Indices check out; this is dereferenceable. 446*ef860a24SChandler Carruth return true; 447*ef860a24SChandler Carruth } 448*ef860a24SChandler Carruth 449*ef860a24SChandler Carruth // If we don't know, assume the worst. 450*ef860a24SChandler Carruth return false; 451*ef860a24SChandler Carruth } 452*ef860a24SChandler Carruth 453*ef860a24SChandler Carruth /// isDereferenceablePointer - Test if this value is always a pointer to 454*ef860a24SChandler Carruth /// allocated and suitably aligned memory for a simple load or store. 455*ef860a24SChandler Carruth bool Value::isDereferenceablePointer() const { 456*ef860a24SChandler Carruth SmallPtrSet<const Value *, 32> Visited; 457*ef860a24SChandler Carruth return ::isDereferenceablePointer(this, Visited); 458*ef860a24SChandler Carruth } 459*ef860a24SChandler Carruth 460*ef860a24SChandler Carruth /// DoPHITranslation - If this value is a PHI node with CurBB as its parent, 461*ef860a24SChandler Carruth /// return the value in the PHI node corresponding to PredBB. If not, return 462*ef860a24SChandler Carruth /// ourself. This is useful if you want to know the value something has in a 463*ef860a24SChandler Carruth /// predecessor block. 464*ef860a24SChandler Carruth Value *Value::DoPHITranslation(const BasicBlock *CurBB, 465*ef860a24SChandler Carruth const BasicBlock *PredBB) { 466*ef860a24SChandler Carruth PHINode *PN = dyn_cast<PHINode>(this); 467*ef860a24SChandler Carruth if (PN && PN->getParent() == CurBB) 468*ef860a24SChandler Carruth return PN->getIncomingValueForBlock(PredBB); 469*ef860a24SChandler Carruth return this; 470*ef860a24SChandler Carruth } 471*ef860a24SChandler Carruth 472*ef860a24SChandler Carruth LLVMContext &Value::getContext() const { return VTy->getContext(); } 473*ef860a24SChandler Carruth 474*ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 475*ef860a24SChandler Carruth // ValueHandleBase Class 476*ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 477*ef860a24SChandler Carruth 478*ef860a24SChandler Carruth /// AddToExistingUseList - Add this ValueHandle to the use list for VP, where 479*ef860a24SChandler Carruth /// List is known to point into the existing use list. 480*ef860a24SChandler Carruth void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) { 481*ef860a24SChandler Carruth assert(List && "Handle list is null?"); 482*ef860a24SChandler Carruth 483*ef860a24SChandler Carruth // Splice ourselves into the list. 484*ef860a24SChandler Carruth Next = *List; 485*ef860a24SChandler Carruth *List = this; 486*ef860a24SChandler Carruth setPrevPtr(List); 487*ef860a24SChandler Carruth if (Next) { 488*ef860a24SChandler Carruth Next->setPrevPtr(&Next); 489*ef860a24SChandler Carruth assert(VP.getPointer() == Next->VP.getPointer() && "Added to wrong list?"); 490*ef860a24SChandler Carruth } 491*ef860a24SChandler Carruth } 492*ef860a24SChandler Carruth 493*ef860a24SChandler Carruth void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) { 494*ef860a24SChandler Carruth assert(List && "Must insert after existing node"); 495*ef860a24SChandler Carruth 496*ef860a24SChandler Carruth Next = List->Next; 497*ef860a24SChandler Carruth setPrevPtr(&List->Next); 498*ef860a24SChandler Carruth List->Next = this; 499*ef860a24SChandler Carruth if (Next) 500*ef860a24SChandler Carruth Next->setPrevPtr(&Next); 501*ef860a24SChandler Carruth } 502*ef860a24SChandler Carruth 503*ef860a24SChandler Carruth /// AddToUseList - Add this ValueHandle to the use list for VP. 504*ef860a24SChandler Carruth void ValueHandleBase::AddToUseList() { 505*ef860a24SChandler Carruth assert(VP.getPointer() && "Null pointer doesn't have a use list!"); 506*ef860a24SChandler Carruth 507*ef860a24SChandler Carruth LLVMContextImpl *pImpl = VP.getPointer()->getContext().pImpl; 508*ef860a24SChandler Carruth 509*ef860a24SChandler Carruth if (VP.getPointer()->HasValueHandle) { 510*ef860a24SChandler Carruth // If this value already has a ValueHandle, then it must be in the 511*ef860a24SChandler Carruth // ValueHandles map already. 512*ef860a24SChandler Carruth ValueHandleBase *&Entry = pImpl->ValueHandles[VP.getPointer()]; 513*ef860a24SChandler Carruth assert(Entry != 0 && "Value doesn't have any handles?"); 514*ef860a24SChandler Carruth AddToExistingUseList(&Entry); 515*ef860a24SChandler Carruth return; 516*ef860a24SChandler Carruth } 517*ef860a24SChandler Carruth 518*ef860a24SChandler Carruth // Ok, it doesn't have any handles yet, so we must insert it into the 519*ef860a24SChandler Carruth // DenseMap. However, doing this insertion could cause the DenseMap to 520*ef860a24SChandler Carruth // reallocate itself, which would invalidate all of the PrevP pointers that 521*ef860a24SChandler Carruth // point into the old table. Handle this by checking for reallocation and 522*ef860a24SChandler Carruth // updating the stale pointers only if needed. 523*ef860a24SChandler Carruth DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles; 524*ef860a24SChandler Carruth const void *OldBucketPtr = Handles.getPointerIntoBucketsArray(); 525*ef860a24SChandler Carruth 526*ef860a24SChandler Carruth ValueHandleBase *&Entry = Handles[VP.getPointer()]; 527*ef860a24SChandler Carruth assert(Entry == 0 && "Value really did already have handles?"); 528*ef860a24SChandler Carruth AddToExistingUseList(&Entry); 529*ef860a24SChandler Carruth VP.getPointer()->HasValueHandle = true; 530*ef860a24SChandler Carruth 531*ef860a24SChandler Carruth // If reallocation didn't happen or if this was the first insertion, don't 532*ef860a24SChandler Carruth // walk the table. 533*ef860a24SChandler Carruth if (Handles.isPointerIntoBucketsArray(OldBucketPtr) || 534*ef860a24SChandler Carruth Handles.size() == 1) { 535*ef860a24SChandler Carruth return; 536*ef860a24SChandler Carruth } 537*ef860a24SChandler Carruth 538*ef860a24SChandler Carruth // Okay, reallocation did happen. Fix the Prev Pointers. 539*ef860a24SChandler Carruth for (DenseMap<Value*, ValueHandleBase*>::iterator I = Handles.begin(), 540*ef860a24SChandler Carruth E = Handles.end(); I != E; ++I) { 541*ef860a24SChandler Carruth assert(I->second && I->first == I->second->VP.getPointer() && 542*ef860a24SChandler Carruth "List invariant broken!"); 543*ef860a24SChandler Carruth I->second->setPrevPtr(&I->second); 544*ef860a24SChandler Carruth } 545*ef860a24SChandler Carruth } 546*ef860a24SChandler Carruth 547*ef860a24SChandler Carruth /// RemoveFromUseList - Remove this ValueHandle from its current use list. 548*ef860a24SChandler Carruth void ValueHandleBase::RemoveFromUseList() { 549*ef860a24SChandler Carruth assert(VP.getPointer() && VP.getPointer()->HasValueHandle && 550*ef860a24SChandler Carruth "Pointer doesn't have a use list!"); 551*ef860a24SChandler Carruth 552*ef860a24SChandler Carruth // Unlink this from its use list. 553*ef860a24SChandler Carruth ValueHandleBase **PrevPtr = getPrevPtr(); 554*ef860a24SChandler Carruth assert(*PrevPtr == this && "List invariant broken"); 555*ef860a24SChandler Carruth 556*ef860a24SChandler Carruth *PrevPtr = Next; 557*ef860a24SChandler Carruth if (Next) { 558*ef860a24SChandler Carruth assert(Next->getPrevPtr() == &Next && "List invariant broken"); 559*ef860a24SChandler Carruth Next->setPrevPtr(PrevPtr); 560*ef860a24SChandler Carruth return; 561*ef860a24SChandler Carruth } 562*ef860a24SChandler Carruth 563*ef860a24SChandler Carruth // If the Next pointer was null, then it is possible that this was the last 564*ef860a24SChandler Carruth // ValueHandle watching VP. If so, delete its entry from the ValueHandles 565*ef860a24SChandler Carruth // map. 566*ef860a24SChandler Carruth LLVMContextImpl *pImpl = VP.getPointer()->getContext().pImpl; 567*ef860a24SChandler Carruth DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles; 568*ef860a24SChandler Carruth if (Handles.isPointerIntoBucketsArray(PrevPtr)) { 569*ef860a24SChandler Carruth Handles.erase(VP.getPointer()); 570*ef860a24SChandler Carruth VP.getPointer()->HasValueHandle = false; 571*ef860a24SChandler Carruth } 572*ef860a24SChandler Carruth } 573*ef860a24SChandler Carruth 574*ef860a24SChandler Carruth 575*ef860a24SChandler Carruth void ValueHandleBase::ValueIsDeleted(Value *V) { 576*ef860a24SChandler Carruth assert(V->HasValueHandle && "Should only be called if ValueHandles present"); 577*ef860a24SChandler Carruth 578*ef860a24SChandler Carruth // Get the linked list base, which is guaranteed to exist since the 579*ef860a24SChandler Carruth // HasValueHandle flag is set. 580*ef860a24SChandler Carruth LLVMContextImpl *pImpl = V->getContext().pImpl; 581*ef860a24SChandler Carruth ValueHandleBase *Entry = pImpl->ValueHandles[V]; 582*ef860a24SChandler Carruth assert(Entry && "Value bit set but no entries exist"); 583*ef860a24SChandler Carruth 584*ef860a24SChandler Carruth // We use a local ValueHandleBase as an iterator so that ValueHandles can add 585*ef860a24SChandler Carruth // and remove themselves from the list without breaking our iteration. This 586*ef860a24SChandler Carruth // is not really an AssertingVH; we just have to give ValueHandleBase a kind. 587*ef860a24SChandler Carruth // Note that we deliberately do not the support the case when dropping a value 588*ef860a24SChandler Carruth // handle results in a new value handle being permanently added to the list 589*ef860a24SChandler Carruth // (as might occur in theory for CallbackVH's): the new value handle will not 590*ef860a24SChandler Carruth // be processed and the checking code will mete out righteous punishment if 591*ef860a24SChandler Carruth // the handle is still present once we have finished processing all the other 592*ef860a24SChandler Carruth // value handles (it is fine to momentarily add then remove a value handle). 593*ef860a24SChandler Carruth for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) { 594*ef860a24SChandler Carruth Iterator.RemoveFromUseList(); 595*ef860a24SChandler Carruth Iterator.AddToExistingUseListAfter(Entry); 596*ef860a24SChandler Carruth assert(Entry->Next == &Iterator && "Loop invariant broken."); 597*ef860a24SChandler Carruth 598*ef860a24SChandler Carruth switch (Entry->getKind()) { 599*ef860a24SChandler Carruth case Assert: 600*ef860a24SChandler Carruth break; 601*ef860a24SChandler Carruth case Tracking: 602*ef860a24SChandler Carruth // Mark that this value has been deleted by setting it to an invalid Value 603*ef860a24SChandler Carruth // pointer. 604*ef860a24SChandler Carruth Entry->operator=(DenseMapInfo<Value *>::getTombstoneKey()); 605*ef860a24SChandler Carruth break; 606*ef860a24SChandler Carruth case Weak: 607*ef860a24SChandler Carruth // Weak just goes to null, which will unlink it from the list. 608*ef860a24SChandler Carruth Entry->operator=(0); 609*ef860a24SChandler Carruth break; 610*ef860a24SChandler Carruth case Callback: 611*ef860a24SChandler Carruth // Forward to the subclass's implementation. 612*ef860a24SChandler Carruth static_cast<CallbackVH*>(Entry)->deleted(); 613*ef860a24SChandler Carruth break; 614*ef860a24SChandler Carruth } 615*ef860a24SChandler Carruth } 616*ef860a24SChandler Carruth 617*ef860a24SChandler Carruth // All callbacks, weak references, and assertingVHs should be dropped by now. 618*ef860a24SChandler Carruth if (V->HasValueHandle) { 619*ef860a24SChandler Carruth #ifndef NDEBUG // Only in +Asserts mode... 620*ef860a24SChandler Carruth dbgs() << "While deleting: " << *V->getType() << " %" << V->getName() 621*ef860a24SChandler Carruth << "\n"; 622*ef860a24SChandler Carruth if (pImpl->ValueHandles[V]->getKind() == Assert) 623*ef860a24SChandler Carruth llvm_unreachable("An asserting value handle still pointed to this" 624*ef860a24SChandler Carruth " value!"); 625*ef860a24SChandler Carruth 626*ef860a24SChandler Carruth #endif 627*ef860a24SChandler Carruth llvm_unreachable("All references to V were not removed?"); 628*ef860a24SChandler Carruth } 629*ef860a24SChandler Carruth } 630*ef860a24SChandler Carruth 631*ef860a24SChandler Carruth 632*ef860a24SChandler Carruth void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) { 633*ef860a24SChandler Carruth assert(Old->HasValueHandle &&"Should only be called if ValueHandles present"); 634*ef860a24SChandler Carruth assert(Old != New && "Changing value into itself!"); 635*ef860a24SChandler Carruth 636*ef860a24SChandler Carruth // Get the linked list base, which is guaranteed to exist since the 637*ef860a24SChandler Carruth // HasValueHandle flag is set. 638*ef860a24SChandler Carruth LLVMContextImpl *pImpl = Old->getContext().pImpl; 639*ef860a24SChandler Carruth ValueHandleBase *Entry = pImpl->ValueHandles[Old]; 640*ef860a24SChandler Carruth 641*ef860a24SChandler Carruth assert(Entry && "Value bit set but no entries exist"); 642*ef860a24SChandler Carruth 643*ef860a24SChandler Carruth // We use a local ValueHandleBase as an iterator so that 644*ef860a24SChandler Carruth // ValueHandles can add and remove themselves from the list without 645*ef860a24SChandler Carruth // breaking our iteration. This is not really an AssertingVH; we 646*ef860a24SChandler Carruth // just have to give ValueHandleBase some kind. 647*ef860a24SChandler Carruth for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) { 648*ef860a24SChandler Carruth Iterator.RemoveFromUseList(); 649*ef860a24SChandler Carruth Iterator.AddToExistingUseListAfter(Entry); 650*ef860a24SChandler Carruth assert(Entry->Next == &Iterator && "Loop invariant broken."); 651*ef860a24SChandler Carruth 652*ef860a24SChandler Carruth switch (Entry->getKind()) { 653*ef860a24SChandler Carruth case Assert: 654*ef860a24SChandler Carruth // Asserting handle does not follow RAUW implicitly. 655*ef860a24SChandler Carruth break; 656*ef860a24SChandler Carruth case Tracking: 657*ef860a24SChandler Carruth // Tracking goes to new value like a WeakVH. Note that this may make it 658*ef860a24SChandler Carruth // something incompatible with its templated type. We don't want to have a 659*ef860a24SChandler Carruth // virtual (or inline) interface to handle this though, so instead we make 660*ef860a24SChandler Carruth // the TrackingVH accessors guarantee that a client never sees this value. 661*ef860a24SChandler Carruth 662*ef860a24SChandler Carruth // FALLTHROUGH 663*ef860a24SChandler Carruth case Weak: 664*ef860a24SChandler Carruth // Weak goes to the new value, which will unlink it from Old's list. 665*ef860a24SChandler Carruth Entry->operator=(New); 666*ef860a24SChandler Carruth break; 667*ef860a24SChandler Carruth case Callback: 668*ef860a24SChandler Carruth // Forward to the subclass's implementation. 669*ef860a24SChandler Carruth static_cast<CallbackVH*>(Entry)->allUsesReplacedWith(New); 670*ef860a24SChandler Carruth break; 671*ef860a24SChandler Carruth } 672*ef860a24SChandler Carruth } 673*ef860a24SChandler Carruth 674*ef860a24SChandler Carruth #ifndef NDEBUG 675*ef860a24SChandler Carruth // If any new tracking or weak value handles were added while processing the 676*ef860a24SChandler Carruth // list, then complain about it now. 677*ef860a24SChandler Carruth if (Old->HasValueHandle) 678*ef860a24SChandler Carruth for (Entry = pImpl->ValueHandles[Old]; Entry; Entry = Entry->Next) 679*ef860a24SChandler Carruth switch (Entry->getKind()) { 680*ef860a24SChandler Carruth case Tracking: 681*ef860a24SChandler Carruth case Weak: 682*ef860a24SChandler Carruth dbgs() << "After RAUW from " << *Old->getType() << " %" 683*ef860a24SChandler Carruth << Old->getName() << " to " << *New->getType() << " %" 684*ef860a24SChandler Carruth << New->getName() << "\n"; 685*ef860a24SChandler Carruth llvm_unreachable("A tracking or weak value handle still pointed to the" 686*ef860a24SChandler Carruth " old value!\n"); 687*ef860a24SChandler Carruth default: 688*ef860a24SChandler Carruth break; 689*ef860a24SChandler Carruth } 690*ef860a24SChandler Carruth #endif 691*ef860a24SChandler Carruth } 692*ef860a24SChandler Carruth 693*ef860a24SChandler Carruth // Default implementation for CallbackVH. 694*ef860a24SChandler Carruth void CallbackVH::allUsesReplacedWith(Value *) {} 695*ef860a24SChandler Carruth 696*ef860a24SChandler Carruth void CallbackVH::deleted() { 697*ef860a24SChandler Carruth setValPtr(NULL); 698*ef860a24SChandler Carruth } 699