45 assert(Ty &&
"Value defined with a null type: Error!");
50 : VTy(
checkType(ty)), UseList(nullptr), SubclassID(scid),
51 HasValueHandle(0), SubclassOptionalData(0), SubclassData(0),
52 NumUserOperands(0), IsUsedByMD(
false), HasName(
false) {
58 "invalid CallInst type!");
59 else if (SubclassID != BasicBlockVal &&
60 (SubclassID < ConstantFirstVal || SubclassID > ConstantLastVal))
62 "Cannot create non-first-class values except for constants!");
63 static_assert(
sizeof(
Value) == 2 *
sizeof(
void *) + 2 *
sizeof(
unsigned),
74 #ifndef NDEBUG // Only in -g mode...
81 dbgs() <<
"While deleting: " << *VTy <<
" %" <<
getName() <<
"\n";
82 for (
auto *U :
users())
83 dbgs() <<
"Use still stuck around after Def is destroyed:" << *U <<
"\n";
95 #define HANDLE_VALUE(Name) \
96 case Value::Name##Val: \
97 delete static_cast<Name *>(this); \
99 #define HANDLE_MEMORY_VALUE(Name) \
100 case Value::Name##Val: \
101 static_cast<DerivedUser *>(this)->DeleteValue( \
102 static_cast<DerivedUser *>(this)); \
104 #define HANDLE_INSTRUCTION(Name)
105 #include "llvm/IR/Value.def"
107 #define HANDLE_INST(N, OPC, CLASS) \
108 case Value::InstructionVal + Instruction::OPC: \
109 delete static_cast<CLASS *>(this); \
111 #define HANDLE_USER_INST(N, OPC, CLASS)
112 #include "llvm/IR/Instruction.def"
119 void Value::destroyValueName() {
130 if (UI ==
E)
return false;
138 if (UI ==
E)
return false;
152 for (; BI != BE && UI != UE; ++BI, ++UI) {
158 if (
User &&
User->getParent() == BB)
173 ST = PP->getValueSymbolTable();
174 }
else if (
BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
176 ST =
P->getValueSymbolTable();
177 }
else if (
GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
178 if (
Module *
P = GV->getParent())
179 ST = &
P->getValueSymbolTable();
180 }
else if (
Argument *
A = dyn_cast<Argument>(V)) {
182 ST =
P->getValueSymbolTable();
184 assert(isa<Constant>(V) &&
"Unknown value type!");
196 "No name entry found!");
205 "HasName bit out of sync!");
227 void Value::setNameImpl(
const Twine &NewName) {
229 if (
getContext().shouldDiscardValueNames() && !isa<GlobalValue>(
this))
239 "Null bytes are not allowed in names");
245 assert(!
getType()->isVoidTy() &&
"Cannot assign a name to void values!");
253 if (NameRef.
empty()) {
285 setNameImpl(NewName);
286 if (
Function *
F = dyn_cast<Function>(
this))
287 F->recalculateIntrinsicID();
325 assert(!Failure &&
"V has a name, so it should have a ST!"); (void)Failure;
347 ST->reinsertValue(
this);
365 if (!Cache.
insert(Expr).second)
397 void Value::doRAUW(
Value *New,
bool NoMetadata) {
398 assert(New &&
"Value::replaceAllUsesWith(<null>) is invalid!");
400 "this->replaceAllUsesWith(expr(this)) is NOT valid!");
402 "replaceAllUses of value with new value of different type!");
414 if (
auto *
C = dyn_cast<Constant>(U.
getUser())) {
415 if (!isa<GlobalValue>(
C)) {
416 C->handleOperandChange(
this, New);
424 if (
BasicBlock *BB = dyn_cast<BasicBlock>(
this))
425 BB->replaceSuccessorsPhiUsesWith(cast<BasicBlock>(New));
439 assert(New &&
"Value::replaceUsesOutsideBlock(<null>, BB) is invalid!");
441 "this->replaceUsesOutsideBlock(expr(this), BB) is NOT valid!");
443 "replaceUses of value with new value of different type!");
444 assert(BB &&
"Basic block that may contain a use of 'New' must be defined\n");
451 if (Usr && Usr->getParent() == BB)
461 PSK_ZeroIndicesAndAliases,
462 PSK_ZeroIndicesAndAliasesAndBarriers,
463 PSK_InBoundsConstantIndices,
467 template <Po
interStripKind StripKind>
468 static const Value *stripPointerCastsAndOffsets(
const Value *V) {
478 if (
auto *
GEP = dyn_cast<GEPOperator>(V)) {
480 case PSK_ZeroIndicesAndAliases:
481 case PSK_ZeroIndicesAndAliasesAndBarriers:
482 case PSK_ZeroIndices:
483 if (!
GEP->hasAllZeroIndices())
486 case PSK_InBoundsConstantIndices:
487 if (!
GEP->hasAllConstantIndices())
491 if (!
GEP->isInBounds())
495 V =
GEP->getPointerOperand();
498 V = cast<Operator>(V)->getOperand(0);
499 }
else if (
auto *GA = dyn_cast<GlobalAlias>(V)) {
500 if (StripKind == PSK_ZeroIndices || GA->isInterposable())
502 V = GA->getAliasee();
505 if (
const Value *RV =
CS.getReturnedArgOperand()) {
512 if (StripKind == PSK_ZeroIndicesAndAliasesAndBarriers &&
513 CS.getIntrinsicID() == Intrinsic::invariant_group_barrier) {
514 V =
CS.getArgOperand(0);
521 }
while (Visited.
insert(V).second);
528 return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndAliases>(
this);
532 return stripPointerCastsAndOffsets<PSK_ZeroIndices>(
this);
536 return stripPointerCastsAndOffsets<PSK_InBoundsConstantIndices>(
this);
540 return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndAliasesAndBarriers>(
551 getType())->getAddressSpace()) &&
552 "The offset must have exactly as many bits as our pointer.");
558 const Value *V =
this;
560 if (
auto *
GEP = dyn_cast<GEPOperator>(V)) {
561 if (!
GEP->isInBounds())
563 APInt GEPOffset(Offset);
564 if (!
GEP->accumulateConstantOffset(DL, GEPOffset))
567 V =
GEP->getPointerOperand();
569 V = cast<Operator>(V)->getOperand(0);
570 }
else if (
auto *GA = dyn_cast<GlobalAlias>(V)) {
571 V = GA->getAliasee();
574 if (
const Value *RV = CS.getReturnedArgOperand()) {
582 }
while (Visited.insert(V).second);
588 return stripPointerCastsAndOffsets<PSK_InBounds>(
this);
592 bool &CanBeNull)
const {
595 unsigned DerefBytes = 0;
597 if (
const Argument *
A = dyn_cast<Argument>(
this)) {
598 DerefBytes =
A->getDereferenceableBytes();
599 if (DerefBytes == 0 &&
A->hasByValAttr() &&
A->getType()->isSized()) {
603 if (DerefBytes == 0) {
604 DerefBytes =
A->getDereferenceableOrNullBytes();
609 if (DerefBytes == 0) {
613 }
else if (
const LoadInst *LI = dyn_cast<LoadInst>(
this)) {
615 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
618 if (DerefBytes == 0) {
621 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
626 }
else if (
auto *AI = dyn_cast<AllocaInst>(
this)) {
627 if (AI->getAllocatedType()->isSized()) {
631 }
else if (
auto *GV = dyn_cast<GlobalVariable>(
this)) {
632 if (GV->getValueType()->isSized() && !GV->hasExternalWeakLinkage()) {
646 if (
auto *GO = dyn_cast<GlobalObject>(
this)) {
647 Align = GO->getAlignment();
649 if (
auto *GVar = dyn_cast<GlobalVariable>(GO)) {
650 Type *ObjectType = GVar->getValueType();
655 if (GVar->isStrongDefinitionForLinker())
662 }
else if (
const Argument *
A = dyn_cast<Argument>(
this)) {
663 Align =
A->getParamAlignment();
665 if (!Align &&
A->hasStructRetAttr()) {
667 Type *EltTy = cast<PointerType>(
A->getType())->getElementType();
671 }
else if (
const AllocaInst *AI = dyn_cast<AllocaInst>(
this)) {
672 Align = AI->getAlignment();
674 Type *AllocatedType = AI->getAllocatedType();
679 Align = CS.getAttributes().getRetAlignment();
680 else if (
const LoadInst *LI = dyn_cast<LoadInst>(
this))
682 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
692 if (PN && PN->getParent() == CurBB)
700 if (!UseList || !UseList->Next)
705 Use *Current = UseList->Next;
706 Head->Next =
nullptr;
708 Use *Next = Current->Next;
709 Current->Next = Head;
710 Head->setPrev(&Current->Next);
715 Head->setPrev(&UseList);
733 assert(List &&
"Handle list is null?");
740 Next->setPrevPtr(&Next);
745 void ValueHandleBase::AddToExistingUseListAfter(
ValueHandleBase *List) {
746 assert(List &&
"Must insert after existing node");
749 setPrevPtr(&List->Next);
752 Next->setPrevPtr(&Next);
755 void ValueHandleBase::AddToUseList() {
764 assert(Entry &&
"Value doesn't have any handles?");
765 AddToExistingUseList(&Entry);
778 assert(!Entry &&
"Value really did already have handles?");
779 AddToExistingUseList(&Entry);
785 Handles.
size() == 1) {
791 E = Handles.
end();
I !=
E; ++
I) {
792 assert(
I->second &&
I->first ==
I->second->getValPtr() &&
793 "List invariant broken!");
794 I->second->setPrevPtr(&
I->second);
800 "Pointer doesn't have a use list!");
804 assert(*PrevPtr ==
this &&
"List invariant broken");
808 assert(Next->getPrevPtr() == &Next &&
"List invariant broken");
809 Next->setPrevPtr(PrevPtr);
825 assert(V->HasValueHandle &&
"Should only be called if ValueHandles present");
831 assert(Entry &&
"Value bit set but no entries exist");
843 Iterator.RemoveFromUseList();
844 Iterator.AddToExistingUseListAfter(Entry);
845 assert(Entry->Next == &Iterator &&
"Loop invariant broken.");
847 switch (Entry->getKind()) {
854 Entry->operator=(
nullptr);
864 if (V->HasValueHandle) {
865 #ifndef NDEBUG // Only in +Asserts mode...
878 assert(Old->HasValueHandle &&
"Should only be called if ValueHandles present");
879 assert(Old != New &&
"Changing value into itself!");
881 "replaceAllUses of value with new value of different type!");
888 assert(Entry &&
"Value bit set but no entries exist");
895 Iterator.RemoveFromUseList();
896 Iterator.AddToExistingUseListAfter(Entry);
897 assert(Entry->Next == &Iterator &&
"Loop invariant broken.");
899 switch (Entry->getKind()) {
906 Entry->operator=(New);
910 static_cast<CallbackVH*
>(Entry)->allUsesReplacedWith(New);
918 if (Old->HasValueHandle)
919 for (Entry = pImpl->ValueHandles[Old]; Entry; Entry = Entry->Next)
920 switch (Entry->getKind()) {
922 dbgs() <<
"After RAUW from " << *Old->
getType() <<
" %"
926 "A weak tracking value handle still pointed to the old value!\n");
934 void CallbackVH::anchor() {}
const Value * stripPointerCastsNoFollowAliases() const
Strip off pointer casts and all-zero GEPs.
This is the common base class of value handles.
A parsed version of the target data layout string in and methods for querying it. ...
This class provides a symbol table of name/value pairs.
static void ValueIsDeleted(Value *V)
This class represents an incoming formal argument to a Function.
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
A Module instance is used to store all the information related to an LLVM module. ...
void setValue(const ValueTy &V)
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs, and aliases.
bool hasNUses(unsigned N) const
Return true if this Value has exactly N users.
unsigned getPrefTypeAlignment(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
void deleteValue()
Delete a pointer to a generic Value.
An instruction for reading from memory.
use_iterator_impl< const Use > const_use_iterator
const Value * stripInBoundsOffsets() const
Strip off pointer casts and inbounds GEPs.
StringRef getName() const
Return a constant reference to the value's name.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
iterator begin()
Instruction iterator methods.
static Type * checkType(Type *Ty)
unsigned getPointerAlignment(const DataLayout &DL) const
Returns an alignment of the pointer value.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
bool hasSwiftErrorAttr() const
Return true if this argument has the swifterror attribute.
const Value * stripPointerCastsAndBarriers() const
Strip off pointer casts, all-zero GEPs, aliases and barriers.
A Use represents the edge between a Value definition and its users.
const void * getPointerIntoBucketsArray() const
getPointerIntoBucketsArray() - Return an opaque pointer into the buckets array.
void setName(const Twine &Name)
Change the name of the value.
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
DenseMap< const Value *, ValueName * > ValueNames
void Destroy(AllocatorTy &Allocator)
Destroy - Destroy this StringMapEntry, releasing memory back to the specified allocator.
User * getUser() const LLVM_READONLY
Returns the User that contains this Use.
A constant value that is initialized with an expression using other constant values.
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
uint64_t getLimitedValue(uint64_t Limit=~0ULL) const
getLimitedValue - If the value is smaller than the specified limit, return it, otherwise return the l...
bool isFirstClassType() const
Return true if the type is "first class", meaning it is a valid type for a Value. ...
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
static void ValueIsRAUWd(Value *Old, Value *New)
void takeName(Value *V)
Transfer the name from V to this value.
ValueName * getValueName() const
use_iterator_impl< Use > use_iterator
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Function Alias Analysis false
bool erase(const KeyT &Val)
void RemoveFromUseList()
Remove this ValueHandle from its current use list.
void assertModuleIsMaterializedImpl() const
Value * getValPtr() const
LLVM Basic Block Representation.
The instances of the Type class are immutable: once they are created, they are never changed...
This is an important class for using LLVM in a threaded context.
This is an important base class in LLVM.
void replaceNonMetadataUsesWith(Value *V)
Change non-metadata uses of this to point to a new Value.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool isUsedByMetadata() const
Return true if there is metadata referencing this value.
unsigned getBitWidth() const
Return the number of bits in the APInt.
unsigned getValueID() const
Return an ID for the concrete type of this object.
ValueHandlesTy ValueHandles
bool isPointerTy() const
True if this is an instance of PointerType.
Value(Type *Ty, unsigned scid)
LLVMContext & getContext() const
All values hold a context through their type.
bool isUsedInBasicBlock(const BasicBlock *BB) const
Check if this value is used in the specified basic block.
unsigned getPreferredAlignment(const GlobalVariable *GV) const
Returns the preferred alignment of the specified global.
static StringMapEntry * Create(StringRef Key, AllocatorTy &Allocator, InitTy &&...InitVals)
Create a StringMapEntry for the specified key construct the value using InitiVals.
LLVMContextImpl *const pImpl
const Value * DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB) const
Translate PHI node to its predecessor from the given basic block.
unsigned getABITypeAlignment(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
Iterator for intrusive lists based on ilist_node.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
bool isSwiftError() const
Return true if this alloca is used as a swifterror argument to a call.
This is the shared class of boolean and integer constants.
StringRef toStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single StringRef if it can be represented as such.
bool isMaterialized() const
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
bool isSwiftError() const
Return true if this value is a swifterror value.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Class for arbitrary precision integers.
Value * getIncomingValueForBlock(const BasicBlock *BB) const
iterator_range< user_iterator > users()
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
user_iterator_impl< const User > const_user_iterator
llvm_unreachable("Unknown RegBank!")
bool isPointerIntoBucketsArray(const void *Ptr) const
isPointerIntoBucketsArray - Return true if the specified pointer points somewhere into the DenseMap's...
bool isStructTy() const
True if this is an instance of StructType.
unsigned getOpcode() const
Return the opcode for this Instruction or ConstantExpr.
void reverseUseList()
Reverse the use-list.
unsigned getPointerDereferenceableBytes(const DataLayout &DL, bool &CanBeNull) const
Returns the number of bytes known to be dereferenceable for the pointer value.
Establish a view to a call site for examination.
unsigned getPointerSizeInBits(unsigned AS=0) const
Layout pointer size, in bits FIXME: The defaults need to be removed once all of the backends/clients ...
static bool getSymTab(Value *V, ValueSymbolTable *&ST)
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
const Value * stripInBoundsConstantOffsets() const
Strip off pointer casts and all-constant inbounds GEPs.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
uint64_t getTypeStoreSize(Type *Ty) const
Returns the maximum number of bytes that may be overwritten by storing the specified type...
bool hasNUsesOrMore(unsigned N) const
Return true if this value has N users or more.
~Value()
Value's destructor should be virtual by design, but that would require that Value and all of its subc...
LLVM_NODISCARD size_t find_first_of(char C, size_t From=0) const
Find the first character in the string that is C, or npos if not found.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
user_iterator user_begin()
Module * getParent()
Get the module that this global value is contained inside of...
LLVM Value Representation.
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
bool isTriviallyEmpty() const
Check if this twine is trivially empty; a false return value does not necessarily mean the twine is e...
void setValueName(ValueName *VN)
Value handle with callbacks on RAUW and destruction.
StringRef - Represent a constant reference to a string, i.e.
void replaceUsesOutsideBlock(Value *V, BasicBlock *BB)
replaceUsesOutsideBlock - Go through the uses list for this definition and make each use point to "V"...
unsigned getNumUses() const
This method computes the number of uses of this Value.
const Value * stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, APInt &Offset) const
Accumulate offsets from stripInBoundsConstantOffsets().
bool isVoidTy() const
Return true if this is 'void'.
an instruction to allocate memory on the stack
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.