Commit 108b20d1 authored by Stephan Herhut's avatar Stephan Herhut Committed by Commit Bot

[cleanup] Use iterators for queue management in register allocator

The register allocator uses std::find to search for an element to be
removed from the active/inactive queues repeatedly. As we already know
the exact position of the element to remove, it is better to use an
iterator right away.

Change-Id: I2cd318a5960113d18b3749b2010f8028fe66158d
Reviewed-on: https://chromium-review.googlesource.com/c/1304542
Commit-Queue: Stephan Herhut <herhut@chromium.org>
Reviewed-by: 's avatarClemens Hammacher <clemensh@chromium.org>
Reviewed-by: 's avatarMichael Starzinger <mstarzinger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#57121}
parent c8445bfd
......@@ -26,12 +26,6 @@ static constexpr int kFloat32Bit =
static constexpr int kSimd128Bit =
RepresentationBit(MachineRepresentation::kSimd128);
void RemoveElement(ZoneVector<LiveRange*>* v, LiveRange* range) {
auto it = std::find(v->begin(), v->end(), range);
DCHECK(it != v->end());
v->erase(it);
}
int GetRegisterCount(const RegisterConfiguration* cfg, RegisterKind kind) {
return kind == FP_REGISTERS ? cfg->num_double_registers()
: cfg->num_general_registers();
......@@ -2808,25 +2802,27 @@ void LinearScanAllocator::AllocateRegisters() {
if (current->IsTopLevel() && TryReuseSpillForPhi(current->TopLevel()))
continue;
for (size_t i = 0; i < active_live_ranges().size(); ++i) {
LiveRange* cur_active = active_live_ranges()[i];
for (auto it = active_live_ranges().begin();
it != active_live_ranges().end();) {
LiveRange* cur_active = *it;
if (cur_active->End() <= position) {
ActiveToHandled(cur_active);
--i; // The live range was removed from the list of active live ranges.
it = ActiveToHandled(it);
} else if (!cur_active->Covers(position)) {
ActiveToInactive(cur_active);
--i; // The live range was removed from the list of active live ranges.
it = ActiveToInactive(it);
} else {
++it;
}
}
for (size_t i = 0; i < inactive_live_ranges().size(); ++i) {
LiveRange* cur_inactive = inactive_live_ranges()[i];
for (auto it = inactive_live_ranges().begin();
it != inactive_live_ranges().end();) {
LiveRange* cur_inactive = *it;
if (cur_inactive->End() <= position) {
InactiveToHandled(cur_inactive);
--i; // Live range was removed from the list of inactive live ranges.
it = InactiveToHandled(it);
} else if (cur_inactive->Covers(position)) {
InactiveToActive(cur_inactive);
--i; // Live range was removed from the list of inactive live ranges.
it = InactiveToActive(it);
} else {
++it;
}
}
......@@ -2891,34 +2887,36 @@ void LinearScanAllocator::AddToUnhandled(LiveRange* range) {
unhandled_live_ranges().insert(range);
}
void LinearScanAllocator::ActiveToHandled(LiveRange* range) {
RemoveElement(&active_live_ranges(), range);
ZoneVector<LiveRange*>::iterator LinearScanAllocator::ActiveToHandled(
const ZoneVector<LiveRange*>::iterator it) {
TRACE("Moving live range %d:%d from active to handled\n",
range->TopLevel()->vreg(), range->relative_id());
(*it)->TopLevel()->vreg(), (*it)->relative_id());
return active_live_ranges().erase(it);
}
void LinearScanAllocator::ActiveToInactive(LiveRange* range) {
RemoveElement(&active_live_ranges(), range);
ZoneVector<LiveRange*>::iterator LinearScanAllocator::ActiveToInactive(
const ZoneVector<LiveRange*>::iterator it) {
LiveRange* range = *it;
inactive_live_ranges().push_back(range);
TRACE("Moving live range %d:%d from active to inactive\n",
range->TopLevel()->vreg(), range->relative_id());
(range)->TopLevel()->vreg(), range->relative_id());
return active_live_ranges().erase(it);
}
void LinearScanAllocator::InactiveToHandled(LiveRange* range) {
RemoveElement(&inactive_live_ranges(), range);
ZoneVector<LiveRange*>::iterator LinearScanAllocator::InactiveToHandled(
ZoneVector<LiveRange*>::iterator it) {
TRACE("Moving live range %d:%d from inactive to handled\n",
range->TopLevel()->vreg(), range->relative_id());
(*it)->TopLevel()->vreg(), (*it)->relative_id());
return inactive_live_ranges().erase(it);
}
void LinearScanAllocator::InactiveToActive(LiveRange* range) {
RemoveElement(&inactive_live_ranges(), range);
ZoneVector<LiveRange*>::iterator LinearScanAllocator::InactiveToActive(
ZoneVector<LiveRange*>::iterator it) {
LiveRange* range = *it;
active_live_ranges().push_back(range);
TRACE("Moving live range %d:%d from inactive to active\n",
range->TopLevel()->vreg(), range->relative_id());
return inactive_live_ranges().erase(it);
}
void LinearScanAllocator::GetFPRegisterSet(MachineRepresentation rep,
......@@ -3286,14 +3284,19 @@ void LinearScanAllocator::SplitAndSpillIntersecting(LiveRange* current) {
DCHECK(current->HasRegisterAssigned());
int reg = current->assigned_register();
LifetimePosition split_pos = current->Start();
for (size_t i = 0; i < active_live_ranges().size(); ++i) {
LiveRange* range = active_live_ranges()[i];
for (auto it = active_live_ranges().begin();
it != active_live_ranges().end();) {
LiveRange* range = *it;
if (kSimpleFPAliasing || !check_fp_aliasing()) {
if (range->assigned_register() != reg) continue;
if (range->assigned_register() != reg) {
++it;
continue;
}
} else {
if (!data()->config()->AreAliases(current->representation(), reg,
range->representation(),
range->assigned_register())) {
++it;
continue;
}
}
......@@ -3315,22 +3318,30 @@ void LinearScanAllocator::SplitAndSpillIntersecting(LiveRange* current) {
next_pos->pos()));
SpillBetweenUntil(range, spill_pos, current->Start(), next_pos->pos());
}
ActiveToHandled(range);
--i;
it = ActiveToHandled(it);
}
for (size_t i = 0; i < inactive_live_ranges().size(); ++i) {
LiveRange* range = inactive_live_ranges()[i];
for (auto it = inactive_live_ranges().begin();
it != inactive_live_ranges().end();) {
LiveRange* range = *it;
DCHECK(range->End() > current->Start());
if (range->TopLevel()->IsFixed()) continue;
if (range->TopLevel()->IsFixed()) {
++it;
continue;
}
if (kSimpleFPAliasing || !check_fp_aliasing()) {
if (range->assigned_register() != reg) continue;
if (range->assigned_register() != reg) {
++it;
continue;
}
} else {
if (!data()->config()->AreAliases(current->representation(), reg,
range->representation(),
range->assigned_register()))
range->assigned_register())) {
++it;
continue;
}
}
LifetimePosition next_intersection = range->FirstIntersection(current);
if (next_intersection.IsValid()) {
......@@ -3341,8 +3352,9 @@ void LinearScanAllocator::SplitAndSpillIntersecting(LiveRange* current) {
next_intersection = Min(next_intersection, next_pos->pos());
SpillBetween(range, split_pos, next_intersection);
}
InactiveToHandled(range);
--i;
it = InactiveToHandled(it);
} else {
++it;
}
}
}
......
......@@ -1058,10 +1058,14 @@ class LinearScanAllocator final : public RegisterAllocator {
void AddToActive(LiveRange* range);
void AddToInactive(LiveRange* range);
void AddToUnhandled(LiveRange* range);
void ActiveToHandled(LiveRange* range);
void ActiveToInactive(LiveRange* range);
void InactiveToHandled(LiveRange* range);
void InactiveToActive(LiveRange* range);
ZoneVector<LiveRange*>::iterator ActiveToHandled(
ZoneVector<LiveRange*>::iterator it);
ZoneVector<LiveRange*>::iterator ActiveToInactive(
ZoneVector<LiveRange*>::iterator it);
ZoneVector<LiveRange*>::iterator InactiveToHandled(
ZoneVector<LiveRange*>::iterator it);
ZoneVector<LiveRange*>::iterator InactiveToActive(
ZoneVector<LiveRange*>::iterator it);
// Helper methods for allocating registers.
bool TryReuseSpillForPhi(TopLevelLiveRange* range);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment