Skip to content

Commit 16c93aa

Browse files
author
Krzysztof Parzyszek
committed
[Hexagon] Establish size limit for RegisterSet in hexbit
This should reduce compilation time for huge functions.
1 parent cfb8169 commit 16c93aa

File tree

1 file changed

+55
-27
lines changed

1 file changed

+55
-27
lines changed

llvm/lib/Target/Hexagon/HexagonBitSimplify.cpp

+55-27
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include <algorithm>
4040
#include <cassert>
4141
#include <cstdint>
42+
#include <deque>
4243
#include <iterator>
4344
#include <limits>
4445
#include <utility>
@@ -62,6 +63,9 @@ static cl::opt<unsigned> MaxBitSplit("hexbit-max-bitsplit", cl::Hidden,
6263
cl::init(std::numeric_limits<unsigned>::max()));
6364
static unsigned CountBitSplit = 0;
6465

66+
static cl::opt<unsigned> RegisterSetLimit("hexbit-registerset-limit",
67+
cl::Hidden, cl::init(1000));
68+
6569
namespace llvm {
6670

6771
void initializeHexagonBitSimplifyPass(PassRegistry& Registry);
@@ -72,23 +76,29 @@ namespace llvm {
7276
namespace {
7377

7478
// Set of virtual registers, based on BitVector.
75-
struct RegisterSet : private BitVector {
79+
struct RegisterSet {
7680
RegisterSet() = default;
77-
explicit RegisterSet(unsigned s, bool t = false) : BitVector(s, t) {}
81+
explicit RegisterSet(unsigned s, bool t = false) : Bits(s, t) {}
7882
RegisterSet(const RegisterSet &RS) = default;
7983

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+
}
8292

8393
unsigned find_first() const {
84-
int First = BitVector::find_first();
94+
int First = Bits.find_first();
8595
if (First < 0)
8696
return 0;
8797
return x2v(First);
8898
}
8999

90100
unsigned find_next(unsigned Prev) const {
91-
int Next = BitVector::find_next(v2x(Prev));
101+
int Next = Bits.find_next(v2x(Prev));
92102
if (Next < 0)
93103
return 0;
94104
return x2v(Next);
@@ -97,54 +107,72 @@ namespace {
97107
RegisterSet &insert(unsigned R) {
98108
unsigned Idx = v2x(R);
99109
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;
101121
}
102122
RegisterSet &remove(unsigned R) {
103123
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;
107134
}
108135

109136
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;
111140
}
112141
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;
114145
}
115146

116-
reference operator[](unsigned R) {
117-
unsigned Idx = v2x(R);
118-
ensure(Idx);
119-
return BitVector::operator[](Idx);
120-
}
121147
bool operator[](unsigned R) const {
122148
unsigned Idx = v2x(R);
123-
assert(Idx < size());
124-
return BitVector::operator[](Idx);
149+
return Idx < Bits.size() ? Bits[Idx] : false;
125150
}
126151
bool has(unsigned R) const {
127152
unsigned Idx = v2x(R);
128-
if (Idx >= size())
153+
if (Idx >= Bits.size())
129154
return false;
130-
return BitVector::test(Idx);
155+
return Bits.test(Idx);
131156
}
132157

133158
bool empty() const {
134-
return !BitVector::any();
159+
return !Bits.any();
135160
}
136161
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);
139164
}
140165
bool intersects(const RegisterSet &Rs) const {
141-
return BitVector::anyCommon(Rs);
166+
return Bits.anyCommon(Rs.Bits);
142167
}
143168

144169
private:
170+
BitVector Bits;
171+
std::deque<unsigned> LRU;
172+
145173
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));
148176
}
149177

150178
static inline unsigned v2x(unsigned v) {

0 commit comments

Comments
 (0)