39
39
#include < algorithm>
40
40
#include < cassert>
41
41
#include < cstdint>
42
+ #include < deque>
42
43
#include < iterator>
43
44
#include < limits>
44
45
#include < utility>
@@ -62,6 +63,9 @@ static cl::opt<unsigned> MaxBitSplit("hexbit-max-bitsplit", cl::Hidden,
62
63
cl::init (std::numeric_limits<unsigned >::max()));
63
64
static unsigned CountBitSplit = 0 ;
64
65
66
+ static cl::opt<unsigned > RegisterSetLimit (" hexbit-registerset-limit" ,
67
+ cl::Hidden, cl::init(1000 ));
68
+
65
69
namespace llvm {
66
70
67
71
void initializeHexagonBitSimplifyPass (PassRegistry& Registry);
@@ -72,23 +76,29 @@ namespace llvm {
72
76
namespace {
73
77
74
78
// Set of virtual registers, based on BitVector.
75
- struct RegisterSet : private BitVector {
79
+ struct RegisterSet {
76
80
RegisterSet () = default ;
77
- explicit RegisterSet (unsigned s, bool t = false ) : BitVector (s, t) {}
81
+ explicit RegisterSet (unsigned s, bool t = false ) : Bits (s, t) {}
78
82
RegisterSet (const RegisterSet &RS) = default ;
79
83
80
- using BitVector::clear;
81
- using BitVector::count;
84
+ void clear () {
85
+ Bits.clear ();
86
+ LRU.clear ();
87
+ }
88
+
89
+ unsigned count () const {
90
+ return Bits.count ();
91
+ }
82
92
83
93
unsigned find_first () const {
84
- int First = BitVector:: find_first ();
94
+ int First = Bits. find_first ();
85
95
if (First < 0 )
86
96
return 0 ;
87
97
return x2v (First);
88
98
}
89
99
90
100
unsigned find_next (unsigned Prev) const {
91
- int Next = BitVector:: find_next (v2x (Prev));
101
+ int Next = Bits. find_next (v2x (Prev));
92
102
if (Next < 0 )
93
103
return 0 ;
94
104
return x2v (Next);
@@ -97,54 +107,72 @@ namespace {
97
107
RegisterSet &insert (unsigned R) {
98
108
unsigned Idx = v2x (R);
99
109
ensure (Idx);
100
- return static_cast <RegisterSet&>(BitVector::set (Idx));
110
+ bool Exists = Bits.test (Idx);
111
+ Bits.set (Idx);
112
+ if (!Exists) {
113
+ LRU.push_back (Idx);
114
+ if (LRU.size () > RegisterSetLimit) {
115
+ unsigned T = LRU.front ();
116
+ Bits.reset (T);
117
+ LRU.pop_front ();
118
+ }
119
+ }
120
+ return *this ;
101
121
}
102
122
RegisterSet &remove (unsigned R) {
103
123
unsigned Idx = v2x (R);
104
- if (Idx >= size ())
105
- return *this ;
106
- return static_cast <RegisterSet&>(BitVector::reset (Idx));
124
+ if (Idx < Bits.size ()) {
125
+ bool Exists = Bits.test (Idx);
126
+ Bits.reset (Idx);
127
+ if (Exists) {
128
+ auto F = llvm::find (LRU, Idx);
129
+ assert (F != LRU.end ());
130
+ LRU.erase (F);
131
+ }
132
+ }
133
+ return *this ;
107
134
}
108
135
109
136
RegisterSet &insert (const RegisterSet &Rs) {
110
- return static_cast <RegisterSet&>(BitVector::operator |=(Rs));
137
+ for (unsigned R = Rs.find_first (); R; R = Rs.find_next (R))
138
+ insert (R);
139
+ return *this ;
111
140
}
112
141
RegisterSet &remove (const RegisterSet &Rs) {
113
- return static_cast <RegisterSet&>(BitVector::reset (Rs));
142
+ for (unsigned R = Rs.find_first (); R; R = Rs.find_next (R))
143
+ remove (R);
144
+ return *this ;
114
145
}
115
146
116
- reference operator [](unsigned R) {
117
- unsigned Idx = v2x (R);
118
- ensure (Idx);
119
- return BitVector::operator [](Idx);
120
- }
121
147
bool operator [](unsigned R) const {
122
148
unsigned Idx = v2x (R);
123
- assert (Idx < size ());
124
- return BitVector::operator [](Idx);
149
+ return Idx < Bits.size () ? Bits[Idx] : false ;
125
150
}
126
151
bool has (unsigned R) const {
127
152
unsigned Idx = v2x (R);
128
- if (Idx >= size ())
153
+ if (Idx >= Bits. size ())
129
154
return false ;
130
- return BitVector:: test (Idx);
155
+ return Bits. test (Idx);
131
156
}
132
157
133
158
bool empty () const {
134
- return !BitVector:: any ();
159
+ return !Bits. any ();
135
160
}
136
161
bool includes (const RegisterSet &Rs) const {
137
- // A.BitVector:: test(B) <=> A-B != {}
138
- return !Rs.BitVector :: test (* this );
162
+ // A.test(B) <=> A-B != {}
163
+ return !Rs.Bits . test (Bits );
139
164
}
140
165
bool intersects (const RegisterSet &Rs) const {
141
- return BitVector:: anyCommon (Rs);
166
+ return Bits. anyCommon (Rs. Bits );
142
167
}
143
168
144
169
private:
170
+ BitVector Bits;
171
+ std::deque<unsigned > LRU;
172
+
145
173
void ensure (unsigned Idx) {
146
- if (size () <= Idx)
147
- resize (std::max (Idx+1 , 32U ));
174
+ if (Bits. size () <= Idx)
175
+ Bits. resize (std::max (Idx+1 , 32U ));
148
176
}
149
177
150
178
static inline unsigned v2x (unsigned v) {
0 commit comments