16
16
#ifndef LLVM_USE_H
17
17
#define LLVM_USE_H
18
18
19
- #include " llvm/ADT/ilist"
19
+ #include " llvm/Support/Casting.h"
20
+ #include " llvm/ADT/iterator"
20
21
21
22
namespace llvm {
22
23
23
- template <typename NodeTy> struct ilist_traits ;
24
24
class Value ;
25
25
class User ;
26
26
@@ -62,43 +62,28 @@ class Use {
62
62
Value *operator ->() { return Val; }
63
63
const Value *operator ->() const { return Val; }
64
64
65
+ Use *getNext () const { return Next; }
65
66
private:
66
- // NOTE!! The Next/Prev fields MUST stay at the start of this structure. The
67
- // end-token for the ilist is allocated as JUST the next/prev pair to reduce
68
- // memory usage instead of allocating an entire Use.
69
- struct NextPrevPtrs {
70
- Use *Next, *Prev;
71
- } UseLinks;
72
-
67
+ Use *Next, **Prev;
73
68
Value *Val;
74
69
User *U;
75
- friend struct ilist_traits <Use>;
76
- };
77
70
78
- template <>
79
- struct ilist_traits <Use> {
80
- static Use *getPrev (Use *N) { return N->UseLinks .Prev ; }
81
- static Use *getNext (Use *N) { return N->UseLinks .Next ; }
82
- static const Use *getPrev (const Use *N) { return N->UseLinks .Prev ; }
83
- static const Use *getNext (const Use *N) { return N->UseLinks .Next ; }
84
- static void setPrev (Use *N, Use *Prev) { N->UseLinks .Prev = Prev; }
85
- static void setNext (Use *N, Use *Next) { N->UseLinks .Next = Next; }
86
-
87
- // / createSentinel - this is used to create the end marker for the use list.
88
- // / Note that we only allocate a UseLinks structure, which is just enough to
89
- // / hold the next/prev pointers. This saves us 8 bytes of memory for every
90
- // / Value allocated.
91
- static Use *createSentinel () { return (Use*)new Use::NextPrevPtrs (); }
92
- static void destroySentinel (Use *S) { delete (Use::NextPrevPtrs*)S; }
93
-
94
- void addNodeToList (Use *NTy) {}
95
- void removeNodeFromList (Use *NTy) {}
96
- void transferNodesFromList (iplist<Use, ilist_traits> &L2,
97
- ilist_iterator<Use> first,
98
- ilist_iterator<Use> last) {}
99
- };
71
+ void addToList (Use **List) {
72
+ Next = *List;
73
+ if (Next) Next->Prev = &Next;
74
+ Prev = List;
75
+ *List = this ;
76
+ }
77
+ void removeFromList () {
78
+ *Prev = Next;
79
+ if (Next) Next->Prev = Prev;
80
+ }
100
81
82
+ friend class Value ;
83
+ };
101
84
85
+ // simplify_type - Allow clients to treat uses just like values when using
86
+ // casting operators.
102
87
template <> struct simplify_type <Use> {
103
88
typedef Value* SimpleType;
104
89
static SimpleType getSimplifiedValue (const Use &Val) {
@@ -112,64 +97,49 @@ template<> struct simplify_type<const Use> {
112
97
}
113
98
};
114
99
115
- struct UseListIteratorWrapper : public iplist <Use>::iterator {
116
- typedef iplist<Use>::iterator Super;
117
- UseListIteratorWrapper () {}
118
- UseListIteratorWrapper (const Super &RHS) : Super(RHS) {}
119
100
120
- UseListIteratorWrapper &operator =(const Super &RHS) {
121
- Super::operator =(RHS);
122
- return *this ;
123
- }
124
101
125
- inline User *operator *() const ;
126
- User *operator ->() const { return operator *(); }
102
+ template <typename UserTy> // UserTy == 'User' or 'const User'
103
+ class value_use_iterator : public forward_iterator <UserTy*, ptrdiff_t > {
104
+ typedef forward_iterator<UserTy*, ptrdiff_t > super;
105
+ typedef value_use_iterator<UserTy> _Self;
106
+
107
+ Use *U;
108
+ value_use_iterator (Use *u) : U(u) {}
109
+ friend class Value ;
110
+ public:
111
+ typedef typename super::reference reference;
112
+ typedef typename super::pointer pointer;
127
113
128
- UseListIteratorWrapper operator --() { return Super:: operator --(); }
129
- UseListIteratorWrapper operator ++ () { return Super:: operator ++(); }
114
+ value_use_iterator ( const _Self &I) : U(I.U) { }
115
+ value_use_iterator () {}
130
116
131
- UseListIteratorWrapper operator --(int ) { // postdecrement operators...
132
- UseListIteratorWrapper tmp = *this ;
133
- --*this ;
134
- return tmp;
117
+ bool operator ==(const _Self &x) const {
118
+ return U == x.U ;
135
119
}
136
- UseListIteratorWrapper operator ++(int ) { // postincrement operators...
137
- UseListIteratorWrapper tmp = *this ;
138
- ++*this ;
139
- return tmp;
120
+ bool operator !=(const _Self &x) const {
121
+ return !operator ==(x);
140
122
}
141
- };
142
-
143
- struct UseListConstIteratorWrapper : public iplist <Use>::const_iterator {
144
- typedef iplist<Use>::const_iterator Super;
145
- UseListConstIteratorWrapper () {}
146
- UseListConstIteratorWrapper (const Super &RHS) : Super(RHS) {}
147
123
148
- // Allow conversion from non-const to const iterators
149
- UseListConstIteratorWrapper (const UseListIteratorWrapper &RHS) : Super(RHS) {}
150
- UseListConstIteratorWrapper (const iplist<Use>::iterator &RHS) : Super(RHS) {}
151
-
152
- UseListConstIteratorWrapper &operator =(const Super &RHS) {
153
- Super::operator =(RHS);
154
- return *this ;
124
+ // Iterator traversal: forward iteration only
125
+ _Self &operator ++() { // Preincrement
126
+ assert (U && " Cannot increment end iterator!" );
127
+ U = U->getNext ();
128
+ return *this ;
129
+ }
130
+ _Self operator ++(int ) { // Postincrement
131
+ _Self tmp = *this ; ++*this ; return tmp;
155
132
}
156
133
157
- inline const User *operator *() const ;
158
- const User *operator ->() const { return operator *(); }
134
+ // Retrieve a reference to the current SCC
135
+ UserTy *operator *() const {
136
+ assert (U && " Cannot increment end iterator!" );
137
+ return U->getUser ();
138
+ }
159
139
160
- UseListConstIteratorWrapper operator --() { return Super::operator --(); }
161
- UseListConstIteratorWrapper operator ++() { return Super::operator ++(); }
140
+ UserTy *operator ->() const { return operator *(); }
162
141
163
- UseListConstIteratorWrapper operator --(int ) { // postdecrement operators...
164
- UseListConstIteratorWrapper tmp = *this ;
165
- --*this ;
166
- return tmp;
167
- }
168
- UseListConstIteratorWrapper operator ++(int ) { // postincrement operators...
169
- UseListConstIteratorWrapper tmp = *this ;
170
- ++*this ;
171
- return tmp;
172
- }
142
+ Use &getUse () const { return *U; }
173
143
};
174
144
175
145
} // End llvm namespace
0 commit comments