Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1096)

Unified Diff: src/compiler/register-allocator.cc

Issue 426233002: Land the Fan (disabled) (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Review feedback, rebase and "git cl format" Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/compiler/register-allocator.h ('k') | src/compiler/representation-change.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/compiler/register-allocator.cc
diff --git a/src/lithium-allocator.cc b/src/compiler/register-allocator.cc
similarity index 59%
copy from src/lithium-allocator.cc
copy to src/compiler/register-allocator.cc
index 10a34d144fe17841c6f6f1f0d582ee10d3c06abe..342615e140127aa94f48ef41ac47f46e7ef3bdd0 100644
--- a/src/lithium-allocator.cc
+++ b/src/compiler/register-allocator.cc
@@ -1,33 +1,16 @@
-// Copyright 2012 the V8 project authors. All rights reserved.
+// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "src/v8.h"
+#include "src/compiler/register-allocator.h"
+#include "src/compiler/linkage.h"
#include "src/hydrogen.h"
-#include "src/lithium-allocator-inl.h"
#include "src/string-stream.h"
-#if V8_TARGET_ARCH_IA32
-#include "src/ia32/lithium-ia32.h" // NOLINT
-#elif V8_TARGET_ARCH_X64
-#include "src/x64/lithium-x64.h" // NOLINT
-#elif V8_TARGET_ARCH_ARM64
-#include "src/arm64/lithium-arm64.h" // NOLINT
-#elif V8_TARGET_ARCH_ARM
-#include "src/arm/lithium-arm.h" // NOLINT
-#elif V8_TARGET_ARCH_MIPS
-#include "src/mips/lithium-mips.h" // NOLINT
-#elif V8_TARGET_ARCH_MIPS64
-#include "src/mips64/lithium-mips64.h" // NOLINT
-#elif V8_TARGET_ARCH_X87
-#include "src/x87/lithium-x87.h" // NOLINT
-#else
-#error "Unknown architecture."
-#endif
-
namespace v8 {
namespace internal {
+namespace compiler {
static inline LifetimePosition Min(LifetimePosition a, LifetimePosition b) {
return a.Value() < b.Value() ? a : b;
@@ -39,9 +22,8 @@ static inline LifetimePosition Max(LifetimePosition a, LifetimePosition b) {
}
-UsePosition::UsePosition(LifetimePosition pos,
- LOperand* operand,
- LOperand* hint)
+UsePosition::UsePosition(LifetimePosition pos, InstructionOperand* operand,
+ InstructionOperand* hint)
: operand_(operand),
hint_(hint),
pos_(pos),
@@ -49,9 +31,8 @@ UsePosition::UsePosition(LifetimePosition pos,
requires_reg_(false),
register_beneficial_(true) {
if (operand_ != NULL && operand_->IsUnallocated()) {
- LUnallocated* unalloc = LUnallocated::cast(operand_);
- requires_reg_ = unalloc->HasRegisterPolicy() ||
- unalloc->HasDoubleRegisterPolicy();
+ const UnallocatedOperand* unalloc = UnallocatedOperand::cast(operand_);
+ requires_reg_ = unalloc->HasRegisterPolicy();
register_beneficial_ = !unalloc->HasAnyPolicy();
}
ASSERT(pos_.IsValid());
@@ -63,19 +44,15 @@ bool UsePosition::HasHint() const {
}
-bool UsePosition::RequiresRegister() const {
- return requires_reg_;
-}
+bool UsePosition::RequiresRegister() const { return requires_reg_; }
-bool UsePosition::RegisterIsBeneficial() const {
- return register_beneficial_;
-}
+bool UsePosition::RegisterIsBeneficial() const { return register_beneficial_; }
void UseInterval::SplitAt(LifetimePosition pos, Zone* zone) {
ASSERT(Contains(pos) && pos.Value() != start().Value());
- UseInterval* after = new(zone) UseInterval(pos, end_);
+ UseInterval* after = new (zone) UseInterval(pos, end_);
after->next_ = next_;
next_ = after;
end_ = pos;
@@ -115,6 +92,8 @@ bool LiveRange::HasOverlap(UseInterval* target) const {
LiveRange::LiveRange(int id, Zone* zone)
: id_(id),
spilled_(false),
+ is_phi_(false),
+ is_non_loop_phi_(false),
kind_(UNALLOCATED_REGISTERS),
assigned_register_(kInvalidAssignment),
last_interval_(NULL),
@@ -125,8 +104,8 @@ LiveRange::LiveRange(int id, Zone* zone)
current_interval_(NULL),
last_processed_use_(NULL),
current_hint_operand_(NULL),
- spill_operand_(new(zone) LOperand()),
- spill_start_index_(kMaxInt) { }
+ spill_operand_(new (zone) InstructionOperand()),
+ spill_start_index_(kMaxInt) {}
void LiveRange::set_assigned_register(int reg, Zone* zone) {
@@ -151,7 +130,7 @@ bool LiveRange::HasAllocatedSpillOperand() const {
}
-void LiveRange::SetSpillOperand(LOperand* operand) {
+void LiveRange::SetSpillOperand(InstructionOperand* operand) {
ASSERT(!operand->IsUnallocated());
ASSERT(spill_operand_ != NULL);
ASSERT(spill_operand_->IsIgnored());
@@ -206,21 +185,21 @@ bool LiveRange::CanBeSpilled(LifetimePosition pos) {
// at the current or the immediate next position.
UsePosition* use_pos = NextRegisterPosition(pos);
if (use_pos == NULL) return true;
- return
- use_pos->pos().Value() > pos.NextInstruction().InstructionEnd().Value();
+ return use_pos->pos().Value() >
+ pos.NextInstruction().InstructionEnd().Value();
}
-LOperand* LiveRange::CreateAssignedOperand(Zone* zone) {
- LOperand* op = NULL;
+InstructionOperand* LiveRange::CreateAssignedOperand(Zone* zone) {
+ InstructionOperand* op = NULL;
if (HasRegisterAssigned()) {
ASSERT(!IsSpilled());
switch (Kind()) {
case GENERAL_REGISTERS:
- op = LRegister::Create(assigned_register(), zone);
+ op = RegisterOperand::Create(assigned_register(), zone);
break;
case DOUBLE_REGISTERS:
- op = LDoubleRegister::Create(assigned_register(), zone);
+ op = DoubleRegisterOperand::Create(assigned_register(), zone);
break;
default:
UNREACHABLE();
@@ -230,7 +209,8 @@ LOperand* LiveRange::CreateAssignedOperand(Zone* zone) {
op = TopLevel()->GetSpillOperand();
ASSERT(!op->IsUnallocated());
} else {
- LUnallocated* unalloc = new(zone) LUnallocated(LUnallocated::NONE);
+ UnallocatedOperand* unalloc =
+ new (zone) UnallocatedOperand(UnallocatedOperand::NONE);
unalloc->set_virtual_register(id_);
op = unalloc;
}
@@ -253,17 +233,16 @@ void LiveRange::AdvanceLastProcessedMarker(
UseInterval* to_start_of, LifetimePosition but_not_past) const {
if (to_start_of == NULL) return;
if (to_start_of->start().Value() > but_not_past.Value()) return;
- LifetimePosition start =
- current_interval_ == NULL ? LifetimePosition::Invalid()
- : current_interval_->start();
+ LifetimePosition start = current_interval_ == NULL
+ ? LifetimePosition::Invalid()
+ : current_interval_->start();
if (to_start_of->start().Value() > start.Value()) {
current_interval_ = to_start_of;
}
}
-void LiveRange::SplitAt(LifetimePosition position,
- LiveRange* result,
+void LiveRange::SplitAt(LifetimePosition position, LiveRange* result,
Zone* zone) {
ASSERT(Start().Value() < position.Value());
ASSERT(result->IsEmpty());
@@ -297,9 +276,10 @@ void LiveRange::SplitAt(LifetimePosition position,
// Partition original use intervals to the two live ranges.
UseInterval* before = current;
UseInterval* after = before->next();
- result->last_interval_ = (last_interval_ == before)
- ? after // Only interval in the range after split.
- : last_interval_; // Last interval of the original range.
+ result->last_interval_ =
+ (last_interval_ == before)
+ ? after // Only interval in the range after split.
+ : last_interval_; // Last interval of the original range.
result->first_interval_ = after;
last_interval_ = before;
@@ -369,7 +349,8 @@ bool LiveRange::ShouldBeAllocatedBefore(const LiveRange* other) const {
void LiveRange::ShortenTo(LifetimePosition start) {
- LAllocator::TraceAlloc("Shorten live range %d to [%d\n", id_, start.Value());
+ RegisterAllocator::TraceAlloc("Shorten live range %d to [%d\n", id_,
+ start.Value());
ASSERT(first_interval_ != NULL);
ASSERT(first_interval_->start().Value() <= start.Value());
ASSERT(start.Value() < first_interval_->end().Value());
@@ -377,13 +358,10 @@ void LiveRange::ShortenTo(LifetimePosition start) {
}
-void LiveRange::EnsureInterval(LifetimePosition start,
- LifetimePosition end,
+void LiveRange::EnsureInterval(LifetimePosition start, LifetimePosition end,
Zone* zone) {
- LAllocator::TraceAlloc("Ensure live range %d in interval [%d %d[\n",
- id_,
- start.Value(),
- end.Value());
+ RegisterAllocator::TraceAlloc("Ensure live range %d in interval [%d %d[\n",
+ id_, start.Value(), end.Value());
LifetimePosition new_end = end;
while (first_interval_ != NULL &&
first_interval_->start().Value() <= end.Value()) {
@@ -393,7 +371,7 @@ void LiveRange::EnsureInterval(LifetimePosition start,
first_interval_ = first_interval_->next();
}
- UseInterval* new_interval = new(zone) UseInterval(start, new_end);
+ UseInterval* new_interval = new (zone) UseInterval(start, new_end);
new_interval->next_ = first_interval_;
first_interval_ = new_interval;
if (new_interval->next() == NULL) {
@@ -402,22 +380,19 @@ void LiveRange::EnsureInterval(LifetimePosition start,
}
-void LiveRange::AddUseInterval(LifetimePosition start,
- LifetimePosition end,
+void LiveRange::AddUseInterval(LifetimePosition start, LifetimePosition end,
Zone* zone) {
- LAllocator::TraceAlloc("Add to live range %d interval [%d %d[\n",
- id_,
- start.Value(),
- end.Value());
+ RegisterAllocator::TraceAlloc("Add to live range %d interval [%d %d[\n", id_,
+ start.Value(), end.Value());
if (first_interval_ == NULL) {
- UseInterval* interval = new(zone) UseInterval(start, end);
+ UseInterval* interval = new (zone) UseInterval(start, end);
first_interval_ = interval;
last_interval_ = interval;
} else {
if (end.Value() == first_interval_->start().Value()) {
first_interval_->set_start(start);
} else if (end.Value() < first_interval_->start().Value()) {
- UseInterval* interval = new(zone) UseInterval(start, end);
+ UseInterval* interval = new (zone) UseInterval(start, end);
interval->set_next(first_interval_);
first_interval_ = interval;
} else {
@@ -433,13 +408,11 @@ void LiveRange::AddUseInterval(LifetimePosition start,
void LiveRange::AddUsePosition(LifetimePosition pos,
- LOperand* operand,
- LOperand* hint,
- Zone* zone) {
- LAllocator::TraceAlloc("Add to live range %d use position %d\n",
- id_,
- pos.Value());
- UsePosition* use_pos = new(zone) UsePosition(pos, operand, hint);
+ InstructionOperand* operand,
+ InstructionOperand* hint, Zone* zone) {
+ RegisterAllocator::TraceAlloc("Add to live range %d use position %d\n", id_,
+ pos.Value());
+ UsePosition* use_pos = new (zone) UsePosition(pos, operand, hint);
UsePosition* prev_hint = NULL;
UsePosition* prev = NULL;
UsePosition* current = first_pos_;
@@ -464,7 +437,7 @@ void LiveRange::AddUsePosition(LifetimePosition pos,
void LiveRange::ConvertOperands(Zone* zone) {
- LOperand* op = CreateAssignedOperand(zone);
+ InstructionOperand* op = CreateAssignedOperand(zone);
UsePosition* use_pos = first_pos();
while (use_pos != NULL) {
ASSERT(Start().Value() <= use_pos->pos().Value() &&
@@ -490,8 +463,7 @@ bool LiveRange::CanCover(LifetimePosition position) const {
bool LiveRange::Covers(LifetimePosition position) {
if (!CanCover(position)) return false;
UseInterval* start_search = FirstSearchIntervalForPosition(position);
- for (UseInterval* interval = start_search;
- interval != NULL;
+ for (UseInterval* interval = start_search; interval != NULL;
interval = interval->next()) {
ASSERT(interval->next() == NULL ||
interval->next()->start().Value() >= interval->start().Value());
@@ -527,56 +499,57 @@ LifetimePosition LiveRange::FirstIntersection(LiveRange* other) {
}
-LAllocator::LAllocator(int num_values, HGraph* graph)
- : zone_(graph->isolate()),
- chunk_(NULL),
- live_in_sets_(graph->blocks()->length(), zone()),
- live_ranges_(num_values * 2, zone()),
+RegisterAllocator::RegisterAllocator(InstructionSequence* code)
+ : zone_(code->isolate()),
+ code_(code),
+ live_in_sets_(code->BasicBlockCount(), zone()),
+ live_ranges_(code->VirtualRegisterCount() * 2, zone()),
fixed_live_ranges_(NULL),
fixed_double_live_ranges_(NULL),
- unhandled_live_ranges_(num_values * 2, zone()),
+ unhandled_live_ranges_(code->VirtualRegisterCount() * 2, zone()),
active_live_ranges_(8, zone()),
inactive_live_ranges_(8, zone()),
reusable_slots_(8, zone()),
- next_virtual_register_(num_values),
- first_artificial_register_(num_values),
mode_(UNALLOCATED_REGISTERS),
num_registers_(-1),
- graph_(graph),
- has_osr_entry_(false),
- allocation_ok_(true) { }
+ allocation_ok_(true) {}
-void LAllocator::InitializeLivenessAnalysis() {
+void RegisterAllocator::InitializeLivenessAnalysis() {
// Initialize the live_in sets for each block to NULL.
- int block_count = graph_->blocks()->length();
+ int block_count = code()->BasicBlockCount();
live_in_sets_.Initialize(block_count, zone());
live_in_sets_.AddBlock(NULL, block_count, zone());
}
-BitVector* LAllocator::ComputeLiveOut(HBasicBlock* block) {
+BitVector* RegisterAllocator::ComputeLiveOut(BasicBlock* block) {
// Compute live out for the given block, except not including backward
// successor edges.
- BitVector* live_out = new(zone()) BitVector(next_virtual_register_, zone());
+ BitVector* live_out =
+ new (zone()) BitVector(code()->VirtualRegisterCount(), zone());
// Process all successor blocks.
- for (HSuccessorIterator it(block->end()); !it.Done(); it.Advance()) {
+ BasicBlock::Successors successors = block->successors();
+ for (BasicBlock::Successors::iterator i = successors.begin();
+ i != successors.end(); ++i) {
// Add values live on entry to the successor. Note the successor's
// live_in will not be computed yet for backwards edges.
- HBasicBlock* successor = it.Current();
- BitVector* live_in = live_in_sets_[successor->block_id()];
+ BasicBlock* successor = *i;
+ BitVector* live_in = live_in_sets_[successor->rpo_number_];
if (live_in != NULL) live_out->Union(*live_in);
// All phi input operands corresponding to this successor edge are live
// out from this block.
int index = successor->PredecessorIndexOf(block);
- const ZoneList<HPhi*>* phis = successor->phis();
- for (int i = 0; i < phis->length(); ++i) {
- HPhi* phi = phis->at(i);
- if (!phi->OperandAt(index)->IsConstant()) {
- live_out->Add(phi->OperandAt(index)->id());
- }
+ ASSERT(index >= 0);
+ ASSERT(index < static_cast<int>(successor->PredecessorCount()));
+ for (BasicBlock::const_iterator j = successor->begin();
+ j != successor->end(); ++j) {
+ Node* phi = *j;
+ if (phi->opcode() != IrOpcode::kPhi) continue;
+ Node* input = phi->InputAt(index);
+ live_out->Add(input->id());
}
}
@@ -584,14 +557,14 @@ BitVector* LAllocator::ComputeLiveOut(HBasicBlock* block) {
}
-void LAllocator::AddInitialIntervals(HBasicBlock* block,
- BitVector* live_out) {
+void RegisterAllocator::AddInitialIntervals(BasicBlock* block,
+ BitVector* live_out) {
// Add an interval that includes the entire block to the live range for
// each live_out value.
- LifetimePosition start = LifetimePosition::FromInstructionIndex(
- block->first_instruction_index());
+ LifetimePosition start =
+ LifetimePosition::FromInstructionIndex(block->first_instruction_index());
LifetimePosition end = LifetimePosition::FromInstructionIndex(
- block->last_instruction_index()).NextInstruction();
+ block->last_instruction_index()).NextInstruction();
BitVector::Iterator iterator(live_out);
while (!iterator.Done()) {
int operand_index = iterator.Current();
@@ -602,43 +575,47 @@ void LAllocator::AddInitialIntervals(HBasicBlock* block,
}
-int LAllocator::FixedDoubleLiveRangeID(int index) {
+int RegisterAllocator::FixedDoubleLiveRangeID(int index) {
return -index - 1 - Register::kMaxNumAllocatableRegisters;
}
-LOperand* LAllocator::AllocateFixed(LUnallocated* operand,
- int pos,
- bool is_tagged) {
+InstructionOperand* RegisterAllocator::AllocateFixed(
+ UnallocatedOperand* operand, int pos, bool is_tagged) {
TraceAlloc("Allocating fixed reg for op %d\n", operand->virtual_register());
ASSERT(operand->HasFixedPolicy());
if (operand->HasFixedSlotPolicy()) {
- operand->ConvertTo(LOperand::STACK_SLOT, operand->fixed_slot_index());
+ operand->ConvertTo(InstructionOperand::STACK_SLOT,
+ operand->fixed_slot_index());
} else if (operand->HasFixedRegisterPolicy()) {
int reg_index = operand->fixed_register_index();
- operand->ConvertTo(LOperand::REGISTER, reg_index);
+ operand->ConvertTo(InstructionOperand::REGISTER, reg_index);
} else if (operand->HasFixedDoubleRegisterPolicy()) {
int reg_index = operand->fixed_register_index();
- operand->ConvertTo(LOperand::DOUBLE_REGISTER, reg_index);
+ operand->ConvertTo(InstructionOperand::DOUBLE_REGISTER, reg_index);
} else {
UNREACHABLE();
}
if (is_tagged) {
TraceAlloc("Fixed reg is tagged at %d\n", pos);
- LInstruction* instr = InstructionAt(pos);
+ Instruction* instr = InstructionAt(pos);
if (instr->HasPointerMap()) {
- instr->pointer_map()->RecordPointer(operand, chunk()->zone());
+ instr->pointer_map()->RecordPointer(operand, code_zone());
}
}
return operand;
}
-LiveRange* LAllocator::FixedLiveRangeFor(int index) {
+LiveRange* RegisterAllocator::FixedLiveRangeFor(int index) {
ASSERT(index < Register::kMaxNumAllocatableRegisters);
LiveRange* result = fixed_live_ranges_[index];
if (result == NULL) {
- result = new(zone()) LiveRange(FixedLiveRangeID(index), chunk()->zone());
+ // TODO(titzer): add a utility method to allocate a new LiveRange:
+ // The LiveRange object itself can go in this zone, but the
+ // InstructionOperand needs
+ // to go in the code zone, since it may survive register allocation.
+ result = new (zone()) LiveRange(FixedLiveRangeID(index), code_zone());
ASSERT(result->IsFixed());
result->kind_ = GENERAL_REGISTERS;
SetLiveRangeAssignedRegister(result, index);
@@ -648,12 +625,11 @@ LiveRange* LAllocator::FixedLiveRangeFor(int index) {
}
-LiveRange* LAllocator::FixedDoubleLiveRangeFor(int index) {
+LiveRange* RegisterAllocator::FixedDoubleLiveRangeFor(int index) {
ASSERT(index < DoubleRegister::NumAllocatableRegisters());
LiveRange* result = fixed_double_live_ranges_[index];
if (result == NULL) {
- result = new(zone()) LiveRange(FixedDoubleLiveRangeID(index),
- chunk()->zone());
+ result = new (zone()) LiveRange(FixedDoubleLiveRangeID(index), code_zone());
ASSERT(result->IsFixed());
result->kind_ = DOUBLE_REGISTERS;
SetLiveRangeAssignedRegister(result, index);
@@ -663,40 +639,28 @@ LiveRange* LAllocator::FixedDoubleLiveRangeFor(int index) {
}
-LiveRange* LAllocator::LiveRangeFor(int index) {
+LiveRange* RegisterAllocator::LiveRangeFor(int index) {
if (index >= live_ranges_.length()) {
live_ranges_.AddBlock(NULL, index - live_ranges_.length() + 1, zone());
}
LiveRange* result = live_ranges_[index];
if (result == NULL) {
- result = new(zone()) LiveRange(index, chunk()->zone());
+ result = new (zone()) LiveRange(index, code_zone());
live_ranges_[index] = result;
}
return result;
}
-LGap* LAllocator::GetLastGap(HBasicBlock* block) {
+GapInstruction* RegisterAllocator::GetLastGap(BasicBlock* block) {
int last_instruction = block->last_instruction_index();
- int index = chunk_->NearestGapPos(last_instruction);
- return GapAt(index);
-}
-
-
-HPhi* LAllocator::LookupPhi(LOperand* operand) const {
- if (!operand->IsUnallocated()) return NULL;
- int index = LUnallocated::cast(operand)->virtual_register();
- HValue* instr = graph_->LookupValue(index);
- if (instr != NULL && instr->IsPhi()) {
- return HPhi::cast(instr);
- }
- return NULL;
+ return code()->GapAt(last_instruction - 1);
}
-LiveRange* LAllocator::LiveRangeFor(LOperand* operand) {
+LiveRange* RegisterAllocator::LiveRangeFor(InstructionOperand* operand) {
if (operand->IsUnallocated()) {
- return LiveRangeFor(LUnallocated::cast(operand)->virtual_register());
+ return LiveRangeFor(UnallocatedOperand::cast(operand)->virtual_register());
} else if (operand->IsRegister()) {
return FixedLiveRangeFor(operand->index());
} else if (operand->IsDoubleRegister()) {
@@ -707,9 +671,9 @@ LiveRange* LAllocator::LiveRangeFor(LOperand* operand) {
}
-void LAllocator::Define(LifetimePosition position,
- LOperand* operand,
- LOperand* hint) {
+void RegisterAllocator::Define(LifetimePosition position,
+ InstructionOperand* operand,
+ InstructionOperand* hint) {
LiveRange* range = LiveRangeFor(operand);
if (range == NULL) return;
@@ -722,58 +686,58 @@ void LAllocator::Define(LifetimePosition position,
}
if (operand->IsUnallocated()) {
- LUnallocated* unalloc_operand = LUnallocated::cast(operand);
+ UnallocatedOperand* unalloc_operand = UnallocatedOperand::cast(operand);
range->AddUsePosition(position, unalloc_operand, hint, zone());
}
}
-void LAllocator::Use(LifetimePosition block_start,
- LifetimePosition position,
- LOperand* operand,
- LOperand* hint) {
+void RegisterAllocator::Use(LifetimePosition block_start,
+ LifetimePosition position,
+ InstructionOperand* operand,
+ InstructionOperand* hint) {
LiveRange* range = LiveRangeFor(operand);
if (range == NULL) return;
if (operand->IsUnallocated()) {
- LUnallocated* unalloc_operand = LUnallocated::cast(operand);
+ UnallocatedOperand* unalloc_operand = UnallocatedOperand::cast(operand);
range->AddUsePosition(position, unalloc_operand, hint, zone());
}
range->AddUseInterval(block_start, position, zone());
}
-void LAllocator::AddConstraintsGapMove(int index,
- LOperand* from,
- LOperand* to) {
- LGap* gap = GapAt(index);
- LParallelMove* move = gap->GetOrCreateParallelMove(LGap::START,
- chunk()->zone());
+void RegisterAllocator::AddConstraintsGapMove(int index,
+ InstructionOperand* from,
+ InstructionOperand* to) {
+ GapInstruction* gap = code()->GapAt(index);
+ ParallelMove* move =
+ gap->GetOrCreateParallelMove(GapInstruction::START, code_zone());
if (from->IsUnallocated()) {
- const ZoneList<LMoveOperands>* move_operands = move->move_operands();
+ const ZoneList<MoveOperands>* move_operands = move->move_operands();
for (int i = 0; i < move_operands->length(); ++i) {
- LMoveOperands cur = move_operands->at(i);
- LOperand* cur_to = cur.destination();
+ MoveOperands cur = move_operands->at(i);
+ InstructionOperand* cur_to = cur.destination();
if (cur_to->IsUnallocated()) {
- if (LUnallocated::cast(cur_to)->virtual_register() ==
- LUnallocated::cast(from)->virtual_register()) {
- move->AddMove(cur.source(), to, chunk()->zone());
+ if (UnallocatedOperand::cast(cur_to)->virtual_register() ==
+ UnallocatedOperand::cast(from)->virtual_register()) {
+ move->AddMove(cur.source(), to, code_zone());
return;
}
}
}
}
- move->AddMove(from, to, chunk()->zone());
+ move->AddMove(from, to, code_zone());
}
-void LAllocator::MeetRegisterConstraints(HBasicBlock* block) {
+void RegisterAllocator::MeetRegisterConstraints(BasicBlock* block) {
int start = block->first_instruction_index();
int end = block->last_instruction_index();
- if (start == -1) return;
+ ASSERT_NE(-1, start);
for (int i = start; i <= end; ++i) {
- if (IsGapAt(i)) {
- LInstruction* instr = NULL;
- LInstruction* prev_instr = NULL;
+ if (code()->IsGapAt(i)) {
+ Instruction* instr = NULL;
+ Instruction* prev_instr = NULL;
if (i < end) instr = InstructionAt(i + 1);
if (i > start) prev_instr = InstructionAt(i - 1);
MeetConstraintsBetween(prev_instr, instr, i);
@@ -783,315 +747,304 @@ void LAllocator::MeetRegisterConstraints(HBasicBlock* block) {
}
-void LAllocator::MeetConstraintsBetween(LInstruction* first,
- LInstruction* second,
- int gap_index) {
- // Handle fixed temporaries.
+void RegisterAllocator::MeetConstraintsBetween(Instruction* first,
+ Instruction* second,
+ int gap_index) {
if (first != NULL) {
- for (TempIterator it(first); !it.Done(); it.Advance()) {
- LUnallocated* temp = LUnallocated::cast(it.Current());
+ // Handle fixed temporaries.
+ for (size_t i = 0; i < first->TempCount(); i++) {
+ UnallocatedOperand* temp = UnallocatedOperand::cast(first->TempAt(i));
if (temp->HasFixedPolicy()) {
AllocateFixed(temp, gap_index - 1, false);
}
}
- }
- // Handle fixed output operand.
- if (first != NULL && first->Output() != NULL) {
- LUnallocated* first_output = LUnallocated::cast(first->Output());
- LiveRange* range = LiveRangeFor(first_output->virtual_register());
- bool assigned = false;
- if (first_output->HasFixedPolicy()) {
- LUnallocated* output_copy = first_output->CopyUnconstrained(
- chunk()->zone());
- bool is_tagged = HasTaggedValue(first_output->virtual_register());
- AllocateFixed(first_output, gap_index, is_tagged);
-
- // This value is produced on the stack, we never need to spill it.
- if (first_output->IsStackSlot()) {
- range->SetSpillOperand(first_output);
+ // Handle constant/fixed output operands.
+ for (size_t i = 0; i < first->OutputCount(); i++) {
+ InstructionOperand* output = first->OutputAt(i);
+ if (output->IsConstant()) {
+ int output_vreg = output->index();
+ LiveRange* range = LiveRangeFor(output_vreg);
range->SetSpillStartIndex(gap_index - 1);
- assigned = true;
- }
- chunk_->AddGapMove(gap_index, first_output, output_copy);
- }
+ range->SetSpillOperand(output);
+ } else {
+ UnallocatedOperand* first_output = UnallocatedOperand::cast(output);
+ LiveRange* range = LiveRangeFor(first_output->virtual_register());
+ bool assigned = false;
+ if (first_output->HasFixedPolicy()) {
+ UnallocatedOperand* output_copy =
+ first_output->CopyUnconstrained(code_zone());
+ bool is_tagged = HasTaggedValue(first_output->virtual_register());
+ AllocateFixed(first_output, gap_index, is_tagged);
+
+ // This value is produced on the stack, we never need to spill it.
+ if (first_output->IsStackSlot()) {
+ range->SetSpillOperand(first_output);
+ range->SetSpillStartIndex(gap_index - 1);
+ assigned = true;
+ }
+ code()->AddGapMove(gap_index, first_output, output_copy);
+ }
- if (!assigned) {
- range->SetSpillStartIndex(gap_index);
-
- // This move to spill operand is not a real use. Liveness analysis
- // and splitting of live ranges do not account for it.
- // Thus it should be inserted to a lifetime position corresponding to
- // the instruction end.
- LGap* gap = GapAt(gap_index);
- LParallelMove* move = gap->GetOrCreateParallelMove(LGap::BEFORE,
- chunk()->zone());
- move->AddMove(first_output, range->GetSpillOperand(),
- chunk()->zone());
+ if (!assigned) {
+ range->SetSpillStartIndex(gap_index);
+
+ // This move to spill operand is not a real use. Liveness analysis
+ // and splitting of live ranges do not account for it.
+ // Thus it should be inserted to a lifetime position corresponding to
+ // the instruction end.
+ GapInstruction* gap = code()->GapAt(gap_index);
+ ParallelMove* move =
+ gap->GetOrCreateParallelMove(GapInstruction::BEFORE, code_zone());
+ move->AddMove(first_output, range->GetSpillOperand(), code_zone());
+ }
+ }
}
}
- // Handle fixed input operands of second instruction.
if (second != NULL) {
- for (UseIterator it(second); !it.Done(); it.Advance()) {
- LUnallocated* cur_input = LUnallocated::cast(it.Current());
+ // Handle fixed input operands of second instruction.
+ for (size_t i = 0; i < second->InputCount(); i++) {
+ InstructionOperand* input = second->InputAt(i);
+ if (input->IsImmediate()) continue; // Ignore immediates.
+ UnallocatedOperand* cur_input = UnallocatedOperand::cast(input);
if (cur_input->HasFixedPolicy()) {
- LUnallocated* input_copy = cur_input->CopyUnconstrained(
- chunk()->zone());
+ UnallocatedOperand* input_copy =
+ cur_input->CopyUnconstrained(code_zone());
bool is_tagged = HasTaggedValue(cur_input->virtual_register());
AllocateFixed(cur_input, gap_index + 1, is_tagged);
AddConstraintsGapMove(gap_index, input_copy, cur_input);
- } else if (cur_input->HasWritableRegisterPolicy()) {
- // The live range of writable input registers always goes until the end
- // of the instruction.
- ASSERT(!cur_input->IsUsedAtStart());
-
- LUnallocated* input_copy = cur_input->CopyUnconstrained(
- chunk()->zone());
- int vreg = GetVirtualRegister();
- if (!AllocationOk()) return;
- cur_input->set_virtual_register(vreg);
-
- if (RequiredRegisterKind(input_copy->virtual_register()) ==
- DOUBLE_REGISTERS) {
- double_artificial_registers_.Add(
- cur_input->virtual_register() - first_artificial_register_,
- zone());
- }
-
- AddConstraintsGapMove(gap_index, input_copy, cur_input);
}
}
- }
- // Handle "output same as input" for second instruction.
- if (second != NULL && second->Output() != NULL) {
- LUnallocated* second_output = LUnallocated::cast(second->Output());
- if (second_output->HasSameAsInputPolicy()) {
- LUnallocated* cur_input = LUnallocated::cast(second->FirstInput());
- int output_vreg = second_output->virtual_register();
- int input_vreg = cur_input->virtual_register();
-
- LUnallocated* input_copy = cur_input->CopyUnconstrained(
- chunk()->zone());
- cur_input->set_virtual_register(second_output->virtual_register());
- AddConstraintsGapMove(gap_index, input_copy, cur_input);
-
- if (HasTaggedValue(input_vreg) && !HasTaggedValue(output_vreg)) {
- int index = gap_index + 1;
- LInstruction* instr = InstructionAt(index);
- if (instr->HasPointerMap()) {
- instr->pointer_map()->RecordPointer(input_copy, chunk()->zone());
+ // Handle "output same as input" for second instruction.
+ for (size_t i = 0; i < second->OutputCount(); i++) {
+ InstructionOperand* output = second->Output();
+ if (!output->IsUnallocated()) continue;
+ UnallocatedOperand* second_output = UnallocatedOperand::cast(output);
+ if (second_output->HasSameAsInputPolicy()) {
+ ASSERT(second->OutputCount() == 1); // Only valid for one output.
+ UnallocatedOperand* cur_input =
+ UnallocatedOperand::cast(second->InputAt(0));
+ int output_vreg = second_output->virtual_register();
+ int input_vreg = cur_input->virtual_register();
+
+ UnallocatedOperand* input_copy =
+ cur_input->CopyUnconstrained(code_zone());
+ cur_input->set_virtual_register(second_output->virtual_register());
+ AddConstraintsGapMove(gap_index, input_copy, cur_input);
+
+ if (HasTaggedValue(input_vreg) && !HasTaggedValue(output_vreg)) {
+ int index = gap_index + 1;
+ Instruction* instr = InstructionAt(index);
+ if (instr->HasPointerMap()) {
+ instr->pointer_map()->RecordPointer(input_copy, code_zone());
+ }
+ } else if (!HasTaggedValue(input_vreg) && HasTaggedValue(output_vreg)) {
+ // The input is assumed to immediately have a tagged representation,
+ // before the pointer map can be used. I.e. the pointer map at the
+ // instruction will include the output operand (whose value at the
+ // beginning of the instruction is equal to the input operand). If
+ // this is not desired, then the pointer map at this instruction needs
+ // to be adjusted manually.
}
- } else if (!HasTaggedValue(input_vreg) && HasTaggedValue(output_vreg)) {
- // The input is assumed to immediately have a tagged representation,
- // before the pointer map can be used. I.e. the pointer map at the
- // instruction will include the output operand (whose value at the
- // beginning of the instruction is equal to the input operand). If
- // this is not desired, then the pointer map at this instruction needs
- // to be adjusted manually.
}
}
}
}
-void LAllocator::ProcessInstructions(HBasicBlock* block, BitVector* live) {
+bool RegisterAllocator::IsOutputRegisterOf(Instruction* instr, int index) {
+ for (size_t i = 0; i < instr->OutputCount(); i++) {
+ InstructionOperand* output = instr->OutputAt(i);
+ if (output->IsRegister() && output->index() == index) return true;
+ }
+ return false;
+}
+
+
+bool RegisterAllocator::IsOutputDoubleRegisterOf(Instruction* instr,
+ int index) {
+ for (size_t i = 0; i < instr->OutputCount(); i++) {
+ InstructionOperand* output = instr->OutputAt(i);
+ if (output->IsDoubleRegister() && output->index() == index) return true;
+ }
+ return false;
+}
+
+
+void RegisterAllocator::ProcessInstructions(BasicBlock* block,
+ BitVector* live) {
int block_start = block->first_instruction_index();
- int index = block->last_instruction_index();
LifetimePosition block_start_position =
LifetimePosition::FromInstructionIndex(block_start);
- while (index >= block_start) {
+ for (int index = block->last_instruction_index(); index >= block_start;
+ index--) {
LifetimePosition curr_position =
LifetimePosition::FromInstructionIndex(index);
- if (IsGapAt(index)) {
- // We have a gap at this position.
- LGap* gap = GapAt(index);
- LParallelMove* move = gap->GetOrCreateParallelMove(LGap::START,
- chunk()->zone());
- const ZoneList<LMoveOperands>* move_operands = move->move_operands();
+ Instruction* instr = InstructionAt(index);
+ ASSERT(instr != NULL);
+ if (instr->IsGapMoves()) {
+ // Process the moves of the gap instruction, making their sources live.
+ GapInstruction* gap = code()->GapAt(index);
+
+ // TODO(titzer): no need to create the parallel move if it doesn't exist.
+ ParallelMove* move =
+ gap->GetOrCreateParallelMove(GapInstruction::START, code_zone());
+ const ZoneList<MoveOperands>* move_operands = move->move_operands();
for (int i = 0; i < move_operands->length(); ++i) {
- LMoveOperands* cur = &move_operands->at(i);
+ MoveOperands* cur = &move_operands->at(i);
if (cur->IsIgnored()) continue;
- LOperand* from = cur->source();
- LOperand* to = cur->destination();
- HPhi* phi = LookupPhi(to);
- LOperand* hint = to;
- if (phi != NULL) {
- // This is a phi resolving move.
- if (!phi->block()->IsLoopHeader()) {
- hint = LiveRangeFor(phi->id())->current_hint_operand();
- }
- } else {
- if (to->IsUnallocated()) {
- if (live->Contains(LUnallocated::cast(to)->virtual_register())) {
+ InstructionOperand* from = cur->source();
+ InstructionOperand* to = cur->destination();
+ InstructionOperand* hint = to;
+ if (to->IsUnallocated()) {
+ int to_vreg = UnallocatedOperand::cast(to)->virtual_register();
+ LiveRange* to_range = LiveRangeFor(to_vreg);
+ if (to_range->is_phi()) {
+ if (to_range->is_non_loop_phi()) {
+ hint = to_range->current_hint_operand();
+ }
+ } else {
+ if (live->Contains(to_vreg)) {
Define(curr_position, to, from);
- live->Remove(LUnallocated::cast(to)->virtual_register());
+ live->Remove(to_vreg);
} else {
cur->Eliminate();
continue;
}
- } else {
- Define(curr_position, to, from);
}
+ } else {
+ Define(curr_position, to, from);
}
Use(block_start_position, curr_position, from, hint);
if (from->IsUnallocated()) {
- live->Add(LUnallocated::cast(from)->virtual_register());
+ live->Add(UnallocatedOperand::cast(from)->virtual_register());
}
}
} else {
- ASSERT(!IsGapAt(index));
- LInstruction* instr = InstructionAt(index);
-
- if (instr != NULL) {
- LOperand* output = instr->Output();
- if (output != NULL) {
- if (output->IsUnallocated()) {
- live->Remove(LUnallocated::cast(output)->virtual_register());
- }
- Define(curr_position, output, NULL);
+ // Process output, inputs, and temps of this non-gap instruction.
+ for (size_t i = 0; i < instr->OutputCount(); i++) {
+ InstructionOperand* output = instr->OutputAt(i);
+ if (output->IsUnallocated()) {
+ int out_vreg = UnallocatedOperand::cast(output)->virtual_register();
+ live->Remove(out_vreg);
+ } else if (output->IsConstant()) {
+ int out_vreg = output->index();
+ live->Remove(out_vreg);
}
+ Define(curr_position, output, NULL);
+ }
- if (instr->ClobbersRegisters()) {
- for (int i = 0; i < Register::kMaxNumAllocatableRegisters; ++i) {
- if (output == NULL || !output->IsRegister() ||
- output->index() != i) {
- LiveRange* range = FixedLiveRangeFor(i);
- range->AddUseInterval(curr_position,
- curr_position.InstructionEnd(),
- zone());
- }
+ if (instr->ClobbersRegisters()) {
+ for (int i = 0; i < Register::kMaxNumAllocatableRegisters; ++i) {
+ if (!IsOutputRegisterOf(instr, i)) {
+ LiveRange* range = FixedLiveRangeFor(i);
+ range->AddUseInterval(curr_position, curr_position.InstructionEnd(),
+ zone());
}
}
+ }
- if (instr->ClobbersDoubleRegisters(isolate())) {
- for (int i = 0; i < DoubleRegister::NumAllocatableRegisters(); ++i) {
- if (output == NULL || !output->IsDoubleRegister() ||
- output->index() != i) {
- LiveRange* range = FixedDoubleLiveRangeFor(i);
- range->AddUseInterval(curr_position,
- curr_position.InstructionEnd(),
- zone());
- }
+ if (instr->ClobbersDoubleRegisters()) {
+ for (int i = 0; i < DoubleRegister::NumAllocatableRegisters(); ++i) {
+ if (!IsOutputDoubleRegisterOf(instr, i)) {
+ LiveRange* range = FixedDoubleLiveRangeFor(i);
+ range->AddUseInterval(curr_position, curr_position.InstructionEnd(),
+ zone());
}
}
+ }
- for (UseIterator it(instr); !it.Done(); it.Advance()) {
- LOperand* input = it.Current();
-
- LifetimePosition use_pos;
- if (input->IsUnallocated() &&
- LUnallocated::cast(input)->IsUsedAtStart()) {
- use_pos = curr_position;
- } else {
- use_pos = curr_position.InstructionEnd();
- }
-
- Use(block_start_position, use_pos, input, NULL);
- if (input->IsUnallocated()) {
- live->Add(LUnallocated::cast(input)->virtual_register());
- }
+ for (size_t i = 0; i < instr->InputCount(); i++) {
+ InstructionOperand* input = instr->InputAt(i);
+ if (input->IsImmediate()) continue; // Ignore immediates.
+ LifetimePosition use_pos;
+ if (input->IsUnallocated() &&
+ UnallocatedOperand::cast(input)->IsUsedAtStart()) {
+ use_pos = curr_position;
+ } else {
+ use_pos = curr_position.InstructionEnd();
}
- for (TempIterator it(instr); !it.Done(); it.Advance()) {
- LOperand* temp = it.Current();
- if (instr->ClobbersTemps()) {
- if (temp->IsRegister()) continue;
- if (temp->IsUnallocated()) {
- LUnallocated* temp_unalloc = LUnallocated::cast(temp);
- if (temp_unalloc->HasFixedPolicy()) {
- continue;
- }
- }
- }
- Use(block_start_position, curr_position.InstructionEnd(), temp, NULL);
- Define(curr_position, temp, NULL);
+ Use(block_start_position, use_pos, input, NULL);
+ if (input->IsUnallocated()) {
+ live->Add(UnallocatedOperand::cast(input)->virtual_register());
+ }
+ }
+ for (size_t i = 0; i < instr->TempCount(); i++) {
+ InstructionOperand* temp = instr->TempAt(i);
+ if (instr->ClobbersTemps()) {
+ if (temp->IsRegister()) continue;
if (temp->IsUnallocated()) {
- LUnallocated* temp_unalloc = LUnallocated::cast(temp);
- if (temp_unalloc->HasDoubleRegisterPolicy()) {
- double_artificial_registers_.Add(
- temp_unalloc->virtual_register() - first_artificial_register_,
- zone());
+ UnallocatedOperand* temp_unalloc = UnallocatedOperand::cast(temp);
+ if (temp_unalloc->HasFixedPolicy()) {
+ continue;
}
}
}
+ Use(block_start_position, curr_position.InstructionEnd(), temp, NULL);
+ Define(curr_position, temp, NULL);
}
}
-
- index = index - 1;
}
}
-void LAllocator::ResolvePhis(HBasicBlock* block) {
- const ZoneList<HPhi*>* phis = block->phis();
- for (int i = 0; i < phis->length(); ++i) {
- HPhi* phi = phis->at(i);
- LUnallocated* phi_operand =
- new(chunk()->zone()) LUnallocated(LUnallocated::NONE);
+void RegisterAllocator::ResolvePhis(BasicBlock* block) {
+ for (BasicBlock::const_iterator i = block->begin(); i != block->end(); ++i) {
+ Node* phi = *i;
+ if (phi->opcode() != IrOpcode::kPhi) continue;
+
+ UnallocatedOperand* phi_operand =
+ new (code_zone()) UnallocatedOperand(UnallocatedOperand::NONE);
phi_operand->set_virtual_register(phi->id());
- for (int j = 0; j < phi->OperandCount(); ++j) {
- HValue* op = phi->OperandAt(j);
- LOperand* operand = NULL;
- if (op->IsConstant() && op->EmitAtUses()) {
- HConstant* constant = HConstant::cast(op);
- operand = chunk_->DefineConstantOperand(constant);
- } else {
- ASSERT(!op->EmitAtUses());
- LUnallocated* unalloc =
- new(chunk()->zone()) LUnallocated(LUnallocated::ANY);
- unalloc->set_virtual_register(op->id());
- operand = unalloc;
- }
- HBasicBlock* cur_block = block->predecessors()->at(j);
+
+ int j = 0;
+ Node::Inputs inputs = phi->inputs();
+ for (Node::Inputs::iterator iter(inputs.begin()); iter != inputs.end();
+ ++iter, ++j) {
+ Node* op = *iter;
+ // TODO(mstarzinger): Use a ValueInputIterator instead.
+ if (j >= block->PredecessorCount()) continue;
+ UnallocatedOperand* operand =
+ new (code_zone()) UnallocatedOperand(UnallocatedOperand::ANY);
+ operand->set_virtual_register(op->id());
+ BasicBlock* cur_block = block->PredecessorAt(j);
// The gap move must be added without any special processing as in
// the AddConstraintsGapMove.
- chunk_->AddGapMove(cur_block->last_instruction_index() - 1,
- operand,
+ code()->AddGapMove(cur_block->last_instruction_index() - 1, operand,
phi_operand);
- // We are going to insert a move before the branch instruction.
- // Some branch instructions (e.g. loops' back edges)
- // can potentially cause a GC so they have a pointer map.
- // By inserting a move we essentially create a copy of a
- // value which is invisible to PopulatePointerMaps(), because we store
- // it into a location different from the operand of a live range
- // covering a branch instruction.
- // Thus we need to manually record a pointer.
- LInstruction* branch =
- InstructionAt(cur_block->last_instruction_index());
- if (branch->HasPointerMap()) {
- if (phi->representation().IsTagged() && !phi->type().IsSmi()) {
- branch->pointer_map()->RecordPointer(phi_operand, chunk()->zone());
- } else if (!phi->representation().IsDouble()) {
- branch->pointer_map()->RecordUntagged(phi_operand, chunk()->zone());
- }
- }
+ Instruction* branch = InstructionAt(cur_block->last_instruction_index());
+ ASSERT(!branch->HasPointerMap());
+ USE(branch);
}
LiveRange* live_range = LiveRangeFor(phi->id());
- LLabel* label = chunk_->GetLabel(phi->block()->block_id());
- label->GetOrCreateParallelMove(LGap::START, chunk()->zone())->
- AddMove(phi_operand, live_range->GetSpillOperand(), chunk()->zone());
- live_range->SetSpillStartIndex(phi->block()->first_instruction_index());
+ BlockStartInstruction* block_start = code()->GetBlockStart(block);
+ block_start->GetOrCreateParallelMove(GapInstruction::START, code_zone())
+ ->AddMove(phi_operand, live_range->GetSpillOperand(), code_zone());
+ live_range->SetSpillStartIndex(block->first_instruction_index());
+
+ // We use the phi-ness of some nodes in some later heuristics.
+ live_range->set_is_phi(true);
+ if (!block->IsLoopHeader()) {
+ live_range->set_is_non_loop_phi(true);
+ }
}
}
-bool LAllocator::Allocate(LChunk* chunk) {
- ASSERT(chunk_ == NULL);
- chunk_ = static_cast<LPlatformChunk*>(chunk);
- assigned_registers_ =
- new(chunk->zone()) BitVector(Register::NumAllocatableRegisters(),
- chunk->zone());
- assigned_double_registers_ =
- new(chunk->zone()) BitVector(DoubleRegister::NumAllocatableRegisters(),
- chunk->zone());
+bool RegisterAllocator::Allocate() {
+ assigned_registers_ = new (code_zone())
+ BitVector(Register::NumAllocatableRegisters(), code_zone());
+ assigned_double_registers_ = new (code_zone())
+ BitVector(DoubleRegister::NumAllocatableRegisters(), code_zone());
MeetRegisterConstraints();
if (!AllocationOk()) return false;
ResolvePhis();
@@ -1103,36 +1056,33 @@ bool LAllocator::Allocate(LChunk* chunk) {
PopulatePointerMaps();
ConnectRanges();
ResolveControlFlow();
+ code()->frame()->SetAllocatedRegisters(assigned_registers_);
+ code()->frame()->SetAllocatedDoubleRegisters(assigned_double_registers_);
return true;
}
-void LAllocator::MeetRegisterConstraints() {
- LAllocatorPhase phase("L_Register constraints", this);
- const ZoneList<HBasicBlock*>* blocks = graph_->blocks();
- for (int i = 0; i < blocks->length(); ++i) {
- HBasicBlock* block = blocks->at(i);
- MeetRegisterConstraints(block);
+void RegisterAllocator::MeetRegisterConstraints() {
+ RegisterAllocatorPhase phase("L_Register constraints", this);
+ for (int i = 0; i < code()->BasicBlockCount(); ++i) {
+ MeetRegisterConstraints(code()->BlockAt(i));
if (!AllocationOk()) return;
}
}
-void LAllocator::ResolvePhis() {
- LAllocatorPhase phase("L_Resolve phis", this);
+void RegisterAllocator::ResolvePhis() {
+ RegisterAllocatorPhase phase("L_Resolve phis", this);
// Process the blocks in reverse order.
- const ZoneList<HBasicBlock*>* blocks = graph_->blocks();
- for (int block_id = blocks->length() - 1; block_id >= 0; --block_id) {
- HBasicBlock* block = blocks->at(block_id);
- ResolvePhis(block);
+ for (int i = code()->BasicBlockCount() - 1; i >= 0; --i) {
+ ResolvePhis(code()->BlockAt(i));
}
}
-void LAllocator::ResolveControlFlow(LiveRange* range,
- HBasicBlock* block,
- HBasicBlock* pred) {
+void RegisterAllocator::ResolveControlFlow(LiveRange* range, BasicBlock* block,
+ BasicBlock* pred) {
LifetimePosition pred_end =
LifetimePosition::FromInstructionIndex(pred->last_instruction_index());
LifetimePosition cur_start =
@@ -1155,63 +1105,51 @@ void LAllocator::ResolveControlFlow(LiveRange* range,
if (cur_cover->IsSpilled()) return;
ASSERT(pred_cover != NULL && cur_cover != NULL);
if (pred_cover != cur_cover) {
- LOperand* pred_op = pred_cover->CreateAssignedOperand(chunk()->zone());
- LOperand* cur_op = cur_cover->CreateAssignedOperand(chunk()->zone());
+ InstructionOperand* pred_op =
+ pred_cover->CreateAssignedOperand(code_zone());
+ InstructionOperand* cur_op = cur_cover->CreateAssignedOperand(code_zone());
if (!pred_op->Equals(cur_op)) {
- LGap* gap = NULL;
- if (block->predecessors()->length() == 1) {
- gap = GapAt(block->first_instruction_index());
+ GapInstruction* gap = NULL;
+ if (block->PredecessorCount() == 1) {
+ gap = code()->GapAt(block->first_instruction_index());
} else {
- ASSERT(pred->end()->SecondSuccessor() == NULL);
+ ASSERT(pred->SuccessorCount() == 1);
gap = GetLastGap(pred);
- // We are going to insert a move before the branch instruction.
- // Some branch instructions (e.g. loops' back edges)
- // can potentially cause a GC so they have a pointer map.
- // By inserting a move we essentially create a copy of a
- // value which is invisible to PopulatePointerMaps(), because we store
- // it into a location different from the operand of a live range
- // covering a branch instruction.
- // Thus we need to manually record a pointer.
- LInstruction* branch = InstructionAt(pred->last_instruction_index());
- if (branch->HasPointerMap()) {
- if (HasTaggedValue(range->id())) {
- branch->pointer_map()->RecordPointer(cur_op, chunk()->zone());
- } else if (!cur_op->IsDoubleStackSlot() &&
- !cur_op->IsDoubleRegister()) {
- branch->pointer_map()->RemovePointer(cur_op);
- }
- }
+ Instruction* branch = InstructionAt(pred->last_instruction_index());
+ ASSERT(!branch->HasPointerMap());
+ USE(branch);
}
- gap->GetOrCreateParallelMove(
- LGap::START, chunk()->zone())->AddMove(pred_op, cur_op,
- chunk()->zone());
+ gap->GetOrCreateParallelMove(GapInstruction::START, code_zone())
+ ->AddMove(pred_op, cur_op, code_zone());
}
}
}
-LParallelMove* LAllocator::GetConnectingParallelMove(LifetimePosition pos) {
+ParallelMove* RegisterAllocator::GetConnectingParallelMove(
+ LifetimePosition pos) {
int index = pos.InstructionIndex();
- if (IsGapAt(index)) {
- LGap* gap = GapAt(index);
+ if (code()->IsGapAt(index)) {
+ GapInstruction* gap = code()->GapAt(index);
return gap->GetOrCreateParallelMove(
- pos.IsInstructionStart() ? LGap::START : LGap::END, chunk()->zone());
+ pos.IsInstructionStart() ? GapInstruction::START : GapInstruction::END,
+ code_zone());
}
int gap_pos = pos.IsInstructionStart() ? (index - 1) : (index + 1);
- return GapAt(gap_pos)->GetOrCreateParallelMove(
- (gap_pos < index) ? LGap::AFTER : LGap::BEFORE, chunk()->zone());
+ return code()->GapAt(gap_pos)->GetOrCreateParallelMove(
+ (gap_pos < index) ? GapInstruction::AFTER : GapInstruction::BEFORE,
+ code_zone());
}
-HBasicBlock* LAllocator::GetBlock(LifetimePosition pos) {
- LGap* gap = GapAt(chunk_->NearestGapPos(pos.InstructionIndex()));
- return gap->block();
+BasicBlock* RegisterAllocator::GetBlock(LifetimePosition pos) {
+ return code()->GetBasicBlock(pos.InstructionIndex());
}
-void LAllocator::ConnectRanges() {
- LAllocatorPhase phase("L_Connect ranges", this);
+void RegisterAllocator::ConnectRanges() {
+ RegisterAllocatorPhase phase("L_Connect ranges", this);
for (int i = 0; i < live_ranges()->length(); ++i) {
LiveRange* first_range = live_ranges()->at(i);
if (first_range == NULL || first_range->parent() != NULL) continue;
@@ -1229,13 +1167,12 @@ void LAllocator::ConnectRanges() {
should_insert = CanEagerlyResolveControlFlow(GetBlock(pos));
}
if (should_insert) {
- LParallelMove* move = GetConnectingParallelMove(pos);
- LOperand* prev_operand = first_range->CreateAssignedOperand(
- chunk()->zone());
- LOperand* cur_operand = second_range->CreateAssignedOperand(
- chunk()->zone());
- move->AddMove(prev_operand, cur_operand,
- chunk()->zone());
+ ParallelMove* move = GetConnectingParallelMove(pos);
+ InstructionOperand* prev_operand =
+ first_range->CreateAssignedOperand(code_zone());
+ InstructionOperand* cur_operand =
+ second_range->CreateAssignedOperand(code_zone());
+ move->AddMove(prev_operand, cur_operand, code_zone());
}
}
}
@@ -1247,24 +1184,25 @@ void LAllocator::ConnectRanges() {
}
-bool LAllocator::CanEagerlyResolveControlFlow(HBasicBlock* block) const {
- if (block->predecessors()->length() != 1) return false;
- return block->predecessors()->first()->block_id() == block->block_id() - 1;
+bool RegisterAllocator::CanEagerlyResolveControlFlow(BasicBlock* block) const {
+ if (block->PredecessorCount() != 1) return false;
+ return block->PredecessorAt(0)->rpo_number_ == block->rpo_number_ - 1;
}
-void LAllocator::ResolveControlFlow() {
- LAllocatorPhase phase("L_Resolve control flow", this);
- const ZoneList<HBasicBlock*>* blocks = graph_->blocks();
- for (int block_id = 1; block_id < blocks->length(); ++block_id) {
- HBasicBlock* block = blocks->at(block_id);
+void RegisterAllocator::ResolveControlFlow() {
+ RegisterAllocatorPhase phase("L_Resolve control flow", this);
+ for (int block_id = 1; block_id < code()->BasicBlockCount(); ++block_id) {
+ BasicBlock* block = code()->BlockAt(block_id);
if (CanEagerlyResolveControlFlow(block)) continue;
- BitVector* live = live_in_sets_[block->block_id()];
+ BitVector* live = live_in_sets_[block->rpo_number_];
BitVector::Iterator iterator(live);
while (!iterator.Done()) {
int operand_index = iterator.Current();
- for (int i = 0; i < block->predecessors()->length(); ++i) {
- HBasicBlock* cur = block->predecessors()->at(i);
+ BasicBlock::Predecessors predecessors = block->predecessors();
+ for (BasicBlock::Predecessors::iterator i = predecessors.begin();
+ i != predecessors.end(); ++i) {
+ BasicBlock* cur = *i;
LiveRange* cur_range = LiveRangeFor(operand_index);
ResolveControlFlow(cur_range, block, cur);
}
@@ -1274,13 +1212,13 @@ void LAllocator::ResolveControlFlow() {
}
-void LAllocator::BuildLiveRanges() {
- LAllocatorPhase phase("L_Build live ranges", this);
+void RegisterAllocator::BuildLiveRanges() {
+ RegisterAllocatorPhase phase("L_Build live ranges", this);
InitializeLivenessAnalysis();
// Process the blocks in reverse order.
- const ZoneList<HBasicBlock*>* blocks = graph_->blocks();
- for (int block_id = blocks->length() - 1; block_id >= 0; --block_id) {
- HBasicBlock* block = blocks->at(block_id);
+ for (int block_id = code()->BasicBlockCount() - 1; block_id >= 0;
+ --block_id) {
+ BasicBlock* block = code()->BlockAt(block_id);
BitVector* live = ComputeLiveOut(block);
// Initially consider all live_out values live for the entire block. We
// will shorten these intervals if necessary.
@@ -1290,22 +1228,26 @@ void LAllocator::BuildLiveRanges() {
// live values.
ProcessInstructions(block, live);
// All phi output operands are killed by this block.
- const ZoneList<HPhi*>* phis = block->phis();
- for (int i = 0; i < phis->length(); ++i) {
+ for (BasicBlock::const_iterator i = block->begin(); i != block->end();
+ ++i) {
+ Node* phi = *i;
+ if (phi->opcode() != IrOpcode::kPhi) continue;
+
// The live range interval already ends at the first instruction of the
// block.
- HPhi* phi = phis->at(i);
live->Remove(phi->id());
- LOperand* hint = NULL;
- LOperand* phi_operand = NULL;
- LGap* gap = GetLastGap(phi->block()->predecessors()->at(0));
- LParallelMove* move = gap->GetOrCreateParallelMove(LGap::START,
- chunk()->zone());
+ InstructionOperand* hint = NULL;
+ InstructionOperand* phi_operand = NULL;
+ GapInstruction* gap = GetLastGap(block->PredecessorAt(0));
+
+ // TODO(titzer): no need to create the parallel move if it doesn't exit.
+ ParallelMove* move =
+ gap->GetOrCreateParallelMove(GapInstruction::START, code_zone());
for (int j = 0; j < move->move_operands()->length(); ++j) {
- LOperand* to = move->move_operands()->at(j).destination();
+ InstructionOperand* to = move->move_operands()->at(j).destination();
if (to->IsUnallocated() &&
- LUnallocated::cast(to)->virtual_register() == phi->id()) {
+ UnallocatedOperand::cast(to)->virtual_register() == phi->id()) {
hint = move->move_operands()->at(j).source();
phi_operand = to;
break;
@@ -1314,7 +1256,7 @@ void LAllocator::BuildLiveRanges() {
ASSERT(hint != NULL);
LifetimePosition block_start = LifetimePosition::FromInstructionIndex(
- block->first_instruction_index());
+ block->first_instruction_index());
Define(block_start, phi_operand, hint);
}
@@ -1322,19 +1264,16 @@ void LAllocator::BuildLiveRanges() {
// out on backward successor edges.
live_in_sets_[block_id] = live;
- // If this block is a loop header go back and patch up the necessary
- // predecessor blocks.
if (block->IsLoopHeader()) {
- // TODO(kmillikin): Need to be able to get the last block of the loop
- // in the loop information. Add a live range stretching from the first
- // loop instruction to the last for each value live on entry to the
- // header.
- HBasicBlock* back_edge = block->loop_information()->GetLastBackEdge();
+ // Add a live range stretching from the first loop instruction to the last
+ // for each value live on entry to the header.
BitVector::Iterator iterator(live);
LifetimePosition start = LifetimePosition::FromInstructionIndex(
block->first_instruction_index());
- LifetimePosition end = LifetimePosition::FromInstructionIndex(
- back_edge->last_instruction_index()).NextInstruction();
+ int end_index =
+ code()->BlockAt(block->loop_end_)->last_instruction_index();
+ LifetimePosition end =
+ LifetimePosition::FromInstructionIndex(end_index).NextInstruction();
while (!iterator.Done()) {
int operand_index = iterator.Current();
LiveRange* range = LiveRangeFor(operand_index);
@@ -1342,7 +1281,8 @@ void LAllocator::BuildLiveRanges() {
iterator.Advance();
}
- for (int i = block->block_id() + 1; i <= back_edge->block_id(); ++i) {
+ // Insert all values into the live in sets of all blocks in the loop.
+ for (int i = block->rpo_number_ + 1; i < block->loop_end_; ++i) {
live_in_sets_[i]->Union(*live);
}
}
@@ -1354,18 +1294,24 @@ void LAllocator::BuildLiveRanges() {
while (!iterator.Done()) {
found = true;
int operand_index = iterator.Current();
- if (chunk_->info()->IsStub()) {
- CodeStub::Major major_key = chunk_->info()->code_stub()->MajorKey();
- PrintF("Function: %s\n", CodeStub::MajorName(major_key, false));
+ PrintF("Register allocator error: live v%d reached first block.\n",
+ operand_index);
+ LiveRange* range = LiveRangeFor(operand_index);
+ PrintF(" (first use is at %d)\n", range->first_pos()->pos().Value());
+ CompilationInfo* info = code()->linkage()->info();
+ if (info->IsStub()) {
+ if (info->code_stub() == NULL) {
+ PrintF("\n");
+ } else {
+ CodeStub::Major major_key = info->code_stub()->MajorKey();
+ PrintF(" (function: %s)\n", CodeStub::MajorName(major_key, false));
+ }
} else {
- ASSERT(chunk_->info()->IsOptimizing());
+ ASSERT(info->IsOptimizing());
AllowHandleDereference allow_deref;
- PrintF("Function: %s\n",
- chunk_->info()->function()->debug_name()->ToCString().get());
+ PrintF(" (function: %s)\n",
+ info->function()->debug_name()->ToCString().get());
}
- PrintF("Value %d used before first definition!\n", operand_index);
- LiveRange* range = LiveRangeFor(operand_index);
- PrintF("First use is at %d\n", range->first_pos()->pos().Value());
iterator.Advance();
}
ASSERT(!found);
@@ -1376,39 +1322,54 @@ void LAllocator::BuildLiveRanges() {
for (int i = 0; i < live_ranges_.length(); ++i) {
if (live_ranges_[i] != NULL) {
live_ranges_[i]->kind_ = RequiredRegisterKind(live_ranges_[i]->id());
+
+ // TODO(bmeurer): This is a horrible hack to make sure that for constant
+ // live ranges, every use requires the constant to be in a register.
+ // Without this hack, all uses with "any" policy would get the constant
+ // operand assigned.
+ LiveRange* range = live_ranges_[i];
+ if (range->HasAllocatedSpillOperand() &&
+ range->GetSpillOperand()->IsConstant()) {
+ for (UsePosition* pos = range->first_pos(); pos != NULL;
+ pos = pos->next_) {
+ pos->register_beneficial_ = true;
+ pos->requires_reg_ = true;
+ }
+ }
}
}
}
-bool LAllocator::SafePointsAreInOrder() const {
- const ZoneList<LPointerMap*>* pointer_maps = chunk_->pointer_maps();
+bool RegisterAllocator::SafePointsAreInOrder() const {
int safe_point = 0;
- for (int i = 0; i < pointer_maps->length(); ++i) {
- LPointerMap* map = pointer_maps->at(i);
- if (safe_point > map->lithium_position()) return false;
- safe_point = map->lithium_position();
+ const PointerMapDeque* pointer_maps = code()->pointer_maps();
+ for (PointerMapDeque::const_iterator it = pointer_maps->begin();
+ it != pointer_maps->end(); ++it) {
+ PointerMap* map = *it;
+ if (safe_point > map->instruction_position()) return false;
+ safe_point = map->instruction_position();
}
return true;
}
-void LAllocator::PopulatePointerMaps() {
- LAllocatorPhase phase("L_Populate pointer maps", this);
- const ZoneList<LPointerMap*>* pointer_maps = chunk_->pointer_maps();
+void RegisterAllocator::PopulatePointerMaps() {
+ RegisterAllocatorPhase phase("L_Populate pointer maps", this);
ASSERT(SafePointsAreInOrder());
// Iterate over all safe point positions and record a pointer
// for all spilled live ranges at this point.
- int first_safe_point_index = 0;
int last_range_start = 0;
+ const PointerMapDeque* pointer_maps = code()->pointer_maps();
+ PointerMapDeque::const_iterator first_it = pointer_maps->begin();
for (int range_idx = 0; range_idx < live_ranges()->length(); ++range_idx) {
LiveRange* range = live_ranges()->at(range_idx);
if (range == NULL) continue;
// Iterate over the first parts of multi-part live ranges.
if (range->parent() != NULL) continue;
- // Skip non-pointer values.
+ // Skip non-reference values.
if (!HasTaggedValue(range->id())) continue;
// Skip empty live ranges.
if (range->IsEmpty()) continue;
@@ -1422,29 +1383,23 @@ void LAllocator::PopulatePointerMaps() {
ASSERT(cur->Start().InstructionIndex() >= start);
}
- // Most of the ranges are in order, but not all. Keep an eye on when
- // they step backwards and reset the first_safe_point_index so we don't
- // miss any safe points.
- if (start < last_range_start) {
- first_safe_point_index = 0;
- }
+ // Most of the ranges are in order, but not all. Keep an eye on when they
+ // step backwards and reset the first_it so we don't miss any safe points.
+ if (start < last_range_start) first_it = pointer_maps->begin();
last_range_start = start;
// Step across all the safe points that are before the start of this range,
// recording how far we step in order to save doing this for the next range.
- while (first_safe_point_index < pointer_maps->length()) {
- LPointerMap* map = pointer_maps->at(first_safe_point_index);
- int safe_point = map->lithium_position();
- if (safe_point >= start) break;
- first_safe_point_index++;
+ for (; first_it != pointer_maps->end(); ++first_it) {
+ PointerMap* map = *first_it;
+ if (map->instruction_position() >= start) break;
}
// Step through the safe points to see whether they are in the range.
- for (int safe_point_index = first_safe_point_index;
- safe_point_index < pointer_maps->length();
- ++safe_point_index) {
- LPointerMap* map = pointer_maps->at(safe_point_index);
- int safe_point = map->lithium_position();
+ for (PointerMapDeque::const_iterator it = first_it;
+ it != pointer_maps->end(); ++it) {
+ PointerMap* map = *it;
+ int safe_point = map->instruction_position();
// The safe points are sorted so we can stop searching here.
if (safe_point - 1 > end) break;
@@ -1462,42 +1417,44 @@ void LAllocator::PopulatePointerMaps() {
// Check if the live range is spilled and the safe point is after
// the spill position.
if (range->HasAllocatedSpillOperand() &&
- safe_point >= range->spill_start_index()) {
+ safe_point >= range->spill_start_index() &&
+ !range->GetSpillOperand()->IsConstant()) {
TraceAlloc("Pointer for range %d (spilled at %d) at safe point %d\n",
range->id(), range->spill_start_index(), safe_point);
- map->RecordPointer(range->GetSpillOperand(), chunk()->zone());
+ map->RecordPointer(range->GetSpillOperand(), code_zone());
}
if (!cur->IsSpilled()) {
- TraceAlloc("Pointer in register for range %d (start at %d) "
- "at safe point %d\n",
- cur->id(), cur->Start().Value(), safe_point);
- LOperand* operand = cur->CreateAssignedOperand(chunk()->zone());
+ TraceAlloc(
+ "Pointer in register for range %d (start at %d) "
+ "at safe point %d\n",
+ cur->id(), cur->Start().Value(), safe_point);
+ InstructionOperand* operand = cur->CreateAssignedOperand(code_zone());
ASSERT(!operand->IsStackSlot());
- map->RecordPointer(operand, chunk()->zone());
+ map->RecordPointer(operand, code_zone());
}
}
}
}
-void LAllocator::AllocateGeneralRegisters() {
- LAllocatorPhase phase("L_Allocate general registers", this);
+void RegisterAllocator::AllocateGeneralRegisters() {
+ RegisterAllocatorPhase phase("L_Allocate general registers", this);
num_registers_ = Register::NumAllocatableRegisters();
mode_ = GENERAL_REGISTERS;
AllocateRegisters();
}
-void LAllocator::AllocateDoubleRegisters() {
- LAllocatorPhase phase("L_Allocate double registers", this);
+void RegisterAllocator::AllocateDoubleRegisters() {
+ RegisterAllocatorPhase phase("L_Allocate double registers", this);
num_registers_ = DoubleRegister::NumAllocatableRegisters();
mode_ = DOUBLE_REGISTERS;
AllocateRegisters();
}
-void LAllocator::AllocateRegisters() {
+void RegisterAllocator::AllocateRegisters() {
ASSERT(unhandled_live_ranges_.is_empty());
for (int i = 0; i < live_ranges_.length(); ++i) {
@@ -1539,14 +1496,13 @@ void LAllocator::AllocateRegisters() {
#ifdef DEBUG
allocation_finger_ = position;
#endif
- TraceAlloc("Processing interval %d start=%d\n",
- current->id(),
+ TraceAlloc("Processing interval %d start=%d\n", current->id(),
position.Value());
if (current->HasAllocatedSpillOperand()) {
TraceAlloc("Live range %d already has a spill operand\n", current->id());
LifetimePosition next_pos = position;
- if (IsGapAt(next_pos.InstructionIndex())) {
+ if (code()->IsGapAt(next_pos.InstructionIndex())) {
next_pos = next_pos.NextInstruction();
}
UsePosition* pos = current->NextUsePositionRegisterIsBeneficial(next_pos);
@@ -1607,7 +1563,7 @@ void LAllocator::AllocateRegisters() {
}
-const char* LAllocator::RegisterName(int allocation_index) {
+const char* RegisterAllocator::RegisterName(int allocation_index) {
if (mode_ == GENERAL_REGISTERS) {
return Register::AllocationIndexToString(allocation_index);
} else {
@@ -1616,7 +1572,7 @@ const char* LAllocator::RegisterName(int allocation_index) {
}
-void LAllocator::TraceAlloc(const char* msg, ...) {
+void RegisterAllocator::TraceAlloc(const char* msg, ...) {
if (FLAG_trace_alloc) {
va_list arguments;
va_start(arguments, msg);
@@ -1626,41 +1582,31 @@ void LAllocator::TraceAlloc(const char* msg, ...) {
}
-bool LAllocator::HasTaggedValue(int virtual_register) const {
- HValue* value = graph_->LookupValue(virtual_register);
- if (value == NULL) return false;
- return value->representation().IsTagged() && !value->type().IsSmi();
+bool RegisterAllocator::HasTaggedValue(int virtual_register) const {
+ return code()->IsReference(virtual_register);
}
-RegisterKind LAllocator::RequiredRegisterKind(int virtual_register) const {
- if (virtual_register < first_artificial_register_) {
- HValue* value = graph_->LookupValue(virtual_register);
- if (value != NULL && value->representation().IsDouble()) {
- return DOUBLE_REGISTERS;
- }
- } else if (double_artificial_registers_.Contains(
- virtual_register - first_artificial_register_)) {
- return DOUBLE_REGISTERS;
- }
-
- return GENERAL_REGISTERS;
+RegisterKind RegisterAllocator::RequiredRegisterKind(
+ int virtual_register) const {
+ return (code()->IsDouble(virtual_register)) ? DOUBLE_REGISTERS
+ : GENERAL_REGISTERS;
}
-void LAllocator::AddToActive(LiveRange* range) {
+void RegisterAllocator::AddToActive(LiveRange* range) {
TraceAlloc("Add live range %d to active\n", range->id());
active_live_ranges_.Add(range, zone());
}
-void LAllocator::AddToInactive(LiveRange* range) {
+void RegisterAllocator::AddToInactive(LiveRange* range) {
TraceAlloc("Add live range %d to inactive\n", range->id());
inactive_live_ranges_.Add(range, zone());
}
-void LAllocator::AddToUnhandledSorted(LiveRange* range) {
+void RegisterAllocator::AddToUnhandledSorted(LiveRange* range) {
if (range == NULL || range->IsEmpty()) return;
ASSERT(!range->HasRegisterAssigned() && !range->IsSpilled());
ASSERT(allocation_finger_.Value() <= range->Start().Value());
@@ -1679,7 +1625,7 @@ void LAllocator::AddToUnhandledSorted(LiveRange* range) {
}
-void LAllocator::AddToUnhandledUnsorted(LiveRange* range) {
+void RegisterAllocator::AddToUnhandledUnsorted(LiveRange* range) {
if (range == NULL || range->IsEmpty()) return;
ASSERT(!range->HasRegisterAssigned() && !range->IsSpilled());
TraceAlloc("Add live range %d to unhandled unsorted at end\n", range->id());
@@ -1699,13 +1645,13 @@ static int UnhandledSortHelper(LiveRange* const* a, LiveRange* const* b) {
// Sort the unhandled live ranges so that the ranges to be processed first are
// at the end of the array list. This is convenient for the register allocation
// algorithm because it is efficient to remove elements from the end.
-void LAllocator::SortUnhandled() {
+void RegisterAllocator::SortUnhandled() {
TraceAlloc("Sort unhandled\n");
unhandled_live_ranges_.Sort(&UnhandledSortHelper);
}
-bool LAllocator::UnhandledIsSorted() {
+bool RegisterAllocator::UnhandledIsSorted() {
int len = unhandled_live_ranges_.length();
for (int i = 1; i < len; i++) {
LiveRange* a = unhandled_live_ranges_.at(i - 1);
@@ -1716,32 +1662,34 @@ bool LAllocator::UnhandledIsSorted() {
}
-void LAllocator::FreeSpillSlot(LiveRange* range) {
+void RegisterAllocator::FreeSpillSlot(LiveRange* range) {
// Check that we are the last range.
if (range->next() != NULL) return;
if (!range->TopLevel()->HasAllocatedSpillOperand()) return;
- int index = range->TopLevel()->GetSpillOperand()->index();
- if (index >= 0) {
+ InstructionOperand* spill_operand = range->TopLevel()->GetSpillOperand();
+ if (spill_operand->IsConstant()) return;
+ if (spill_operand->index() >= 0) {
reusable_slots_.Add(range, zone());
}
}
-LOperand* LAllocator::TryReuseSpillSlot(LiveRange* range) {
+InstructionOperand* RegisterAllocator::TryReuseSpillSlot(LiveRange* range) {
if (reusable_slots_.is_empty()) return NULL;
if (reusable_slots_.first()->End().Value() >
range->TopLevel()->Start().Value()) {
return NULL;
}
- LOperand* result = reusable_slots_.first()->TopLevel()->GetSpillOperand();
+ InstructionOperand* result =
+ reusable_slots_.first()->TopLevel()->GetSpillOperand();
reusable_slots_.Remove(0);
return result;
}
-void LAllocator::ActiveToHandled(LiveRange* range) {
+void RegisterAllocator::ActiveToHandled(LiveRange* range) {
ASSERT(active_live_ranges_.Contains(range));
active_live_ranges_.RemoveElement(range);
TraceAlloc("Moving live range %d from active to handled\n", range->id());
@@ -1749,7 +1697,7 @@ void LAllocator::ActiveToHandled(LiveRange* range) {
}
-void LAllocator::ActiveToInactive(LiveRange* range) {
+void RegisterAllocator::ActiveToInactive(LiveRange* range) {
ASSERT(active_live_ranges_.Contains(range));
active_live_ranges_.RemoveElement(range);
inactive_live_ranges_.Add(range, zone());
@@ -1757,7 +1705,7 @@ void LAllocator::ActiveToInactive(LiveRange* range) {
}
-void LAllocator::InactiveToHandled(LiveRange* range) {
+void RegisterAllocator::InactiveToHandled(LiveRange* range) {
ASSERT(inactive_live_ranges_.Contains(range));
inactive_live_ranges_.RemoveElement(range);
TraceAlloc("Moving live range %d from inactive to handled\n", range->id());
@@ -1765,7 +1713,7 @@ void LAllocator::InactiveToHandled(LiveRange* range) {
}
-void LAllocator::InactiveToActive(LiveRange* range) {
+void RegisterAllocator::InactiveToActive(LiveRange* range) {
ASSERT(inactive_live_ranges_.Contains(range));
inactive_live_ranges_.RemoveElement(range);
active_live_ranges_.Add(range, zone());
@@ -1779,7 +1727,7 @@ STATIC_ASSERT(DoubleRegister::kMaxNumAllocatableRegisters >=
Register::kMaxNumAllocatableRegisters);
-bool LAllocator::TryAllocateFreeReg(LiveRange* current) {
+bool RegisterAllocator::TryAllocateFreeReg(LiveRange* current) {
LifetimePosition free_until_pos[DoubleRegister::kMaxNumAllocatableRegisters];
for (int i = 0; i < num_registers_; i++) {
@@ -1802,21 +1750,18 @@ bool LAllocator::TryAllocateFreeReg(LiveRange* current) {
free_until_pos[cur_reg] = Min(free_until_pos[cur_reg], next_intersection);
}
- LOperand* hint = current->FirstHint();
+ InstructionOperand* hint = current->FirstHint();
if (hint != NULL && (hint->IsRegister() || hint->IsDoubleRegister())) {
int register_index = hint->index();
TraceAlloc(
"Found reg hint %s (free until [%d) for live range %d (end %d[).\n",
- RegisterName(register_index),
- free_until_pos[register_index].Value(),
- current->id(),
- current->End().Value());
+ RegisterName(register_index), free_until_pos[register_index].Value(),
+ current->id(), current->End().Value());
// The desired register is free until the end of the current live range.
if (free_until_pos[register_index].Value() >= current->End().Value()) {
TraceAlloc("Assigning preferred reg %s to live range %d\n",
- RegisterName(register_index),
- current->id());
+ RegisterName(register_index), current->id());
SetLiveRangeAssignedRegister(current, register_index);
return true;
}
@@ -1849,8 +1794,7 @@ bool LAllocator::TryAllocateFreeReg(LiveRange* current) {
// Register reg is available at the range start and is free until
// the range end.
ASSERT(pos.Value() >= current->End().Value());
- TraceAlloc("Assigning free reg %s to live range %d\n",
- RegisterName(reg),
+ TraceAlloc("Assigning free reg %s to live range %d\n", RegisterName(reg),
current->id());
SetLiveRangeAssignedRegister(current, reg);
@@ -1858,7 +1802,7 @@ bool LAllocator::TryAllocateFreeReg(LiveRange* current) {
}
-void LAllocator::AllocateBlockedReg(LiveRange* current) {
+void RegisterAllocator::AllocateBlockedReg(LiveRange* current) {
UsePosition* register_use = current->NextRegisterPosition(current->Start());
if (register_use == NULL) {
// There is no use in the current live range that requires a register.
@@ -1882,8 +1826,8 @@ void LAllocator::AllocateBlockedReg(LiveRange* current) {
block_pos[cur_reg] = use_pos[cur_reg] =
LifetimePosition::FromInstructionIndex(0);
} else {
- UsePosition* next_use = range->NextUsePositionRegisterIsBeneficial(
- current->Start());
+ UsePosition* next_use =
+ range->NextUsePositionRegisterIsBeneficial(current->Start());
if (next_use == NULL) {
use_pos[cur_reg] = range->End();
} else {
@@ -1925,8 +1869,7 @@ void LAllocator::AllocateBlockedReg(LiveRange* current) {
if (block_pos[reg].Value() < current->End().Value()) {
// Register becomes blocked before the current range end. Split before that
// position.
- LiveRange* tail = SplitBetween(current,
- current->Start(),
+ LiveRange* tail = SplitBetween(current, current->Start(),
block_pos[reg].InstructionStart());
if (!AllocationOk()) return;
AddToUnhandledSorted(tail);
@@ -1934,8 +1877,7 @@ void LAllocator::AllocateBlockedReg(LiveRange* current) {
// Register reg is not blocked for the whole range.
ASSERT(block_pos[reg].Value() >= current->End().Value());
- TraceAlloc("Assigning blocked reg %s to live range %d\n",
- RegisterName(reg),
+ TraceAlloc("Assigning blocked reg %s to live range %d\n", RegisterName(reg),
current->id());
SetLiveRangeAssignedRegister(current, reg);
@@ -1946,16 +1888,15 @@ void LAllocator::AllocateBlockedReg(LiveRange* current) {
}
-LifetimePosition LAllocator::FindOptimalSpillingPos(LiveRange* range,
- LifetimePosition pos) {
- HBasicBlock* block = GetBlock(pos.InstructionStart());
- HBasicBlock* loop_header =
- block->IsLoopHeader() ? block : block->parent_loop_header();
+LifetimePosition RegisterAllocator::FindOptimalSpillingPos(
+ LiveRange* range, LifetimePosition pos) {
+ BasicBlock* block = GetBlock(pos.InstructionStart());
+ BasicBlock* loop_header =
+ block->IsLoopHeader() ? block : code()->GetContainingLoop(block);
if (loop_header == NULL) return pos;
- UsePosition* prev_use =
- range->PreviousUsePositionRegisterIsBeneficial(pos);
+ UsePosition* prev_use = range->PreviousUsePositionRegisterIsBeneficial(pos);
while (loop_header != NULL) {
// We are going to spill live range inside the loop.
@@ -1972,14 +1913,14 @@ LifetimePosition LAllocator::FindOptimalSpillingPos(LiveRange* range,
}
// Try hoisting out to an outer loop.
- loop_header = loop_header->parent_loop_header();
+ loop_header = code()->GetContainingLoop(loop_header);
}
return pos;
}
-void LAllocator::SplitAndSpillIntersecting(LiveRange* current) {
+void RegisterAllocator::SplitAndSpillIntersecting(LiveRange* current) {
ASSERT(current->HasRegisterAssigned());
int reg = current->assigned_register();
LifetimePosition split_pos = current->Start();
@@ -2029,13 +1970,14 @@ void LAllocator::SplitAndSpillIntersecting(LiveRange* current) {
}
-bool LAllocator::IsBlockBoundary(LifetimePosition pos) {
+bool RegisterAllocator::IsBlockBoundary(LifetimePosition pos) {
return pos.IsInstructionStart() &&
- InstructionAt(pos.InstructionIndex())->IsLabel();
+ InstructionAt(pos.InstructionIndex())->IsBlockStart();
}
-LiveRange* LAllocator::SplitRangeAt(LiveRange* range, LifetimePosition pos) {
+LiveRange* RegisterAllocator::SplitRangeAt(LiveRange* range,
+ LifetimePosition pos) {
ASSERT(!range->IsFixed());
TraceAlloc("Splitting live range %d at %d\n", range->id(), pos.Value());
@@ -2044,7 +1986,7 @@ LiveRange* LAllocator::SplitRangeAt(LiveRange* range, LifetimePosition pos) {
// We can't properly connect liveranges if split occured at the end
// of control instruction.
ASSERT(pos.IsInstructionStart() ||
- !chunk_->instructions()->at(pos.InstructionIndex())->IsControl());
+ !InstructionAt(pos.InstructionIndex())->IsControl());
int vreg = GetVirtualRegister();
if (!AllocationOk()) return NULL;
@@ -2054,14 +1996,12 @@ LiveRange* LAllocator::SplitRangeAt(LiveRange* range, LifetimePosition pos) {
}
-LiveRange* LAllocator::SplitBetween(LiveRange* range,
- LifetimePosition start,
- LifetimePosition end) {
+LiveRange* RegisterAllocator::SplitBetween(LiveRange* range,
+ LifetimePosition start,
+ LifetimePosition end) {
ASSERT(!range->IsFixed());
TraceAlloc("Splitting live range %d in position between [%d, %d]\n",
- range->id(),
- start.Value(),
- end.Value());
+ range->id(), start.Value(), end.Value());
LifetimePosition split_pos = FindOptimalSplitPos(start, end);
ASSERT(split_pos.Value() >= start.Value());
@@ -2069,8 +2009,8 @@ LiveRange* LAllocator::SplitBetween(LiveRange* range,
}
-LifetimePosition LAllocator::FindOptimalSplitPos(LifetimePosition start,
- LifetimePosition end) {
+LifetimePosition RegisterAllocator::FindOptimalSplitPos(LifetimePosition start,
+ LifetimePosition end) {
int start_instr = start.InstructionIndex();
int end_instr = end.InstructionIndex();
ASSERT(start_instr <= end_instr);
@@ -2078,8 +2018,8 @@ LifetimePosition LAllocator::FindOptimalSplitPos(LifetimePosition start,
// We have no choice
if (start_instr == end_instr) return end;
- HBasicBlock* start_block = GetBlock(start);
- HBasicBlock* end_block = GetBlock(end);
+ BasicBlock* start_block = GetBlock(start);
+ BasicBlock* end_block = GetBlock(end);
if (end_block == start_block) {
// The interval is split in the same basic block. Split at the latest
@@ -2087,11 +2027,13 @@ LifetimePosition LAllocator::FindOptimalSplitPos(LifetimePosition start,
return end;
}
- HBasicBlock* block = end_block;
+ BasicBlock* block = end_block;
// Find header of outermost loop.
- while (block->parent_loop_header() != NULL &&
- block->parent_loop_header()->block_id() > start_block->block_id()) {
- block = block->parent_loop_header();
+ // TODO(titzer): fix redundancy below.
+ while (code()->GetContainingLoop(block) != NULL &&
+ code()->GetContainingLoop(block)->rpo_number_ >
+ start_block->rpo_number_) {
+ block = code()->GetContainingLoop(block);
}
// We did not find any suitable outer loop. Split at the latest possible
@@ -2103,24 +2045,23 @@ LifetimePosition LAllocator::FindOptimalSplitPos(LifetimePosition start,
}
-void LAllocator::SpillAfter(LiveRange* range, LifetimePosition pos) {
+void RegisterAllocator::SpillAfter(LiveRange* range, LifetimePosition pos) {
LiveRange* second_part = SplitRangeAt(range, pos);
if (!AllocationOk()) return;
Spill(second_part);
}
-void LAllocator::SpillBetween(LiveRange* range,
- LifetimePosition start,
- LifetimePosition end) {
+void RegisterAllocator::SpillBetween(LiveRange* range, LifetimePosition start,
+ LifetimePosition end) {
SpillBetweenUntil(range, start, start, end);
}
-void LAllocator::SpillBetweenUntil(LiveRange* range,
- LifetimePosition start,
- LifetimePosition until,
- LifetimePosition end) {
+void RegisterAllocator::SpillBetweenUntil(LiveRange* range,
+ LifetimePosition start,
+ LifetimePosition until,
+ LifetimePosition end) {
CHECK(start.Value() < end.Value());
LiveRange* second_part = SplitRangeAt(range, start);
if (!AllocationOk()) return;
@@ -2130,8 +2071,7 @@ void LAllocator::SpillBetweenUntil(LiveRange* range,
// Split it at position between ]start+1, end[, spill the middle part
// and put the rest to unhandled.
LiveRange* third_part = SplitBetween(
- second_part,
- Max(second_part->Start().InstructionEnd(), until),
+ second_part, Max(second_part->Start().InstructionEnd(), until),
end.PrevInstruction().InstructionEnd());
if (!AllocationOk()) return;
@@ -2147,29 +2087,37 @@ void LAllocator::SpillBetweenUntil(LiveRange* range,
}
-void LAllocator::Spill(LiveRange* range) {
+void RegisterAllocator::Spill(LiveRange* range) {
ASSERT(!range->IsSpilled());
TraceAlloc("Spilling live range %d\n", range->id());
LiveRange* first = range->TopLevel();
if (!first->HasAllocatedSpillOperand()) {
- LOperand* op = TryReuseSpillSlot(range);
- if (op == NULL) op = chunk_->GetNextSpillSlot(range->Kind());
+ InstructionOperand* op = TryReuseSpillSlot(range);
+ if (op == NULL) {
+ // Allocate a new operand referring to the spill slot.
+ RegisterKind kind = range->Kind();
+ int index = code()->frame()->AllocateSpillSlot(kind == DOUBLE_REGISTERS);
+ if (kind == DOUBLE_REGISTERS) {
+ op = DoubleStackSlotOperand::Create(index, zone());
+ } else {
+ ASSERT(kind == GENERAL_REGISTERS);
+ op = StackSlotOperand::Create(index, zone());
+ }
+ }
first->SetSpillOperand(op);
}
- range->MakeSpilled(chunk()->zone());
+ range->MakeSpilled(code_zone());
}
-int LAllocator::RegisterCount() const {
- return num_registers_;
-}
+int RegisterAllocator::RegisterCount() const { return num_registers_; }
#ifdef DEBUG
-void LAllocator::Verify() const {
+void RegisterAllocator::Verify() const {
for (int i = 0; i < live_ranges()->length(); ++i) {
LiveRange* current = live_ranges()->at(i);
if (current != NULL) current->Verify();
@@ -2180,32 +2128,39 @@ void LAllocator::Verify() const {
#endif
-LAllocatorPhase::LAllocatorPhase(const char* name, LAllocator* allocator)
- : CompilationPhase(name, allocator->graph()->info()),
+void RegisterAllocator::SetLiveRangeAssignedRegister(LiveRange* range,
+ int reg) {
+ if (range->Kind() == DOUBLE_REGISTERS) {
+ assigned_double_registers_->Add(reg);
+ } else {
+ ASSERT(range->Kind() == GENERAL_REGISTERS);
+ assigned_registers_->Add(reg);
+ }
+ range->set_assigned_register(reg, code_zone());
+}
+
+
+RegisterAllocatorPhase::RegisterAllocatorPhase(const char* name,
+ RegisterAllocator* allocator)
+ : CompilationPhase(name, allocator->code()->linkage()->info()),
allocator_(allocator) {
- if (FLAG_hydrogen_stats) {
+ if (FLAG_turbo_stats) {
allocator_zone_start_allocation_size_ =
allocator->zone()->allocation_size();
}
}
-LAllocatorPhase::~LAllocatorPhase() {
- if (FLAG_hydrogen_stats) {
+RegisterAllocatorPhase::~RegisterAllocatorPhase() {
+ if (FLAG_turbo_stats) {
unsigned size = allocator_->zone()->allocation_size() -
allocator_zone_start_allocation_size_;
- isolate()->GetHStatistics()->SaveTiming(name(), base::TimeDelta(), size);
+ isolate()->GetTStatistics()->SaveTiming(name(), base::TimeDelta(), size);
}
-
- if (ShouldProduceTraceOutput()) {
- isolate()->GetHTracer()->TraceLithium(name(), allocator_->chunk());
- isolate()->GetHTracer()->TraceLiveRanges(name(), allocator_);
- }
-
#ifdef DEBUG
if (allocator_ != NULL) allocator_->Verify();
#endif
}
-
-
-} } // namespace v8::internal
+}
+}
+} // namespace v8::internal::compiler
« no previous file with comments | « src/compiler/register-allocator.h ('k') | src/compiler/representation-change.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698