43 cl::desc(
"Maximum size for the name of non-global values."));
49 assert(Ty &&
"Value defined with a null type: Error!");
54 : VTy(
checkType(ty)), UseList(nullptr), SubclassID(scid),
55 HasValueHandle(0), SubclassOptionalData(0), SubclassData(0),
56 NumUserOperands(0), IsUsedByMD(
false), HasName(
false) {
57 static_assert(ConstantFirstVal == 0,
"!(SubclassID < ConstantFirstVal)");
63 "invalid CallInst type!");
64 else if (SubclassID != BasicBlockVal &&
65 ( SubclassID > ConstantLastVal))
67 "Cannot create non-first-class values except for constants!");
68 static_assert(
sizeof(
Value) == 2 *
sizeof(
void *) + 2 *
sizeof(
unsigned),
79 #ifndef NDEBUG // Only in -g mode... 86 dbgs() <<
"While deleting: " << *VTy <<
" %" <<
getName() <<
"\n";
87 for (
auto *U :
users())
88 dbgs() <<
"Use still stuck around after Def is destroyed:" << *U <<
"\n";
100 #define HANDLE_VALUE(Name) \ 101 case Value::Name##Val: \ 102 delete static_cast<Name *>(this); \ 104 #define HANDLE_MEMORY_VALUE(Name) \ 105 case Value::Name##Val: \ 106 static_cast<DerivedUser *>(this)->DeleteValue( \ 107 static_cast<DerivedUser *>(this)); \ 109 #define HANDLE_INSTRUCTION(Name) 110 #include "llvm/IR/Value.def" 112 #define HANDLE_INST(N, OPC, CLASS) \ 113 case Value::InstructionVal + Instruction::OPC: \ 114 delete static_cast<CLASS *>(this); \ 116 #define HANDLE_USER_INST(N, OPC, CLASS) 117 #include "llvm/IR/Instruction.def" 124 void Value::destroyValueName() {
148 for (; BI != BE && UI != UE; ++BI, ++UI) {
154 if (
User &&
User->getParent() == BB)
169 ST = PP->getValueSymbolTable();
170 }
else if (
BasicBlock *BB = dyn_cast<BasicBlock>(V)) {
172 ST =
P->getValueSymbolTable();
173 }
else if (
GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
174 if (
Module *
P = GV->getParent())
175 ST = &
P->getValueSymbolTable();
176 }
else if (
Argument *A = dyn_cast<Argument>(V)) {
178 ST =
P->getValueSymbolTable();
180 assert(isa<Constant>(V) &&
"Unknown value type!");
192 "No name entry found!");
201 "HasName bit out of sync!");
223 void Value::setNameImpl(
const Twine &NewName) {
225 if (
getContext().shouldDiscardValueNames() && !isa<GlobalValue>(
this))
235 "Null bytes are not allowed in names");
246 assert(!
getType()->isVoidTy() &&
"Cannot assign a name to void values!");
254 if (NameRef.
empty()) {
286 setNameImpl(NewName);
287 if (
Function *
F = dyn_cast<Function>(
this))
288 F->recalculateIntrinsicID();
326 assert(!Failure &&
"V has a name, so it should have a ST!"); (void)Failure;
348 ST->reinsertValue(
this);
366 if (!Cache.
insert(Expr).second)
398 void Value::doRAUW(
Value *New, ReplaceMetadataUses ReplaceMetaUses) {
399 assert(New &&
"Value::replaceAllUsesWith(<null>) is invalid!");
401 "this->replaceAllUsesWith(expr(this)) is NOT valid!");
403 "replaceAllUses of value with new value of different type!");
415 if (
auto *
C = dyn_cast<Constant>(U.
getUser())) {
416 if (!isa<GlobalValue>(
C)) {
417 C->handleOperandChange(
this, New);
425 if (
BasicBlock *BB = dyn_cast<BasicBlock>(
this))
426 BB->replaceSuccessorsPhiUsesWith(cast<BasicBlock>(New));
430 doRAUW(New, ReplaceMetadataUses::Yes);
434 doRAUW(New, ReplaceMetadataUses::No);
440 assert(New &&
"Value::replaceUsesOutsideBlock(<null>, BB) is invalid!");
442 "this->replaceUsesOutsideBlock(expr(this), BB) is NOT valid!");
444 "replaceUses of value with new value of different type!");
445 assert(BB &&
"Basic block that may contain a use of 'New' must be defined\n");
452 if (Usr && Usr->getParent() == BB)
462 PSK_ZeroIndicesAndAliases,
463 PSK_ZeroIndicesAndAliasesAndInvariantGroups,
464 PSK_InBoundsConstantIndices,
468 template <Po
interStripKind StripKind>
469 static const Value *stripPointerCastsAndOffsets(
const Value *V) {
479 if (
auto *
GEP = dyn_cast<GEPOperator>(V)) {
481 case PSK_ZeroIndicesAndAliases:
482 case PSK_ZeroIndicesAndAliasesAndInvariantGroups:
483 case PSK_ZeroIndices:
484 if (!
GEP->hasAllZeroIndices())
487 case PSK_InBoundsConstantIndices:
488 if (!
GEP->hasAllConstantIndices())
492 if (!
GEP->isInBounds())
496 V =
GEP->getPointerOperand();
499 V = cast<Operator>(V)->getOperand(0);
500 }
else if (
auto *GA = dyn_cast<GlobalAlias>(V)) {
501 if (StripKind == PSK_ZeroIndices || GA->isInterposable())
503 V = GA->getAliasee();
505 if (
const auto *Call = dyn_cast<CallBase>(V)) {
506 if (
const Value *RV = Call->getReturnedArgOperand()) {
513 if (StripKind == PSK_ZeroIndicesAndAliasesAndInvariantGroups &&
516 V = Call->getArgOperand(0);
523 }
while (Visited.
insert(V).second);
530 return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndAliases>(
this);
534 return stripPointerCastsAndOffsets<PSK_ZeroIndices>(
this);
538 return stripPointerCastsAndOffsets<PSK_InBoundsConstantIndices>(
this);
542 return stripPointerCastsAndOffsets<PSK_ZeroIndicesAndAliasesAndInvariantGroups>(
553 getType())->getAddressSpace()) &&
554 "The offset bit width does not match the DL specification.");
560 const Value *V =
this;
562 if (
auto *
GEP = dyn_cast<GEPOperator>(V)) {
563 if (!
GEP->isInBounds())
565 APInt GEPOffset(Offset);
566 if (!
GEP->accumulateConstantOffset(DL, GEPOffset))
569 V =
GEP->getPointerOperand();
571 V = cast<Operator>(V)->getOperand(0);
572 }
else if (
auto *GA = dyn_cast<GlobalAlias>(V)) {
573 V = GA->getAliasee();
575 if (
const auto *Call = dyn_cast<CallBase>(V))
576 if (
const Value *RV = Call->getReturnedArgOperand()) {
584 }
while (Visited.insert(V).second);
590 return stripPointerCastsAndOffsets<PSK_InBounds>(
this);
594 bool &CanBeNull)
const {
597 uint64_t DerefBytes = 0;
599 if (
const Argument *A = dyn_cast<Argument>(
this)) {
600 DerefBytes = A->getDereferenceableBytes();
601 if (DerefBytes == 0 && (A->hasByValAttr() || A->hasStructRetAttr())) {
602 Type *PT = cast<PointerType>(A->getType())->getElementType();
606 if (DerefBytes == 0) {
607 DerefBytes = A->getDereferenceableOrNullBytes();
610 }
else if (
const auto *Call = dyn_cast<CallBase>(
this)) {
612 if (DerefBytes == 0) {
617 }
else if (
const LoadInst *LI = dyn_cast<LoadInst>(
this)) {
619 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
622 if (DerefBytes == 0) {
625 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
630 }
else if (
auto *AI = dyn_cast<AllocaInst>(
this)) {
631 if (!AI->isArrayAllocation()) {
635 }
else if (
auto *GV = dyn_cast<GlobalVariable>(
this)) {
636 if (GV->getValueType()->isSized() && !GV->hasExternalWeakLinkage()) {
650 if (
auto *GO = dyn_cast<GlobalObject>(
this)) {
653 if (isa<Function>(GO))
655 Align = GO->getAlignment();
657 if (
auto *GVar = dyn_cast<GlobalVariable>(GO)) {
658 Type *ObjectType = GVar->getValueType();
663 if (GVar->isStrongDefinitionForLinker())
670 }
else if (
const Argument *A = dyn_cast<Argument>(
this)) {
671 Align = A->getParamAlignment();
673 if (!Align && A->hasStructRetAttr()) {
675 Type *EltTy = cast<PointerType>(A->getType())->getElementType();
679 }
else if (
const AllocaInst *AI = dyn_cast<AllocaInst>(
this)) {
680 Align = AI->getAlignment();
682 Type *AllocatedType = AI->getAllocatedType();
686 }
else if (
const auto *Call = dyn_cast<CallBase>(
this))
687 Align = Call->getAttributes().getRetAlignment();
688 else if (
const LoadInst *LI = dyn_cast<LoadInst>(
this))
690 ConstantInt *CI = mdconst::extract<ConstantInt>(MD->getOperand(0));
700 if (PN && PN->getParent() == CurBB)
708 if (!UseList || !UseList->Next)
713 Use *Current = UseList->Next;
714 Head->Next =
nullptr;
716 Use *Next = Current->Next;
717 Current->Next = Head;
718 Head->setPrev(&Current->Next);
723 Head->setPrev(&UseList);
729 return Arg->hasSwiftErrorAttr();
741 assert(List &&
"Handle list is null?");
748 Next->setPrevPtr(&Next);
749 assert(getValPtr() == Next->getValPtr() &&
"Added to wrong list?");
753 void ValueHandleBase::AddToExistingUseListAfter(
ValueHandleBase *List) {
754 assert(List &&
"Must insert after existing node");
757 setPrevPtr(&List->Next);
760 Next->setPrevPtr(&Next);
763 void ValueHandleBase::AddToUseList() {
764 assert(getValPtr() &&
"Null pointer doesn't have a use list!");
768 if (getValPtr()->HasValueHandle) {
772 assert(Entry &&
"Value doesn't have any handles?");
773 AddToExistingUseList(&Entry);
786 assert(!Entry &&
"Value really did already have handles?");
787 AddToExistingUseList(&Entry);
788 getValPtr()->HasValueHandle =
true;
793 Handles.
size() == 1) {
799 E = Handles.
end();
I !=
E; ++
I) {
800 assert(
I->second &&
I->first ==
I->second->getValPtr() &&
801 "List invariant broken!");
802 I->second->setPrevPtr(&
I->second);
807 assert(getValPtr() && getValPtr()->HasValueHandle &&
808 "Pointer doesn't have a use list!");
812 assert(*PrevPtr ==
this &&
"List invariant broken");
816 assert(Next->getPrevPtr() == &Next &&
"List invariant broken");
817 Next->setPrevPtr(PrevPtr);
827 Handles.
erase(getValPtr());
828 getValPtr()->HasValueHandle =
false;
833 assert(V->HasValueHandle &&
"Should only be called if ValueHandles present");
839 assert(Entry &&
"Value bit set but no entries exist");
851 Iterator.RemoveFromUseList();
852 Iterator.AddToExistingUseListAfter(Entry);
853 assert(Entry->Next == &Iterator &&
"Loop invariant broken.");
855 switch (Entry->getKind()) {
862 Entry->operator=(
nullptr);
872 if (V->HasValueHandle) {
873 #ifndef NDEBUG // Only in +Asserts mode... 886 assert(Old->HasValueHandle &&
"Should only be called if ValueHandles present");
887 assert(Old != New &&
"Changing value into itself!");
889 "replaceAllUses of value with new value of different type!");
896 assert(Entry &&
"Value bit set but no entries exist");
903 Iterator.RemoveFromUseList();
904 Iterator.AddToExistingUseListAfter(Entry);
905 assert(Entry->Next == &Iterator &&
"Loop invariant broken.");
907 switch (Entry->getKind()) {
914 Entry->operator=(New);
918 static_cast<CallbackVH*
>(Entry)->allUsesReplacedWith(New);
926 if (Old->HasValueHandle)
927 for (Entry = pImpl->ValueHandles[Old]; Entry; Entry = Entry->Next)
928 switch (Entry->getKind()) {
930 dbgs() <<
"After RAUW from " << *Old->
getType() <<
" %" 934 "A weak tracking value handle still pointed to the old value!\n");
942 void CallbackVH::anchor() {}
This is the common base class of value handles.
A parsed version of the target data layout string in and methods for querying it. ...
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
This class provides a symbol table of name/value pairs.
static void ValueIsDeleted(Value *V)
unsigned getIndexSizeInBits(unsigned AS) const
Size in bits of index used for address calculation in getelementptr.
GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2)
This class represents an incoming formal argument to a Function.
unsigned getValueID() const
Return an ID for the concrete type of this object.
const Value * stripInBoundsOffsets() const
Strip off pointer casts and inbounds GEPs.
This class represents lattice values for constants.
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. ...
bool isSized(SmallPtrSetImpl< Type *> *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
void setValue(const ValueTy &V)
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
bool isTriviallyEmpty() const
Check if this twine is trivially empty; a false return value does not necessarily mean the twine is e...
StringRef toStringRef(SmallVectorImpl< char > &Out) const
This returns the twine as a single StringRef if it can be represented as such.
bool isSwiftError() const
Return true if this alloca is used as a swifterror argument to a call.
LLVMContext & getContext() const
All values hold a context through their type.
void deleteValue()
Delete a pointer to a generic Value.
const void * getPointerIntoBucketsArray() const
getPointerIntoBucketsArray() - Return an opaque pointer into the buckets array.
An instruction for reading from memory.
const Value * DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB) const
Translate PHI node to its predecessor from the given basic block.
unsigned getBitWidth() const
Return the number of bits in the APInt.
unsigned getPointerAlignment(const DataLayout &DL) const
Returns an alignment of the pointer value.
LLVMContext & getContext() const
Return the LLVMContext in which this type was uniqued.
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)
amdgpu Simplify well known AMD library false Value Value const Twine & Name
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
A Use represents the edge between a Value definition and its users.
void setName(const Twine &Name)
Change the name of the value.
void assertModuleIsMaterializedImpl() const
DenseMap< const Value *, ValueName * > ValueNames
static StringMapEntry * Create(StringRef Key, AllocatorTy &Allocator, InitTy &&... InitVals)
Create a StringMapEntry for the specified key construct the value using InitiVals.
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.
Type * getType() const
All values are typed, get the type of this value.
bool isFirstClassType() const
Return true if the type is "first class", meaning it is a valid type for a Value. ...
bool isSwiftError() const
Return true if this value is a swifterror value.
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
static void ValueIsRAUWd(Value *Old, Value *New)
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
void takeName(Value *V)
Transfer the name from V to this value.
use_iterator_impl< Use > use_iterator
bool isVoidTy() const
Return true if this is 'void'.
initializer< Ty > init(const Ty &Val)
bool erase(const KeyT &Val)
void RemoveFromUseList()
Remove this ValueHandle from its current use list.
bool hasNUsesOrMore(unsigned N) const
Return true if this value has N users or more.
bool hasNUses(unsigned N) const
Return true if this Value has exactly N users.
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.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This is an important base class in LLVM.
void replaceNonMetadataUsesWith(Value *V)
Change non-metadata uses of this to point to a new Value.
Value * getIncomingValueForBlock(const BasicBlock *BB) const
This file contains the declarations for the subclasses of Constant, which represent the different fla...
bool isPointerTy() const
True if this is an instance of PointerType.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
unsigned getPrefTypeAlignment(Type *Ty) const
Returns the preferred stack/global alignment for the specified type.
bool isUsedInBasicBlock(const BasicBlock *BB) const
Check if this value is used in the specified basic block.
ValueHandlesTy ValueHandles
bool hasNItemsOrMore(IterTy &&Begin, IterTy &&End, unsigned N, typename std::enable_if< !std::is_same< typename std::iterator_traits< typename std::remove_reference< decltype(Begin)>::type >::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr)
Return true if the sequence [Begin, End) has N or more items.
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs, and aliases.
Value(Type *Ty, unsigned scid)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVMContextImpl *const pImpl
Iterator for intrusive lists based on ilist_node.
static cl::opt< unsigned > NonGlobalValueMaxNameSize("non-global-value-max-name-size", cl::Hidden, cl::init(1024), cl::desc("Maximum size for the name of non-global values."))
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
uint64_t getLimitedValue(uint64_t Limit=~0ULL) const
getLimitedValue - If the value is smaller than the specified limit, return it, otherwise return the l...
This is the shared class of boolean and integer constants.
bool materialized_use_empty() const
Module.h This file contains the declarations for the Module class.
ValueName * getValueName() const
unsigned getABITypeAlignment(Type *Ty) const
Returns the minimum ABI-required alignment for the specified type.
uint64_t getPointerDereferenceableBytes(const DataLayout &DL, bool &CanBeNull) const
Returns the number of bytes known to be dereferenceable for the pointer value.
bool isPointerIntoBucketsArray(const void *Ptr) const
isPointerIntoBucketsArray - Return true if the specified pointer points somewhere into the DenseMap's...
const Value * stripPointerCastsAndInvariantGroups() const
Strip off pointer casts, all-zero GEPs, aliases and invariant group info.
bool isUsedByMetadata() const
Return true if there is metadata referencing this value.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
unsigned getPreferredAlignment(const GlobalVariable *GV) const
Returns the preferred alignment of the specified global.
const Value * stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, APInt &Offset) const
Accumulate offsets from stripInBoundsConstantOffsets().
Class for arbitrary precision integers.
iterator_range< user_iterator > users()
user_iterator_impl< const User > const_user_iterator
amdgpu Simplify well known AMD library false Value Value * Arg
void reverseUseList()
Reverse the use-list.
bool isMaterialized() const
unsigned getNumUses() const
This method computes the number of uses of this Value.
const Value * stripPointerCastsNoFollowAliases() const
Strip off pointer casts and all-zero GEPs.
StringRef getName() const
Return a constant reference to the value's name.
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.
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)
~Value()
Value's destructor should be virtual by design, but that would require that Value and all of its subc...
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.
uint64_t getTypeStoreSize(Type *Ty) const
Returns the maximum number of bytes that may be overwritten by storing the specified type...
unsigned getOpcode() const
Return the opcode for this Instruction or ConstantExpr.
bool hasNItems(IterTy &&Begin, IterTy &&End, unsigned N, typename std::enable_if< !std::is_same< typename std::iterator_traits< typename std::remove_reference< decltype(Begin)>::type >::iterator_category, std::random_access_iterator_tag >::value, void >::type *=nullptr)
Return true if the sequence [Begin, End) has exactly N items.
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"...
const Value * stripInBoundsConstantOffsets() const
Strip off pointer casts and all-constant inbounds GEPs.
static bool contains(SmallPtrSetImpl< ConstantExpr *> &Cache, ConstantExpr *Expr, Constant *C)
bool isStructTy() const
True if this is an instance of StructType.
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.