llvm.org GIT mirror llvm / 5252c43
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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@154279 91177308-0d34-0410-b5e6-96231b3b80d8 Bill Wendling 8 years ago
2 changed file(s) with 42 addition(s) and 31 deletion(s). Raw diff Collapse all Expand all
4848 Tracking,
4949 Weak
5050 };
51
5152 private:
52
5353 PointerIntPair PrevPair;
5454 ValueHandleBase *Next;
55 Value *VP;
55
56 // A subclass may want to store some information along with the value
57 // pointer. Allow them to do this by making the value pointer a pointer-int
58 // pair. The 'setValPtrInt' and 'getValPtrInt' methods below give them this
59 // access.
60 PointerIntPair VP;
5661
5762 explicit ValueHandleBase(const ValueHandleBase&); // DO NOT IMPLEMENT.
5863 public:
5964 explicit ValueHandleBase(HandleBaseKind Kind)
60 : PrevPair(0, Kind), Next(0), VP(0) {}
65 : PrevPair(0, Kind), Next(0), VP(0, 0) {}
6166 ValueHandleBase(HandleBaseKind Kind, Value *V)
62 : PrevPair(0, Kind), Next(0), VP(V) {
63 if (isValid(VP))
67 : PrevPair(0, Kind), Next(0), VP(V, 0) {
68 if (isValid(VP.getPointer()))
6469 AddToUseList();
6570 }
6671 ValueHandleBase(HandleBaseKind Kind, const ValueHandleBase &RHS)
6772 : PrevPair(0, Kind), Next(0), VP(RHS.VP) {
68 if (isValid(VP))
73 if (isValid(VP.getPointer()))
6974 AddToExistingUseList(RHS.getPrevPtr());
7075 }
7176 ~ValueHandleBase() {
72 if (isValid(VP))
77 if (isValid(VP.getPointer()))
7378 RemoveFromUseList();
7479 }
7580
7681 Value *operator=(Value *RHS) {
77 if (VP == RHS) return RHS;
78 if (isValid(VP)) RemoveFromUseList();
79 VP = RHS;
80 if (isValid(VP)) AddToUseList();
82 if (VP.getPointer() == RHS) return RHS;
83 if (isValid(VP.getPointer())) RemoveFromUseList();
84 VP.setPointer(RHS);
85 if (isValid(VP.getPointer())) AddToUseList();
8186 return RHS;
8287 }
8388
8489 Value *operator=(const ValueHandleBase &RHS) {
85 if (VP == RHS.VP) return RHS.VP;
86 if (isValid(VP)) RemoveFromUseList();
87 VP = RHS.VP;
88 if (isValid(VP)) AddToExistingUseList(RHS.getPrevPtr());
89 return VP;
90 if (VP.getPointer() == RHS.VP.getPointer()) return RHS.VP.getPointer();
91 if (isValid(VP.getPointer())) RemoveFromUseList();
92 VP.setPointer(RHS.VP.getPointer());
93 if (isValid(VP.getPointer())) AddToExistingUseList(RHS.getPrevPtr());
94 return VP.getPointer();
9095 }
9196
9297 Value *operator->() const { return getValPtr(); }
9398 Value &operator*() const { return *getValPtr(); }
9499
95100 protected:
96 Value *getValPtr() const { return VP; }
101 Value *getValPtr() const { return VP.getPointer(); }
102
103 void setValPtrInt(unsigned K) { VP.setInt(K); }
104 unsigned getValPtrInt() const { return VP.getInt(); }
105
97106 static bool isValid(Value *V) {
98107 return V &&
99108 V != DenseMapInfo::getEmptyKey() &&
476476 setPrevPtr(List);
477477 if (Next) {
478478 Next->setPrevPtr(&Next);
479 assert(VP == Next->VP && "Added to wrong list?");
479 assert(VP.getPointer() == Next->VP.getPointer() && "Added to wrong list?");
480480 }
481481 }
482482
492492
493493 /// AddToUseList - Add this ValueHandle to the use list for VP.
494494 void ValueHandleBase::AddToUseList() {
495 assert(VP && "Null pointer doesn't have a use list!");
496
497 LLVMContextImpl *pImpl = VP->getContext().pImpl;
498
499 if (VP->HasValueHandle) {
495 assert(VP.getPointer() && "Null pointer doesn't have a use list!");
496
497 LLVMContextImpl *pImpl = VP.getPointer()->getContext().pImpl;
498
499 if (VP.getPointer()->HasValueHandle) {
500500 // If this value already has a ValueHandle, then it must be in the
501501 // ValueHandles map already.
502 ValueHandleBase *&Entry = pImpl->ValueHandles[VP];
502 ValueHandleBase *&Entry = pImpl->ValueHandles[VP.getPointer()];
503503 assert(Entry != 0 && "Value doesn't have any handles?");
504504 AddToExistingUseList(&Entry);
505505 return;
513513 DenseMap &Handles = pImpl->ValueHandles;
514514 const void *OldBucketPtr = Handles.getPointerIntoBucketsArray();
515515
516 ValueHandleBase *&Entry = Handles[VP];
516 ValueHandleBase *&Entry = Handles[VP.getPointer()];
517517 assert(Entry == 0 && "Value really did already have handles?");
518518 AddToExistingUseList(&Entry);
519 VP->HasValueHandle = true;
519 VP.getPointer()->HasValueHandle = true;
520520
521521 // If reallocation didn't happen or if this was the first insertion, don't
522522 // walk the table.
528528 // Okay, reallocation did happen. Fix the Prev Pointers.
529529 for (DenseMap::iterator I = Handles.begin(),
530530 E = Handles.end(); I != E; ++I) {
531 assert(I->second && I->first == I->second->VP && "List invariant broken!");
531 assert(I->second && I->first == I->second->VP.getPointer() &&
532 "List invariant broken!");
532533 I->second->setPrevPtr(&I->second);
533534 }
534535 }
535536
536537 /// RemoveFromUseList - Remove this ValueHandle from its current use list.
537538 void ValueHandleBase::RemoveFromUseList() {
538 assert(VP && VP->HasValueHandle && "Pointer doesn't have a use list!");
539 assert(VP.getPointer() && VP.getPointer()->HasValueHandle &&
540 "Pointer doesn't have a use list!");
539541
540542 // Unlink this from its use list.
541543 ValueHandleBase **PrevPtr = getPrevPtr();
551553 // If the Next pointer was null, then it is possible that this was the last
552554 // ValueHandle watching VP. If so, delete its entry from the ValueHandles
553555 // map.
554 LLVMContextImpl *pImpl = VP->getContext().pImpl;
556 LLVMContextImpl *pImpl = VP.getPointer()->getContext().pImpl;
555557 DenseMap &Handles = pImpl->ValueHandles;
556558 if (Handles.isPointerIntoBucketsArray(PrevPtr)) {
557 Handles.erase(VP);
558 VP->HasValueHandle = false;
559 Handles.erase(VP.getPointer());
560 VP.getPointer()->HasValueHandle = false;
559561 }
560562 }
561563