diff options
author | Matt Arsenault <Matthew.Arsenault@amd.com> | 2025-05-06 17:20:37 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-05-06 17:20:37 +0200 |
commit | 87f312aad6ede636cd2de5d18f3058bf2caf5651 (patch) | |
tree | a4762bd4af0e57e75861c2093b0912464fb69e5d /llvm/lib/IR/Value.cpp | |
parent | a363ccaf18da166a51206070f074360ca35462ca (diff) | |
download | llvm-87f312aad6ede636cd2de5d18f3058bf2caf5651.zip llvm-87f312aad6ede636cd2de5d18f3058bf2caf5651.tar.gz llvm-87f312aad6ede636cd2de5d18f3058bf2caf5651.tar.bz2 |
IR: Remove uselist for constantdata (#137313)
This is a resurrected version of the patch attached to this RFC:
https://discourse.llvm.org/t/rfc-constantdata-should-not-have-use-lists/42606
In this adaptation, there are a few differences. In the original patch, the Use's
use list was replaced with an unsigned* to the reference count in the value. This
version leaves them as null and leaves the ref counting only in Value.
Remove use-lists from instances of ConstantData (which are shared
across modules and have no operands).
To continue supporting most of the use-list API, store a ref-count in
place of the use-list; this is for API like Value::use_empty and
Value::hasNUses. Operations that actually need the use-list -- like
Value::use_begin -- will assert.
This change has three benefits:
1. The compiler output cannot in any way depend on the use-list order
of instances of ConstantData.
2. There's no use-list traffic when adding and removing simple
constants from operand lists (although there is ref-count traffic;
YMMV).
3. It's cheaper to serialize use-lists (since we're no longer
serializing the use-list order of things like i32 0).
The downside is that you can't look at all the users of ConstantData,
but traversals of users of i32 0 are already ill-advised.
Possible follow-ups:
- Track if an instance of a ConstantVector/ConstantArray/etc. is known
to have all ConstantData arguments, and drop the use-lists to
ref-counts in those cases. Callers need to check Value::hasUseList
before iterating through the use-list.
- Remove even the ref-counts. I'm not sure they have any benefit
besides minimizing the scope of this commit, and maintaining the
counts is not free.
Fixes #58629
Co-authored-by: Duncan P. N. Exon Smith <dexonsmith@apple.com>
Diffstat (limited to 'llvm/lib/IR/Value.cpp')
-rw-r--r-- | llvm/lib/IR/Value.cpp | 26 |
1 files changed, 18 insertions, 8 deletions
diff --git a/llvm/lib/IR/Value.cpp b/llvm/lib/IR/Value.cpp index aa97b70..74a9605 100644 --- a/llvm/lib/IR/Value.cpp +++ b/llvm/lib/IR/Value.cpp @@ -53,7 +53,7 @@ static inline Type *checkType(Type *Ty) { Value::Value(Type *ty, unsigned scid) : SubclassID(scid), HasValueHandle(0), SubclassOptionalData(0), SubclassData(0), NumUserOperands(0), IsUsedByMD(false), HasName(false), - HasMetadata(false), VTy(checkType(ty)), UseList(nullptr) { + HasMetadata(false), VTy(checkType(ty)) { static_assert(ConstantFirstVal == 0, "!(SubclassID < ConstantFirstVal)"); // FIXME: Why isn't this in the subclass gunk?? // Note, we cannot call isa<CallInst> before the CallInst has been @@ -148,10 +148,14 @@ void Value::destroyValueName() { } bool Value::hasNUses(unsigned N) const { + if (!hasUseList()) + return Uses.Count == N; return hasNItems(use_begin(), use_end(), N); } bool Value::hasNUsesOrMore(unsigned N) const { + if (!hasUseList()) + return Uses.Count >= N; return hasNItemsOrMore(use_begin(), use_end(), N); } @@ -232,6 +236,8 @@ void Value::dropDroppableUse(Use &U) { } bool Value::isUsedInBasicBlock(const BasicBlock *BB) const { + assert(hasUseList() && "ConstantData has no use-list"); + // This can be computed either by scanning the instructions in BB, or by // scanning the use list of this Value. Both lists can be very long, but // usually one is quite short. @@ -253,6 +259,9 @@ bool Value::isUsedInBasicBlock(const BasicBlock *BB) const { } unsigned Value::getNumUses() const { + if (!hasUseList()) + return Uses.Count; + return (unsigned)std::distance(use_begin(), use_end()); } @@ -499,6 +508,7 @@ static bool contains(Value *Expr, Value *V) { #endif // NDEBUG void Value::doRAUW(Value *New, ReplaceMetadataUses ReplaceMetaUses) { + assert(hasUseList() && "Cannot replace constant data"); assert(New && "Value::replaceAllUsesWith(<null>) is invalid!"); assert(!contains(New, this) && "this->replaceAllUsesWith(expr(this)) is NOT valid!"); @@ -512,7 +522,7 @@ void Value::doRAUW(Value *New, ReplaceMetadataUses ReplaceMetaUses) { ValueAsMetadata::handleRAUW(this, New); while (!materialized_use_empty()) { - Use &U = *UseList; + Use &U = *Uses.List; // Must handle Constants specially, we cannot call replaceUsesOfWith on a // constant because they are uniqued. if (auto *C = dyn_cast<Constant>(U.getUser())) { @@ -844,7 +854,7 @@ bool Value::canBeFreed() const { // which is why we need the explicit opt in on a per collector basis. if (!F->hasGC()) return true; - + const auto &GCName = F->getGC(); if (GCName == "statepoint-example") { auto *PT = cast<PointerType>(this->getType()); @@ -1092,12 +1102,12 @@ const Value *Value::DoPHITranslation(const BasicBlock *CurBB, LLVMContext &Value::getContext() const { return VTy->getContext(); } void Value::reverseUseList() { - if (!UseList || !UseList->Next) + if (!Uses.List || !Uses.List->Next || !hasUseList()) // No need to reverse 0 or 1 uses. return; - Use *Head = UseList; - Use *Current = UseList->Next; + Use *Head = Uses.List; + Use *Current = Uses.List->Next; Head->Next = nullptr; while (Current) { Use *Next = Current->Next; @@ -1106,8 +1116,8 @@ void Value::reverseUseList() { Head = Current; Current = Next; } - UseList = Head; - Head->Prev = &UseList; + Uses.List = Head; + Head->Prev = &Uses.List; } bool Value::isSwiftError() const { |