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 = ...@@ -26,12 +26,6 @@ static constexpr int kFloat32Bit =
static constexpr int kSimd128Bit = static constexpr int kSimd128Bit =
RepresentationBit(MachineRepresentation::kSimd128); 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) { int GetRegisterCount(const RegisterConfiguration* cfg, RegisterKind kind) {
return kind == FP_REGISTERS ? cfg->num_double_registers() return kind == FP_REGISTERS ? cfg->num_double_registers()
: cfg->num_general_registers(); : cfg->num_general_registers();
...@@ -2808,25 +2802,27 @@ void LinearScanAllocator::AllocateRegisters() { ...@@ -2808,25 +2802,27 @@ void LinearScanAllocator::AllocateRegisters() {
if (current->IsTopLevel() && TryReuseSpillForPhi(current->TopLevel())) if (current->IsTopLevel() && TryReuseSpillForPhi(current->TopLevel()))
continue; continue;
for (size_t i = 0; i < active_live_ranges().size(); ++i) { for (auto it = active_live_ranges().begin();
LiveRange* cur_active = active_live_ranges()[i]; it != active_live_ranges().end();) {
LiveRange* cur_active = *it;
if (cur_active->End() <= position) { if (cur_active->End() <= position) {
ActiveToHandled(cur_active); it = ActiveToHandled(it);
--i; // The live range was removed from the list of active live ranges.
} else if (!cur_active->Covers(position)) { } else if (!cur_active->Covers(position)) {
ActiveToInactive(cur_active); it = ActiveToInactive(it);
--i; // The live range was removed from the list of active live ranges. } else {
++it;
} }
} }
for (size_t i = 0; i < inactive_live_ranges().size(); ++i) { for (auto it = inactive_live_ranges().begin();
LiveRange* cur_inactive = inactive_live_ranges()[i]; it != inactive_live_ranges().end();) {
LiveRange* cur_inactive = *it;
if (cur_inactive->End() <= position) { if (cur_inactive->End() <= position) {
InactiveToHandled(cur_inactive); it = InactiveToHandled(it);
--i; // Live range was removed from the list of inactive live ranges.
} else if (cur_inactive->Covers(position)) { } else if (cur_inactive->Covers(position)) {
InactiveToActive(cur_inactive); it = InactiveToActive(it);
--i; // Live range was removed from the list of inactive live ranges. } else {
++it;
} }
} }
...@@ -2891,34 +2887,36 @@ void LinearScanAllocator::AddToUnhandled(LiveRange* range) { ...@@ -2891,34 +2887,36 @@ void LinearScanAllocator::AddToUnhandled(LiveRange* range) {
unhandled_live_ranges().insert(range); unhandled_live_ranges().insert(range);
} }
ZoneVector<LiveRange*>::iterator LinearScanAllocator::ActiveToHandled(
void LinearScanAllocator::ActiveToHandled(LiveRange* range) { const ZoneVector<LiveRange*>::iterator it) {
RemoveElement(&active_live_ranges(), range);
TRACE("Moving live range %d:%d from active to handled\n", 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);
} }
ZoneVector<LiveRange*>::iterator LinearScanAllocator::ActiveToInactive(
void LinearScanAllocator::ActiveToInactive(LiveRange* range) { const ZoneVector<LiveRange*>::iterator it) {
RemoveElement(&active_live_ranges(), range); LiveRange* range = *it;
inactive_live_ranges().push_back(range); inactive_live_ranges().push_back(range);
TRACE("Moving live range %d:%d from active to inactive\n", 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);
} }
ZoneVector<LiveRange*>::iterator LinearScanAllocator::InactiveToHandled(
void LinearScanAllocator::InactiveToHandled(LiveRange* range) { ZoneVector<LiveRange*>::iterator it) {
RemoveElement(&inactive_live_ranges(), range);
TRACE("Moving live range %d:%d from inactive to handled\n", 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);
} }
ZoneVector<LiveRange*>::iterator LinearScanAllocator::InactiveToActive(
void LinearScanAllocator::InactiveToActive(LiveRange* range) { ZoneVector<LiveRange*>::iterator it) {
RemoveElement(&inactive_live_ranges(), range); LiveRange* range = *it;
active_live_ranges().push_back(range); active_live_ranges().push_back(range);
TRACE("Moving live range %d:%d from inactive to active\n", TRACE("Moving live range %d:%d from inactive to active\n",
range->TopLevel()->vreg(), range->relative_id()); range->TopLevel()->vreg(), range->relative_id());
return inactive_live_ranges().erase(it);
} }
void LinearScanAllocator::GetFPRegisterSet(MachineRepresentation rep, void LinearScanAllocator::GetFPRegisterSet(MachineRepresentation rep,
...@@ -3286,14 +3284,19 @@ void LinearScanAllocator::SplitAndSpillIntersecting(LiveRange* current) { ...@@ -3286,14 +3284,19 @@ void LinearScanAllocator::SplitAndSpillIntersecting(LiveRange* current) {
DCHECK(current->HasRegisterAssigned()); DCHECK(current->HasRegisterAssigned());
int reg = current->assigned_register(); int reg = current->assigned_register();
LifetimePosition split_pos = current->Start(); LifetimePosition split_pos = current->Start();
for (size_t i = 0; i < active_live_ranges().size(); ++i) { for (auto it = active_live_ranges().begin();
LiveRange* range = active_live_ranges()[i]; it != active_live_ranges().end();) {
LiveRange* range = *it;
if (kSimpleFPAliasing || !check_fp_aliasing()) { if (kSimpleFPAliasing || !check_fp_aliasing()) {
if (range->assigned_register() != reg) continue; if (range->assigned_register() != reg) {
++it;
continue;
}
} else { } else {
if (!data()->config()->AreAliases(current->representation(), reg, if (!data()->config()->AreAliases(current->representation(), reg,
range->representation(), range->representation(),
range->assigned_register())) { range->assigned_register())) {
++it;
continue; continue;
} }
} }
...@@ -3315,22 +3318,30 @@ void LinearScanAllocator::SplitAndSpillIntersecting(LiveRange* current) { ...@@ -3315,22 +3318,30 @@ void LinearScanAllocator::SplitAndSpillIntersecting(LiveRange* current) {
next_pos->pos())); next_pos->pos()));
SpillBetweenUntil(range, spill_pos, current->Start(), next_pos->pos()); SpillBetweenUntil(range, spill_pos, current->Start(), next_pos->pos());
} }
ActiveToHandled(range); it = ActiveToHandled(it);
--i;
} }
for (size_t i = 0; i < inactive_live_ranges().size(); ++i) { for (auto it = inactive_live_ranges().begin();
LiveRange* range = inactive_live_ranges()[i]; it != inactive_live_ranges().end();) {
LiveRange* range = *it;
DCHECK(range->End() > current->Start()); DCHECK(range->End() > current->Start());
if (range->TopLevel()->IsFixed()) continue; if (range->TopLevel()->IsFixed()) {
++it;
continue;
}
if (kSimpleFPAliasing || !check_fp_aliasing()) { if (kSimpleFPAliasing || !check_fp_aliasing()) {
if (range->assigned_register() != reg) continue; if (range->assigned_register() != reg) {
++it;
continue;
}
} else { } else {
if (!data()->config()->AreAliases(current->representation(), reg, if (!data()->config()->AreAliases(current->representation(), reg,
range->representation(), range->representation(),
range->assigned_register())) range->assigned_register())) {
++it;
continue; continue;
} }
}
LifetimePosition next_intersection = range->FirstIntersection(current); LifetimePosition next_intersection = range->FirstIntersection(current);
if (next_intersection.IsValid()) { if (next_intersection.IsValid()) {
...@@ -3341,8 +3352,9 @@ void LinearScanAllocator::SplitAndSpillIntersecting(LiveRange* current) { ...@@ -3341,8 +3352,9 @@ void LinearScanAllocator::SplitAndSpillIntersecting(LiveRange* current) {
next_intersection = Min(next_intersection, next_pos->pos()); next_intersection = Min(next_intersection, next_pos->pos());
SpillBetween(range, split_pos, next_intersection); SpillBetween(range, split_pos, next_intersection);
} }
InactiveToHandled(range); it = InactiveToHandled(it);
--i; } else {
++it;
} }
} }
} }
......
...@@ -1058,10 +1058,14 @@ class LinearScanAllocator final : public RegisterAllocator { ...@@ -1058,10 +1058,14 @@ class LinearScanAllocator final : public RegisterAllocator {
void AddToActive(LiveRange* range); void AddToActive(LiveRange* range);
void AddToInactive(LiveRange* range); void AddToInactive(LiveRange* range);
void AddToUnhandled(LiveRange* range); void AddToUnhandled(LiveRange* range);
void ActiveToHandled(LiveRange* range); ZoneVector<LiveRange*>::iterator ActiveToHandled(
void ActiveToInactive(LiveRange* range); ZoneVector<LiveRange*>::iterator it);
void InactiveToHandled(LiveRange* range); ZoneVector<LiveRange*>::iterator ActiveToInactive(
void InactiveToActive(LiveRange* range); 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. // Helper methods for allocating registers.
bool TryReuseSpillForPhi(TopLevelLiveRange* range); 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