10b57cec5SDimitry Andric //===-- Value.cpp - Implement the Value class -----------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This file implements the Value, ValueHandle, and User classes. 100b57cec5SDimitry Andric // 110b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 120b57cec5SDimitry Andric 130b57cec5SDimitry Andric #include "llvm/IR/Value.h" 140b57cec5SDimitry Andric #include "LLVMContextImpl.h" 150b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 160b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h" 170b57cec5SDimitry Andric #include "llvm/ADT/SetVector.h" 180b57cec5SDimitry Andric #include "llvm/IR/Constant.h" 190b57cec5SDimitry Andric #include "llvm/IR/Constants.h" 200b57cec5SDimitry Andric #include "llvm/IR/DataLayout.h" 210b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h" 220b57cec5SDimitry Andric #include "llvm/IR/DerivedUser.h" 230b57cec5SDimitry Andric #include "llvm/IR/GetElementPtrTypeIterator.h" 240b57cec5SDimitry Andric #include "llvm/IR/InstrTypes.h" 250b57cec5SDimitry Andric #include "llvm/IR/Instructions.h" 260b57cec5SDimitry Andric #include "llvm/IR/IntrinsicInst.h" 270b57cec5SDimitry Andric #include "llvm/IR/Module.h" 280b57cec5SDimitry Andric #include "llvm/IR/Operator.h" 290b57cec5SDimitry Andric #include "llvm/IR/Statepoint.h" 300b57cec5SDimitry Andric #include "llvm/IR/ValueHandle.h" 310b57cec5SDimitry Andric #include "llvm/IR/ValueSymbolTable.h" 320b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 330b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 340b57cec5SDimitry Andric #include "llvm/Support/ManagedStatic.h" 350b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 360b57cec5SDimitry Andric #include <algorithm> 370b57cec5SDimitry Andric 380b57cec5SDimitry Andric using namespace llvm; 390b57cec5SDimitry Andric 400b57cec5SDimitry Andric static cl::opt<unsigned> NonGlobalValueMaxNameSize( 410b57cec5SDimitry Andric "non-global-value-max-name-size", cl::Hidden, cl::init(1024), 420b57cec5SDimitry Andric cl::desc("Maximum size for the name of non-global values.")); 430b57cec5SDimitry Andric 440b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 450b57cec5SDimitry Andric // Value Class 460b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 470b57cec5SDimitry Andric static inline Type *checkType(Type *Ty) { 480b57cec5SDimitry Andric assert(Ty && "Value defined with a null type: Error!"); 490b57cec5SDimitry Andric return Ty; 500b57cec5SDimitry Andric } 510b57cec5SDimitry Andric 520b57cec5SDimitry Andric Value::Value(Type *ty, unsigned scid) 530b57cec5SDimitry Andric : VTy(checkType(ty)), UseList(nullptr), SubclassID(scid), 540b57cec5SDimitry Andric HasValueHandle(0), SubclassOptionalData(0), SubclassData(0), 550b57cec5SDimitry Andric NumUserOperands(0), IsUsedByMD(false), HasName(false) { 560b57cec5SDimitry Andric static_assert(ConstantFirstVal == 0, "!(SubclassID < ConstantFirstVal)"); 570b57cec5SDimitry Andric // FIXME: Why isn't this in the subclass gunk?? 580b57cec5SDimitry Andric // Note, we cannot call isa<CallInst> before the CallInst has been 590b57cec5SDimitry Andric // constructed. 600b57cec5SDimitry Andric if (SubclassID == Instruction::Call || SubclassID == Instruction::Invoke || 610b57cec5SDimitry Andric SubclassID == Instruction::CallBr) 620b57cec5SDimitry Andric assert((VTy->isFirstClassType() || VTy->isVoidTy() || VTy->isStructTy()) && 630b57cec5SDimitry Andric "invalid CallInst type!"); 640b57cec5SDimitry Andric else if (SubclassID != BasicBlockVal && 650b57cec5SDimitry Andric (/*SubclassID < ConstantFirstVal ||*/ SubclassID > ConstantLastVal)) 660b57cec5SDimitry Andric assert((VTy->isFirstClassType() || VTy->isVoidTy()) && 670b57cec5SDimitry Andric "Cannot create non-first-class values except for constants!"); 680b57cec5SDimitry Andric static_assert(sizeof(Value) == 2 * sizeof(void *) + 2 * sizeof(unsigned), 690b57cec5SDimitry Andric "Value too big"); 700b57cec5SDimitry Andric } 710b57cec5SDimitry Andric 720b57cec5SDimitry Andric Value::~Value() { 730b57cec5SDimitry Andric // Notify all ValueHandles (if present) that this value is going away. 740b57cec5SDimitry Andric if (HasValueHandle) 750b57cec5SDimitry Andric ValueHandleBase::ValueIsDeleted(this); 760b57cec5SDimitry Andric if (isUsedByMetadata()) 770b57cec5SDimitry Andric ValueAsMetadata::handleDeletion(this); 780b57cec5SDimitry Andric 790b57cec5SDimitry Andric #ifndef NDEBUG // Only in -g mode... 800b57cec5SDimitry Andric // Check to make sure that there are no uses of this value that are still 810b57cec5SDimitry Andric // around when the value is destroyed. If there are, then we have a dangling 820b57cec5SDimitry Andric // reference and something is wrong. This code is here to print out where 830b57cec5SDimitry Andric // the value is still being referenced. 840b57cec5SDimitry Andric // 850b57cec5SDimitry Andric if (!use_empty()) { 860b57cec5SDimitry Andric dbgs() << "While deleting: " << *VTy << " %" << getName() << "\n"; 870b57cec5SDimitry Andric for (auto *U : users()) 880b57cec5SDimitry Andric dbgs() << "Use still stuck around after Def is destroyed:" << *U << "\n"; 890b57cec5SDimitry Andric } 900b57cec5SDimitry Andric #endif 910b57cec5SDimitry Andric assert(use_empty() && "Uses remain when a value is destroyed!"); 920b57cec5SDimitry Andric 930b57cec5SDimitry Andric // If this value is named, destroy the name. This should not be in a symtab 940b57cec5SDimitry Andric // at this point. 950b57cec5SDimitry Andric destroyValueName(); 960b57cec5SDimitry Andric } 970b57cec5SDimitry Andric 980b57cec5SDimitry Andric void Value::deleteValue() { 990b57cec5SDimitry Andric switch (getValueID()) { 1000b57cec5SDimitry Andric #define HANDLE_VALUE(Name) \ 1010b57cec5SDimitry Andric case Value::Name##Val: \ 1020b57cec5SDimitry Andric delete static_cast<Name *>(this); \ 1030b57cec5SDimitry Andric break; 1040b57cec5SDimitry Andric #define HANDLE_MEMORY_VALUE(Name) \ 1050b57cec5SDimitry Andric case Value::Name##Val: \ 1060b57cec5SDimitry Andric static_cast<DerivedUser *>(this)->DeleteValue( \ 1070b57cec5SDimitry Andric static_cast<DerivedUser *>(this)); \ 1080b57cec5SDimitry Andric break; 1090b57cec5SDimitry Andric #define HANDLE_INSTRUCTION(Name) /* nothing */ 1100b57cec5SDimitry Andric #include "llvm/IR/Value.def" 1110b57cec5SDimitry Andric 1120b57cec5SDimitry Andric #define HANDLE_INST(N, OPC, CLASS) \ 1130b57cec5SDimitry Andric case Value::InstructionVal + Instruction::OPC: \ 1140b57cec5SDimitry Andric delete static_cast<CLASS *>(this); \ 1150b57cec5SDimitry Andric break; 1160b57cec5SDimitry Andric #define HANDLE_USER_INST(N, OPC, CLASS) 1170b57cec5SDimitry Andric #include "llvm/IR/Instruction.def" 1180b57cec5SDimitry Andric 1190b57cec5SDimitry Andric default: 1200b57cec5SDimitry Andric llvm_unreachable("attempting to delete unknown value kind"); 1210b57cec5SDimitry Andric } 1220b57cec5SDimitry Andric } 1230b57cec5SDimitry Andric 1240b57cec5SDimitry Andric void Value::destroyValueName() { 1250b57cec5SDimitry Andric ValueName *Name = getValueName(); 1260b57cec5SDimitry Andric if (Name) 1270b57cec5SDimitry Andric Name->Destroy(); 1280b57cec5SDimitry Andric setValueName(nullptr); 1290b57cec5SDimitry Andric } 1300b57cec5SDimitry Andric 1310b57cec5SDimitry Andric bool Value::hasNUses(unsigned N) const { 1320b57cec5SDimitry Andric return hasNItems(use_begin(), use_end(), N); 1330b57cec5SDimitry Andric } 1340b57cec5SDimitry Andric 1350b57cec5SDimitry Andric bool Value::hasNUsesOrMore(unsigned N) const { 1360b57cec5SDimitry Andric return hasNItemsOrMore(use_begin(), use_end(), N); 1370b57cec5SDimitry Andric } 1380b57cec5SDimitry Andric 1390b57cec5SDimitry Andric bool Value::isUsedInBasicBlock(const BasicBlock *BB) const { 1400b57cec5SDimitry Andric // This can be computed either by scanning the instructions in BB, or by 1410b57cec5SDimitry Andric // scanning the use list of this Value. Both lists can be very long, but 1420b57cec5SDimitry Andric // usually one is quite short. 1430b57cec5SDimitry Andric // 1440b57cec5SDimitry Andric // Scan both lists simultaneously until one is exhausted. This limits the 1450b57cec5SDimitry Andric // search to the shorter list. 1460b57cec5SDimitry Andric BasicBlock::const_iterator BI = BB->begin(), BE = BB->end(); 1470b57cec5SDimitry Andric const_user_iterator UI = user_begin(), UE = user_end(); 1480b57cec5SDimitry Andric for (; BI != BE && UI != UE; ++BI, ++UI) { 1490b57cec5SDimitry Andric // Scan basic block: Check if this Value is used by the instruction at BI. 1500b57cec5SDimitry Andric if (is_contained(BI->operands(), this)) 1510b57cec5SDimitry Andric return true; 1520b57cec5SDimitry Andric // Scan use list: Check if the use at UI is in BB. 1530b57cec5SDimitry Andric const auto *User = dyn_cast<Instruction>(*UI); 1540b57cec5SDimitry Andric if (User && User->getParent() == BB) 1550b57cec5SDimitry Andric return true; 1560b57cec5SDimitry Andric } 1570b57cec5SDimitry Andric return false; 1580b57cec5SDimitry Andric } 1590b57cec5SDimitry Andric 1600b57cec5SDimitry Andric unsigned Value::getNumUses() const { 1610b57cec5SDimitry Andric return (unsigned)std::distance(use_begin(), use_end()); 1620b57cec5SDimitry Andric } 1630b57cec5SDimitry Andric 1640b57cec5SDimitry Andric static bool getSymTab(Value *V, ValueSymbolTable *&ST) { 1650b57cec5SDimitry Andric ST = nullptr; 1660b57cec5SDimitry Andric if (Instruction *I = dyn_cast<Instruction>(V)) { 1670b57cec5SDimitry Andric if (BasicBlock *P = I->getParent()) 1680b57cec5SDimitry Andric if (Function *PP = P->getParent()) 1690b57cec5SDimitry Andric ST = PP->getValueSymbolTable(); 1700b57cec5SDimitry Andric } else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) { 1710b57cec5SDimitry Andric if (Function *P = BB->getParent()) 1720b57cec5SDimitry Andric ST = P->getValueSymbolTable(); 1730b57cec5SDimitry Andric } else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) { 1740b57cec5SDimitry Andric if (Module *P = GV->getParent()) 1750b57cec5SDimitry Andric ST = &P->getValueSymbolTable(); 1760b57cec5SDimitry Andric } else if (Argument *A = dyn_cast<Argument>(V)) { 1770b57cec5SDimitry Andric if (Function *P = A->getParent()) 1780b57cec5SDimitry Andric ST = P->getValueSymbolTable(); 1790b57cec5SDimitry Andric } else { 1800b57cec5SDimitry Andric assert(isa<Constant>(V) && "Unknown value type!"); 1810b57cec5SDimitry Andric return true; // no name is setable for this. 1820b57cec5SDimitry Andric } 1830b57cec5SDimitry Andric return false; 1840b57cec5SDimitry Andric } 1850b57cec5SDimitry Andric 1860b57cec5SDimitry Andric ValueName *Value::getValueName() const { 1870b57cec5SDimitry Andric if (!HasName) return nullptr; 1880b57cec5SDimitry Andric 1890b57cec5SDimitry Andric LLVMContext &Ctx = getContext(); 1900b57cec5SDimitry Andric auto I = Ctx.pImpl->ValueNames.find(this); 1910b57cec5SDimitry Andric assert(I != Ctx.pImpl->ValueNames.end() && 1920b57cec5SDimitry Andric "No name entry found!"); 1930b57cec5SDimitry Andric 1940b57cec5SDimitry Andric return I->second; 1950b57cec5SDimitry Andric } 1960b57cec5SDimitry Andric 1970b57cec5SDimitry Andric void Value::setValueName(ValueName *VN) { 1980b57cec5SDimitry Andric LLVMContext &Ctx = getContext(); 1990b57cec5SDimitry Andric 2000b57cec5SDimitry Andric assert(HasName == Ctx.pImpl->ValueNames.count(this) && 2010b57cec5SDimitry Andric "HasName bit out of sync!"); 2020b57cec5SDimitry Andric 2030b57cec5SDimitry Andric if (!VN) { 2040b57cec5SDimitry Andric if (HasName) 2050b57cec5SDimitry Andric Ctx.pImpl->ValueNames.erase(this); 2060b57cec5SDimitry Andric HasName = false; 2070b57cec5SDimitry Andric return; 2080b57cec5SDimitry Andric } 2090b57cec5SDimitry Andric 2100b57cec5SDimitry Andric HasName = true; 2110b57cec5SDimitry Andric Ctx.pImpl->ValueNames[this] = VN; 2120b57cec5SDimitry Andric } 2130b57cec5SDimitry Andric 2140b57cec5SDimitry Andric StringRef Value::getName() const { 2150b57cec5SDimitry Andric // Make sure the empty string is still a C string. For historical reasons, 2160b57cec5SDimitry Andric // some clients want to call .data() on the result and expect it to be null 2170b57cec5SDimitry Andric // terminated. 2180b57cec5SDimitry Andric if (!hasName()) 2190b57cec5SDimitry Andric return StringRef("", 0); 2200b57cec5SDimitry Andric return getValueName()->getKey(); 2210b57cec5SDimitry Andric } 2220b57cec5SDimitry Andric 2230b57cec5SDimitry Andric void Value::setNameImpl(const Twine &NewName) { 2240b57cec5SDimitry Andric // Fast-path: LLVMContext can be set to strip out non-GlobalValue names 2250b57cec5SDimitry Andric if (getContext().shouldDiscardValueNames() && !isa<GlobalValue>(this)) 2260b57cec5SDimitry Andric return; 2270b57cec5SDimitry Andric 2280b57cec5SDimitry Andric // Fast path for common IRBuilder case of setName("") when there is no name. 2290b57cec5SDimitry Andric if (NewName.isTriviallyEmpty() && !hasName()) 2300b57cec5SDimitry Andric return; 2310b57cec5SDimitry Andric 2320b57cec5SDimitry Andric SmallString<256> NameData; 2330b57cec5SDimitry Andric StringRef NameRef = NewName.toStringRef(NameData); 2340b57cec5SDimitry Andric assert(NameRef.find_first_of(0) == StringRef::npos && 2350b57cec5SDimitry Andric "Null bytes are not allowed in names"); 2360b57cec5SDimitry Andric 2370b57cec5SDimitry Andric // Name isn't changing? 2380b57cec5SDimitry Andric if (getName() == NameRef) 2390b57cec5SDimitry Andric return; 2400b57cec5SDimitry Andric 2410b57cec5SDimitry Andric // Cap the size of non-GlobalValue names. 2420b57cec5SDimitry Andric if (NameRef.size() > NonGlobalValueMaxNameSize && !isa<GlobalValue>(this)) 2430b57cec5SDimitry Andric NameRef = 2440b57cec5SDimitry Andric NameRef.substr(0, std::max(1u, (unsigned)NonGlobalValueMaxNameSize)); 2450b57cec5SDimitry Andric 2460b57cec5SDimitry Andric assert(!getType()->isVoidTy() && "Cannot assign a name to void values!"); 2470b57cec5SDimitry Andric 2480b57cec5SDimitry Andric // Get the symbol table to update for this object. 2490b57cec5SDimitry Andric ValueSymbolTable *ST; 2500b57cec5SDimitry Andric if (getSymTab(this, ST)) 2510b57cec5SDimitry Andric return; // Cannot set a name on this value (e.g. constant). 2520b57cec5SDimitry Andric 2530b57cec5SDimitry Andric if (!ST) { // No symbol table to update? Just do the change. 2540b57cec5SDimitry Andric if (NameRef.empty()) { 2550b57cec5SDimitry Andric // Free the name for this value. 2560b57cec5SDimitry Andric destroyValueName(); 2570b57cec5SDimitry Andric return; 2580b57cec5SDimitry Andric } 2590b57cec5SDimitry Andric 2600b57cec5SDimitry Andric // NOTE: Could optimize for the case the name is shrinking to not deallocate 2610b57cec5SDimitry Andric // then reallocated. 2620b57cec5SDimitry Andric destroyValueName(); 2630b57cec5SDimitry Andric 2640b57cec5SDimitry Andric // Create the new name. 2650b57cec5SDimitry Andric setValueName(ValueName::Create(NameRef)); 2660b57cec5SDimitry Andric getValueName()->setValue(this); 2670b57cec5SDimitry Andric return; 2680b57cec5SDimitry Andric } 2690b57cec5SDimitry Andric 2700b57cec5SDimitry Andric // NOTE: Could optimize for the case the name is shrinking to not deallocate 2710b57cec5SDimitry Andric // then reallocated. 2720b57cec5SDimitry Andric if (hasName()) { 2730b57cec5SDimitry Andric // Remove old name. 2740b57cec5SDimitry Andric ST->removeValueName(getValueName()); 2750b57cec5SDimitry Andric destroyValueName(); 2760b57cec5SDimitry Andric 2770b57cec5SDimitry Andric if (NameRef.empty()) 2780b57cec5SDimitry Andric return; 2790b57cec5SDimitry Andric } 2800b57cec5SDimitry Andric 2810b57cec5SDimitry Andric // Name is changing to something new. 2820b57cec5SDimitry Andric setValueName(ST->createValueName(NameRef, this)); 2830b57cec5SDimitry Andric } 2840b57cec5SDimitry Andric 2850b57cec5SDimitry Andric void Value::setName(const Twine &NewName) { 2860b57cec5SDimitry Andric setNameImpl(NewName); 2870b57cec5SDimitry Andric if (Function *F = dyn_cast<Function>(this)) 2880b57cec5SDimitry Andric F->recalculateIntrinsicID(); 2890b57cec5SDimitry Andric } 2900b57cec5SDimitry Andric 2910b57cec5SDimitry Andric void Value::takeName(Value *V) { 2920b57cec5SDimitry Andric ValueSymbolTable *ST = nullptr; 2930b57cec5SDimitry Andric // If this value has a name, drop it. 2940b57cec5SDimitry Andric if (hasName()) { 2950b57cec5SDimitry Andric // Get the symtab this is in. 2960b57cec5SDimitry Andric if (getSymTab(this, ST)) { 2970b57cec5SDimitry Andric // We can't set a name on this value, but we need to clear V's name if 2980b57cec5SDimitry Andric // it has one. 2990b57cec5SDimitry Andric if (V->hasName()) V->setName(""); 3000b57cec5SDimitry Andric return; // Cannot set a name on this value (e.g. constant). 3010b57cec5SDimitry Andric } 3020b57cec5SDimitry Andric 3030b57cec5SDimitry Andric // Remove old name. 3040b57cec5SDimitry Andric if (ST) 3050b57cec5SDimitry Andric ST->removeValueName(getValueName()); 3060b57cec5SDimitry Andric destroyValueName(); 3070b57cec5SDimitry Andric } 3080b57cec5SDimitry Andric 3090b57cec5SDimitry Andric // Now we know that this has no name. 3100b57cec5SDimitry Andric 3110b57cec5SDimitry Andric // If V has no name either, we're done. 3120b57cec5SDimitry Andric if (!V->hasName()) return; 3130b57cec5SDimitry Andric 3140b57cec5SDimitry Andric // Get this's symtab if we didn't before. 3150b57cec5SDimitry Andric if (!ST) { 3160b57cec5SDimitry Andric if (getSymTab(this, ST)) { 3170b57cec5SDimitry Andric // Clear V's name. 3180b57cec5SDimitry Andric V->setName(""); 3190b57cec5SDimitry Andric return; // Cannot set a name on this value (e.g. constant). 3200b57cec5SDimitry Andric } 3210b57cec5SDimitry Andric } 3220b57cec5SDimitry Andric 3230b57cec5SDimitry Andric // Get V's ST, this should always succed, because V has a name. 3240b57cec5SDimitry Andric ValueSymbolTable *VST; 3250b57cec5SDimitry Andric bool Failure = getSymTab(V, VST); 3260b57cec5SDimitry Andric assert(!Failure && "V has a name, so it should have a ST!"); (void)Failure; 3270b57cec5SDimitry Andric 3280b57cec5SDimitry Andric // If these values are both in the same symtab, we can do this very fast. 3290b57cec5SDimitry Andric // This works even if both values have no symtab yet. 3300b57cec5SDimitry Andric if (ST == VST) { 3310b57cec5SDimitry Andric // Take the name! 3320b57cec5SDimitry Andric setValueName(V->getValueName()); 3330b57cec5SDimitry Andric V->setValueName(nullptr); 3340b57cec5SDimitry Andric getValueName()->setValue(this); 3350b57cec5SDimitry Andric return; 3360b57cec5SDimitry Andric } 3370b57cec5SDimitry Andric 3380b57cec5SDimitry Andric // Otherwise, things are slightly more complex. Remove V's name from VST and 3390b57cec5SDimitry Andric // then reinsert it into ST. 3400b57cec5SDimitry Andric 3410b57cec5SDimitry Andric if (VST) 3420b57cec5SDimitry Andric VST->removeValueName(V->getValueName()); 3430b57cec5SDimitry Andric setValueName(V->getValueName()); 3440b57cec5SDimitry Andric V->setValueName(nullptr); 3450b57cec5SDimitry Andric getValueName()->setValue(this); 3460b57cec5SDimitry Andric 3470b57cec5SDimitry Andric if (ST) 3480b57cec5SDimitry Andric ST->reinsertValue(this); 3490b57cec5SDimitry Andric } 3500b57cec5SDimitry Andric 3510b57cec5SDimitry Andric void Value::assertModuleIsMaterializedImpl() const { 3520b57cec5SDimitry Andric #ifndef NDEBUG 3530b57cec5SDimitry Andric const GlobalValue *GV = dyn_cast<GlobalValue>(this); 3540b57cec5SDimitry Andric if (!GV) 3550b57cec5SDimitry Andric return; 3560b57cec5SDimitry Andric const Module *M = GV->getParent(); 3570b57cec5SDimitry Andric if (!M) 3580b57cec5SDimitry Andric return; 3590b57cec5SDimitry Andric assert(M->isMaterialized()); 3600b57cec5SDimitry Andric #endif 3610b57cec5SDimitry Andric } 3620b57cec5SDimitry Andric 3630b57cec5SDimitry Andric #ifndef NDEBUG 3640b57cec5SDimitry Andric static bool contains(SmallPtrSetImpl<ConstantExpr *> &Cache, ConstantExpr *Expr, 3650b57cec5SDimitry Andric Constant *C) { 3660b57cec5SDimitry Andric if (!Cache.insert(Expr).second) 3670b57cec5SDimitry Andric return false; 3680b57cec5SDimitry Andric 3690b57cec5SDimitry Andric for (auto &O : Expr->operands()) { 3700b57cec5SDimitry Andric if (O == C) 3710b57cec5SDimitry Andric return true; 3720b57cec5SDimitry Andric auto *CE = dyn_cast<ConstantExpr>(O); 3730b57cec5SDimitry Andric if (!CE) 3740b57cec5SDimitry Andric continue; 3750b57cec5SDimitry Andric if (contains(Cache, CE, C)) 3760b57cec5SDimitry Andric return true; 3770b57cec5SDimitry Andric } 3780b57cec5SDimitry Andric return false; 3790b57cec5SDimitry Andric } 3800b57cec5SDimitry Andric 3810b57cec5SDimitry Andric static bool contains(Value *Expr, Value *V) { 3820b57cec5SDimitry Andric if (Expr == V) 3830b57cec5SDimitry Andric return true; 3840b57cec5SDimitry Andric 3850b57cec5SDimitry Andric auto *C = dyn_cast<Constant>(V); 3860b57cec5SDimitry Andric if (!C) 3870b57cec5SDimitry Andric return false; 3880b57cec5SDimitry Andric 3890b57cec5SDimitry Andric auto *CE = dyn_cast<ConstantExpr>(Expr); 3900b57cec5SDimitry Andric if (!CE) 3910b57cec5SDimitry Andric return false; 3920b57cec5SDimitry Andric 3930b57cec5SDimitry Andric SmallPtrSet<ConstantExpr *, 4> Cache; 3940b57cec5SDimitry Andric return contains(Cache, CE, C); 3950b57cec5SDimitry Andric } 3960b57cec5SDimitry Andric #endif // NDEBUG 3970b57cec5SDimitry Andric 3980b57cec5SDimitry Andric void Value::doRAUW(Value *New, ReplaceMetadataUses ReplaceMetaUses) { 3990b57cec5SDimitry Andric assert(New && "Value::replaceAllUsesWith(<null>) is invalid!"); 4000b57cec5SDimitry Andric assert(!contains(New, this) && 4010b57cec5SDimitry Andric "this->replaceAllUsesWith(expr(this)) is NOT valid!"); 4020b57cec5SDimitry Andric assert(New->getType() == getType() && 4030b57cec5SDimitry Andric "replaceAllUses of value with new value of different type!"); 4040b57cec5SDimitry Andric 4050b57cec5SDimitry Andric // Notify all ValueHandles (if present) that this value is going away. 4060b57cec5SDimitry Andric if (HasValueHandle) 4070b57cec5SDimitry Andric ValueHandleBase::ValueIsRAUWd(this, New); 4080b57cec5SDimitry Andric if (ReplaceMetaUses == ReplaceMetadataUses::Yes && isUsedByMetadata()) 4090b57cec5SDimitry Andric ValueAsMetadata::handleRAUW(this, New); 4100b57cec5SDimitry Andric 4110b57cec5SDimitry Andric while (!materialized_use_empty()) { 4120b57cec5SDimitry Andric Use &U = *UseList; 4130b57cec5SDimitry Andric // Must handle Constants specially, we cannot call replaceUsesOfWith on a 4140b57cec5SDimitry Andric // constant because they are uniqued. 4150b57cec5SDimitry Andric if (auto *C = dyn_cast<Constant>(U.getUser())) { 4160b57cec5SDimitry Andric if (!isa<GlobalValue>(C)) { 4170b57cec5SDimitry Andric C->handleOperandChange(this, New); 4180b57cec5SDimitry Andric continue; 4190b57cec5SDimitry Andric } 4200b57cec5SDimitry Andric } 4210b57cec5SDimitry Andric 4220b57cec5SDimitry Andric U.set(New); 4230b57cec5SDimitry Andric } 4240b57cec5SDimitry Andric 4250b57cec5SDimitry Andric if (BasicBlock *BB = dyn_cast<BasicBlock>(this)) 4260b57cec5SDimitry Andric BB->replaceSuccessorsPhiUsesWith(cast<BasicBlock>(New)); 4270b57cec5SDimitry Andric } 4280b57cec5SDimitry Andric 4290b57cec5SDimitry Andric void Value::replaceAllUsesWith(Value *New) { 4300b57cec5SDimitry Andric doRAUW(New, ReplaceMetadataUses::Yes); 4310b57cec5SDimitry Andric } 4320b57cec5SDimitry Andric 4330b57cec5SDimitry Andric void Value::replaceNonMetadataUsesWith(Value *New) { 4340b57cec5SDimitry Andric doRAUW(New, ReplaceMetadataUses::No); 4350b57cec5SDimitry Andric } 4360b57cec5SDimitry Andric 4370b57cec5SDimitry Andric // Like replaceAllUsesWith except it does not handle constants or basic blocks. 4380b57cec5SDimitry Andric // This routine leaves uses within BB. 4390b57cec5SDimitry Andric void Value::replaceUsesOutsideBlock(Value *New, BasicBlock *BB) { 4400b57cec5SDimitry Andric assert(New && "Value::replaceUsesOutsideBlock(<null>, BB) is invalid!"); 4410b57cec5SDimitry Andric assert(!contains(New, this) && 4420b57cec5SDimitry Andric "this->replaceUsesOutsideBlock(expr(this), BB) is NOT valid!"); 4430b57cec5SDimitry Andric assert(New->getType() == getType() && 4440b57cec5SDimitry Andric "replaceUses of value with new value of different type!"); 4450b57cec5SDimitry Andric assert(BB && "Basic block that may contain a use of 'New' must be defined\n"); 4460b57cec5SDimitry Andric 447*8bcb0991SDimitry Andric replaceUsesWithIf(New, [BB](Use &U) { 448*8bcb0991SDimitry Andric auto *I = dyn_cast<Instruction>(U.getUser()); 449*8bcb0991SDimitry Andric // Don't replace if it's an instruction in the BB basic block. 450*8bcb0991SDimitry Andric return !I || I->getParent() != BB; 451*8bcb0991SDimitry Andric }); 4520b57cec5SDimitry Andric } 4530b57cec5SDimitry Andric 4540b57cec5SDimitry Andric namespace { 4550b57cec5SDimitry Andric // Various metrics for how much to strip off of pointers. 4560b57cec5SDimitry Andric enum PointerStripKind { 4570b57cec5SDimitry Andric PSK_ZeroIndices, 4580b57cec5SDimitry Andric PSK_ZeroIndicesAndAliases, 459*8bcb0991SDimitry Andric PSK_ZeroIndicesSameRepresentation, 460*8bcb0991SDimitry Andric PSK_ZeroIndicesAndInvariantGroups, 4610b57cec5SDimitry Andric PSK_InBoundsConstantIndices, 4620b57cec5SDimitry Andric PSK_InBounds 4630b57cec5SDimitry Andric }; 4640b57cec5SDimitry Andric 4650b57cec5SDimitry Andric template <PointerStripKind StripKind> 4660b57cec5SDimitry Andric static const Value *stripPointerCastsAndOffsets(const Value *V) { 4670b57cec5SDimitry Andric if (!V->getType()->isPointerTy()) 4680b57cec5SDimitry Andric return V; 4690b57cec5SDimitry Andric 4700b57cec5SDimitry Andric // Even though we don't look through PHI nodes, we could be called on an 4710b57cec5SDimitry Andric // instruction in an unreachable block, which may be on a cycle. 4720b57cec5SDimitry Andric SmallPtrSet<const Value *, 4> Visited; 4730b57cec5SDimitry Andric 4740b57cec5SDimitry Andric Visited.insert(V); 4750b57cec5SDimitry Andric do { 4760b57cec5SDimitry Andric if (auto *GEP = dyn_cast<GEPOperator>(V)) { 4770b57cec5SDimitry Andric switch (StripKind) { 4780b57cec5SDimitry Andric case PSK_ZeroIndices: 479*8bcb0991SDimitry Andric case PSK_ZeroIndicesAndAliases: 480*8bcb0991SDimitry Andric case PSK_ZeroIndicesSameRepresentation: 481*8bcb0991SDimitry Andric case PSK_ZeroIndicesAndInvariantGroups: 4820b57cec5SDimitry Andric if (!GEP->hasAllZeroIndices()) 4830b57cec5SDimitry Andric return V; 4840b57cec5SDimitry Andric break; 4850b57cec5SDimitry Andric case PSK_InBoundsConstantIndices: 4860b57cec5SDimitry Andric if (!GEP->hasAllConstantIndices()) 4870b57cec5SDimitry Andric return V; 4880b57cec5SDimitry Andric LLVM_FALLTHROUGH; 4890b57cec5SDimitry Andric case PSK_InBounds: 4900b57cec5SDimitry Andric if (!GEP->isInBounds()) 4910b57cec5SDimitry Andric return V; 4920b57cec5SDimitry Andric break; 4930b57cec5SDimitry Andric } 4940b57cec5SDimitry Andric V = GEP->getPointerOperand(); 4950b57cec5SDimitry Andric } else if (Operator::getOpcode(V) == Instruction::BitCast) { 4960b57cec5SDimitry Andric V = cast<Operator>(V)->getOperand(0); 497*8bcb0991SDimitry Andric } else if (StripKind != PSK_ZeroIndicesSameRepresentation && 4980b57cec5SDimitry Andric Operator::getOpcode(V) == Instruction::AddrSpaceCast) { 4990b57cec5SDimitry Andric // TODO: If we know an address space cast will not change the 5000b57cec5SDimitry Andric // representation we could look through it here as well. 5010b57cec5SDimitry Andric V = cast<Operator>(V)->getOperand(0); 502*8bcb0991SDimitry Andric } else if (StripKind == PSK_ZeroIndicesAndAliases && isa<GlobalAlias>(V)) { 503*8bcb0991SDimitry Andric V = cast<GlobalAlias>(V)->getAliasee(); 5040b57cec5SDimitry Andric } else { 5050b57cec5SDimitry Andric if (const auto *Call = dyn_cast<CallBase>(V)) { 5060b57cec5SDimitry Andric if (const Value *RV = Call->getReturnedArgOperand()) { 5070b57cec5SDimitry Andric V = RV; 5080b57cec5SDimitry Andric continue; 5090b57cec5SDimitry Andric } 5100b57cec5SDimitry Andric // The result of launder.invariant.group must alias it's argument, 5110b57cec5SDimitry Andric // but it can't be marked with returned attribute, that's why it needs 5120b57cec5SDimitry Andric // special case. 513*8bcb0991SDimitry Andric if (StripKind == PSK_ZeroIndicesAndInvariantGroups && 5140b57cec5SDimitry Andric (Call->getIntrinsicID() == Intrinsic::launder_invariant_group || 5150b57cec5SDimitry Andric Call->getIntrinsicID() == Intrinsic::strip_invariant_group)) { 5160b57cec5SDimitry Andric V = Call->getArgOperand(0); 5170b57cec5SDimitry Andric continue; 5180b57cec5SDimitry Andric } 5190b57cec5SDimitry Andric } 5200b57cec5SDimitry Andric return V; 5210b57cec5SDimitry Andric } 5220b57cec5SDimitry Andric assert(V->getType()->isPointerTy() && "Unexpected operand type!"); 5230b57cec5SDimitry Andric } while (Visited.insert(V).second); 5240b57cec5SDimitry Andric 5250b57cec5SDimitry Andric return V; 5260b57cec5SDimitry Andric } 5270b57cec5SDimitry Andric } // end anonymous namespace 5280b57cec5SDimitry Andric 5290b57cec5SDimitry Andric const Value *Value::stripPointerCasts() const { 530*8bcb0991SDimitry Andric return stripPointerCastsAndOffsets<PSK_ZeroIndices>(this); 531*8bcb0991SDimitry Andric } 532*8bcb0991SDimitry Andric 533*8bcb0991SDimitry Andric const Value *Value::stripPointerCastsAndAliases() const { 5340b57cec5SDimitry Andric return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndAliases>(this); 5350b57cec5SDimitry Andric } 5360b57cec5SDimitry Andric 5370b57cec5SDimitry Andric const Value *Value::stripPointerCastsSameRepresentation() const { 538*8bcb0991SDimitry Andric return stripPointerCastsAndOffsets<PSK_ZeroIndicesSameRepresentation>(this); 5390b57cec5SDimitry Andric } 5400b57cec5SDimitry Andric 5410b57cec5SDimitry Andric const Value *Value::stripInBoundsConstantOffsets() const { 5420b57cec5SDimitry Andric return stripPointerCastsAndOffsets<PSK_InBoundsConstantIndices>(this); 5430b57cec5SDimitry Andric } 5440b57cec5SDimitry Andric 5450b57cec5SDimitry Andric const Value *Value::stripPointerCastsAndInvariantGroups() const { 546*8bcb0991SDimitry Andric return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndInvariantGroups>(this); 5470b57cec5SDimitry Andric } 5480b57cec5SDimitry Andric 5490b57cec5SDimitry Andric const Value * 5500b57cec5SDimitry Andric Value::stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, 5510b57cec5SDimitry Andric bool AllowNonInbounds) const { 5520b57cec5SDimitry Andric if (!getType()->isPtrOrPtrVectorTy()) 5530b57cec5SDimitry Andric return this; 5540b57cec5SDimitry Andric 5550b57cec5SDimitry Andric unsigned BitWidth = Offset.getBitWidth(); 5560b57cec5SDimitry Andric assert(BitWidth == DL.getIndexTypeSizeInBits(getType()) && 5570b57cec5SDimitry Andric "The offset bit width does not match the DL specification."); 5580b57cec5SDimitry Andric 5590b57cec5SDimitry Andric // Even though we don't look through PHI nodes, we could be called on an 5600b57cec5SDimitry Andric // instruction in an unreachable block, which may be on a cycle. 5610b57cec5SDimitry Andric SmallPtrSet<const Value *, 4> Visited; 5620b57cec5SDimitry Andric Visited.insert(this); 5630b57cec5SDimitry Andric const Value *V = this; 5640b57cec5SDimitry Andric do { 5650b57cec5SDimitry Andric if (auto *GEP = dyn_cast<GEPOperator>(V)) { 5660b57cec5SDimitry Andric // If in-bounds was requested, we do not strip non-in-bounds GEPs. 5670b57cec5SDimitry Andric if (!AllowNonInbounds && !GEP->isInBounds()) 5680b57cec5SDimitry Andric return V; 5690b57cec5SDimitry Andric 5700b57cec5SDimitry Andric // If one of the values we have visited is an addrspacecast, then 5710b57cec5SDimitry Andric // the pointer type of this GEP may be different from the type 5720b57cec5SDimitry Andric // of the Ptr parameter which was passed to this function. This 5730b57cec5SDimitry Andric // means when we construct GEPOffset, we need to use the size 5740b57cec5SDimitry Andric // of GEP's pointer type rather than the size of the original 5750b57cec5SDimitry Andric // pointer type. 5760b57cec5SDimitry Andric APInt GEPOffset(DL.getIndexTypeSizeInBits(V->getType()), 0); 5770b57cec5SDimitry Andric if (!GEP->accumulateConstantOffset(DL, GEPOffset)) 5780b57cec5SDimitry Andric return V; 5790b57cec5SDimitry Andric 5800b57cec5SDimitry Andric // Stop traversal if the pointer offset wouldn't fit in the bit-width 5810b57cec5SDimitry Andric // provided by the Offset argument. This can happen due to AddrSpaceCast 5820b57cec5SDimitry Andric // stripping. 5830b57cec5SDimitry Andric if (GEPOffset.getMinSignedBits() > BitWidth) 5840b57cec5SDimitry Andric return V; 5850b57cec5SDimitry Andric 5860b57cec5SDimitry Andric Offset += GEPOffset.sextOrTrunc(BitWidth); 5870b57cec5SDimitry Andric V = GEP->getPointerOperand(); 5880b57cec5SDimitry Andric } else if (Operator::getOpcode(V) == Instruction::BitCast || 5890b57cec5SDimitry Andric Operator::getOpcode(V) == Instruction::AddrSpaceCast) { 5900b57cec5SDimitry Andric V = cast<Operator>(V)->getOperand(0); 5910b57cec5SDimitry Andric } else if (auto *GA = dyn_cast<GlobalAlias>(V)) { 5920b57cec5SDimitry Andric if (!GA->isInterposable()) 5930b57cec5SDimitry Andric V = GA->getAliasee(); 5940b57cec5SDimitry Andric } else if (const auto *Call = dyn_cast<CallBase>(V)) { 5950b57cec5SDimitry Andric if (const Value *RV = Call->getReturnedArgOperand()) 5960b57cec5SDimitry Andric V = RV; 5970b57cec5SDimitry Andric } 5980b57cec5SDimitry Andric assert(V->getType()->isPtrOrPtrVectorTy() && "Unexpected operand type!"); 5990b57cec5SDimitry Andric } while (Visited.insert(V).second); 6000b57cec5SDimitry Andric 6010b57cec5SDimitry Andric return V; 6020b57cec5SDimitry Andric } 6030b57cec5SDimitry Andric 6040b57cec5SDimitry Andric const Value *Value::stripInBoundsOffsets() const { 6050b57cec5SDimitry Andric return stripPointerCastsAndOffsets<PSK_InBounds>(this); 6060b57cec5SDimitry Andric } 6070b57cec5SDimitry Andric 6080b57cec5SDimitry Andric uint64_t Value::getPointerDereferenceableBytes(const DataLayout &DL, 6090b57cec5SDimitry Andric bool &CanBeNull) const { 6100b57cec5SDimitry Andric assert(getType()->isPointerTy() && "must be pointer"); 6110b57cec5SDimitry Andric 6120b57cec5SDimitry Andric uint64_t DerefBytes = 0; 6130b57cec5SDimitry Andric CanBeNull = false; 6140b57cec5SDimitry Andric if (const Argument *A = dyn_cast<Argument>(this)) { 6150b57cec5SDimitry Andric DerefBytes = A->getDereferenceableBytes(); 6160b57cec5SDimitry Andric if (DerefBytes == 0 && (A->hasByValAttr() || A->hasStructRetAttr())) { 6170b57cec5SDimitry Andric Type *PT = cast<PointerType>(A->getType())->getElementType(); 6180b57cec5SDimitry Andric if (PT->isSized()) 6190b57cec5SDimitry Andric DerefBytes = DL.getTypeStoreSize(PT); 6200b57cec5SDimitry Andric } 6210b57cec5SDimitry Andric if (DerefBytes == 0) { 6220b57cec5SDimitry Andric DerefBytes = A->getDereferenceableOrNullBytes(); 6230b57cec5SDimitry Andric CanBeNull = true; 6240b57cec5SDimitry Andric } 6250b57cec5SDimitry Andric } else if (const auto *Call = dyn_cast<CallBase>(this)) { 6260b57cec5SDimitry Andric DerefBytes = Call->getDereferenceableBytes(AttributeList::ReturnIndex); 6270b57cec5SDimitry Andric if (DerefBytes == 0) { 6280b57cec5SDimitry Andric DerefBytes = 6290b57cec5SDimitry Andric Call->getDereferenceableOrNullBytes(AttributeList::ReturnIndex); 6300b57cec5SDimitry Andric CanBeNull = true; 6310b57cec5SDimitry Andric } 6320b57cec5SDimitry Andric } else if (const LoadInst *LI = dyn_cast<LoadInst>(this)) { 6330b57cec5SDimitry Andric if (MDNode *MD = LI->getMetadata(LLVMContext::MD_dereferenceable)) { 6340b57cec5SDimitry Andric ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0)); 6350b57cec5SDimitry Andric DerefBytes = CI->getLimitedValue(); 6360b57cec5SDimitry Andric } 6370b57cec5SDimitry Andric if (DerefBytes == 0) { 6380b57cec5SDimitry Andric if (MDNode *MD = 6390b57cec5SDimitry Andric LI->getMetadata(LLVMContext::MD_dereferenceable_or_null)) { 6400b57cec5SDimitry Andric ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0)); 6410b57cec5SDimitry Andric DerefBytes = CI->getLimitedValue(); 6420b57cec5SDimitry Andric } 6430b57cec5SDimitry Andric CanBeNull = true; 6440b57cec5SDimitry Andric } 645*8bcb0991SDimitry Andric } else if (auto *IP = dyn_cast<IntToPtrInst>(this)) { 646*8bcb0991SDimitry Andric if (MDNode *MD = IP->getMetadata(LLVMContext::MD_dereferenceable)) { 647*8bcb0991SDimitry Andric ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0)); 648*8bcb0991SDimitry Andric DerefBytes = CI->getLimitedValue(); 649*8bcb0991SDimitry Andric } 650*8bcb0991SDimitry Andric if (DerefBytes == 0) { 651*8bcb0991SDimitry Andric if (MDNode *MD = 652*8bcb0991SDimitry Andric IP->getMetadata(LLVMContext::MD_dereferenceable_or_null)) { 653*8bcb0991SDimitry Andric ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0)); 654*8bcb0991SDimitry Andric DerefBytes = CI->getLimitedValue(); 655*8bcb0991SDimitry Andric } 656*8bcb0991SDimitry Andric CanBeNull = true; 657*8bcb0991SDimitry Andric } 6580b57cec5SDimitry Andric } else if (auto *AI = dyn_cast<AllocaInst>(this)) { 6590b57cec5SDimitry Andric if (!AI->isArrayAllocation()) { 6600b57cec5SDimitry Andric DerefBytes = DL.getTypeStoreSize(AI->getAllocatedType()); 6610b57cec5SDimitry Andric CanBeNull = false; 6620b57cec5SDimitry Andric } 6630b57cec5SDimitry Andric } else if (auto *GV = dyn_cast<GlobalVariable>(this)) { 6640b57cec5SDimitry Andric if (GV->getValueType()->isSized() && !GV->hasExternalWeakLinkage()) { 6650b57cec5SDimitry Andric // TODO: Don't outright reject hasExternalWeakLinkage but set the 6660b57cec5SDimitry Andric // CanBeNull flag. 6670b57cec5SDimitry Andric DerefBytes = DL.getTypeStoreSize(GV->getValueType()); 6680b57cec5SDimitry Andric CanBeNull = false; 6690b57cec5SDimitry Andric } 6700b57cec5SDimitry Andric } 6710b57cec5SDimitry Andric return DerefBytes; 6720b57cec5SDimitry Andric } 6730b57cec5SDimitry Andric 674*8bcb0991SDimitry Andric MaybeAlign Value::getPointerAlignment(const DataLayout &DL) const { 6750b57cec5SDimitry Andric assert(getType()->isPointerTy() && "must be pointer"); 6760b57cec5SDimitry Andric if (auto *GO = dyn_cast<GlobalObject>(this)) { 6770b57cec5SDimitry Andric if (isa<Function>(GO)) { 678*8bcb0991SDimitry Andric const MaybeAlign FunctionPtrAlign = DL.getFunctionPtrAlign(); 6790b57cec5SDimitry Andric switch (DL.getFunctionPtrAlignType()) { 6800b57cec5SDimitry Andric case DataLayout::FunctionPtrAlignType::Independent: 681*8bcb0991SDimitry Andric return FunctionPtrAlign; 6820b57cec5SDimitry Andric case DataLayout::FunctionPtrAlignType::MultipleOfFunctionAlign: 683*8bcb0991SDimitry Andric return std::max(FunctionPtrAlign, MaybeAlign(GO->getAlignment())); 6840b57cec5SDimitry Andric } 685*8bcb0991SDimitry Andric llvm_unreachable("Unhandled FunctionPtrAlignType"); 6860b57cec5SDimitry Andric } 687*8bcb0991SDimitry Andric const MaybeAlign Alignment(GO->getAlignment()); 688*8bcb0991SDimitry Andric if (!Alignment) { 6890b57cec5SDimitry Andric if (auto *GVar = dyn_cast<GlobalVariable>(GO)) { 6900b57cec5SDimitry Andric Type *ObjectType = GVar->getValueType(); 6910b57cec5SDimitry Andric if (ObjectType->isSized()) { 6920b57cec5SDimitry Andric // If the object is defined in the current Module, we'll be giving 6930b57cec5SDimitry Andric // it the preferred alignment. Otherwise, we have to assume that it 6940b57cec5SDimitry Andric // may only have the minimum ABI alignment. 6950b57cec5SDimitry Andric if (GVar->isStrongDefinitionForLinker()) 696*8bcb0991SDimitry Andric return MaybeAlign(DL.getPreferredAlignment(GVar)); 6970b57cec5SDimitry Andric else 698*8bcb0991SDimitry Andric return Align(DL.getABITypeAlignment(ObjectType)); 6990b57cec5SDimitry Andric } 7000b57cec5SDimitry Andric } 7010b57cec5SDimitry Andric } 702*8bcb0991SDimitry Andric return Alignment; 7030b57cec5SDimitry Andric } else if (const Argument *A = dyn_cast<Argument>(this)) { 704*8bcb0991SDimitry Andric const MaybeAlign Alignment(A->getParamAlignment()); 705*8bcb0991SDimitry Andric if (!Alignment && A->hasStructRetAttr()) { 7060b57cec5SDimitry Andric // An sret parameter has at least the ABI alignment of the return type. 7070b57cec5SDimitry Andric Type *EltTy = cast<PointerType>(A->getType())->getElementType(); 7080b57cec5SDimitry Andric if (EltTy->isSized()) 709*8bcb0991SDimitry Andric return Align(DL.getABITypeAlignment(EltTy)); 7100b57cec5SDimitry Andric } 711*8bcb0991SDimitry Andric return Alignment; 7120b57cec5SDimitry Andric } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(this)) { 713*8bcb0991SDimitry Andric const MaybeAlign Alignment(AI->getAlignment()); 714*8bcb0991SDimitry Andric if (!Alignment) { 7150b57cec5SDimitry Andric Type *AllocatedType = AI->getAllocatedType(); 7160b57cec5SDimitry Andric if (AllocatedType->isSized()) 717*8bcb0991SDimitry Andric return MaybeAlign(DL.getPrefTypeAlignment(AllocatedType)); 7180b57cec5SDimitry Andric } 719*8bcb0991SDimitry Andric return Alignment; 720*8bcb0991SDimitry Andric } else if (const auto *Call = dyn_cast<CallBase>(this)) { 721*8bcb0991SDimitry Andric const MaybeAlign Alignment(Call->getRetAlignment()); 722*8bcb0991SDimitry Andric if (!Alignment && Call->getCalledFunction()) 723*8bcb0991SDimitry Andric return MaybeAlign( 724*8bcb0991SDimitry Andric Call->getCalledFunction()->getAttributes().getRetAlignment()); 725*8bcb0991SDimitry Andric return Alignment; 726*8bcb0991SDimitry Andric } else if (const LoadInst *LI = dyn_cast<LoadInst>(this)) { 7270b57cec5SDimitry Andric if (MDNode *MD = LI->getMetadata(LLVMContext::MD_align)) { 7280b57cec5SDimitry Andric ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0)); 729*8bcb0991SDimitry Andric return MaybeAlign(CI->getLimitedValue()); 7300b57cec5SDimitry Andric } 731*8bcb0991SDimitry Andric } 732*8bcb0991SDimitry Andric return llvm::None; 7330b57cec5SDimitry Andric } 7340b57cec5SDimitry Andric 7350b57cec5SDimitry Andric const Value *Value::DoPHITranslation(const BasicBlock *CurBB, 7360b57cec5SDimitry Andric const BasicBlock *PredBB) const { 7370b57cec5SDimitry Andric auto *PN = dyn_cast<PHINode>(this); 7380b57cec5SDimitry Andric if (PN && PN->getParent() == CurBB) 7390b57cec5SDimitry Andric return PN->getIncomingValueForBlock(PredBB); 7400b57cec5SDimitry Andric return this; 7410b57cec5SDimitry Andric } 7420b57cec5SDimitry Andric 7430b57cec5SDimitry Andric LLVMContext &Value::getContext() const { return VTy->getContext(); } 7440b57cec5SDimitry Andric 7450b57cec5SDimitry Andric void Value::reverseUseList() { 7460b57cec5SDimitry Andric if (!UseList || !UseList->Next) 7470b57cec5SDimitry Andric // No need to reverse 0 or 1 uses. 7480b57cec5SDimitry Andric return; 7490b57cec5SDimitry Andric 7500b57cec5SDimitry Andric Use *Head = UseList; 7510b57cec5SDimitry Andric Use *Current = UseList->Next; 7520b57cec5SDimitry Andric Head->Next = nullptr; 7530b57cec5SDimitry Andric while (Current) { 7540b57cec5SDimitry Andric Use *Next = Current->Next; 7550b57cec5SDimitry Andric Current->Next = Head; 7560b57cec5SDimitry Andric Head->setPrev(&Current->Next); 7570b57cec5SDimitry Andric Head = Current; 7580b57cec5SDimitry Andric Current = Next; 7590b57cec5SDimitry Andric } 7600b57cec5SDimitry Andric UseList = Head; 7610b57cec5SDimitry Andric Head->setPrev(&UseList); 7620b57cec5SDimitry Andric } 7630b57cec5SDimitry Andric 7640b57cec5SDimitry Andric bool Value::isSwiftError() const { 7650b57cec5SDimitry Andric auto *Arg = dyn_cast<Argument>(this); 7660b57cec5SDimitry Andric if (Arg) 7670b57cec5SDimitry Andric return Arg->hasSwiftErrorAttr(); 7680b57cec5SDimitry Andric auto *Alloca = dyn_cast<AllocaInst>(this); 7690b57cec5SDimitry Andric if (!Alloca) 7700b57cec5SDimitry Andric return false; 7710b57cec5SDimitry Andric return Alloca->isSwiftError(); 7720b57cec5SDimitry Andric } 7730b57cec5SDimitry Andric 7740b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 7750b57cec5SDimitry Andric // ValueHandleBase Class 7760b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 7770b57cec5SDimitry Andric 7780b57cec5SDimitry Andric void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) { 7790b57cec5SDimitry Andric assert(List && "Handle list is null?"); 7800b57cec5SDimitry Andric 7810b57cec5SDimitry Andric // Splice ourselves into the list. 7820b57cec5SDimitry Andric Next = *List; 7830b57cec5SDimitry Andric *List = this; 7840b57cec5SDimitry Andric setPrevPtr(List); 7850b57cec5SDimitry Andric if (Next) { 7860b57cec5SDimitry Andric Next->setPrevPtr(&Next); 7870b57cec5SDimitry Andric assert(getValPtr() == Next->getValPtr() && "Added to wrong list?"); 7880b57cec5SDimitry Andric } 7890b57cec5SDimitry Andric } 7900b57cec5SDimitry Andric 7910b57cec5SDimitry Andric void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) { 7920b57cec5SDimitry Andric assert(List && "Must insert after existing node"); 7930b57cec5SDimitry Andric 7940b57cec5SDimitry Andric Next = List->Next; 7950b57cec5SDimitry Andric setPrevPtr(&List->Next); 7960b57cec5SDimitry Andric List->Next = this; 7970b57cec5SDimitry Andric if (Next) 7980b57cec5SDimitry Andric Next->setPrevPtr(&Next); 7990b57cec5SDimitry Andric } 8000b57cec5SDimitry Andric 8010b57cec5SDimitry Andric void ValueHandleBase::AddToUseList() { 8020b57cec5SDimitry Andric assert(getValPtr() && "Null pointer doesn't have a use list!"); 8030b57cec5SDimitry Andric 8040b57cec5SDimitry Andric LLVMContextImpl *pImpl = getValPtr()->getContext().pImpl; 8050b57cec5SDimitry Andric 8060b57cec5SDimitry Andric if (getValPtr()->HasValueHandle) { 8070b57cec5SDimitry Andric // If this value already has a ValueHandle, then it must be in the 8080b57cec5SDimitry Andric // ValueHandles map already. 8090b57cec5SDimitry Andric ValueHandleBase *&Entry = pImpl->ValueHandles[getValPtr()]; 8100b57cec5SDimitry Andric assert(Entry && "Value doesn't have any handles?"); 8110b57cec5SDimitry Andric AddToExistingUseList(&Entry); 8120b57cec5SDimitry Andric return; 8130b57cec5SDimitry Andric } 8140b57cec5SDimitry Andric 8150b57cec5SDimitry Andric // Ok, it doesn't have any handles yet, so we must insert it into the 8160b57cec5SDimitry Andric // DenseMap. However, doing this insertion could cause the DenseMap to 8170b57cec5SDimitry Andric // reallocate itself, which would invalidate all of the PrevP pointers that 8180b57cec5SDimitry Andric // point into the old table. Handle this by checking for reallocation and 8190b57cec5SDimitry Andric // updating the stale pointers only if needed. 8200b57cec5SDimitry Andric DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles; 8210b57cec5SDimitry Andric const void *OldBucketPtr = Handles.getPointerIntoBucketsArray(); 8220b57cec5SDimitry Andric 8230b57cec5SDimitry Andric ValueHandleBase *&Entry = Handles[getValPtr()]; 8240b57cec5SDimitry Andric assert(!Entry && "Value really did already have handles?"); 8250b57cec5SDimitry Andric AddToExistingUseList(&Entry); 8260b57cec5SDimitry Andric getValPtr()->HasValueHandle = true; 8270b57cec5SDimitry Andric 8280b57cec5SDimitry Andric // If reallocation didn't happen or if this was the first insertion, don't 8290b57cec5SDimitry Andric // walk the table. 8300b57cec5SDimitry Andric if (Handles.isPointerIntoBucketsArray(OldBucketPtr) || 8310b57cec5SDimitry Andric Handles.size() == 1) { 8320b57cec5SDimitry Andric return; 8330b57cec5SDimitry Andric } 8340b57cec5SDimitry Andric 8350b57cec5SDimitry Andric // Okay, reallocation did happen. Fix the Prev Pointers. 8360b57cec5SDimitry Andric for (DenseMap<Value*, ValueHandleBase*>::iterator I = Handles.begin(), 8370b57cec5SDimitry Andric E = Handles.end(); I != E; ++I) { 8380b57cec5SDimitry Andric assert(I->second && I->first == I->second->getValPtr() && 8390b57cec5SDimitry Andric "List invariant broken!"); 8400b57cec5SDimitry Andric I->second->setPrevPtr(&I->second); 8410b57cec5SDimitry Andric } 8420b57cec5SDimitry Andric } 8430b57cec5SDimitry Andric 8440b57cec5SDimitry Andric void ValueHandleBase::RemoveFromUseList() { 8450b57cec5SDimitry Andric assert(getValPtr() && getValPtr()->HasValueHandle && 8460b57cec5SDimitry Andric "Pointer doesn't have a use list!"); 8470b57cec5SDimitry Andric 8480b57cec5SDimitry Andric // Unlink this from its use list. 8490b57cec5SDimitry Andric ValueHandleBase **PrevPtr = getPrevPtr(); 8500b57cec5SDimitry Andric assert(*PrevPtr == this && "List invariant broken"); 8510b57cec5SDimitry Andric 8520b57cec5SDimitry Andric *PrevPtr = Next; 8530b57cec5SDimitry Andric if (Next) { 8540b57cec5SDimitry Andric assert(Next->getPrevPtr() == &Next && "List invariant broken"); 8550b57cec5SDimitry Andric Next->setPrevPtr(PrevPtr); 8560b57cec5SDimitry Andric return; 8570b57cec5SDimitry Andric } 8580b57cec5SDimitry Andric 8590b57cec5SDimitry Andric // If the Next pointer was null, then it is possible that this was the last 8600b57cec5SDimitry Andric // ValueHandle watching VP. If so, delete its entry from the ValueHandles 8610b57cec5SDimitry Andric // map. 8620b57cec5SDimitry Andric LLVMContextImpl *pImpl = getValPtr()->getContext().pImpl; 8630b57cec5SDimitry Andric DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles; 8640b57cec5SDimitry Andric if (Handles.isPointerIntoBucketsArray(PrevPtr)) { 8650b57cec5SDimitry Andric Handles.erase(getValPtr()); 8660b57cec5SDimitry Andric getValPtr()->HasValueHandle = false; 8670b57cec5SDimitry Andric } 8680b57cec5SDimitry Andric } 8690b57cec5SDimitry Andric 8700b57cec5SDimitry Andric void ValueHandleBase::ValueIsDeleted(Value *V) { 8710b57cec5SDimitry Andric assert(V->HasValueHandle && "Should only be called if ValueHandles present"); 8720b57cec5SDimitry Andric 8730b57cec5SDimitry Andric // Get the linked list base, which is guaranteed to exist since the 8740b57cec5SDimitry Andric // HasValueHandle flag is set. 8750b57cec5SDimitry Andric LLVMContextImpl *pImpl = V->getContext().pImpl; 8760b57cec5SDimitry Andric ValueHandleBase *Entry = pImpl->ValueHandles[V]; 8770b57cec5SDimitry Andric assert(Entry && "Value bit set but no entries exist"); 8780b57cec5SDimitry Andric 8790b57cec5SDimitry Andric // We use a local ValueHandleBase as an iterator so that ValueHandles can add 8800b57cec5SDimitry Andric // and remove themselves from the list without breaking our iteration. This 8810b57cec5SDimitry Andric // is not really an AssertingVH; we just have to give ValueHandleBase a kind. 8820b57cec5SDimitry Andric // Note that we deliberately do not the support the case when dropping a value 8830b57cec5SDimitry Andric // handle results in a new value handle being permanently added to the list 8840b57cec5SDimitry Andric // (as might occur in theory for CallbackVH's): the new value handle will not 8850b57cec5SDimitry Andric // be processed and the checking code will mete out righteous punishment if 8860b57cec5SDimitry Andric // the handle is still present once we have finished processing all the other 8870b57cec5SDimitry Andric // value handles (it is fine to momentarily add then remove a value handle). 8880b57cec5SDimitry Andric for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) { 8890b57cec5SDimitry Andric Iterator.RemoveFromUseList(); 8900b57cec5SDimitry Andric Iterator.AddToExistingUseListAfter(Entry); 8910b57cec5SDimitry Andric assert(Entry->Next == &Iterator && "Loop invariant broken."); 8920b57cec5SDimitry Andric 8930b57cec5SDimitry Andric switch (Entry->getKind()) { 8940b57cec5SDimitry Andric case Assert: 8950b57cec5SDimitry Andric break; 8960b57cec5SDimitry Andric case Weak: 8970b57cec5SDimitry Andric case WeakTracking: 8980b57cec5SDimitry Andric // WeakTracking and Weak just go to null, which unlinks them 8990b57cec5SDimitry Andric // from the list. 9000b57cec5SDimitry Andric Entry->operator=(nullptr); 9010b57cec5SDimitry Andric break; 9020b57cec5SDimitry Andric case Callback: 9030b57cec5SDimitry Andric // Forward to the subclass's implementation. 9040b57cec5SDimitry Andric static_cast<CallbackVH*>(Entry)->deleted(); 9050b57cec5SDimitry Andric break; 9060b57cec5SDimitry Andric } 9070b57cec5SDimitry Andric } 9080b57cec5SDimitry Andric 9090b57cec5SDimitry Andric // All callbacks, weak references, and assertingVHs should be dropped by now. 9100b57cec5SDimitry Andric if (V->HasValueHandle) { 9110b57cec5SDimitry Andric #ifndef NDEBUG // Only in +Asserts mode... 9120b57cec5SDimitry Andric dbgs() << "While deleting: " << *V->getType() << " %" << V->getName() 9130b57cec5SDimitry Andric << "\n"; 9140b57cec5SDimitry Andric if (pImpl->ValueHandles[V]->getKind() == Assert) 9150b57cec5SDimitry Andric llvm_unreachable("An asserting value handle still pointed to this" 9160b57cec5SDimitry Andric " value!"); 9170b57cec5SDimitry Andric 9180b57cec5SDimitry Andric #endif 9190b57cec5SDimitry Andric llvm_unreachable("All references to V were not removed?"); 9200b57cec5SDimitry Andric } 9210b57cec5SDimitry Andric } 9220b57cec5SDimitry Andric 9230b57cec5SDimitry Andric void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) { 9240b57cec5SDimitry Andric assert(Old->HasValueHandle &&"Should only be called if ValueHandles present"); 9250b57cec5SDimitry Andric assert(Old != New && "Changing value into itself!"); 9260b57cec5SDimitry Andric assert(Old->getType() == New->getType() && 9270b57cec5SDimitry Andric "replaceAllUses of value with new value of different type!"); 9280b57cec5SDimitry Andric 9290b57cec5SDimitry Andric // Get the linked list base, which is guaranteed to exist since the 9300b57cec5SDimitry Andric // HasValueHandle flag is set. 9310b57cec5SDimitry Andric LLVMContextImpl *pImpl = Old->getContext().pImpl; 9320b57cec5SDimitry Andric ValueHandleBase *Entry = pImpl->ValueHandles[Old]; 9330b57cec5SDimitry Andric 9340b57cec5SDimitry Andric assert(Entry && "Value bit set but no entries exist"); 9350b57cec5SDimitry Andric 9360b57cec5SDimitry Andric // We use a local ValueHandleBase as an iterator so that 9370b57cec5SDimitry Andric // ValueHandles can add and remove themselves from the list without 9380b57cec5SDimitry Andric // breaking our iteration. This is not really an AssertingVH; we 9390b57cec5SDimitry Andric // just have to give ValueHandleBase some kind. 9400b57cec5SDimitry Andric for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) { 9410b57cec5SDimitry Andric Iterator.RemoveFromUseList(); 9420b57cec5SDimitry Andric Iterator.AddToExistingUseListAfter(Entry); 9430b57cec5SDimitry Andric assert(Entry->Next == &Iterator && "Loop invariant broken."); 9440b57cec5SDimitry Andric 9450b57cec5SDimitry Andric switch (Entry->getKind()) { 9460b57cec5SDimitry Andric case Assert: 9470b57cec5SDimitry Andric case Weak: 9480b57cec5SDimitry Andric // Asserting and Weak handles do not follow RAUW implicitly. 9490b57cec5SDimitry Andric break; 9500b57cec5SDimitry Andric case WeakTracking: 9510b57cec5SDimitry Andric // Weak goes to the new value, which will unlink it from Old's list. 9520b57cec5SDimitry Andric Entry->operator=(New); 9530b57cec5SDimitry Andric break; 9540b57cec5SDimitry Andric case Callback: 9550b57cec5SDimitry Andric // Forward to the subclass's implementation. 9560b57cec5SDimitry Andric static_cast<CallbackVH*>(Entry)->allUsesReplacedWith(New); 9570b57cec5SDimitry Andric break; 9580b57cec5SDimitry Andric } 9590b57cec5SDimitry Andric } 9600b57cec5SDimitry Andric 9610b57cec5SDimitry Andric #ifndef NDEBUG 9620b57cec5SDimitry Andric // If any new weak value handles were added while processing the 9630b57cec5SDimitry Andric // list, then complain about it now. 9640b57cec5SDimitry Andric if (Old->HasValueHandle) 9650b57cec5SDimitry Andric for (Entry = pImpl->ValueHandles[Old]; Entry; Entry = Entry->Next) 9660b57cec5SDimitry Andric switch (Entry->getKind()) { 9670b57cec5SDimitry Andric case WeakTracking: 9680b57cec5SDimitry Andric dbgs() << "After RAUW from " << *Old->getType() << " %" 9690b57cec5SDimitry Andric << Old->getName() << " to " << *New->getType() << " %" 9700b57cec5SDimitry Andric << New->getName() << "\n"; 9710b57cec5SDimitry Andric llvm_unreachable( 9720b57cec5SDimitry Andric "A weak tracking value handle still pointed to the old value!\n"); 9730b57cec5SDimitry Andric default: 9740b57cec5SDimitry Andric break; 9750b57cec5SDimitry Andric } 9760b57cec5SDimitry Andric #endif 9770b57cec5SDimitry Andric } 9780b57cec5SDimitry Andric 9790b57cec5SDimitry Andric // Pin the vtable to this file. 9800b57cec5SDimitry Andric void CallbackVH::anchor() {} 981