Allow subclasses of the ValueHandleBase to store information as part of the

value pointer by making the value pointer into a pointer-int pair with 2 bits
available for flags.

llvm-svn: 154279
This commit is contained in:
Bill Wendling 2012-04-08 10:16:43 +00:00
parent a6412fb8c0
commit d13bec8fa9
2 changed files with 40 additions and 29 deletions

View File

@ -49,52 +49,61 @@ protected:
Tracking,
Weak
};
private:
private:
PointerIntPair<ValueHandleBase**, 2, HandleBaseKind> PrevPair;
ValueHandleBase *Next;
Value *VP;
// A subclass may want to store some information along with the value
// pointer. Allow them to do this by making the value pointer a pointer-int
// pair. The 'setValPtrInt' and 'getValPtrInt' methods below give them this
// access.
PointerIntPair<Value*, 2> VP;
explicit ValueHandleBase(const ValueHandleBase&); // DO NOT IMPLEMENT.
public:
explicit ValueHandleBase(HandleBaseKind Kind)
: PrevPair(0, Kind), Next(0), VP(0) {}
: PrevPair(0, Kind), Next(0), VP(0, 0) {}
ValueHandleBase(HandleBaseKind Kind, Value *V)
: PrevPair(0, Kind), Next(0), VP(V) {
if (isValid(VP))
: PrevPair(0, Kind), Next(0), VP(V, 0) {
if (isValid(VP.getPointer()))
AddToUseList();
}
ValueHandleBase(HandleBaseKind Kind, const ValueHandleBase &RHS)
: PrevPair(0, Kind), Next(0), VP(RHS.VP) {
if (isValid(VP))
if (isValid(VP.getPointer()))
AddToExistingUseList(RHS.getPrevPtr());
}
~ValueHandleBase() {
if (isValid(VP))
if (isValid(VP.getPointer()))
RemoveFromUseList();
}
Value *operator=(Value *RHS) {
if (VP == RHS) return RHS;
if (isValid(VP)) RemoveFromUseList();
VP = RHS;
if (isValid(VP)) AddToUseList();
if (VP.getPointer() == RHS) return RHS;
if (isValid(VP.getPointer())) RemoveFromUseList();
VP.setPointer(RHS);
if (isValid(VP.getPointer())) AddToUseList();
return RHS;
}
Value *operator=(const ValueHandleBase &RHS) {
if (VP == RHS.VP) return RHS.VP;
if (isValid(VP)) RemoveFromUseList();
VP = RHS.VP;
if (isValid(VP)) AddToExistingUseList(RHS.getPrevPtr());
return VP;
if (VP.getPointer() == RHS.VP.getPointer()) return RHS.VP.getPointer();
if (isValid(VP.getPointer())) RemoveFromUseList();
VP.setPointer(RHS.VP.getPointer());
if (isValid(VP.getPointer())) AddToExistingUseList(RHS.getPrevPtr());
return VP.getPointer();
}
Value *operator->() const { return getValPtr(); }
Value &operator*() const { return *getValPtr(); }
protected:
Value *getValPtr() const { return VP; }
Value *getValPtr() const { return VP.getPointer(); }
void setValPtrInt(unsigned K) { VP.setInt(K); }
unsigned getValPtrInt() const { return VP.getInt(); }
static bool isValid(Value *V) {
return V &&
V != DenseMapInfo<Value *>::getEmptyKey() &&

View File

@ -477,7 +477,7 @@ void ValueHandleBase::AddToExistingUseList(ValueHandleBase **List) {
setPrevPtr(List);
if (Next) {
Next->setPrevPtr(&Next);
assert(VP == Next->VP && "Added to wrong list?");
assert(VP.getPointer() == Next->VP.getPointer() && "Added to wrong list?");
}
}
@ -493,14 +493,14 @@ void ValueHandleBase::AddToExistingUseListAfter(ValueHandleBase *List) {
/// AddToUseList - Add this ValueHandle to the use list for VP.
void ValueHandleBase::AddToUseList() {
assert(VP && "Null pointer doesn't have a use list!");
assert(VP.getPointer() && "Null pointer doesn't have a use list!");
LLVMContextImpl *pImpl = VP->getContext().pImpl;
LLVMContextImpl *pImpl = VP.getPointer()->getContext().pImpl;
if (VP->HasValueHandle) {
if (VP.getPointer()->HasValueHandle) {
// If this value already has a ValueHandle, then it must be in the
// ValueHandles map already.
ValueHandleBase *&Entry = pImpl->ValueHandles[VP];
ValueHandleBase *&Entry = pImpl->ValueHandles[VP.getPointer()];
assert(Entry != 0 && "Value doesn't have any handles?");
AddToExistingUseList(&Entry);
return;
@ -514,10 +514,10 @@ void ValueHandleBase::AddToUseList() {
DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;
const void *OldBucketPtr = Handles.getPointerIntoBucketsArray();
ValueHandleBase *&Entry = Handles[VP];
ValueHandleBase *&Entry = Handles[VP.getPointer()];
assert(Entry == 0 && "Value really did already have handles?");
AddToExistingUseList(&Entry);
VP->HasValueHandle = true;
VP.getPointer()->HasValueHandle = true;
// If reallocation didn't happen or if this was the first insertion, don't
// walk the table.
@ -529,14 +529,16 @@ void ValueHandleBase::AddToUseList() {
// Okay, reallocation did happen. Fix the Prev Pointers.
for (DenseMap<Value*, ValueHandleBase*>::iterator I = Handles.begin(),
E = Handles.end(); I != E; ++I) {
assert(I->second && I->first == I->second->VP && "List invariant broken!");
assert(I->second && I->first == I->second->VP.getPointer() &&
"List invariant broken!");
I->second->setPrevPtr(&I->second);
}
}
/// RemoveFromUseList - Remove this ValueHandle from its current use list.
void ValueHandleBase::RemoveFromUseList() {
assert(VP && VP->HasValueHandle && "Pointer doesn't have a use list!");
assert(VP.getPointer() && VP.getPointer()->HasValueHandle &&
"Pointer doesn't have a use list!");
// Unlink this from its use list.
ValueHandleBase **PrevPtr = getPrevPtr();
@ -552,11 +554,11 @@ void ValueHandleBase::RemoveFromUseList() {
// If the Next pointer was null, then it is possible that this was the last
// ValueHandle watching VP. If so, delete its entry from the ValueHandles
// map.
LLVMContextImpl *pImpl = VP->getContext().pImpl;
LLVMContextImpl *pImpl = VP.getPointer()->getContext().pImpl;
DenseMap<Value*, ValueHandleBase*> &Handles = pImpl->ValueHandles;
if (Handles.isPointerIntoBucketsArray(PrevPtr)) {
Handles.erase(VP);
VP->HasValueHandle = false;
Handles.erase(VP.getPointer());
VP.getPointer()->HasValueHandle = false;
}
}