44 assert(Ty &&
"Value defined with a null type: Error!");
49 : VTy(
checkType(ty)), UseList(nullptr), SubclassID(scid),
50 HasValueHandle(0), SubclassOptionalData(0), SubclassData(0),
51 NumUserOperands(0), IsUsedByMD(
false), HasName(
false) {
57 "invalid CallInst type!");
58 else if (SubclassID != BasicBlockVal &&
59 (SubclassID < ConstantFirstVal || SubclassID > ConstantLastVal))
61 "Cannot create non-first-class values except for constants!");
62 static_assert(
sizeof(
Value) == 3 *
sizeof(
void *) + 2 *
sizeof(
unsigned),
73 #ifndef NDEBUG // Only in -g mode...
80 dbgs() <<
"While deleting: " << *VTy <<
" %" <<
getName() <<
"\n";
81 for (
auto *U :
users())
82 dbgs() <<
"Use still stuck around after Def is destroyed:" << *U <<
"\n";
92 void Value::destroyValueName() {
103 if (UI ==
E)
return false;
111 if (UI ==
E)
return false;
125 for (; BI != BE && UI != UE; ++BI, ++UI) {
131 if (
User &&
User->getParent() == BB)
146 ST = PP->getValueSymbolTable();
147 }
else if (
BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
149 ST =
P->getValueSymbolTable();
150 }
else if (
GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
151 if (
Module *
P = GV->getParent())
152 ST = &
P->getValueSymbolTable();
153 }
else if (
Argument *
A = dyn_cast<Argument>(V)) {
155 ST =
P->getValueSymbolTable();
157 assert(isa<Constant>(V) &&
"Unknown value type!");
169 "No name entry found!");
178 "HasName bit out of sync!");
200 void Value::setNameImpl(
const Twine &NewName) {
202 if (
getContext().shouldDiscardValueNames() && !isa<GlobalValue>(
this))
212 "Null bytes are not allowed in names");
218 assert(!
getType()->isVoidTy() &&
"Cannot assign a name to void values!");
226 if (NameRef.
empty()) {
258 setNameImpl(NewName);
259 if (
Function *
F = dyn_cast<Function>(
this))
260 F->recalculateIntrinsicID();
298 assert(!Failure &&
"V has a name, so it should have a ST!"); (void)Failure;
320 ST->reinsertValue(
this);
338 if (!Cache.
insert(Expr).second)
370 void Value::doRAUW(
Value *New,
bool NoMetadata) {
371 assert(New &&
"Value::replaceAllUsesWith(<null>) is invalid!");
373 "this->replaceAllUsesWith(expr(this)) is NOT valid!");
375 "replaceAllUses of value with new value of different type!");
387 if (
auto *
C = dyn_cast<Constant>(U.
getUser())) {
388 if (!isa<GlobalValue>(
C)) {
389 C->handleOperandChange(
this, New);
397 if (
BasicBlock *BB = dyn_cast<BasicBlock>(
this))
398 BB->replaceSuccessorsPhiUsesWith(cast<BasicBlock>(New));
412 assert(New &&
"Value::replaceUsesOutsideBlock(<null>, BB) is invalid!");
414 "this->replaceUsesOutsideBlock(expr(this), BB) is NOT valid!");
416 "replaceUses of value with new value of different type!");
417 assert(BB &&
"Basic block that may contain a use of 'New' must be defined\n");
424 if (Usr && Usr->getParent() == BB)
434 PSK_ZeroIndicesAndAliases,
435 PSK_InBoundsConstantIndices,
439 template <Po
interStripKind StripKind>
440 static Value *stripPointerCastsAndOffsets(
Value *V) {
452 case PSK_ZeroIndicesAndAliases:
453 case PSK_ZeroIndices:
454 if (!
GEP->hasAllZeroIndices())
457 case PSK_InBoundsConstantIndices:
458 if (!
GEP->hasAllConstantIndices())
462 if (!
GEP->isInBounds())
466 V =
GEP->getPointerOperand();
469 V = cast<Operator>(V)->getOperand(0);
470 }
else if (
GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
471 if (StripKind == PSK_ZeroIndices || GA->isInterposable())
473 V = GA->getAliasee();
476 if (
Value *RV =
CS.getReturnedArgOperand()) {
484 }
while (Visited.
insert(V).second);
491 return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndAliases>(
this);
495 return stripPointerCastsAndOffsets<PSK_ZeroIndices>(
this);
499 return stripPointerCastsAndOffsets<PSK_InBoundsConstantIndices>(
this);
508 getType())->getAddressSpace()) &&
509 "The offset must have exactly as many bits as our pointer.");
518 if (!
GEP->isInBounds())
520 APInt GEPOffset(Offset);
521 if (!
GEP->accumulateConstantOffset(DL, GEPOffset))
524 V =
GEP->getPointerOperand();
526 V = cast<Operator>(V)->getOperand(0);
527 }
else if (
GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
528 V = GA->getAliasee();
531 if (
Value *RV = CS.getReturnedArgOperand()) {
539 }
while (Visited.insert(V).second);
545 return stripPointerCastsAndOffsets<PSK_InBounds>(
this);
549 bool &CanBeNull)
const {
552 unsigned DerefBytes = 0;
554 if (
const Argument *
A = dyn_cast<Argument>(
this)) {
555 DerefBytes =
A->getDereferenceableBytes();
556 if (DerefBytes == 0 &&
A->hasByValAttr() &&
A->getType()->isSized()) {
560 if (DerefBytes == 0) {
561 DerefBytes =
A->getDereferenceableOrNullBytes();
565 DerefBytes = CS.getDereferenceableBytes(0);
566 if (DerefBytes == 0) {
567 DerefBytes = CS.getDereferenceableOrNullBytes(0);
570 }
else if (
const LoadInst *LI = dyn_cast<LoadInst>(
this)) {
572 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
575 if (DerefBytes == 0) {
578 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
583 }
else if (
auto *AI = dyn_cast<AllocaInst>(
this)) {
584 if (AI->getAllocatedType()->isSized()) {
588 }
else if (
auto *GV = dyn_cast<GlobalVariable>(
this)) {
589 if (GV->getValueType()->isSized() && !GV->hasExternalWeakLinkage()) {
603 if (
auto *GO = dyn_cast<GlobalObject>(
this)) {
604 Align = GO->getAlignment();
606 if (
auto *GVar = dyn_cast<GlobalVariable>(GO)) {
607 Type *ObjectType = GVar->getValueType();
612 if (GVar->isStrongDefinitionForLinker())
619 }
else if (
const Argument *
A = dyn_cast<Argument>(
this)) {
620 Align =
A->getParamAlignment();
622 if (!Align &&
A->hasStructRetAttr()) {
624 Type *EltTy = cast<PointerType>(
A->getType())->getElementType();
628 }
else if (
const AllocaInst *AI = dyn_cast<AllocaInst>(
this)) {
629 Align = AI->getAlignment();
631 Type *AllocatedType = AI->getAllocatedType();
637 else if (
const LoadInst *LI = dyn_cast<LoadInst>(
this))
639 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
657 if (!UseList || !UseList->Next)
662 Use *Current = UseList->Next;
663 Head->Next =
nullptr;
665 Use *Next = Current->Next;
666 Current->Next = Head;
667 Head->setPrev(&Current->Next);
672 Head->setPrev(&UseList);
690 assert(List &&
"Handle list is null?");
697 Next->setPrevPtr(&Next);
698 assert(V == Next->V &&
"Added to wrong list?");
702 void ValueHandleBase::AddToExistingUseListAfter(
ValueHandleBase *List) {
703 assert(List &&
"Must insert after existing node");
706 setPrevPtr(&List->Next);
709 Next->setPrevPtr(&Next);
712 void ValueHandleBase::AddToUseList() {
713 assert(V &&
"Null pointer doesn't have a use list!");
717 if (V->HasValueHandle) {
721 assert(Entry &&
"Value doesn't have any handles?");
722 AddToExistingUseList(&Entry);
735 assert(!Entry &&
"Value really did already have handles?");
736 AddToExistingUseList(&Entry);
737 V->HasValueHandle =
true;
742 Handles.
size() == 1) {
748 E = Handles.
end();
I !=
E; ++
I) {
749 assert(
I->second &&
I->first ==
I->second->V &&
750 "List invariant broken!");
751 I->second->setPrevPtr(&
I->second);
755 void ValueHandleBase::RemoveFromUseList() {
756 assert(V && V->HasValueHandle &&
757 "Pointer doesn't have a use list!");
761 assert(*PrevPtr ==
this &&
"List invariant broken");
765 assert(Next->getPrevPtr() == &Next &&
"List invariant broken");
766 Next->setPrevPtr(PrevPtr);
777 V->HasValueHandle =
false;
782 assert(V->HasValueHandle &&
"Should only be called if ValueHandles present");
788 assert(Entry &&
"Value bit set but no entries exist");
800 Iterator.RemoveFromUseList();
801 Iterator.AddToExistingUseListAfter(Entry);
802 assert(Entry->Next == &Iterator &&
"Loop invariant broken.");
804 switch (Entry->getKind()) {
814 Entry->operator=(
nullptr);
824 if (V->HasValueHandle) {
825 #ifndef NDEBUG // Only in +Asserts mode...
838 assert(Old->HasValueHandle &&
"Should only be called if ValueHandles present");
839 assert(Old != New &&
"Changing value into itself!");
841 "replaceAllUses of value with new value of different type!");
848 assert(Entry &&
"Value bit set but no entries exist");
855 Iterator.RemoveFromUseList();
856 Iterator.AddToExistingUseListAfter(Entry);
857 assert(Entry->Next == &Iterator &&
"Loop invariant broken.");
859 switch (Entry->getKind()) {
872 Entry->operator=(New);
876 static_cast<CallbackVH*
>(Entry)->allUsesReplacedWith(New);
884 if (Old->HasValueHandle)
885 for (Entry = pImpl->ValueHandles[Old]; Entry; Entry = Entry->Next)
886 switch (Entry->getKind()) {
889 dbgs() <<
"After RAUW from " << *Old->
getType() <<
" %"
901 void CallbackVH::anchor() {}
This is the common base class of value handles.
use_iterator_impl< Use > use_iterator
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)
Value * stripInBoundsConstantOffsets()
Strip off pointer casts and all-constant inbounds GEPs.
LLVM Argument representation.
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)
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.
An instruction for reading from memory.
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.
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
Value * stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, APInt &Offset)
Accumulate offsets from stripInBoundsConstantOffsets().
void Destroy(AllocatorTy &Allocator)
Destroy - Destroy this StringMapEntry, releasing memory back to the specified allocator.
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...
Function Alias Analysis false
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
static GCRegistry::Add< CoreCLRGC > E("coreclr","CoreCLR-compatible GC")
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Value * stripPointerCastsNoFollowAliases()
Strip off pointer casts and all-zero GEPs.
bool erase(const KeyT &Val)
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.
Value * stripInBoundsOffsets()
Strip off pointer casts and inbounds GEPs.
bool isUsedByMetadata() const
Return true if there is metadata referencing this value.
unsigned getBitWidth() const
Return the number of bits in the APInt.
ValueHandlesTy ValueHandles
User * getUser() const
Returns the User that contains this Use.
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.
use_iterator_impl< const Use > const_use_iterator
bool isUsedInBasicBlock(const BasicBlock *BB) const
Check if this value is used in the specified basic block.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
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
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.
Value * DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB)
Translate PHI node to its predecessor from the given basic block.
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.
Value * stripPointerCasts()
Strip off pointer casts, all-zero GEPs, and aliases.
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
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.
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.
void assertModuleIsMaterialized() const
user_iterator_impl< const User > const_user_iterator
unsigned getPointerDereferenceableBytes(const DataLayout &DL, bool &CanBeNull) const
Returns the number of bytes known to be dereferenceable for the pointer value.
ImmutableCallSite - 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)
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.
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.
static GCRegistry::Add< ErlangGC > A("erlang","erlang-compatible garbage collector")
const BasicBlock * getParent() const
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.