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" 182f2ace98SDmitry Mikulin #include "llvm/ADT/SetVector.h" 19b0407ba0SHal Finkel #include "llvm/IR/CallSite.h" 209fb823bbSChandler Carruth #include "llvm/IR/Constant.h" 219fb823bbSChandler Carruth #include "llvm/IR/Constants.h" 222e42c34dSHal Finkel #include "llvm/IR/DataLayout.h" 239fb823bbSChandler Carruth #include "llvm/IR/DerivedTypes.h" 2496ab8726SReid Kleckner #include "llvm/IR/DerivedUser.h" 2503eb0de9SChandler Carruth #include "llvm/IR/GetElementPtrTypeIterator.h" 269fb823bbSChandler Carruth #include "llvm/IR/InstrTypes.h" 279fb823bbSChandler Carruth #include "llvm/IR/Instructions.h" 28a7343d65SRamkumar Ramachandra #include "llvm/IR/IntrinsicInst.h" 299fb823bbSChandler Carruth #include "llvm/IR/Module.h" 309fb823bbSChandler Carruth #include "llvm/IR/Operator.h" 31a7343d65SRamkumar Ramachandra #include "llvm/IR/Statepoint.h" 324220e9c1SChandler Carruth #include "llvm/IR/ValueHandle.h" 339fb823bbSChandler Carruth #include "llvm/IR/ValueSymbolTable.h" 34ef860a24SChandler Carruth #include "llvm/Support/Debug.h" 35ef860a24SChandler Carruth #include "llvm/Support/ErrorHandling.h" 36ef860a24SChandler Carruth #include "llvm/Support/ManagedStatic.h" 37799003bfSBenjamin Kramer #include "llvm/Support/raw_ostream.h" 38ef860a24SChandler Carruth #include <algorithm> 39ecefe5a8SEugene Zelenko 40ef860a24SChandler Carruth using namespace llvm; 41ef860a24SChandler Carruth 42ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 43ef860a24SChandler Carruth // Value Class 44ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 45ef860a24SChandler Carruth static inline Type *checkType(Type *Ty) { 46ef860a24SChandler Carruth assert(Ty && "Value defined with a null type: Error!"); 4716bf89ecSReid Kleckner return Ty; 48ef860a24SChandler Carruth } 49ef860a24SChandler Carruth 50ef860a24SChandler Carruth Value::Value(Type *ty, unsigned scid) 517349ab93SOwen Anderson : VTy(checkType(ty)), UseList(nullptr), SubclassID(scid), 527349ab93SOwen Anderson HasValueHandle(0), SubclassOptionalData(0), SubclassData(0), 53b4eede2cSPete Cooper NumUserOperands(0), IsUsedByMD(false), HasName(false) { 549fd33f24SSerge Guelton static_assert(ConstantFirstVal == 0, "!(SubclassID < ConstantFirstVal)"); 55ef860a24SChandler Carruth // FIXME: Why isn't this in the subclass gunk?? 56ef860a24SChandler Carruth // Note, we cannot call isa<CallInst> before the CallInst has been 57ef860a24SChandler Carruth // constructed. 58ef860a24SChandler Carruth if (SubclassID == Instruction::Call || SubclassID == Instruction::Invoke) 59ef860a24SChandler Carruth assert((VTy->isFirstClassType() || VTy->isVoidTy() || VTy->isStructTy()) && 60ef860a24SChandler Carruth "invalid CallInst type!"); 61ef860a24SChandler Carruth else if (SubclassID != BasicBlockVal && 629fd33f24SSerge Guelton (/*SubclassID < ConstantFirstVal ||*/ SubclassID > ConstantLastVal)) 63ef860a24SChandler Carruth assert((VTy->isFirstClassType() || VTy->isVoidTy()) && 64ef860a24SChandler Carruth "Cannot create non-first-class values except for constants!"); 6596ab8726SReid Kleckner static_assert(sizeof(Value) == 2 * sizeof(void *) + 2 * sizeof(unsigned), 661762ad3eSReid Kleckner "Value too big"); 67ef860a24SChandler Carruth } 68ef860a24SChandler Carruth 69ef860a24SChandler Carruth Value::~Value() { 70ef860a24SChandler Carruth // Notify all ValueHandles (if present) that this value is going away. 71ef860a24SChandler Carruth if (HasValueHandle) 72ef860a24SChandler Carruth ValueHandleBase::ValueIsDeleted(this); 735bf8fef5SDuncan P. N. Exon Smith if (isUsedByMetadata()) 745bf8fef5SDuncan P. N. Exon Smith ValueAsMetadata::handleDeletion(this); 75ef860a24SChandler Carruth 76ef860a24SChandler Carruth #ifndef NDEBUG // Only in -g mode... 77ef860a24SChandler Carruth // Check to make sure that there are no uses of this value that are still 78ef860a24SChandler Carruth // around when the value is destroyed. If there are, then we have a dangling 79b93fb82bSAndrew Kaylor // reference and something is wrong. This code is here to print out where 80b93fb82bSAndrew Kaylor // the value is still being referenced. 81ef860a24SChandler Carruth // 82ef860a24SChandler Carruth if (!use_empty()) { 83ef860a24SChandler Carruth dbgs() << "While deleting: " << *VTy << " %" << getName() << "\n"; 84b93fb82bSAndrew Kaylor for (auto *U : users()) 85b93fb82bSAndrew Kaylor dbgs() << "Use still stuck around after Def is destroyed:" << *U << "\n"; 86ef860a24SChandler Carruth } 87ef860a24SChandler Carruth #endif 88ef860a24SChandler Carruth assert(use_empty() && "Uses remain when a value is destroyed!"); 89ef860a24SChandler Carruth 90ef860a24SChandler Carruth // If this value is named, destroy the name. This should not be in a symtab 91ef860a24SChandler Carruth // at this point. 925bf8fef5SDuncan P. N. Exon Smith destroyValueName(); 93ef860a24SChandler Carruth } 94ef860a24SChandler Carruth 9596ab8726SReid Kleckner void Value::deleteValue() { 9696ab8726SReid Kleckner switch (getValueID()) { 9796ab8726SReid Kleckner #define HANDLE_VALUE(Name) \ 9896ab8726SReid Kleckner case Value::Name##Val: \ 9996ab8726SReid Kleckner delete static_cast<Name *>(this); \ 10096ab8726SReid Kleckner break; 10196ab8726SReid Kleckner #define HANDLE_MEMORY_VALUE(Name) \ 10296ab8726SReid Kleckner case Value::Name##Val: \ 10396ab8726SReid Kleckner static_cast<DerivedUser *>(this)->DeleteValue( \ 10496ab8726SReid Kleckner static_cast<DerivedUser *>(this)); \ 10596ab8726SReid Kleckner break; 10696ab8726SReid Kleckner #define HANDLE_INSTRUCTION(Name) /* nothing */ 10796ab8726SReid Kleckner #include "llvm/IR/Value.def" 10896ab8726SReid Kleckner 10996ab8726SReid Kleckner #define HANDLE_INST(N, OPC, CLASS) \ 11096ab8726SReid Kleckner case Value::InstructionVal + Instruction::OPC: \ 11196ab8726SReid Kleckner delete static_cast<CLASS *>(this); \ 11296ab8726SReid Kleckner break; 11396ab8726SReid Kleckner #define HANDLE_USER_INST(N, OPC, CLASS) 11496ab8726SReid Kleckner #include "llvm/IR/Instruction.def" 11596ab8726SReid Kleckner 11696ab8726SReid Kleckner default: 11796ab8726SReid Kleckner llvm_unreachable("attempting to delete unknown value kind"); 11896ab8726SReid Kleckner } 11996ab8726SReid Kleckner } 12096ab8726SReid Kleckner 1215bf8fef5SDuncan P. N. Exon Smith void Value::destroyValueName() { 1225bf8fef5SDuncan P. N. Exon Smith ValueName *Name = getValueName(); 1235bf8fef5SDuncan P. N. Exon Smith if (Name) 1245bf8fef5SDuncan P. N. Exon Smith Name->Destroy(); 1255bf8fef5SDuncan P. N. Exon Smith setValueName(nullptr); 1265bf8fef5SDuncan P. N. Exon Smith } 1275bf8fef5SDuncan P. N. Exon Smith 128ef860a24SChandler Carruth bool Value::hasNUses(unsigned N) const { 129ef860a24SChandler Carruth const_use_iterator UI = use_begin(), E = use_end(); 130ef860a24SChandler Carruth 131ef860a24SChandler Carruth for (; N; --N, ++UI) 132ef860a24SChandler Carruth if (UI == E) return false; // Too few. 133ef860a24SChandler Carruth return UI == E; 134ef860a24SChandler Carruth } 135ef860a24SChandler Carruth 136ef860a24SChandler Carruth bool Value::hasNUsesOrMore(unsigned N) const { 137ef860a24SChandler Carruth const_use_iterator UI = use_begin(), E = use_end(); 138ef860a24SChandler Carruth 139ef860a24SChandler Carruth for (; N; --N, ++UI) 140ef860a24SChandler Carruth if (UI == E) return false; // Too few. 141ef860a24SChandler Carruth 142ef860a24SChandler Carruth return true; 143ef860a24SChandler Carruth } 144ef860a24SChandler Carruth 145ef860a24SChandler Carruth bool Value::isUsedInBasicBlock(const BasicBlock *BB) const { 1460925b24dSJakob Stoklund Olesen // This can be computed either by scanning the instructions in BB, or by 1470925b24dSJakob Stoklund Olesen // scanning the use list of this Value. Both lists can be very long, but 1480925b24dSJakob Stoklund Olesen // usually one is quite short. 1490925b24dSJakob Stoklund Olesen // 1500925b24dSJakob Stoklund Olesen // Scan both lists simultaneously until one is exhausted. This limits the 1510925b24dSJakob Stoklund Olesen // search to the shorter list. 1520925b24dSJakob Stoklund Olesen BasicBlock::const_iterator BI = BB->begin(), BE = BB->end(); 153cdf47884SChandler Carruth const_user_iterator UI = user_begin(), UE = user_end(); 1540925b24dSJakob Stoklund Olesen for (; BI != BE && UI != UE; ++BI, ++UI) { 1550925b24dSJakob Stoklund Olesen // Scan basic block: Check if this Value is used by the instruction at BI. 15642531260SDavid Majnemer if (is_contained(BI->operands(), this)) 157ef860a24SChandler Carruth return true; 1580925b24dSJakob Stoklund Olesen // Scan use list: Check if the use at UI is in BB. 15942531260SDavid Majnemer const auto *User = dyn_cast<Instruction>(*UI); 160ef860a24SChandler Carruth if (User && User->getParent() == BB) 161ef860a24SChandler Carruth return true; 162ef860a24SChandler Carruth } 163ef860a24SChandler Carruth return false; 164ef860a24SChandler Carruth } 165ef860a24SChandler Carruth 166ef860a24SChandler Carruth unsigned Value::getNumUses() const { 167ef860a24SChandler Carruth return (unsigned)std::distance(use_begin(), use_end()); 168ef860a24SChandler Carruth } 169ef860a24SChandler Carruth 170ef860a24SChandler Carruth static bool getSymTab(Value *V, ValueSymbolTable *&ST) { 171c620761cSCraig Topper ST = nullptr; 172ef860a24SChandler Carruth if (Instruction *I = dyn_cast<Instruction>(V)) { 173ef860a24SChandler Carruth if (BasicBlock *P = I->getParent()) 174ef860a24SChandler Carruth if (Function *PP = P->getParent()) 175a53d49e1SMehdi Amini ST = PP->getValueSymbolTable(); 176ef860a24SChandler Carruth } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) { 177ef860a24SChandler Carruth if (Function *P = BB->getParent()) 178a53d49e1SMehdi Amini ST = P->getValueSymbolTable(); 179ef860a24SChandler Carruth } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { 180ef860a24SChandler Carruth if (Module *P = GV->getParent()) 181ef860a24SChandler Carruth ST = &P->getValueSymbolTable(); 182ef860a24SChandler Carruth } else if (Argument *A = dyn_cast<Argument>(V)) { 183ef860a24SChandler Carruth if (Function *P = A->getParent()) 184a53d49e1SMehdi Amini ST = P->getValueSymbolTable(); 1855bf8fef5SDuncan P. N. Exon Smith } else { 186ef860a24SChandler Carruth assert(isa<Constant>(V) && "Unknown value type!"); 187ef860a24SChandler Carruth return true; // no name is setable for this. 188ef860a24SChandler Carruth } 189ef860a24SChandler Carruth return false; 190ef860a24SChandler Carruth } 191ef860a24SChandler Carruth 1927349ab93SOwen Anderson ValueName *Value::getValueName() const { 1937349ab93SOwen Anderson if (!HasName) return nullptr; 1947349ab93SOwen Anderson 1957349ab93SOwen Anderson LLVMContext &Ctx = getContext(); 1967349ab93SOwen Anderson auto I = Ctx.pImpl->ValueNames.find(this); 1977349ab93SOwen Anderson assert(I != Ctx.pImpl->ValueNames.end() && 1987349ab93SOwen Anderson "No name entry found!"); 1997349ab93SOwen Anderson 2007349ab93SOwen Anderson return I->second; 2017349ab93SOwen Anderson } 2027349ab93SOwen Anderson 2037349ab93SOwen Anderson void Value::setValueName(ValueName *VN) { 2047349ab93SOwen Anderson LLVMContext &Ctx = getContext(); 2057349ab93SOwen Anderson 2067349ab93SOwen Anderson assert(HasName == Ctx.pImpl->ValueNames.count(this) && 2077349ab93SOwen Anderson "HasName bit out of sync!"); 2087349ab93SOwen Anderson 2097349ab93SOwen Anderson if (!VN) { 2107349ab93SOwen Anderson if (HasName) 2117349ab93SOwen Anderson Ctx.pImpl->ValueNames.erase(this); 2127349ab93SOwen Anderson HasName = false; 2137349ab93SOwen Anderson return; 2147349ab93SOwen Anderson } 2157349ab93SOwen Anderson 2167349ab93SOwen Anderson HasName = true; 2177349ab93SOwen Anderson Ctx.pImpl->ValueNames[this] = VN; 2187349ab93SOwen Anderson } 2197349ab93SOwen Anderson 220ef860a24SChandler Carruth StringRef Value::getName() const { 221ef860a24SChandler Carruth // Make sure the empty string is still a C string. For historical reasons, 222ef860a24SChandler Carruth // some clients want to call .data() on the result and expect it to be null 223ef860a24SChandler Carruth // terminated. 2247349ab93SOwen Anderson if (!hasName()) 2255bf8fef5SDuncan P. N. Exon Smith return StringRef("", 0); 2265bf8fef5SDuncan P. N. Exon Smith return getValueName()->getKey(); 227ef860a24SChandler Carruth } 228ef860a24SChandler Carruth 229a7c0c18cSPete Cooper void Value::setNameImpl(const Twine &NewName) { 23009b4a8daSMehdi Amini // Fast-path: LLVMContext can be set to strip out non-GlobalValue names 23150af49fcSMehdi Amini if (getContext().shouldDiscardValueNames() && !isa<GlobalValue>(this)) 23209b4a8daSMehdi Amini return; 23309b4a8daSMehdi Amini 234ef860a24SChandler Carruth // Fast path for common IRBuilder case of setName("") when there is no name. 235ef860a24SChandler Carruth if (NewName.isTriviallyEmpty() && !hasName()) 236ef860a24SChandler Carruth return; 237ef860a24SChandler Carruth 238ef860a24SChandler Carruth SmallString<256> NameData; 239ef860a24SChandler Carruth StringRef NameRef = NewName.toStringRef(NameData); 24009d689f9SRafael Espindola assert(NameRef.find_first_of(0) == StringRef::npos && 24109d689f9SRafael Espindola "Null bytes are not allowed in names"); 242ef860a24SChandler Carruth 243ef860a24SChandler Carruth // Name isn't changing? 244ef860a24SChandler Carruth if (getName() == NameRef) 245ef860a24SChandler Carruth return; 246ef860a24SChandler Carruth 247ef860a24SChandler Carruth assert(!getType()->isVoidTy() && "Cannot assign a name to void values!"); 248ef860a24SChandler Carruth 249ef860a24SChandler Carruth // Get the symbol table to update for this object. 250ef860a24SChandler Carruth ValueSymbolTable *ST; 251ef860a24SChandler Carruth if (getSymTab(this, ST)) 252ef860a24SChandler Carruth return; // Cannot set a name on this value (e.g. constant). 253ef860a24SChandler Carruth 254ef860a24SChandler Carruth if (!ST) { // No symbol table to update? Just do the change. 255ef860a24SChandler Carruth if (NameRef.empty()) { 256ef860a24SChandler Carruth // Free the name for this value. 2575bf8fef5SDuncan P. N. Exon Smith destroyValueName(); 258ef860a24SChandler Carruth return; 259ef860a24SChandler Carruth } 260ef860a24SChandler Carruth 261ef860a24SChandler Carruth // NOTE: Could optimize for the case the name is shrinking to not deallocate 262ef860a24SChandler Carruth // then reallocated. 2635bf8fef5SDuncan P. N. Exon Smith destroyValueName(); 264ef860a24SChandler Carruth 265ef860a24SChandler Carruth // Create the new name. 2665bf8fef5SDuncan P. N. Exon Smith setValueName(ValueName::Create(NameRef)); 2675bf8fef5SDuncan P. N. Exon Smith getValueName()->setValue(this); 268ef860a24SChandler Carruth return; 269ef860a24SChandler Carruth } 270ef860a24SChandler Carruth 271ef860a24SChandler Carruth // NOTE: Could optimize for the case the name is shrinking to not deallocate 272ef860a24SChandler Carruth // then reallocated. 273ef860a24SChandler Carruth if (hasName()) { 274ef860a24SChandler Carruth // Remove old name. 2755bf8fef5SDuncan P. N. Exon Smith ST->removeValueName(getValueName()); 2765bf8fef5SDuncan P. N. Exon Smith destroyValueName(); 277ef860a24SChandler Carruth 278ef860a24SChandler Carruth if (NameRef.empty()) 279ef860a24SChandler Carruth return; 280ef860a24SChandler Carruth } 281ef860a24SChandler Carruth 282ef860a24SChandler Carruth // Name is changing to something new. 2835bf8fef5SDuncan P. N. Exon Smith setValueName(ST->createValueName(NameRef, this)); 284ef860a24SChandler Carruth } 285ef860a24SChandler Carruth 286a7c0c18cSPete Cooper void Value::setName(const Twine &NewName) { 287a7c0c18cSPete Cooper setNameImpl(NewName); 288a7c0c18cSPete Cooper if (Function *F = dyn_cast<Function>(this)) 289a7c0c18cSPete Cooper F->recalculateIntrinsicID(); 290a7c0c18cSPete Cooper } 291a7c0c18cSPete Cooper 292ef860a24SChandler Carruth void Value::takeName(Value *V) { 293c620761cSCraig Topper ValueSymbolTable *ST = nullptr; 294ef860a24SChandler Carruth // If this value has a name, drop it. 295ef860a24SChandler Carruth if (hasName()) { 296ef860a24SChandler Carruth // Get the symtab this is in. 297ef860a24SChandler Carruth if (getSymTab(this, ST)) { 298ef860a24SChandler Carruth // We can't set a name on this value, but we need to clear V's name if 299ef860a24SChandler Carruth // it has one. 300ef860a24SChandler Carruth if (V->hasName()) V->setName(""); 301ef860a24SChandler Carruth return; // Cannot set a name on this value (e.g. constant). 302ef860a24SChandler Carruth } 303ef860a24SChandler Carruth 304ef860a24SChandler Carruth // Remove old name. 305ef860a24SChandler Carruth if (ST) 3065bf8fef5SDuncan P. N. Exon Smith ST->removeValueName(getValueName()); 3075bf8fef5SDuncan P. N. Exon Smith destroyValueName(); 308ef860a24SChandler Carruth } 309ef860a24SChandler Carruth 310ef860a24SChandler Carruth // Now we know that this has no name. 311ef860a24SChandler Carruth 312ef860a24SChandler Carruth // If V has no name either, we're done. 313ef860a24SChandler Carruth if (!V->hasName()) return; 314ef860a24SChandler Carruth 315ef860a24SChandler Carruth // Get this's symtab if we didn't before. 316ef860a24SChandler Carruth if (!ST) { 317ef860a24SChandler Carruth if (getSymTab(this, ST)) { 318ef860a24SChandler Carruth // Clear V's name. 319ef860a24SChandler Carruth V->setName(""); 320ef860a24SChandler Carruth return; // Cannot set a name on this value (e.g. constant). 321ef860a24SChandler Carruth } 322ef860a24SChandler Carruth } 323ef860a24SChandler Carruth 324ef860a24SChandler Carruth // Get V's ST, this should always succed, because V has a name. 325ef860a24SChandler Carruth ValueSymbolTable *VST; 326ef860a24SChandler Carruth bool Failure = getSymTab(V, VST); 327ef860a24SChandler Carruth assert(!Failure && "V has a name, so it should have a ST!"); (void)Failure; 328ef860a24SChandler Carruth 329ef860a24SChandler Carruth // If these values are both in the same symtab, we can do this very fast. 330ef860a24SChandler Carruth // This works even if both values have no symtab yet. 331ef860a24SChandler Carruth if (ST == VST) { 332ef860a24SChandler Carruth // Take the name! 3335bf8fef5SDuncan P. N. Exon Smith setValueName(V->getValueName()); 3345bf8fef5SDuncan P. N. Exon Smith V->setValueName(nullptr); 3355bf8fef5SDuncan P. N. Exon Smith getValueName()->setValue(this); 336ef860a24SChandler Carruth return; 337ef860a24SChandler Carruth } 338ef860a24SChandler Carruth 339ef860a24SChandler Carruth // Otherwise, things are slightly more complex. Remove V's name from VST and 340ef860a24SChandler Carruth // then reinsert it into ST. 341ef860a24SChandler Carruth 342ef860a24SChandler Carruth if (VST) 3435bf8fef5SDuncan P. N. Exon Smith VST->removeValueName(V->getValueName()); 3445bf8fef5SDuncan P. N. Exon Smith setValueName(V->getValueName()); 3455bf8fef5SDuncan P. N. Exon Smith V->setValueName(nullptr); 3465bf8fef5SDuncan P. N. Exon Smith getValueName()->setValue(this); 347ef860a24SChandler Carruth 348ef860a24SChandler Carruth if (ST) 349ef860a24SChandler Carruth ST->reinsertValue(this); 350ef860a24SChandler Carruth } 351ef860a24SChandler Carruth 352eec48903SCraig Topper void Value::assertModuleIsMaterializedImpl() const { 353675bdcedSTodd Fiala #ifndef NDEBUG 354257a3536SRafael Espindola const GlobalValue *GV = dyn_cast<GlobalValue>(this); 355257a3536SRafael Espindola if (!GV) 356257a3536SRafael Espindola return; 357257a3536SRafael Espindola const Module *M = GV->getParent(); 358257a3536SRafael Espindola if (!M) 359257a3536SRafael Espindola return; 360257a3536SRafael Espindola assert(M->isMaterialized()); 361675bdcedSTodd Fiala #endif 362257a3536SRafael Espindola } 363257a3536SRafael Espindola 364675bdcedSTodd Fiala #ifndef NDEBUG 36571b7b68bSCraig Topper static bool contains(SmallPtrSetImpl<ConstantExpr *> &Cache, ConstantExpr *Expr, 3667e2b7567SRafael Espindola Constant *C) { 36770573dcdSDavid Blaikie if (!Cache.insert(Expr).second) 3687e2b7567SRafael Espindola return false; 3697e2b7567SRafael Espindola 3707e2b7567SRafael Espindola for (auto &O : Expr->operands()) { 3717e2b7567SRafael Espindola if (O == C) 3727e2b7567SRafael Espindola return true; 3737e2b7567SRafael Espindola auto *CE = dyn_cast<ConstantExpr>(O); 3747e2b7567SRafael Espindola if (!CE) 3757e2b7567SRafael Espindola continue; 3767e2b7567SRafael Espindola if (contains(Cache, CE, C)) 3777e2b7567SRafael Espindola return true; 3787e2b7567SRafael Espindola } 3797e2b7567SRafael Espindola return false; 3807e2b7567SRafael Espindola } 3817e2b7567SRafael Espindola 3827e2b7567SRafael Espindola static bool contains(Value *Expr, Value *V) { 3837e2b7567SRafael Espindola if (Expr == V) 3847e2b7567SRafael Espindola return true; 3857e2b7567SRafael Espindola 3867e2b7567SRafael Espindola auto *C = dyn_cast<Constant>(V); 3877e2b7567SRafael Espindola if (!C) 3887e2b7567SRafael Espindola return false; 3897e2b7567SRafael Espindola 3907e2b7567SRafael Espindola auto *CE = dyn_cast<ConstantExpr>(Expr); 3917e2b7567SRafael Espindola if (!CE) 3927e2b7567SRafael Espindola return false; 3937e2b7567SRafael Espindola 3947e2b7567SRafael Espindola SmallPtrSet<ConstantExpr *, 4> Cache; 3957e2b7567SRafael Espindola return contains(Cache, CE, C); 3967e2b7567SRafael Espindola } 397ecefe5a8SEugene Zelenko #endif // NDEBUG 398ef860a24SChandler Carruth 3991ed53963SDavide Italiano void Value::doRAUW(Value *New, bool NoMetadata) { 400ef860a24SChandler Carruth assert(New && "Value::replaceAllUsesWith(<null>) is invalid!"); 4017e2b7567SRafael Espindola assert(!contains(New, this) && 4027e2b7567SRafael Espindola "this->replaceAllUsesWith(expr(this)) is NOT valid!"); 403ef860a24SChandler Carruth assert(New->getType() == getType() && 404ef860a24SChandler Carruth "replaceAllUses of value with new value of different type!"); 405ef860a24SChandler Carruth 406ef860a24SChandler Carruth // Notify all ValueHandles (if present) that this value is going away. 407ef860a24SChandler Carruth if (HasValueHandle) 408ef860a24SChandler Carruth ValueHandleBase::ValueIsRAUWd(this, New); 4091ed53963SDavide Italiano if (!NoMetadata && isUsedByMetadata()) 4105bf8fef5SDuncan P. N. Exon Smith ValueAsMetadata::handleRAUW(this, New); 411ef860a24SChandler Carruth 41281bbf742STeresa Johnson while (!materialized_use_empty()) { 413ef860a24SChandler Carruth Use &U = *UseList; 414ef860a24SChandler Carruth // Must handle Constants specially, we cannot call replaceUsesOfWith on a 415ef860a24SChandler Carruth // constant because they are uniqued. 4166b238633SRafael Espindola if (auto *C = dyn_cast<Constant>(U.getUser())) { 417ef860a24SChandler Carruth if (!isa<GlobalValue>(C)) { 4188914d290SMehdi Amini C->handleOperandChange(this, New); 419ef860a24SChandler Carruth continue; 420ef860a24SChandler Carruth } 421ef860a24SChandler Carruth } 422ef860a24SChandler Carruth 423ef860a24SChandler Carruth U.set(New); 424ef860a24SChandler Carruth } 425ef860a24SChandler Carruth 426ef860a24SChandler Carruth if (BasicBlock *BB = dyn_cast<BasicBlock>(this)) 427ef860a24SChandler Carruth BB->replaceSuccessorsPhiUsesWith(cast<BasicBlock>(New)); 428ef860a24SChandler Carruth } 429ef860a24SChandler Carruth 4301ed53963SDavide Italiano void Value::replaceAllUsesWith(Value *New) { 4311ed53963SDavide Italiano doRAUW(New, false /* NoMetadata */); 4321ed53963SDavide Italiano } 4331ed53963SDavide Italiano 4341ed53963SDavide Italiano void Value::replaceNonMetadataUsesWith(Value *New) { 4351ed53963SDavide Italiano doRAUW(New, true /* NoMetadata */); 4361ed53963SDavide Italiano } 4371ed53963SDavide Italiano 438ec6217c9SGerolf Hoflehner // Like replaceAllUsesWith except it does not handle constants or basic blocks. 439ec6217c9SGerolf Hoflehner // This routine leaves uses within BB. 440ec6217c9SGerolf Hoflehner void Value::replaceUsesOutsideBlock(Value *New, BasicBlock *BB) { 441ec6217c9SGerolf Hoflehner assert(New && "Value::replaceUsesOutsideBlock(<null>, BB) is invalid!"); 442ec6217c9SGerolf Hoflehner assert(!contains(New, this) && 443ec6217c9SGerolf Hoflehner "this->replaceUsesOutsideBlock(expr(this), BB) is NOT valid!"); 444ec6217c9SGerolf Hoflehner assert(New->getType() == getType() && 445ec6217c9SGerolf Hoflehner "replaceUses of value with new value of different type!"); 446ec6217c9SGerolf Hoflehner assert(BB && "Basic block that may contain a use of 'New' must be defined\n"); 447ec6217c9SGerolf Hoflehner 448ec6217c9SGerolf Hoflehner use_iterator UI = use_begin(), E = use_end(); 449ec6217c9SGerolf Hoflehner for (; UI != E;) { 450ec6217c9SGerolf Hoflehner Use &U = *UI; 451ec6217c9SGerolf Hoflehner ++UI; 452ec6217c9SGerolf Hoflehner auto *Usr = dyn_cast<Instruction>(U.getUser()); 453ec6217c9SGerolf Hoflehner if (Usr && Usr->getParent() == BB) 454ec6217c9SGerolf Hoflehner continue; 455ec6217c9SGerolf Hoflehner U.set(New); 456ec6217c9SGerolf Hoflehner } 457ec6217c9SGerolf Hoflehner } 458ec6217c9SGerolf Hoflehner 4591a46affbSDavide Italiano void Value::replaceUsesExceptBlockAddr(Value *New) { 4602f2ace98SDmitry Mikulin SmallSetVector<Constant *, 4> Constants; 4611a46affbSDavide Italiano use_iterator UI = use_begin(), E = use_end(); 4621a46affbSDavide Italiano for (; UI != E;) { 4631a46affbSDavide Italiano Use &U = *UI; 4641a46affbSDavide Italiano ++UI; 4651a46affbSDavide Italiano 4661a46affbSDavide Italiano if (isa<BlockAddress>(U.getUser())) 4671a46affbSDavide Italiano continue; 4681a46affbSDavide Italiano 4691a46affbSDavide Italiano // Must handle Constants specially, we cannot call replaceUsesOfWith on a 4701a46affbSDavide Italiano // constant because they are uniqued. 4711a46affbSDavide Italiano if (auto *C = dyn_cast<Constant>(U.getUser())) { 4721a46affbSDavide Italiano if (!isa<GlobalValue>(C)) { 4732f2ace98SDmitry Mikulin // Save unique users to avoid processing operand replacement 4742f2ace98SDmitry Mikulin // more than once. 4752f2ace98SDmitry Mikulin Constants.insert(C); 4761a46affbSDavide Italiano continue; 4771a46affbSDavide Italiano } 4781a46affbSDavide Italiano } 4791a46affbSDavide Italiano 4801a46affbSDavide Italiano U.set(New); 4811a46affbSDavide Italiano } 4822f2ace98SDmitry Mikulin 4832f2ace98SDmitry Mikulin // Process operand replacement of saved constants. 4842f2ace98SDmitry Mikulin for (auto *C : Constants) 4852f2ace98SDmitry Mikulin C->handleOperandChange(this, New); 4861a46affbSDavide Italiano } 4871a46affbSDavide Italiano 488ef860a24SChandler Carruth namespace { 489ef860a24SChandler Carruth // Various metrics for how much to strip off of pointers. 490ef860a24SChandler Carruth enum PointerStripKind { 491ef860a24SChandler Carruth PSK_ZeroIndices, 492c229a4ffSRafael Espindola PSK_ZeroIndicesAndAliases, 493610c966aSPiotr Padlewski PSK_ZeroIndicesAndAliasesAndBarriers, 494ef860a24SChandler Carruth PSK_InBoundsConstantIndices, 495ef860a24SChandler Carruth PSK_InBounds 496ef860a24SChandler Carruth }; 497ef860a24SChandler Carruth 498ef860a24SChandler Carruth template <PointerStripKind StripKind> 4991dd20e65SCraig Topper static const Value *stripPointerCastsAndOffsets(const Value *V) { 500ef860a24SChandler Carruth if (!V->getType()->isPointerTy()) 501ef860a24SChandler Carruth return V; 502ef860a24SChandler Carruth 503ef860a24SChandler Carruth // Even though we don't look through PHI nodes, we could be called on an 504ef860a24SChandler Carruth // instruction in an unreachable block, which may be on a cycle. 5051dd20e65SCraig Topper SmallPtrSet<const Value *, 4> Visited; 506ef860a24SChandler Carruth 507ef860a24SChandler Carruth Visited.insert(V); 508ef860a24SChandler Carruth do { 5091dd20e65SCraig Topper if (auto *GEP = dyn_cast<GEPOperator>(V)) { 510ef860a24SChandler Carruth switch (StripKind) { 511c229a4ffSRafael Espindola case PSK_ZeroIndicesAndAliases: 512610c966aSPiotr Padlewski case PSK_ZeroIndicesAndAliasesAndBarriers: 513ef860a24SChandler Carruth case PSK_ZeroIndices: 514ef860a24SChandler Carruth if (!GEP->hasAllZeroIndices()) 515ef860a24SChandler Carruth return V; 516ef860a24SChandler Carruth break; 517ef860a24SChandler Carruth case PSK_InBoundsConstantIndices: 518ef860a24SChandler Carruth if (!GEP->hasAllConstantIndices()) 519ef860a24SChandler Carruth return V; 520b03fd12cSJustin Bogner LLVM_FALLTHROUGH; 521ef860a24SChandler Carruth case PSK_InBounds: 522ef860a24SChandler Carruth if (!GEP->isInBounds()) 523ef860a24SChandler Carruth return V; 524ef860a24SChandler Carruth break; 525ef860a24SChandler Carruth } 526ef860a24SChandler Carruth V = GEP->getPointerOperand(); 527b03bd4d9SMatt Arsenault } else if (Operator::getOpcode(V) == Instruction::BitCast || 528b03bd4d9SMatt Arsenault Operator::getOpcode(V) == Instruction::AddrSpaceCast) { 529ef860a24SChandler Carruth V = cast<Operator>(V)->getOperand(0); 5301dd20e65SCraig Topper } else if (auto *GA = dyn_cast<GlobalAlias>(V)) { 5315ce32728SSanjoy Das if (StripKind == PSK_ZeroIndices || GA->isInterposable()) 532ef860a24SChandler Carruth return V; 533ef860a24SChandler Carruth V = GA->getAliasee(); 534ef860a24SChandler Carruth } else { 535610c966aSPiotr Padlewski if (auto CS = ImmutableCallSite(V)) { 5361dd20e65SCraig Topper if (const Value *RV = CS.getReturnedArgOperand()) { 5375c12d8feSHal Finkel V = RV; 5385c12d8feSHal Finkel continue; 5395c12d8feSHal Finkel } 540610c966aSPiotr Padlewski // The result of invariant.group.barrier must alias it's argument, 541610c966aSPiotr Padlewski // but it can't be marked with returned attribute, that's why it needs 542610c966aSPiotr Padlewski // special case. 543610c966aSPiotr Padlewski if (StripKind == PSK_ZeroIndicesAndAliasesAndBarriers && 544610c966aSPiotr Padlewski CS.getIntrinsicID() == Intrinsic::invariant_group_barrier) { 545610c966aSPiotr Padlewski V = CS.getArgOperand(0); 546610c966aSPiotr Padlewski continue; 547610c966aSPiotr Padlewski } 548610c966aSPiotr Padlewski } 549ef860a24SChandler Carruth return V; 550ef860a24SChandler Carruth } 551ef860a24SChandler Carruth assert(V->getType()->isPointerTy() && "Unexpected operand type!"); 55270573dcdSDavid Blaikie } while (Visited.insert(V).second); 553ef860a24SChandler Carruth 554ef860a24SChandler Carruth return V; 555ef860a24SChandler Carruth } 556ecefe5a8SEugene Zelenko } // end anonymous namespace 557ef860a24SChandler Carruth 5581dd20e65SCraig Topper const Value *Value::stripPointerCasts() const { 559c229a4ffSRafael Espindola return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndAliases>(this); 560c229a4ffSRafael Espindola } 561c229a4ffSRafael Espindola 5621dd20e65SCraig Topper const Value *Value::stripPointerCastsNoFollowAliases() const { 563ef860a24SChandler Carruth return stripPointerCastsAndOffsets<PSK_ZeroIndices>(this); 564ef860a24SChandler Carruth } 565ef860a24SChandler Carruth 5661dd20e65SCraig Topper const Value *Value::stripInBoundsConstantOffsets() const { 567ef860a24SChandler Carruth return stripPointerCastsAndOffsets<PSK_InBoundsConstantIndices>(this); 568ef860a24SChandler Carruth } 569ef860a24SChandler Carruth 570610c966aSPiotr Padlewski const Value *Value::stripPointerCastsAndBarriers() const { 571610c966aSPiotr Padlewski return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndAliasesAndBarriers>( 572610c966aSPiotr Padlewski this); 573610c966aSPiotr Padlewski } 574610c966aSPiotr Padlewski 5751dd20e65SCraig Topper const Value * 5761dd20e65SCraig Topper Value::stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, 5771dd20e65SCraig Topper APInt &Offset) const { 578989e6308SChandler Carruth if (!getType()->isPointerTy()) 579989e6308SChandler Carruth return this; 580989e6308SChandler Carruth 581989e6308SChandler Carruth assert(Offset.getBitWidth() == DL.getPointerSizeInBits(cast<PointerType>( 582989e6308SChandler Carruth getType())->getAddressSpace()) && 583989e6308SChandler Carruth "The offset must have exactly as many bits as our pointer."); 584989e6308SChandler Carruth 585989e6308SChandler Carruth // Even though we don't look through PHI nodes, we could be called on an 586989e6308SChandler Carruth // instruction in an unreachable block, which may be on a cycle. 5871dd20e65SCraig Topper SmallPtrSet<const Value *, 4> Visited; 588989e6308SChandler Carruth Visited.insert(this); 5891dd20e65SCraig Topper const Value *V = this; 590989e6308SChandler Carruth do { 5911dd20e65SCraig Topper if (auto *GEP = dyn_cast<GEPOperator>(V)) { 592989e6308SChandler Carruth if (!GEP->isInBounds()) 593989e6308SChandler Carruth return V; 594ba689b33SChandler Carruth APInt GEPOffset(Offset); 595ba689b33SChandler Carruth if (!GEP->accumulateConstantOffset(DL, GEPOffset)) 596989e6308SChandler Carruth return V; 597ba689b33SChandler Carruth Offset = GEPOffset; 598989e6308SChandler Carruth V = GEP->getPointerOperand(); 599d63df510SPhilip Reames } else if (Operator::getOpcode(V) == Instruction::BitCast) { 600989e6308SChandler Carruth V = cast<Operator>(V)->getOperand(0); 6011dd20e65SCraig Topper } else if (auto *GA = dyn_cast<GlobalAlias>(V)) { 602989e6308SChandler Carruth V = GA->getAliasee(); 603989e6308SChandler Carruth } else { 6041dd20e65SCraig Topper if (auto CS = ImmutableCallSite(V)) 6051dd20e65SCraig Topper if (const Value *RV = CS.getReturnedArgOperand()) { 6065c12d8feSHal Finkel V = RV; 6075c12d8feSHal Finkel continue; 6085c12d8feSHal Finkel } 6095c12d8feSHal Finkel 610989e6308SChandler Carruth return V; 611989e6308SChandler Carruth } 612989e6308SChandler Carruth assert(V->getType()->isPointerTy() && "Unexpected operand type!"); 61370573dcdSDavid Blaikie } while (Visited.insert(V).second); 614989e6308SChandler Carruth 615989e6308SChandler Carruth return V; 616989e6308SChandler Carruth } 617989e6308SChandler Carruth 6181dd20e65SCraig Topper const Value *Value::stripInBoundsOffsets() const { 619ef860a24SChandler Carruth return stripPointerCastsAndOffsets<PSK_InBounds>(this); 620ef860a24SChandler Carruth } 621ef860a24SChandler Carruth 622*5d865324SBjorn Steinbrink uint64_t Value::getPointerDereferenceableBytes(const DataLayout &DL, 62300953cbeSSanjoy Das bool &CanBeNull) const { 624345f0148SArtur Pilipenko assert(getType()->isPointerTy() && "must be pointer"); 625345f0148SArtur Pilipenko 626*5d865324SBjorn Steinbrink uint64_t DerefBytes = 0; 627345f0148SArtur Pilipenko CanBeNull = false; 628345f0148SArtur Pilipenko if (const Argument *A = dyn_cast<Argument>(this)) { 629345f0148SArtur Pilipenko DerefBytes = A->getDereferenceableBytes(); 63000953cbeSSanjoy Das if (DerefBytes == 0 && A->hasByValAttr() && A->getType()->isSized()) { 63100953cbeSSanjoy Das DerefBytes = DL.getTypeStoreSize(A->getType()); 63200953cbeSSanjoy Das CanBeNull = false; 63300953cbeSSanjoy Das } 634345f0148SArtur Pilipenko if (DerefBytes == 0) { 635345f0148SArtur Pilipenko DerefBytes = A->getDereferenceableOrNullBytes(); 636345f0148SArtur Pilipenko CanBeNull = true; 637345f0148SArtur Pilipenko } 638345f0148SArtur Pilipenko } else if (auto CS = ImmutableCallSite(this)) { 6396652a52eSReid Kleckner DerefBytes = CS.getDereferenceableBytes(AttributeList::ReturnIndex); 640345f0148SArtur Pilipenko if (DerefBytes == 0) { 6416652a52eSReid Kleckner DerefBytes = CS.getDereferenceableOrNullBytes(AttributeList::ReturnIndex); 642345f0148SArtur Pilipenko CanBeNull = true; 643345f0148SArtur Pilipenko } 644345f0148SArtur Pilipenko } else if (const LoadInst *LI = dyn_cast<LoadInst>(this)) { 645345f0148SArtur Pilipenko if (MDNode *MD = LI->getMetadata(LLVMContext::MD_dereferenceable)) { 646345f0148SArtur Pilipenko ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0)); 647345f0148SArtur Pilipenko DerefBytes = CI->getLimitedValue(); 648345f0148SArtur Pilipenko } 649345f0148SArtur Pilipenko if (DerefBytes == 0) { 650345f0148SArtur Pilipenko if (MDNode *MD = 651345f0148SArtur Pilipenko LI->getMetadata(LLVMContext::MD_dereferenceable_or_null)) { 652345f0148SArtur Pilipenko ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0)); 653345f0148SArtur Pilipenko DerefBytes = CI->getLimitedValue(); 654345f0148SArtur Pilipenko } 655345f0148SArtur Pilipenko CanBeNull = true; 656345f0148SArtur Pilipenko } 65700953cbeSSanjoy Das } else if (auto *AI = dyn_cast<AllocaInst>(this)) { 658*5d865324SBjorn Steinbrink const ConstantInt *ArraySize = dyn_cast<ConstantInt>(AI->getArraySize()); 659*5d865324SBjorn Steinbrink if (ArraySize && AI->getAllocatedType()->isSized()) { 660*5d865324SBjorn Steinbrink DerefBytes = DL.getTypeStoreSize(AI->getAllocatedType()) * 661*5d865324SBjorn Steinbrink ArraySize->getZExtValue(); 66200953cbeSSanjoy Das CanBeNull = false; 66300953cbeSSanjoy Das } 66400953cbeSSanjoy Das } else if (auto *GV = dyn_cast<GlobalVariable>(this)) { 66500953cbeSSanjoy Das if (GV->getValueType()->isSized() && !GV->hasExternalWeakLinkage()) { 66600953cbeSSanjoy Das // TODO: Don't outright reject hasExternalWeakLinkage but set the 66700953cbeSSanjoy Das // CanBeNull flag. 66800953cbeSSanjoy Das DerefBytes = DL.getTypeStoreSize(GV->getValueType()); 66900953cbeSSanjoy Das CanBeNull = false; 67000953cbeSSanjoy Das } 671345f0148SArtur Pilipenko } 672345f0148SArtur Pilipenko return DerefBytes; 673345f0148SArtur Pilipenko } 674345f0148SArtur Pilipenko 675ae51afc5SArtur Pilipenko unsigned Value::getPointerAlignment(const DataLayout &DL) const { 676ae51afc5SArtur Pilipenko assert(getType()->isPointerTy() && "must be pointer"); 677ae51afc5SArtur Pilipenko 678ae51afc5SArtur Pilipenko unsigned Align = 0; 679ae51afc5SArtur Pilipenko if (auto *GO = dyn_cast<GlobalObject>(this)) { 680ae51afc5SArtur Pilipenko Align = GO->getAlignment(); 681ae51afc5SArtur Pilipenko if (Align == 0) { 682ae51afc5SArtur Pilipenko if (auto *GVar = dyn_cast<GlobalVariable>(GO)) { 683ae51afc5SArtur Pilipenko Type *ObjectType = GVar->getValueType(); 684ae51afc5SArtur Pilipenko if (ObjectType->isSized()) { 685ae51afc5SArtur Pilipenko // If the object is defined in the current Module, we'll be giving 686ae51afc5SArtur Pilipenko // it the preferred alignment. Otherwise, we have to assume that it 687ae51afc5SArtur Pilipenko // may only have the minimum ABI alignment. 688ae51afc5SArtur Pilipenko if (GVar->isStrongDefinitionForLinker()) 689ae51afc5SArtur Pilipenko Align = DL.getPreferredAlignment(GVar); 690ae51afc5SArtur Pilipenko else 691ae51afc5SArtur Pilipenko Align = DL.getABITypeAlignment(ObjectType); 692ae51afc5SArtur Pilipenko } 693ae51afc5SArtur Pilipenko } 694ae51afc5SArtur Pilipenko } 695ae51afc5SArtur Pilipenko } else if (const Argument *A = dyn_cast<Argument>(this)) { 696ae51afc5SArtur Pilipenko Align = A->getParamAlignment(); 697ae51afc5SArtur Pilipenko 698ae51afc5SArtur Pilipenko if (!Align && A->hasStructRetAttr()) { 699ae51afc5SArtur Pilipenko // An sret parameter has at least the ABI alignment of the return type. 700ae51afc5SArtur Pilipenko Type *EltTy = cast<PointerType>(A->getType())->getElementType(); 701ae51afc5SArtur Pilipenko if (EltTy->isSized()) 702ae51afc5SArtur Pilipenko Align = DL.getABITypeAlignment(EltTy); 703ae51afc5SArtur Pilipenko } 704c97eac65SArtur Pilipenko } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(this)) { 705ae51afc5SArtur Pilipenko Align = AI->getAlignment(); 706c97eac65SArtur Pilipenko if (Align == 0) { 707c97eac65SArtur Pilipenko Type *AllocatedType = AI->getAllocatedType(); 708c97eac65SArtur Pilipenko if (AllocatedType->isSized()) 709c97eac65SArtur Pilipenko Align = DL.getPrefTypeAlignment(AllocatedType); 710c97eac65SArtur Pilipenko } 711c97eac65SArtur Pilipenko } else if (auto CS = ImmutableCallSite(this)) 712859f8b54SReid Kleckner Align = CS.getAttributes().getRetAlignment(); 713ae51afc5SArtur Pilipenko else if (const LoadInst *LI = dyn_cast<LoadInst>(this)) 714ae51afc5SArtur Pilipenko if (MDNode *MD = LI->getMetadata(LLVMContext::MD_align)) { 715ae51afc5SArtur Pilipenko ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0)); 716ae51afc5SArtur Pilipenko Align = CI->getLimitedValue(); 717ae51afc5SArtur Pilipenko } 718ae51afc5SArtur Pilipenko 719ae51afc5SArtur Pilipenko return Align; 720ae51afc5SArtur Pilipenko } 721ae51afc5SArtur Pilipenko 7221dd20e65SCraig Topper const Value *Value::DoPHITranslation(const BasicBlock *CurBB, 7231dd20e65SCraig Topper const BasicBlock *PredBB) const { 7241dd20e65SCraig Topper auto *PN = dyn_cast<PHINode>(this); 725ef860a24SChandler Carruth if (PN && PN->getParent() == CurBB) 726ef860a24SChandler Carruth return PN->getIncomingValueForBlock(PredBB); 727ef860a24SChandler Carruth return this; 728ef860a24SChandler Carruth } 729ef860a24SChandler Carruth 730ef860a24SChandler Carruth LLVMContext &Value::getContext() const { return VTy->getContext(); } 731ef860a24SChandler Carruth 7323441ffe9SDuncan P. N. Exon Smith void Value::reverseUseList() { 7333441ffe9SDuncan P. N. Exon Smith if (!UseList || !UseList->Next) 7343441ffe9SDuncan P. N. Exon Smith // No need to reverse 0 or 1 uses. 7353441ffe9SDuncan P. N. Exon Smith return; 7363441ffe9SDuncan P. N. Exon Smith 7373441ffe9SDuncan P. N. Exon Smith Use *Head = UseList; 7383441ffe9SDuncan P. N. Exon Smith Use *Current = UseList->Next; 7393441ffe9SDuncan P. N. Exon Smith Head->Next = nullptr; 7403441ffe9SDuncan P. N. Exon Smith while (Current) { 7413441ffe9SDuncan P. N. Exon Smith Use *Next = Current->Next; 7423441ffe9SDuncan P. N. Exon Smith Current->Next = Head; 7433441ffe9SDuncan P. N. Exon Smith Head->setPrev(&Current->Next); 7443441ffe9SDuncan P. N. Exon Smith Head = Current; 7453441ffe9SDuncan P. N. Exon Smith Current = Next; 7463441ffe9SDuncan P. N. Exon Smith } 7473441ffe9SDuncan P. N. Exon Smith UseList = Head; 7483441ffe9SDuncan P. N. Exon Smith Head->setPrev(&UseList); 7493441ffe9SDuncan P. N. Exon Smith } 7503441ffe9SDuncan P. N. Exon Smith 751ad830022SArnold Schwaighofer bool Value::isSwiftError() const { 752ad830022SArnold Schwaighofer auto *Arg = dyn_cast<Argument>(this); 753ad830022SArnold Schwaighofer if (Arg) 754ad830022SArnold Schwaighofer return Arg->hasSwiftErrorAttr(); 755ad830022SArnold Schwaighofer auto *Alloca = dyn_cast<AllocaInst>(this); 756ad830022SArnold Schwaighofer if (!Alloca) 757ad830022SArnold Schwaighofer return false; 758ad830022SArnold Schwaighofer return Alloca->isSwiftError(); 759ad830022SArnold Schwaighofer } 760ad830022SArnold Schwaighofer 761ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 762ef860a24SChandler Carruth // ValueHandleBase Class 763ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 764ef860a24SChandler Carruth 765ef860a24SChandler Carruth void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) { 766ef860a24SChandler Carruth assert(List && "Handle list is null?"); 767ef860a24SChandler Carruth 768ef860a24SChandler Carruth // Splice ourselves into the list. 769ef860a24SChandler Carruth Next = *List; 770ef860a24SChandler Carruth *List = this; 771ef860a24SChandler Carruth setPrevPtr(List); 772ef860a24SChandler Carruth if (Next) { 773ef860a24SChandler Carruth Next->setPrevPtr(&Next); 7746f08789dSSanjoy Das assert(getValPtr() == Next->getValPtr() && "Added to wrong list?"); 775ef860a24SChandler Carruth } 776ef860a24SChandler Carruth } 777ef860a24SChandler Carruth 778ef860a24SChandler Carruth void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) { 779ef860a24SChandler Carruth assert(List && "Must insert after existing node"); 780ef860a24SChandler Carruth 781ef860a24SChandler Carruth Next = List->Next; 782ef860a24SChandler Carruth setPrevPtr(&List->Next); 783ef860a24SChandler Carruth List->Next = this; 784ef860a24SChandler Carruth if (Next) 785ef860a24SChandler Carruth Next->setPrevPtr(&Next); 786ef860a24SChandler Carruth } 787ef860a24SChandler Carruth 788ef860a24SChandler Carruth void ValueHandleBase::AddToUseList() { 7896f08789dSSanjoy Das assert(getValPtr() && "Null pointer doesn't have a use list!"); 790ef860a24SChandler Carruth 7916f08789dSSanjoy Das LLVMContextImpl *pImpl = getValPtr()->getContext().pImpl; 792ef860a24SChandler Carruth 7936f08789dSSanjoy Das if (getValPtr()->HasValueHandle) { 794ef860a24SChandler Carruth // If this value already has a ValueHandle, then it must be in the 795ef860a24SChandler Carruth // ValueHandles map already. 7966f08789dSSanjoy Das ValueHandleBase *&Entry = pImpl->ValueHandles[getValPtr()]; 7972617dcceSCraig Topper assert(Entry && "Value doesn't have any handles?"); 798ef860a24SChandler Carruth AddToExistingUseList(&Entry); 799ef860a24SChandler Carruth return; 800ef860a24SChandler Carruth } 801ef860a24SChandler Carruth 802ef860a24SChandler Carruth // Ok, it doesn't have any handles yet, so we must insert it into the 803ef860a24SChandler Carruth // DenseMap. However, doing this insertion could cause the DenseMap to 804ef860a24SChandler Carruth // reallocate itself, which would invalidate all of the PrevP pointers that 805ef860a24SChandler Carruth // point into the old table. Handle this by checking for reallocation and 806ef860a24SChandler Carruth // updating the stale pointers only if needed. 807ef860a24SChandler Carruth DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles; 808ef860a24SChandler Carruth const void *OldBucketPtr = Handles.getPointerIntoBucketsArray(); 809ef860a24SChandler Carruth 8106f08789dSSanjoy Das ValueHandleBase *&Entry = Handles[getValPtr()]; 8112617dcceSCraig Topper assert(!Entry && "Value really did already have handles?"); 812ef860a24SChandler Carruth AddToExistingUseList(&Entry); 8136f08789dSSanjoy Das getValPtr()->HasValueHandle = true; 814ef860a24SChandler Carruth 815ef860a24SChandler Carruth // If reallocation didn't happen or if this was the first insertion, don't 816ef860a24SChandler Carruth // walk the table. 817ef860a24SChandler Carruth if (Handles.isPointerIntoBucketsArray(OldBucketPtr) || 818ef860a24SChandler Carruth Handles.size() == 1) { 819ef860a24SChandler Carruth return; 820ef860a24SChandler Carruth } 821ef860a24SChandler Carruth 822ef860a24SChandler Carruth // Okay, reallocation did happen. Fix the Prev Pointers. 823ef860a24SChandler Carruth for (DenseMap<Value*, ValueHandleBase*>::iterator I = Handles.begin(), 824ef860a24SChandler Carruth E = Handles.end(); I != E; ++I) { 8256f08789dSSanjoy Das assert(I->second && I->first == I->second->getValPtr() && 826ef860a24SChandler Carruth "List invariant broken!"); 827ef860a24SChandler Carruth I->second->setPrevPtr(&I->second); 828ef860a24SChandler Carruth } 829ef860a24SChandler Carruth } 830ef860a24SChandler Carruth 831ef860a24SChandler Carruth void ValueHandleBase::RemoveFromUseList() { 8326f08789dSSanjoy Das assert(getValPtr() && getValPtr()->HasValueHandle && 833ef860a24SChandler Carruth "Pointer doesn't have a use list!"); 834ef860a24SChandler Carruth 835ef860a24SChandler Carruth // Unlink this from its use list. 836ef860a24SChandler Carruth ValueHandleBase **PrevPtr = getPrevPtr(); 837ef860a24SChandler Carruth assert(*PrevPtr == this && "List invariant broken"); 838ef860a24SChandler Carruth 839ef860a24SChandler Carruth *PrevPtr = Next; 840ef860a24SChandler Carruth if (Next) { 841ef860a24SChandler Carruth assert(Next->getPrevPtr() == &Next && "List invariant broken"); 842ef860a24SChandler Carruth Next->setPrevPtr(PrevPtr); 843ef860a24SChandler Carruth return; 844ef860a24SChandler Carruth } 845ef860a24SChandler Carruth 846ef860a24SChandler Carruth // If the Next pointer was null, then it is possible that this was the last 847ef860a24SChandler Carruth // ValueHandle watching VP. If so, delete its entry from the ValueHandles 848ef860a24SChandler Carruth // map. 8496f08789dSSanjoy Das LLVMContextImpl *pImpl = getValPtr()->getContext().pImpl; 850ef860a24SChandler Carruth DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles; 851ef860a24SChandler Carruth if (Handles.isPointerIntoBucketsArray(PrevPtr)) { 8526f08789dSSanjoy Das Handles.erase(getValPtr()); 8536f08789dSSanjoy Das getValPtr()->HasValueHandle = false; 854ef860a24SChandler Carruth } 855ef860a24SChandler Carruth } 856ef860a24SChandler Carruth 857ef860a24SChandler Carruth void ValueHandleBase::ValueIsDeleted(Value *V) { 858ef860a24SChandler Carruth assert(V->HasValueHandle && "Should only be called if ValueHandles present"); 859ef860a24SChandler Carruth 860ef860a24SChandler Carruth // Get the linked list base, which is guaranteed to exist since the 861ef860a24SChandler Carruth // HasValueHandle flag is set. 862ef860a24SChandler Carruth LLVMContextImpl *pImpl = V->getContext().pImpl; 863ef860a24SChandler Carruth ValueHandleBase *Entry = pImpl->ValueHandles[V]; 864ef860a24SChandler Carruth assert(Entry && "Value bit set but no entries exist"); 865ef860a24SChandler Carruth 866ef860a24SChandler Carruth // We use a local ValueHandleBase as an iterator so that ValueHandles can add 867ef860a24SChandler Carruth // and remove themselves from the list without breaking our iteration. This 868ef860a24SChandler Carruth // is not really an AssertingVH; we just have to give ValueHandleBase a kind. 869ef860a24SChandler Carruth // Note that we deliberately do not the support the case when dropping a value 870ef860a24SChandler Carruth // handle results in a new value handle being permanently added to the list 871ef860a24SChandler Carruth // (as might occur in theory for CallbackVH's): the new value handle will not 872ef860a24SChandler Carruth // be processed and the checking code will mete out righteous punishment if 873ef860a24SChandler Carruth // the handle is still present once we have finished processing all the other 874ef860a24SChandler Carruth // value handles (it is fine to momentarily add then remove a value handle). 875ef860a24SChandler Carruth for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) { 876ef860a24SChandler Carruth Iterator.RemoveFromUseList(); 877ef860a24SChandler Carruth Iterator.AddToExistingUseListAfter(Entry); 878ef860a24SChandler Carruth assert(Entry->Next == &Iterator && "Loop invariant broken."); 879ef860a24SChandler Carruth 880ef860a24SChandler Carruth switch (Entry->getKind()) { 881ef860a24SChandler Carruth case Assert: 882ef860a24SChandler Carruth break; 883f1c0eafdSSanjoy Das case Weak: 884e6bca0eeSSanjoy Das case WeakTracking: 885f1c0eafdSSanjoy Das // WeakTracking and Weak just go to null, which unlinks them 886f1c0eafdSSanjoy Das // from the list. 887c620761cSCraig Topper Entry->operator=(nullptr); 888ef860a24SChandler Carruth break; 889ef860a24SChandler Carruth case Callback: 890ef860a24SChandler Carruth // Forward to the subclass's implementation. 891ef860a24SChandler Carruth static_cast<CallbackVH*>(Entry)->deleted(); 892ef860a24SChandler Carruth break; 893ef860a24SChandler Carruth } 894ef860a24SChandler Carruth } 895ef860a24SChandler Carruth 896ef860a24SChandler Carruth // All callbacks, weak references, and assertingVHs should be dropped by now. 897ef860a24SChandler Carruth if (V->HasValueHandle) { 898ef860a24SChandler Carruth #ifndef NDEBUG // Only in +Asserts mode... 899ef860a24SChandler Carruth dbgs() << "While deleting: " << *V->getType() << " %" << V->getName() 900ef860a24SChandler Carruth << "\n"; 901ef860a24SChandler Carruth if (pImpl->ValueHandles[V]->getKind() == Assert) 902ef860a24SChandler Carruth llvm_unreachable("An asserting value handle still pointed to this" 903ef860a24SChandler Carruth " value!"); 904ef860a24SChandler Carruth 905ef860a24SChandler Carruth #endif 906ef860a24SChandler Carruth llvm_unreachable("All references to V were not removed?"); 907ef860a24SChandler Carruth } 908ef860a24SChandler Carruth } 909ef860a24SChandler Carruth 910ef860a24SChandler Carruth void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) { 911ef860a24SChandler Carruth assert(Old->HasValueHandle &&"Should only be called if ValueHandles present"); 912ef860a24SChandler Carruth assert(Old != New && "Changing value into itself!"); 913c1892e2dSFrederic Riss assert(Old->getType() == New->getType() && 914c1892e2dSFrederic Riss "replaceAllUses of value with new value of different type!"); 915ef860a24SChandler Carruth 916ef860a24SChandler Carruth // Get the linked list base, which is guaranteed to exist since the 917ef860a24SChandler Carruth // HasValueHandle flag is set. 918ef860a24SChandler Carruth LLVMContextImpl *pImpl = Old->getContext().pImpl; 919ef860a24SChandler Carruth ValueHandleBase *Entry = pImpl->ValueHandles[Old]; 920ef860a24SChandler Carruth 921ef860a24SChandler Carruth assert(Entry && "Value bit set but no entries exist"); 922ef860a24SChandler Carruth 923ef860a24SChandler Carruth // We use a local ValueHandleBase as an iterator so that 924ef860a24SChandler Carruth // ValueHandles can add and remove themselves from the list without 925ef860a24SChandler Carruth // breaking our iteration. This is not really an AssertingVH; we 926ef860a24SChandler Carruth // just have to give ValueHandleBase some kind. 927ef860a24SChandler Carruth for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) { 928ef860a24SChandler Carruth Iterator.RemoveFromUseList(); 929ef860a24SChandler Carruth Iterator.AddToExistingUseListAfter(Entry); 930ef860a24SChandler Carruth assert(Entry->Next == &Iterator && "Loop invariant broken."); 931ef860a24SChandler Carruth 932ef860a24SChandler Carruth switch (Entry->getKind()) { 933ef860a24SChandler Carruth case Assert: 934f1c0eafdSSanjoy Das case Weak: 935f1c0eafdSSanjoy Das // Asserting and Weak handles do not follow RAUW implicitly. 936ef860a24SChandler Carruth break; 937e6bca0eeSSanjoy Das case WeakTracking: 9382cbeb00fSSanjoy Das // Weak goes to the new value, which will unlink it from Old's list. 939ef860a24SChandler Carruth Entry->operator=(New); 940ef860a24SChandler Carruth break; 941ef860a24SChandler Carruth case Callback: 942ef860a24SChandler Carruth // Forward to the subclass's implementation. 943ef860a24SChandler Carruth static_cast<CallbackVH*>(Entry)->allUsesReplacedWith(New); 944ef860a24SChandler Carruth break; 945ef860a24SChandler Carruth } 946ef860a24SChandler Carruth } 947ef860a24SChandler Carruth 948ef860a24SChandler Carruth #ifndef NDEBUG 9498a623820SSanjoy Das // If any new weak value handles were added while processing the 950ef860a24SChandler Carruth // list, then complain about it now. 951ef860a24SChandler Carruth if (Old->HasValueHandle) 952ef860a24SChandler Carruth for (Entry = pImpl->ValueHandles[Old]; Entry; Entry = Entry->Next) 953ef860a24SChandler Carruth switch (Entry->getKind()) { 954e6bca0eeSSanjoy Das case WeakTracking: 955ef860a24SChandler Carruth dbgs() << "After RAUW from " << *Old->getType() << " %" 956ef860a24SChandler Carruth << Old->getName() << " to " << *New->getType() << " %" 957ef860a24SChandler Carruth << New->getName() << "\n"; 9588a623820SSanjoy Das llvm_unreachable( 959e6bca0eeSSanjoy Das "A weak tracking value handle still pointed to the old value!\n"); 960ef860a24SChandler Carruth default: 961ef860a24SChandler Carruth break; 962ef860a24SChandler Carruth } 963ef860a24SChandler Carruth #endif 964ef860a24SChandler Carruth } 965ef860a24SChandler Carruth 966d12ccbd3SJuergen Ributzka // Pin the vtable to this file. 967d12ccbd3SJuergen Ributzka void CallbackVH::anchor() {} 968