-
Notifications
You must be signed in to change notification settings - Fork 13.4k
IR: Remove reference counts from ConstantData #137314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
arsenm
wants to merge
2
commits into
users/arsenm/ir/remove-uselist-for-constantdata
Choose a base branch
from
users/arsenm/ir/remove-constantdata-reference-counts
base: users/arsenm/ir/remove-uselist-for-constantdata
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,10 +116,7 @@ class Value { | |
|
||
private: | ||
Type *VTy; | ||
union { | ||
Use *List = nullptr; | ||
unsigned Count; | ||
} Uses; | ||
Use *UseList = nullptr; | ||
|
||
friend class ValueAsMetadata; // Allow access to IsUsedByMD. | ||
friend class ValueHandleBase; // Allow access to HasValueHandle. | ||
|
@@ -347,23 +344,21 @@ class Value { | |
|
||
bool use_empty() const { | ||
assertModuleIsMaterialized(); | ||
return hasUseList() ? Uses.List == nullptr : Uses.Count == 0; | ||
return UseList == nullptr; | ||
} | ||
|
||
bool materialized_use_empty() const { | ||
return hasUseList() ? Uses.List == nullptr : !Uses.Count; | ||
} | ||
bool materialized_use_empty() const { return UseList == nullptr; } | ||
|
||
using use_iterator = use_iterator_impl<Use>; | ||
using const_use_iterator = use_iterator_impl<const Use>; | ||
|
||
use_iterator materialized_use_begin() { | ||
assert(hasUseList()); | ||
return use_iterator(Uses.List); | ||
return use_iterator(UseList); | ||
} | ||
const_use_iterator materialized_use_begin() const { | ||
assert(hasUseList()); | ||
return const_use_iterator(Uses.List); | ||
return const_use_iterator(UseList); | ||
} | ||
use_iterator use_begin() { | ||
assertModuleIsMaterialized(); | ||
|
@@ -397,11 +392,11 @@ class Value { | |
|
||
user_iterator materialized_user_begin() { | ||
assert(hasUseList()); | ||
return user_iterator(Uses.List); | ||
return user_iterator(UseList); | ||
} | ||
const_user_iterator materialized_user_begin() const { | ||
assert(hasUseList()); | ||
return const_user_iterator(Uses.List); | ||
return const_user_iterator(UseList); | ||
} | ||
user_iterator user_begin() { | ||
assertModuleIsMaterialized(); | ||
|
@@ -440,11 +435,7 @@ class Value { | |
/// | ||
/// This is specialized because it is a common request and does not require | ||
/// traversing the whole use list. | ||
bool hasOneUse() const { | ||
if (!hasUseList()) | ||
return Uses.Count == 1; | ||
return hasSingleElement(uses()); | ||
} | ||
bool hasOneUse() const { return UseList && hasSingleElement(uses()); } | ||
|
||
/// Return true if this Value has exactly N uses. | ||
bool hasNUses(unsigned N) const; | ||
|
@@ -518,17 +509,8 @@ class Value { | |
|
||
/// This method should only be used by the Use class. | ||
void addUse(Use &U) { | ||
if (hasUseList()) | ||
U.addToList(Uses.List); | ||
else | ||
U.addToList(Uses.Count); | ||
} | ||
|
||
void removeUse(Use &U) { | ||
if (hasUseList()) | ||
U.removeFromList(Uses.List); | ||
else | ||
U.removeFromList(Uses.Count); | ||
if (UseList || hasUseList()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the |
||
U.addToList(&UseList); | ||
} | ||
|
||
/// Concrete subclass of this. | ||
|
@@ -870,8 +852,7 @@ class Value { | |
/// | ||
/// \return the first element in the list. | ||
/// | ||
/// \note Completely ignores \a Use::PrevOrCount (doesn't read, doesn't | ||
/// update). | ||
/// \note Completely ignores \a Use::Prev (doesn't read, doesn't update). | ||
template <class Compare> | ||
static Use *mergeUseLists(Use *L, Use *R, Compare Cmp) { | ||
Use *Merged; | ||
|
@@ -917,47 +898,8 @@ inline raw_ostream &operator<<(raw_ostream &OS, const Value &V) { | |
return OS; | ||
} | ||
|
||
inline Use::~Use() { | ||
if (Val) | ||
Val->removeUse(*this); | ||
} | ||
|
||
void Use::addToList(unsigned &Count) { | ||
assert(isa<ConstantData>(Val) && "Only ConstantData is ref-counted"); | ||
++Count; | ||
|
||
// We don't have a uselist - clear the remnant if we are replacing a | ||
// non-constant value. | ||
Prev = nullptr; | ||
Next = nullptr; | ||
} | ||
|
||
void Use::addToList(Use *&List) { | ||
assert(!isa<ConstantData>(Val) && "ConstantData has no use-list"); | ||
|
||
Next = List; | ||
if (Next) | ||
Next->Prev = &Next; | ||
Prev = &List; | ||
List = this; | ||
} | ||
|
||
void Use::removeFromList(unsigned &Count) { | ||
assert(isa<ConstantData>(Val)); | ||
assert(Count > 0 && "reference count underflow"); | ||
assert(!Prev && !Next && "should not have uselist remnant"); | ||
--Count; | ||
} | ||
|
||
void Use::removeFromList(Use *&List) { | ||
*Prev = Next; | ||
if (Next) | ||
Next->Prev = Prev; | ||
} | ||
|
||
void Use::set(Value *V) { | ||
if (Val) | ||
Val->removeUse(*this); | ||
removeFromList(); | ||
Val = V; | ||
if (V) | ||
V->addUse(*this); | ||
|
@@ -973,8 +915,28 @@ const Use &Use::operator=(const Use &RHS) { | |
return *this; | ||
} | ||
|
||
void Use::addToList(Use **List) { | ||
Next = *List; | ||
if (Next) | ||
Next->Prev = &Next; | ||
Prev = List; | ||
*Prev = this; | ||
} | ||
|
||
void Use::removeFromList() { | ||
if (Prev) { | ||
*Prev = Next; | ||
if (Next) { | ||
Next->Prev = Prev; | ||
Next = nullptr; | ||
} | ||
|
||
Prev = nullptr; | ||
} | ||
} | ||
|
||
template <class Compare> void Value::sortUseList(Compare Cmp) { | ||
if (!hasUseList() || !Uses.List || !Uses.List->Next) | ||
if (!UseList || !UseList->Next) | ||
// No need to sort 0 or 1 uses. | ||
return; | ||
|
||
|
@@ -987,10 +949,10 @@ template <class Compare> void Value::sortUseList(Compare Cmp) { | |
Use *Slots[MaxSlots]; | ||
|
||
// Collect the first use, turning it into a single-item list. | ||
Use *Next = Uses.List->Next; | ||
Uses.List->Next = nullptr; | ||
Use *Next = UseList->Next; | ||
UseList->Next = nullptr; | ||
unsigned NumSlots = 1; | ||
Slots[0] = Uses.List; | ||
Slots[0] = UseList; | ||
|
||
// Collect all but the last use. | ||
while (Next->Next) { | ||
|
@@ -1026,15 +988,15 @@ template <class Compare> void Value::sortUseList(Compare Cmp) { | |
// Merge all the lists together. | ||
assert(Next && "Expected one more Use"); | ||
assert(!Next->Next && "Expected only one Use"); | ||
Uses.List = Next; | ||
UseList = Next; | ||
for (unsigned I = 0; I < NumSlots; ++I) | ||
if (Slots[I]) | ||
// Since the uses in Slots[I] originally preceded those in Uses.List, send | ||
// Since the uses in Slots[I] originally preceded those in UseList, send | ||
// Slots[I] in as the left parameter to maintain a stable sort. | ||
Uses.List = mergeUseLists(Slots[I], Uses.List, Cmp); | ||
UseList = mergeUseLists(Slots[I], UseList, Cmp); | ||
|
||
// Fix the Prev pointers. | ||
for (Use *I = Uses.List, **Prev = &Uses.List; I; I = I->Next) { | ||
for (Use *I = UseList, **Prev = &UseList; I; I = I->Next) { | ||
I->Prev = Prev; | ||
Prev = &I->Next; | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The previous PR moved the method away from here -- should move them back again? I think it makes more sense to define them here than in Value.h.