Skip to content

Commit b268e24

Browse files
committed
[NFC][regalloc] Separate iteration from AllocationOrder
This separates the two concerns - encapsulation of traversal order; and iteration. Differential Revision: https://reviews.llvm.org/D88256
1 parent 2ea8fec commit b268e24

File tree

4 files changed

+107
-64
lines changed

4 files changed

+107
-64
lines changed

llvm/lib/CodeGen/AllocationOrder.h

+60-29
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
#define LLVM_LIB_CODEGEN_ALLOCATIONORDER_H
1818

1919
#include "llvm/ADT/ArrayRef.h"
20-
#include "llvm/ADT/SmallVector.h"
2120
#include "llvm/ADT/STLExtras.h"
21+
#include "llvm/ADT/SmallVector.h"
2222
#include "llvm/MC/MCRegister.h"
2323

2424
namespace llvm {
@@ -30,12 +30,52 @@ class LiveRegMatrix;
3030
class LLVM_LIBRARY_VISIBILITY AllocationOrder {
3131
const SmallVector<MCPhysReg, 16> Hints;
3232
ArrayRef<MCPhysReg> Order;
33-
int Pos = 0;
34-
35-
// If HardHints is true, *only* Hints will be returned.
36-
const bool HardHints;
33+
// How far into the Order we can iterate. This is 0 if the AllocationOrder is
34+
// constructed with HardHints = true, Order.size() otherwise. While
35+
// technically a size_t, it will participate in comparisons with the
36+
// Iterator's Pos, which must be signed, so it's typed here as signed, too, to
37+
// avoid warnings and under the assumption that the size of Order is
38+
// relatively small.
39+
// IterationLimit defines an invalid iterator position.
40+
const int IterationLimit;
3741

3842
public:
43+
/// Forward iterator for an AllocationOrder.
44+
class Iterator final {
45+
const AllocationOrder &AO;
46+
int Pos = 0;
47+
48+
public:
49+
Iterator(const AllocationOrder &AO, int Pos) : AO(AO), Pos(Pos) {}
50+
51+
/// Return true if the curent position is that of a preferred register.
52+
bool isHint() const { return Pos < 0; }
53+
54+
/// Return the next physical register in the allocation order.
55+
MCRegister operator*() const {
56+
if (Pos < 0)
57+
return AO.Hints.end()[Pos];
58+
assert(Pos < AO.IterationLimit);
59+
return AO.Order[Pos];
60+
}
61+
62+
/// Advance the iterator to the next position. If that's past the Hints
63+
/// list, advance to the first value that's not also in the Hints list.
64+
Iterator &operator++() {
65+
if (Pos < AO.IterationLimit)
66+
++Pos;
67+
while (Pos >= 0 && Pos < AO.IterationLimit && AO.isHint(AO.Order[Pos]))
68+
++Pos;
69+
return *this;
70+
}
71+
72+
bool operator==(const Iterator &Other) const {
73+
assert(&AO == &Other.AO);
74+
return Pos == Other.Pos;
75+
}
76+
77+
bool operator!=(const Iterator &Other) const { return !(*this == Other); }
78+
};
3979

4080
/// Create a new AllocationOrder for VirtReg.
4181
/// @param VirtReg Virtual register to allocate for.
@@ -50,34 +90,25 @@ class LLVM_LIBRARY_VISIBILITY AllocationOrder {
5090
AllocationOrder(SmallVector<MCPhysReg, 16> &&Hints, ArrayRef<MCPhysReg> Order,
5191
bool HardHints)
5292
: Hints(std::move(Hints)), Order(Order),
53-
Pos(-static_cast<int>(this->Hints.size())), HardHints(HardHints) {}
93+
IterationLimit(HardHints ? 0 : static_cast<int>(Order.size())) {}
5494

55-
/// Get the allocation order without reordered hints.
56-
ArrayRef<MCPhysReg> getOrder() const { return Order; }
57-
58-
/// Return the next physical register in the allocation order, or 0.
59-
/// It is safe to call next() again after it returned 0, it will keep
60-
/// returning 0 until rewind() is called.
61-
MCPhysReg next(unsigned Limit = 0) {
62-
if (Pos < 0)
63-
return Hints.end()[Pos++];
64-
if (HardHints)
65-
return 0;
66-
if (!Limit)
67-
Limit = Order.size();
68-
while (Pos < int(Limit)) {
69-
unsigned Reg = Order[Pos++];
70-
if (!isHint(Reg))
71-
return Reg;
72-
}
73-
return 0;
95+
Iterator begin() const {
96+
return Iterator(*this, -(static_cast<int>(Hints.size())));
7497
}
7598

76-
/// Start over from the beginning.
77-
void rewind() { Pos = -int(Hints.size()); }
99+
Iterator end() const { return Iterator(*this, IterationLimit); }
78100

79-
/// Return true if the last register returned from next() was a preferred register.
80-
bool isHint() const { return Pos <= 0; }
101+
Iterator getOrderLimitEnd(unsigned OrderLimit) const {
102+
assert(OrderLimit <= Order.size());
103+
if (OrderLimit == 0)
104+
return end();
105+
Iterator Ret(*this,
106+
std::min(static_cast<int>(OrderLimit) - 1, IterationLimit));
107+
return ++Ret;
108+
}
109+
110+
/// Get the allocation order without reordered hints.
111+
ArrayRef<MCPhysReg> getOrder() const { return Order; }
81112

82113
/// Return true if PhysReg is a preferred register.
83114
bool isHint(unsigned PhysReg) const { return is_contained(Hints, PhysReg); }

llvm/lib/CodeGen/RegAllocBasic.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,8 @@ Register RABasic::selectOrSplit(LiveInterval &VirtReg,
261261
// Check for an available register in this class.
262262
auto Order =
263263
AllocationOrder::create(VirtReg.reg(), *VRM, RegClassInfo, Matrix);
264-
while (Register PhysReg = Order.next()) {
264+
for (MCRegister PhysReg : Order) {
265+
assert(PhysReg.isValid());
265266
// Check for interference in PhysReg
266267
switch (Matrix->checkInterference(VirtReg, PhysReg)) {
267268
case LiveRegMatrix::IK_Free:

llvm/lib/CodeGen/RegAllocGreedy.cpp

+26-19
Original file line numberDiff line numberDiff line change
@@ -757,12 +757,17 @@ Register RAGreedy::tryAssign(LiveInterval &VirtReg,
757757
AllocationOrder &Order,
758758
SmallVectorImpl<Register> &NewVRegs,
759759
const SmallVirtRegSet &FixedRegisters) {
760-
Order.rewind();
761760
Register PhysReg;
762-
while ((PhysReg = Order.next()))
763-
if (!Matrix->checkInterference(VirtReg, PhysReg))
764-
break;
765-
if (!PhysReg || Order.isHint())
761+
for (auto I = Order.begin(), E = Order.end(); I != E && !PhysReg; ++I) {
762+
assert(*I);
763+
if (!Matrix->checkInterference(VirtReg, *I)) {
764+
if (I.isHint())
765+
return *I;
766+
else
767+
PhysReg = *I;
768+
}
769+
}
770+
if (!PhysReg.isValid())
766771
return PhysReg;
767772

768773
// PhysReg is available, but there may be a better choice.
@@ -803,12 +808,12 @@ Register RAGreedy::tryAssign(LiveInterval &VirtReg,
803808
Register RAGreedy::canReassign(LiveInterval &VirtReg, Register PrevReg) {
804809
auto Order =
805810
AllocationOrder::create(VirtReg.reg(), *VRM, RegClassInfo, Matrix);
806-
Register PhysReg;
807-
while ((PhysReg = Order.next())) {
808-
if (PhysReg == PrevReg)
811+
MCRegister PhysReg;
812+
for (auto I = Order.begin(), E = Order.end(); I != E && !PhysReg; ++I) {
813+
if ((*I).id() == PrevReg.id())
809814
continue;
810815

811-
MCRegUnitIterator Units(PhysReg, TRI);
816+
MCRegUnitIterator Units(*I, TRI);
812817
for (; Units.isValid(); ++Units) {
813818
// Instantiate a "subquery", not to be confused with the Queries array.
814819
LiveIntervalUnion::Query subQ(VirtReg, Matrix->getLiveUnions()[*Units]);
@@ -817,7 +822,7 @@ Register RAGreedy::canReassign(LiveInterval &VirtReg, Register PrevReg) {
817822
}
818823
// If no units have interference, break out with the current PhysReg.
819824
if (!Units.isValid())
820-
break;
825+
PhysReg = *I;
821826
}
822827
if (PhysReg)
823828
LLVM_DEBUG(dbgs() << "can reassign: " << VirtReg << " from "
@@ -1134,8 +1139,10 @@ unsigned RAGreedy::tryEvict(LiveInterval &VirtReg,
11341139
}
11351140
}
11361141

1137-
Order.rewind();
1138-
while (MCRegister PhysReg = Order.next(OrderLimit)) {
1142+
for (auto I = Order.begin(), E = Order.getOrderLimitEnd(OrderLimit); I != E;
1143+
++I) {
1144+
MCRegister PhysReg = *I;
1145+
assert(PhysReg);
11391146
if (TRI->getCostPerUse(PhysReg) >= CostPerUseLimit)
11401147
continue;
11411148
// The first use of a callee-saved register in a function has cost 1.
@@ -1156,7 +1163,7 @@ unsigned RAGreedy::tryEvict(LiveInterval &VirtReg,
11561163
BestPhys = PhysReg;
11571164

11581165
// Stop if the hint can be used.
1159-
if (Order.isHint())
1166+
if (I.isHint())
11601167
break;
11611168
}
11621169

@@ -1849,8 +1856,8 @@ unsigned RAGreedy::calculateRegionSplitCost(LiveInterval &VirtReg,
18491856
unsigned &NumCands, bool IgnoreCSR,
18501857
bool *CanCauseEvictionChain) {
18511858
unsigned BestCand = NoCand;
1852-
Order.rewind();
1853-
while (unsigned PhysReg = Order.next()) {
1859+
for (MCPhysReg PhysReg : Order) {
1860+
assert(PhysReg);
18541861
if (IgnoreCSR && isUnusedCalleeSavedReg(PhysReg))
18551862
continue;
18561863

@@ -2288,8 +2295,8 @@ unsigned RAGreedy::tryLocalSplit(LiveInterval &VirtReg, AllocationOrder &Order,
22882295
(1.0f / MBFI->getEntryFreq());
22892296
SmallVector<float, 8> GapWeight;
22902297

2291-
Order.rewind();
2292-
while (unsigned PhysReg = Order.next()) {
2298+
for (MCPhysReg PhysReg : Order) {
2299+
assert(PhysReg);
22932300
// Keep track of the largest spill weight that would need to be evicted in
22942301
// order to make use of PhysReg between UseSlots[I] and UseSlots[I + 1].
22952302
calcGapWeights(PhysReg, GapWeight);
@@ -2606,8 +2613,8 @@ unsigned RAGreedy::tryLastChanceRecoloring(LiveInterval &VirtReg,
26062613
FixedRegisters.insert(VirtReg.reg());
26072614
SmallVector<Register, 4> CurrentNewVRegs;
26082615

2609-
Order.rewind();
2610-
while (Register PhysReg = Order.next()) {
2616+
for (MCRegister PhysReg : Order) {
2617+
assert(PhysReg.isValid());
26112618
LLVM_DEBUG(dbgs() << "Try to assign: " << VirtReg << " to "
26122619
<< printReg(PhysReg, TRI) << '\n');
26132620
RecoloringCandidates.clear();

llvm/unittests/CodeGen/AllocationOrderTest.cpp

+19-15
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212
using namespace llvm;
1313

1414
namespace {
15-
std::vector<MCPhysReg> loadOrder(AllocationOrder &O, unsigned Limit = 0) {
15+
std::vector<MCPhysReg> loadOrder(const AllocationOrder &O, unsigned Limit = 0) {
1616
std::vector<MCPhysReg> Ret;
17-
O.rewind();
18-
while (auto R = O.next(Limit))
19-
Ret.push_back(R);
17+
if (Limit == 0)
18+
for (auto R : O)
19+
Ret.push_back(R);
20+
else
21+
for (auto I = O.begin(), E = O.getOrderLimitEnd(Limit); I != E; ++I)
22+
Ret.push_back(*I);
2023
return Ret;
2124
}
2225
} // namespace
@@ -48,6 +51,7 @@ TEST(AllocationOrderTest, LimitsBasic) {
4851
AllocationOrder O(std::move(Hints), Order, false);
4952
EXPECT_EQ((std::vector<MCPhysReg>{1, 2, 3, 4, 5, 6, 7}), loadOrder(O, 0));
5053
EXPECT_EQ((std::vector<MCPhysReg>{1, 2, 3, 4}), loadOrder(O, 1));
54+
EXPECT_EQ(O.end(), O.getOrderLimitEnd(0));
5155
}
5256

5357
TEST(AllocationOrderTest, LimitsDuplicates) {
@@ -96,19 +100,19 @@ TEST(AllocationOrderTest, IsHintTest) {
96100
SmallVector<MCPhysReg, 16> Hints = {1, 2, 3};
97101
SmallVector<MCPhysReg, 16> Order = {4, 1, 5, 6};
98102
AllocationOrder O(std::move(Hints), Order, false);
99-
O.rewind();
100-
auto V = O.next();
101-
EXPECT_TRUE(O.isHint());
103+
auto I = O.begin();
104+
auto V = *I;
105+
EXPECT_TRUE(I.isHint());
102106
EXPECT_EQ(V, 1U);
103-
O.next();
104-
EXPECT_TRUE(O.isHint());
105-
O.next();
106-
EXPECT_TRUE(O.isHint());
107-
V = O.next();
108-
EXPECT_FALSE(O.isHint());
107+
++I;
108+
EXPECT_TRUE(I.isHint());
109+
++I;
110+
EXPECT_TRUE(I.isHint());
111+
V = *(++I);
112+
EXPECT_FALSE(I.isHint());
109113
EXPECT_EQ(V, 4U);
110-
V = O.next();
114+
V = *(++I);
111115
EXPECT_TRUE(O.isHint(1));
112-
EXPECT_FALSE(O.isHint());
116+
EXPECT_FALSE(I.isHint());
113117
EXPECT_EQ(V, 5U);
114118
}

0 commit comments

Comments
 (0)