#include "llvm/IR/Value.h"
#include "LLVMContextImpl.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/IR/Constant.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DebugInfo.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/DerivedUser.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Operator.h"
#include "llvm/IR/ValueHandle.h"
#include "llvm/IR/ValueSymbolTable.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
using namespace llvm;
static cl::opt<unsigned> UseDerefAtPointSemantics(
"use-dereferenceable-at-point-semantics", cl::Hidden, cl::init(false),
cl::desc("Deref attributes and metadata infer facts at definition only"));
static inline Type *checkType(Type *Ty) {
assert(Ty && "Value defined with a null type: Error!");
return Ty;
}
Value::Value(Type *ty, unsigned scid)
: VTy(checkType(ty)), UseList(nullptr), SubclassID(scid), HasValueHandle(0),
SubclassOptionalData(0), SubclassData(0), NumUserOperands(0),
IsUsedByMD(false), HasName(false), HasMetadata(false) {
static_assert(ConstantFirstVal == 0, "!(SubclassID < ConstantFirstVal)");
unsigned OpCode = 0;
if (SubclassID >= InstructionVal)
OpCode = SubclassID - InstructionVal;
if (OpCode == Instruction::Call || OpCode == Instruction::Invoke ||
OpCode == Instruction::CallBr)
assert((VTy->isFirstClassType() || VTy->isVoidTy() || VTy->isStructTy()) &&
"invalid CallBase type!");
else if (SubclassID != BasicBlockVal &&
( SubclassID > ConstantLastVal))
assert((VTy->isFirstClassType() || VTy->isVoidTy()) &&
"Cannot create non-first-class values except for constants!");
static_assert(sizeof(Value) == 2 * sizeof(void *) + 2 * sizeof(unsigned),
"Value too big");
}
Value::~Value() {
if (HasValueHandle)
ValueHandleBase::ValueIsDeleted(this);
if (isUsedByMetadata())
ValueAsMetadata::handleDeletion(this);
if (HasMetadata)
clearMetadata();
#ifndef NDEBUG
if (!materialized_use_empty()) {
dbgs() << "While deleting: " << *VTy << " %" << getName() << "\n";
for (auto *U : users())
dbgs() << "Use still stuck around after Def is destroyed:" << *U << "\n";
}
#endif
assert(materialized_use_empty() && "Uses remain when a value is destroyed!");
destroyValueName();
}
void Value::deleteValue() {
switch (getValueID()) {
#define HANDLE_VALUE(Name) \
case Value::Name##Val: \
delete static_cast<Name *>(this); \
break;
#define HANDLE_MEMORY_VALUE(Name) \
case Value::Name##Val: \
static_cast<DerivedUser *>(this)->DeleteValue( \
static_cast<DerivedUser *>(this)); \
break;
#define HANDLE_CONSTANT(Name) \
case Value::Name##Val: \
llvm_unreachable("constants should be destroyed with destroyConstant"); \
break;
#define HANDLE_INSTRUCTION(Name)
#include "llvm/IR/Value.def"
#define HANDLE_INST(N, OPC, CLASS) \
case Value::InstructionVal + Instruction::OPC: \
delete static_cast<CLASS *>(this); \
break;
#define HANDLE_USER_INST(N, OPC, CLASS)
#include "llvm/IR/Instruction.def"
default:
llvm_unreachable("attempting to delete unknown value kind");
}
}
void Value::destroyValueName() {
ValueName *Name = getValueName();
if (Name) {
MallocAllocator Allocator;
Name->Destroy(Allocator);
}
setValueName(nullptr);
}
bool Value::hasNUses(unsigned N) const {
return hasNItems(use_begin(), use_end(), N);
}
bool Value::hasNUsesOrMore(unsigned N) const {
return hasNItemsOrMore(use_begin(), use_end(), N);
}
bool Value::hasOneUser() const {
if (use_empty())
return false;
if (hasOneUse())
return true;
return std::equal(++user_begin(), user_end(), user_begin());
}
static bool isUnDroppableUser(const User *U) { return !U->isDroppable(); }
Use *Value::getSingleUndroppableUse() {
Use *Result = nullptr;
for (Use &U : uses()) {
if (!U.getUser()->isDroppable()) {
if (Result)
return nullptr;
Result = &U;
}
}
return Result;
}
User *Value::getUniqueUndroppableUser() {
User *Result = nullptr;
for (auto *U : users()) {
if (!U->isDroppable()) {
if (Result && Result != U)
return nullptr;
Result = U;
}
}
return Result;
}
bool Value::hasNUndroppableUses(unsigned int N) const {
return hasNItems(user_begin(), user_end(), N, isUnDroppableUser);
}
bool Value::hasNUndroppableUsesOrMore(unsigned int N) const {
return hasNItemsOrMore(user_begin(), user_end(), N, isUnDroppableUser);
}
void Value::dropDroppableUses(
llvm::function_ref<bool(const Use *)> ShouldDrop) {
SmallVector<Use *, 8> ToBeEdited;
for (Use &U : uses())
if (U.getUser()->isDroppable() && ShouldDrop(&U))
ToBeEdited.push_back(&U);
for (Use *U : ToBeEdited)
dropDroppableUse(*U);
}
void Value::dropDroppableUsesIn(User &Usr) {
assert(Usr.isDroppable() && "Expected a droppable user!");
for (Use &UsrOp : Usr.operands()) {
if (UsrOp.get() == this)
dropDroppableUse(UsrOp);
}
}
void Value::dropDroppableUse(Use &U) {
U.removeFromList();
if (auto *Assume = dyn_cast<AssumeInst>(U.getUser())) {
unsigned OpNo = U.getOperandNo();
if (OpNo == 0)
U.set(ConstantInt::getTrue(Assume->getContext()));
else {
U.set(UndefValue::get(U.get()->getType()));
CallInst::BundleOpInfo &BOI = Assume->getBundleOpInfoForOperand(OpNo);
BOI.Tag = Assume->getContext().pImpl->getOrInsertBundleTag("ignore");
}
return;
}
llvm_unreachable("unkown droppable use");
}
bool Value::isUsedInBasicBlock(const BasicBlock *BB) const {
BasicBlock::const_iterator BI = BB->begin(), BE = BB->end();
const_user_iterator UI = user_begin(), UE = user_end();
for (; BI != BE && UI != UE; ++BI, ++UI) {
if (is_contained(BI->operands(), this))
return true;
const auto *User = dyn_cast<Instruction>(*UI);
if (User && User->getParent() == BB)
return true;
}
return false;
}
unsigned Value::getNumUses() const {
return (unsigned)std::distance(use_begin(), use_end());
}
static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
ST = nullptr;
if (Instruction *I = dyn_cast<Instruction>(V)) {
if (BasicBlock *P = I->getParent())
if (Function *PP = P->getParent())
ST = PP->getValueSymbolTable();
} else if (BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
if (Function *P = BB->getParent())
ST = P->getValueSymbolTable();
} else if (GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
if (Module *P = GV->getParent())
ST = &P->getValueSymbolTable();
} else if (Argument *A = dyn_cast<Argument>(V)) {
if (Function *P = A->getParent())
ST = P->getValueSymbolTable();
} else {
assert(isa<Constant>(V) && "Unknown value type!");
return true; }
return false;
}
ValueName *Value::getValueName() const {
if (!HasName) return nullptr;
LLVMContext &Ctx = getContext();
auto I = Ctx.pImpl->ValueNames.find(this);
assert(I != Ctx.pImpl->ValueNames.end() &&
"No name entry found!");
return I->second;
}
void Value::setValueName(ValueName *VN) {
LLVMContext &Ctx = getContext();
assert(HasName == Ctx.pImpl->ValueNames.count(this) &&
"HasName bit out of sync!");
if (!VN) {
if (HasName)
Ctx.pImpl->ValueNames.erase(this);
HasName = false;
return;
}
HasName = true;
Ctx.pImpl->ValueNames[this] = VN;
}
StringRef Value::getName() const {
if (!hasName())
return StringRef("", 0);
return getValueName()->getKey();
}
void Value::setNameImpl(const Twine &NewName) {
if (getContext().shouldDiscardValueNames() && !isa<GlobalValue>(this))
return;
if (NewName.isTriviallyEmpty() && !hasName())
return;
SmallString<256> NameData;
StringRef NameRef = NewName.toStringRef(NameData);
assert(NameRef.find_first_of(0) == StringRef::npos &&
"Null bytes are not allowed in names");
if (getName() == NameRef)
return;
assert(!getType()->isVoidTy() && "Cannot assign a name to void values!");
ValueSymbolTable *ST;
if (getSymTab(this, ST))
return;
if (!ST) { if (NameRef.empty()) {
destroyValueName();
return;
}
destroyValueName();
MallocAllocator Allocator;
setValueName(ValueName::Create(NameRef, Allocator));
getValueName()->setValue(this);
return;
}
if (hasName()) {
ST->removeValueName(getValueName());
destroyValueName();
if (NameRef.empty())
return;
}
setValueName(ST->createValueName(NameRef, this));
}
void Value::setName(const Twine &NewName) {
setNameImpl(NewName);
if (Function *F = dyn_cast<Function>(this))
F->recalculateIntrinsicID();
}
void Value::takeName(Value *V) {
assert(V != this && "Illegal call to this->takeName(this)!");
ValueSymbolTable *ST = nullptr;
if (hasName()) {
if (getSymTab(this, ST)) {
if (V->hasName()) V->setName("");
return; }
if (ST)
ST->removeValueName(getValueName());
destroyValueName();
}
if (!V->hasName()) return;
if (!ST) {
if (getSymTab(this, ST)) {
V->setName("");
return; }
}
ValueSymbolTable *VST;
bool Failure = getSymTab(V, VST);
assert(!Failure && "V has a name, so it should have a ST!"); (void)Failure;
if (ST == VST) {
setValueName(V->getValueName());
V->setValueName(nullptr);
getValueName()->setValue(this);
return;
}
if (VST)
VST->removeValueName(V->getValueName());
setValueName(V->getValueName());
V->setValueName(nullptr);
getValueName()->setValue(this);
if (ST)
ST->reinsertValue(this);
}
#ifndef NDEBUG
std::string Value::getNameOrAsOperand() const {
if (!getName().empty())
return std::string(getName());
std::string BBName;
raw_string_ostream OS(BBName);
printAsOperand(OS, false);
return OS.str();
}
#endif
void Value::assertModuleIsMaterializedImpl() const {
#ifndef NDEBUG
const GlobalValue *GV = dyn_cast<GlobalValue>(this);
if (!GV)
return;
const Module *M = GV->getParent();
if (!M)
return;
assert(M->isMaterialized());
#endif
}
#ifndef NDEBUG
static bool contains(SmallPtrSetImpl<ConstantExpr *> &Cache, ConstantExpr *Expr,
Constant *C) {
if (!Cache.insert(Expr).second)
return false;
for (auto &O : Expr->operands()) {
if (O == C)
return true;
auto *CE = dyn_cast<ConstantExpr>(O);
if (!CE)
continue;
if (contains(Cache, CE, C))
return true;
}
return false;
}
static bool contains(Value *Expr, Value *V) {
if (Expr == V)
return true;
auto *C = dyn_cast<Constant>(V);
if (!C)
return false;
auto *CE = dyn_cast<ConstantExpr>(Expr);
if (!CE)
return false;
SmallPtrSet<ConstantExpr *, 4> Cache;
return contains(Cache, CE, C);
}
#endif
void Value::doRAUW(Value *New, ReplaceMetadataUses ReplaceMetaUses) {
assert(New && "Value::replaceAllUsesWith(<null>) is invalid!");
assert(!contains(New, this) &&
"this->replaceAllUsesWith(expr(this)) is NOT valid!");
assert(New->getType() == getType() &&
"replaceAllUses of value with new value of different type!");
if (HasValueHandle)
ValueHandleBase::ValueIsRAUWd(this, New);
if (ReplaceMetaUses == ReplaceMetadataUses::Yes && isUsedByMetadata())
ValueAsMetadata::handleRAUW(this, New);
while (!materialized_use_empty()) {
Use &U = *UseList;
if (auto *C = dyn_cast<Constant>(U.getUser())) {
if (!isa<GlobalValue>(C)) {
C->handleOperandChange(this, New);
continue;
}
}
U.set(New);
}
if (BasicBlock *BB = dyn_cast<BasicBlock>(this))
BB->replaceSuccessorsPhiUsesWith(cast<BasicBlock>(New));
}
void Value::replaceAllUsesWith(Value *New) {
doRAUW(New, ReplaceMetadataUses::Yes);
}
void Value::replaceNonMetadataUsesWith(Value *New) {
doRAUW(New, ReplaceMetadataUses::No);
}
void Value::replaceUsesWithIf(Value *New,
llvm::function_ref<bool(Use &U)> ShouldReplace) {
assert(New && "Value::replaceUsesWithIf(<null>) is invalid!");
assert(New->getType() == getType() &&
"replaceUses of value with new value of different type!");
SmallVector<TrackingVH<Constant>, 8> Consts;
SmallPtrSet<Constant *, 8> Visited;
for (Use &U : llvm::make_early_inc_range(uses())) {
if (!ShouldReplace(U))
continue;
if (auto *C = dyn_cast<Constant>(U.getUser())) {
if (!isa<GlobalValue>(C)) {
if (Visited.insert(C).second)
Consts.push_back(TrackingVH<Constant>(C));
continue;
}
}
U.set(New);
}
while (!Consts.empty()) {
Consts.pop_back_val()->handleOperandChange(this, New);
}
}
static void replaceDbgUsesOutsideBlock(Value *V, Value *New, BasicBlock *BB) {
SmallVector<DbgVariableIntrinsic *> DbgUsers;
findDbgUsers(DbgUsers, V);
for (auto *DVI : DbgUsers) {
if (DVI->getParent() != BB)
DVI->replaceVariableLocationOp(V, New);
}
}
void Value::replaceUsesOutsideBlock(Value *New, BasicBlock *BB) {
assert(New && "Value::replaceUsesOutsideBlock(<null>, BB) is invalid!");
assert(!contains(New, this) &&
"this->replaceUsesOutsideBlock(expr(this), BB) is NOT valid!");
assert(New->getType() == getType() &&
"replaceUses of value with new value of different type!");
assert(BB && "Basic block that may contain a use of 'New' must be defined\n");
replaceDbgUsesOutsideBlock(this, New, BB);
replaceUsesWithIf(New, [BB](Use &U) {
auto *I = dyn_cast<Instruction>(U.getUser());
return !I || I->getParent() != BB;
});
}
namespace {
enum PointerStripKind {
PSK_ZeroIndices,
PSK_ZeroIndicesAndAliases,
PSK_ZeroIndicesSameRepresentation,
PSK_ForAliasAnalysis,
PSK_InBoundsConstantIndices,
PSK_InBounds
};
template <PointerStripKind StripKind> static void NoopCallback(const Value *) {}
template <PointerStripKind StripKind>
static const Value *stripPointerCastsAndOffsets(
const Value *V,
function_ref<void(const Value *)> Func = NoopCallback<StripKind>) {
if (!V->getType()->isPointerTy())
return V;
SmallPtrSet<const Value *, 4> Visited;
Visited.insert(V);
do {
Func(V);
if (auto *GEP = dyn_cast<GEPOperator>(V)) {
switch (StripKind) {
case PSK_ZeroIndices:
case PSK_ZeroIndicesAndAliases:
case PSK_ZeroIndicesSameRepresentation:
case PSK_ForAliasAnalysis:
if (!GEP->hasAllZeroIndices())
return V;
break;
case PSK_InBoundsConstantIndices:
if (!GEP->hasAllConstantIndices())
return V;
LLVM_FALLTHROUGH;
case PSK_InBounds:
if (!GEP->isInBounds())
return V;
break;
}
V = GEP->getPointerOperand();
} else if (Operator::getOpcode(V) == Instruction::BitCast) {
V = cast<Operator>(V)->getOperand(0);
if (!V->getType()->isPointerTy())
return V;
} else if (StripKind != PSK_ZeroIndicesSameRepresentation &&
Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
V = cast<Operator>(V)->getOperand(0);
} else if (StripKind == PSK_ZeroIndicesAndAliases && isa<GlobalAlias>(V)) {
V = cast<GlobalAlias>(V)->getAliasee();
} else if (StripKind == PSK_ForAliasAnalysis && isa<PHINode>(V) &&
cast<PHINode>(V)->getNumIncomingValues() == 1) {
V = cast<PHINode>(V)->getIncomingValue(0);
} else {
if (const auto *Call = dyn_cast<CallBase>(V)) {
if (const Value *RV = Call->getReturnedArgOperand()) {
V = RV;
continue;
}
if (StripKind == PSK_ForAliasAnalysis &&
(Call->getIntrinsicID() == Intrinsic::launder_invariant_group ||
Call->getIntrinsicID() == Intrinsic::strip_invariant_group)) {
V = Call->getArgOperand(0);
continue;
}
}
return V;
}
assert(V->getType()->isPointerTy() && "Unexpected operand type!");
} while (Visited.insert(V).second);
return V;
}
}
const Value *Value::stripPointerCasts() const {
return stripPointerCastsAndOffsets<PSK_ZeroIndices>(this);
}
const Value *Value::stripPointerCastsAndAliases() const {
return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndAliases>(this);
}
const Value *Value::stripPointerCastsSameRepresentation() const {
return stripPointerCastsAndOffsets<PSK_ZeroIndicesSameRepresentation>(this);
}
const Value *Value::stripInBoundsConstantOffsets() const {
return stripPointerCastsAndOffsets<PSK_InBoundsConstantIndices>(this);
}
const Value *Value::stripPointerCastsForAliasAnalysis() const {
return stripPointerCastsAndOffsets<PSK_ForAliasAnalysis>(this);
}
const Value *Value::stripAndAccumulateConstantOffsets(
const DataLayout &DL, APInt &Offset, bool AllowNonInbounds,
bool AllowInvariantGroup,
function_ref<bool(Value &, APInt &)> ExternalAnalysis) const {
if (!getType()->isPtrOrPtrVectorTy())
return this;
unsigned BitWidth = Offset.getBitWidth();
assert(BitWidth == DL.getIndexTypeSizeInBits(getType()) &&
"The offset bit width does not match the DL specification.");
SmallPtrSet<const Value *, 4> Visited;
Visited.insert(this);
const Value *V = this;
do {
if (auto *GEP = dyn_cast<GEPOperator>(V)) {
if (!AllowNonInbounds && !GEP->isInBounds())
return V;
APInt GEPOffset(DL.getIndexTypeSizeInBits(V->getType()), 0);
if (!GEP->accumulateConstantOffset(DL, GEPOffset, ExternalAnalysis))
return V;
if (GEPOffset.getMinSignedBits() > BitWidth)
return V;
APInt GEPOffsetST = GEPOffset.sextOrTrunc(BitWidth);
if (!ExternalAnalysis) {
Offset += GEPOffsetST;
} else {
bool Overflow = false;
APInt OldOffset = Offset;
Offset = Offset.sadd_ov(GEPOffsetST, Overflow);
if (Overflow) {
Offset = OldOffset;
return V;
}
}
V = GEP->getPointerOperand();
} else if (Operator::getOpcode(V) == Instruction::BitCast ||
Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
V = cast<Operator>(V)->getOperand(0);
} else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
if (!GA->isInterposable())
V = GA->getAliasee();
} else if (const auto *Call = dyn_cast<CallBase>(V)) {
if (const Value *RV = Call->getReturnedArgOperand())
V = RV;
if (AllowInvariantGroup && Call->isLaunderOrStripInvariantGroup())
V = Call->getArgOperand(0);
}
assert(V->getType()->isPtrOrPtrVectorTy() && "Unexpected operand type!");
} while (Visited.insert(V).second);
return V;
}
const Value *
Value::stripInBoundsOffsets(function_ref<void(const Value *)> Func) const {
return stripPointerCastsAndOffsets<PSK_InBounds>(this, Func);
}
bool Value::canBeFreed() const {
assert(getType()->isPointerTy());
if (isa<Constant>(this))
return false;
if (auto *A = dyn_cast<Argument>(this)) {
if (A->hasPointeeInMemoryValueAttr())
return false;
const Function *F = A->getParent();
if (F->doesNotFreeMemory() && F->hasNoSync())
return false;
}
const Function *F = nullptr;
if (auto *I = dyn_cast<Instruction>(this))
F = I->getFunction();
if (auto *A = dyn_cast<Argument>(this))
F = A->getParent();
if (!F)
return true;
if (!F->hasGC())
return true;
const auto &GCName = F->getGC();
if (GCName == "statepoint-example") {
auto *PT = cast<PointerType>(this->getType());
if (PT->getAddressSpace() != 1)
return true;
for (auto &Fn : *F->getParent())
if (Fn.getIntrinsicID() == Intrinsic::experimental_gc_statepoint)
return true;
return false;
}
return true;
}
uint64_t Value::getPointerDereferenceableBytes(const DataLayout &DL,
bool &CanBeNull,
bool &CanBeFreed) const {
assert(getType()->isPointerTy() && "must be pointer");
uint64_t DerefBytes = 0;
CanBeNull = false;
CanBeFreed = UseDerefAtPointSemantics && canBeFreed();
if (const Argument *A = dyn_cast<Argument>(this)) {
DerefBytes = A->getDereferenceableBytes();
if (DerefBytes == 0) {
if (Type *ArgMemTy = A->getPointeeInMemoryValueType()) {
if (ArgMemTy->isSized()) {
DerefBytes = DL.getTypeStoreSize(ArgMemTy).getKnownMinSize();
}
}
}
if (DerefBytes == 0) {
DerefBytes = A->getDereferenceableOrNullBytes();
CanBeNull = true;
}
} else if (const auto *Call = dyn_cast<CallBase>(this)) {
DerefBytes = Call->getRetDereferenceableBytes();
if (DerefBytes == 0) {
DerefBytes = Call->getRetDereferenceableOrNullBytes();
CanBeNull = true;
}
} else if (const LoadInst *LI = dyn_cast<LoadInst>(this)) {
if (MDNode *MD = LI->getMetadata(LLVMContext::MD_dereferenceable)) {
ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
DerefBytes = CI->getLimitedValue();
}
if (DerefBytes == 0) {
if (MDNode *MD =
LI->getMetadata(LLVMContext::MD_dereferenceable_or_null)) {
ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
DerefBytes = CI->getLimitedValue();
}
CanBeNull = true;
}
} else if (auto *IP = dyn_cast<IntToPtrInst>(this)) {
if (MDNode *MD = IP->getMetadata(LLVMContext::MD_dereferenceable)) {
ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
DerefBytes = CI->getLimitedValue();
}
if (DerefBytes == 0) {
if (MDNode *MD =
IP->getMetadata(LLVMContext::MD_dereferenceable_or_null)) {
ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
DerefBytes = CI->getLimitedValue();
}
CanBeNull = true;
}
} else if (auto *AI = dyn_cast<AllocaInst>(this)) {
if (!AI->isArrayAllocation()) {
DerefBytes =
DL.getTypeStoreSize(AI->getAllocatedType()).getKnownMinSize();
CanBeNull = false;
CanBeFreed = false;
}
} else if (auto *GV = dyn_cast<GlobalVariable>(this)) {
if (GV->getValueType()->isSized() && !GV->hasExternalWeakLinkage()) {
DerefBytes = DL.getTypeStoreSize(GV->getValueType()).getFixedSize();
CanBeNull = false;
CanBeFreed = false;
}
}
return DerefBytes;
}
Align Value::getPointerAlignment(const DataLayout &DL) const {
assert(getType()->isPointerTy() && "must be pointer");
if (auto *GO = dyn_cast<GlobalObject>(this)) {
if (isa<Function>(GO)) {
Align FunctionPtrAlign = DL.getFunctionPtrAlign().valueOrOne();
switch (DL.getFunctionPtrAlignType()) {
case DataLayout::FunctionPtrAlignType::Independent:
return FunctionPtrAlign;
case DataLayout::FunctionPtrAlignType::MultipleOfFunctionAlign:
return std::max(FunctionPtrAlign, GO->getAlign().valueOrOne());
}
llvm_unreachable("Unhandled FunctionPtrAlignType");
}
const MaybeAlign Alignment(GO->getAlign());
if (!Alignment) {
if (auto *GVar = dyn_cast<GlobalVariable>(GO)) {
Type *ObjectType = GVar->getValueType();
if (ObjectType->isSized()) {
if (GVar->isStrongDefinitionForLinker())
return DL.getPreferredAlign(GVar);
else
return DL.getABITypeAlign(ObjectType);
}
}
}
return Alignment.valueOrOne();
} else if (const Argument *A = dyn_cast<Argument>(this)) {
const MaybeAlign Alignment = A->getParamAlign();
if (!Alignment && A->hasStructRetAttr()) {
Type *EltTy = A->getParamStructRetType();
if (EltTy->isSized())
return DL.getABITypeAlign(EltTy);
}
return Alignment.valueOrOne();
} else if (const AllocaInst *AI = dyn_cast<AllocaInst>(this)) {
return AI->getAlign();
} else if (const auto *Call = dyn_cast<CallBase>(this)) {
MaybeAlign Alignment = Call->getRetAlign();
if (!Alignment && Call->getCalledFunction())
Alignment = Call->getCalledFunction()->getAttributes().getRetAlignment();
return Alignment.valueOrOne();
} else if (const LoadInst *LI = dyn_cast<LoadInst>(this)) {
if (MDNode *MD = LI->getMetadata(LLVMContext::MD_align)) {
ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
return Align(CI->getLimitedValue());
}
} else if (auto *CstPtr = dyn_cast<Constant>(this)) {
CstPtr = CstPtr->stripPointerCasts();
if (auto *CstInt = dyn_cast_or_null<ConstantInt>(ConstantExpr::getPtrToInt(
const_cast<Constant *>(CstPtr), DL.getIntPtrType(getType()),
true))) {
size_t TrailingZeros = CstInt->getValue().countTrailingZeros();
return Align(TrailingZeros < Value::MaxAlignmentExponent
? uint64_t(1) << TrailingZeros
: Value::MaximumAlignment);
}
}
return Align(1);
}
const Value *Value::DoPHITranslation(const BasicBlock *CurBB,
const BasicBlock *PredBB) const {
auto *PN = dyn_cast<PHINode>(this);
if (PN && PN->getParent() == CurBB)
return PN->getIncomingValueForBlock(PredBB);
return this;
}
LLVMContext &Value::getContext() const { return VTy->getContext(); }
void Value::reverseUseList() {
if (!UseList || !UseList->Next)
return;
Use *Head = UseList;
Use *Current = UseList->Next;
Head->Next = nullptr;
while (Current) {
Use *Next = Current->Next;
Current->Next = Head;
Head->Prev = &Current->Next;
Head = Current;
Current = Next;
}
UseList = Head;
Head->Prev = &UseList;
}
bool Value::isSwiftError() const {
auto *Arg = dyn_cast<Argument>(this);
if (Arg)
return Arg->hasSwiftErrorAttr();
auto *Alloca = dyn_cast<AllocaInst>(this);
if (!Alloca)
return false;
return Alloca->isSwiftError();
}
bool Value::isTransitiveUsedByMetadataOnly() const {
SmallVector<const User *, 32> WorkList(user_begin(), user_end());
SmallPtrSet<const User *, 32> Visited(user_begin(), user_end());
while (!WorkList.empty()) {
const User *U = WorkList.pop_back_val();
if (!isa<Constant>(U) || isa<GlobalValue>(U))
return false;
for (const User *UU : U->users())
if (Visited.insert(UU).second)
WorkList.push_back(UU);
}
return true;
}
void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) {
assert(List && "Handle list is null?");
Next = *List;
*List = this;
setPrevPtr(List);
if (Next) {
Next->setPrevPtr(&Next);
assert(getValPtr() == Next->getValPtr() && "Added to wrong list?");
}
}
void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) {
assert(List && "Must insert after existing node");
Next = List->Next;
setPrevPtr(&List->Next);
List->Next = this;
if (Next)
Next->setPrevPtr(&Next);
}
void ValueHandleBase::AddToUseList() {
assert(getValPtr() && "Null pointer doesn't have a use list!");
LLVMContextImpl *pImpl = getValPtr()->getContext().pImpl;
if (getValPtr()->HasValueHandle) {
ValueHandleBase *&Entry = pImpl->ValueHandles[getValPtr()];
assert(Entry && "Value doesn't have any handles?");
AddToExistingUseList(&Entry);
return;
}
DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;
const void *OldBucketPtr = Handles.getPointerIntoBucketsArray();
ValueHandleBase *&Entry = Handles[getValPtr()];
assert(!Entry && "Value really did already have handles?");
AddToExistingUseList(&Entry);
getValPtr()->HasValueHandle = true;
if (Handles.isPointerIntoBucketsArray(OldBucketPtr) ||
Handles.size() == 1) {
return;
}
for (DenseMap<Value*, ValueHandleBase*>::iterator I = Handles.begin(),
E = Handles.end(); I != E; ++I) {
assert(I->second && I->first == I->second->getValPtr() &&
"List invariant broken!");
I->second->setPrevPtr(&I->second);
}
}
void ValueHandleBase::RemoveFromUseList() {
assert(getValPtr() && getValPtr()->HasValueHandle &&
"Pointer doesn't have a use list!");
ValueHandleBase **PrevPtr = getPrevPtr();
assert(*PrevPtr == this && "List invariant broken");
*PrevPtr = Next;
if (Next) {
assert(Next->getPrevPtr() == &Next && "List invariant broken");
Next->setPrevPtr(PrevPtr);
return;
}
LLVMContextImpl *pImpl = getValPtr()->getContext().pImpl;
DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;
if (Handles.isPointerIntoBucketsArray(PrevPtr)) {
Handles.erase(getValPtr());
getValPtr()->HasValueHandle = false;
}
}
void ValueHandleBase::ValueIsDeleted(Value *V) {
assert(V->HasValueHandle && "Should only be called if ValueHandles present");
LLVMContextImpl *pImpl = V->getContext().pImpl;
ValueHandleBase *Entry = pImpl->ValueHandles[V];
assert(Entry && "Value bit set but no entries exist");
for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
Iterator.RemoveFromUseList();
Iterator.AddToExistingUseListAfter(Entry);
assert(Entry->Next == &Iterator && "Loop invariant broken.");
switch (Entry->getKind()) {
case Assert:
break;
case Weak:
case WeakTracking:
Entry->operator=(nullptr);
break;
case Callback:
static_cast<CallbackVH*>(Entry)->deleted();
break;
}
}
if (V->HasValueHandle) {
#ifndef NDEBUG
dbgs() << "While deleting: " << *V->getType() << " %" << V->getName()
<< "\n";
if (pImpl->ValueHandles[V]->getKind() == Assert)
llvm_unreachable("An asserting value handle still pointed to this"
" value!");
#endif
llvm_unreachable("All references to V were not removed?");
}
}
void ValueHandleBase::ValueIsRAUWd(Value *Old, Value *New) {
assert(Old->HasValueHandle &&"Should only be called if ValueHandles present");
assert(Old != New && "Changing value into itself!");
assert(Old->getType() == New->getType() &&
"replaceAllUses of value with new value of different type!");
LLVMContextImpl *pImpl = Old->getContext().pImpl;
ValueHandleBase *Entry = pImpl->ValueHandles[Old];
assert(Entry && "Value bit set but no entries exist");
for (ValueHandleBase Iterator(Assert, *Entry); Entry; Entry = Iterator.Next) {
Iterator.RemoveFromUseList();
Iterator.AddToExistingUseListAfter(Entry);
assert(Entry->Next == &Iterator && "Loop invariant broken.");
switch (Entry->getKind()) {
case Assert:
case Weak:
break;
case WeakTracking:
Entry->operator=(New);
break;
case Callback:
static_cast<CallbackVH*>(Entry)->allUsesReplacedWith(New);
break;
}
}
#ifndef NDEBUG
if (Old->HasValueHandle)
for (Entry = pImpl->ValueHandles[Old]; Entry; Entry = Entry->Next)
switch (Entry->getKind()) {
case WeakTracking:
dbgs() << "After RAUW from " << *Old->getType() << " %"
<< Old->getName() << " to " << *New->getType() << " %"
<< New->getName() << "\n";
llvm_unreachable(
"A weak tracking value handle still pointed to the old value!\n");
default:
break;
}
#endif
}
void CallbackVH::anchor() {}