2014-11-03 13:26:46 +00:00
|
|
|
// 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/base/utils/random-number-generator.h"
|
|
|
|
#include "src/compiler/register-allocator.h"
|
2014-11-12 14:53:30 +00:00
|
|
|
#include "src/compiler/register-allocator-verifier.h"
|
2014-11-03 13:26:46 +00:00
|
|
|
#include "test/unittests/test-utils.h"
|
|
|
|
#include "testing/gmock/include/gmock/gmock.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace compiler {
|
|
|
|
|
|
|
|
typedef BasicBlock::RpoNumber Rpo;
|
|
|
|
|
2014-11-04 09:21:12 +00:00
|
|
|
static const char*
|
|
|
|
general_register_names_[RegisterConfiguration::kMaxGeneralRegisters];
|
|
|
|
static const char*
|
|
|
|
double_register_names_[RegisterConfiguration::kMaxDoubleRegisters];
|
|
|
|
static char register_names_[10 * (RegisterConfiguration::kMaxGeneralRegisters +
|
|
|
|
RegisterConfiguration::kMaxDoubleRegisters)];
|
2014-11-03 13:26:46 +00:00
|
|
|
|
|
|
|
static void InitializeRegisterNames() {
|
|
|
|
char* loc = register_names_;
|
2014-11-04 09:21:12 +00:00
|
|
|
for (int i = 0; i < RegisterConfiguration::kMaxGeneralRegisters; ++i) {
|
2014-11-03 13:26:46 +00:00
|
|
|
general_register_names_[i] = loc;
|
|
|
|
loc += base::OS::SNPrintF(loc, 100, "gp_%d", i);
|
|
|
|
*loc++ = 0;
|
|
|
|
}
|
2014-11-04 09:21:12 +00:00
|
|
|
for (int i = 0; i < RegisterConfiguration::kMaxDoubleRegisters; ++i) {
|
2014-11-03 13:26:46 +00:00
|
|
|
double_register_names_[i] = loc;
|
|
|
|
loc += base::OS::SNPrintF(loc, 100, "fp_%d", i) + 1;
|
|
|
|
*loc++ = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-06 08:28:15 +00:00
|
|
|
|
2014-11-13 09:41:58 +00:00
|
|
|
class RegisterAllocatorTest : public TestWithZone {
|
|
|
|
public:
|
|
|
|
static const int kDefaultNRegs = 4;
|
|
|
|
static const int kNoValue = kMinInt;
|
2014-11-06 08:28:15 +00:00
|
|
|
|
2014-11-13 09:41:58 +00:00
|
|
|
struct VReg {
|
|
|
|
VReg() : value_(kNoValue) {}
|
|
|
|
VReg(PhiInstruction* phi) : value_(phi->virtual_register()) {} // NOLINT
|
|
|
|
explicit VReg(int value) : value_(value) {}
|
|
|
|
int value_;
|
|
|
|
};
|
2014-11-06 08:28:15 +00:00
|
|
|
|
2014-11-13 09:41:58 +00:00
|
|
|
enum TestOperandType {
|
|
|
|
kInvalid,
|
|
|
|
kSameAsFirst,
|
|
|
|
kRegister,
|
|
|
|
kFixedRegister,
|
|
|
|
kSlot,
|
|
|
|
kFixedSlot,
|
|
|
|
kImmediate,
|
|
|
|
kNone
|
|
|
|
};
|
2014-11-06 08:28:15 +00:00
|
|
|
|
2014-11-13 09:41:58 +00:00
|
|
|
struct TestOperand {
|
|
|
|
TestOperand() : type_(kInvalid), vreg_(), value_(kNoValue) {}
|
|
|
|
TestOperand(TestOperandType type, int imm)
|
|
|
|
: type_(type), vreg_(), value_(imm) {}
|
|
|
|
TestOperand(TestOperandType type, VReg vreg, int value = kNoValue)
|
|
|
|
: type_(type), vreg_(vreg), value_(value) {}
|
2014-11-06 08:28:15 +00:00
|
|
|
|
2014-11-13 09:41:58 +00:00
|
|
|
TestOperandType type_;
|
|
|
|
VReg vreg_;
|
|
|
|
int value_;
|
|
|
|
};
|
2014-11-06 08:28:15 +00:00
|
|
|
|
2014-11-13 09:41:58 +00:00
|
|
|
static TestOperand Same() { return TestOperand(kSameAsFirst, VReg()); }
|
2014-11-06 08:28:15 +00:00
|
|
|
|
2014-11-13 09:41:58 +00:00
|
|
|
static TestOperand Reg(VReg vreg, int index = kNoValue) {
|
|
|
|
TestOperandType type = kRegister;
|
|
|
|
if (index != kNoValue) type = kFixedRegister;
|
|
|
|
return TestOperand(type, vreg, index);
|
|
|
|
}
|
2014-11-06 08:28:15 +00:00
|
|
|
|
2014-11-13 09:41:58 +00:00
|
|
|
static TestOperand Reg(int index = kNoValue) { return Reg(VReg(), index); }
|
2014-11-03 13:26:46 +00:00
|
|
|
|
2014-11-13 09:41:58 +00:00
|
|
|
static TestOperand Slot(VReg vreg, int index = kNoValue) {
|
|
|
|
TestOperandType type = kSlot;
|
|
|
|
if (index != kNoValue) type = kFixedSlot;
|
|
|
|
return TestOperand(type, vreg, index);
|
|
|
|
}
|
2014-11-03 13:26:46 +00:00
|
|
|
|
2014-11-13 09:41:58 +00:00
|
|
|
static TestOperand Slot(int index = kNoValue) { return Slot(VReg(), index); }
|
|
|
|
|
|
|
|
static TestOperand Use(VReg vreg) { return TestOperand(kNone, vreg); }
|
|
|
|
|
|
|
|
static TestOperand Use() { return Use(VReg()); }
|
|
|
|
|
|
|
|
enum BlockCompletionType { kBlockEnd, kFallThrough, kBranch, kJump };
|
|
|
|
|
|
|
|
struct BlockCompletion {
|
|
|
|
BlockCompletionType type_;
|
|
|
|
TestOperand op_;
|
|
|
|
int offset_0_;
|
|
|
|
int offset_1_;
|
|
|
|
};
|
|
|
|
|
|
|
|
static BlockCompletion FallThrough() {
|
|
|
|
BlockCompletion completion = {kFallThrough, TestOperand(), 1, kNoValue};
|
|
|
|
return completion;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BlockCompletion Jump(int offset) {
|
|
|
|
BlockCompletion completion = {kJump, TestOperand(), offset, kNoValue};
|
|
|
|
return completion;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BlockCompletion Branch(TestOperand op, int left_offset,
|
|
|
|
int right_offset) {
|
|
|
|
BlockCompletion completion = {kBranch, op, left_offset, right_offset};
|
|
|
|
return completion;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BlockCompletion Last() {
|
|
|
|
BlockCompletion completion = {kBlockEnd, TestOperand(), kNoValue, kNoValue};
|
|
|
|
return completion;
|
|
|
|
}
|
2014-11-03 13:26:46 +00:00
|
|
|
|
|
|
|
RegisterAllocatorTest()
|
2014-11-14 16:44:38 +00:00
|
|
|
: frame_(nullptr),
|
|
|
|
sequence_(nullptr),
|
|
|
|
num_general_registers_(kDefaultNRegs),
|
2014-11-04 09:21:12 +00:00
|
|
|
num_double_registers_(kDefaultNRegs),
|
2014-11-03 13:26:46 +00:00
|
|
|
instruction_blocks_(zone()),
|
2014-11-13 09:41:58 +00:00
|
|
|
current_instruction_index_(-1),
|
2014-11-06 08:28:15 +00:00
|
|
|
current_block_(nullptr),
|
2014-11-12 14:53:30 +00:00
|
|
|
block_returns_(false) {
|
2014-11-03 13:26:46 +00:00
|
|
|
InitializeRegisterNames();
|
2014-11-04 09:21:12 +00:00
|
|
|
}
|
|
|
|
|
2014-11-06 08:28:15 +00:00
|
|
|
void SetNumRegs(int num_general_registers, int num_double_registers) {
|
2014-11-13 09:41:58 +00:00
|
|
|
CHECK(config_.is_empty());
|
|
|
|
CHECK(instructions_.empty());
|
2014-11-06 08:28:15 +00:00
|
|
|
CHECK(instruction_blocks_.empty());
|
|
|
|
num_general_registers_ = num_general_registers;
|
|
|
|
num_double_registers_ = num_double_registers;
|
|
|
|
}
|
|
|
|
|
2014-11-04 09:21:12 +00:00
|
|
|
RegisterConfiguration* config() {
|
|
|
|
if (config_.is_empty()) {
|
|
|
|
config_.Reset(new RegisterConfiguration(
|
|
|
|
num_general_registers_, num_double_registers_, num_double_registers_,
|
|
|
|
general_register_names_, double_register_names_));
|
|
|
|
}
|
|
|
|
return config_.get();
|
2014-11-03 13:26:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Frame* frame() {
|
2014-11-14 16:44:38 +00:00
|
|
|
if (frame_ == nullptr) {
|
|
|
|
frame_ = new (zone()) Frame();
|
2014-11-03 13:26:46 +00:00
|
|
|
}
|
2014-11-14 16:44:38 +00:00
|
|
|
return frame_;
|
2014-11-03 13:26:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
InstructionSequence* sequence() {
|
2014-11-14 16:44:38 +00:00
|
|
|
if (sequence_ == nullptr) {
|
|
|
|
sequence_ =
|
|
|
|
new (zone()) InstructionSequence(zone(), &instruction_blocks_);
|
2014-11-03 13:26:46 +00:00
|
|
|
}
|
2014-11-14 16:44:38 +00:00
|
|
|
return sequence_;
|
2014-11-03 13:26:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
RegisterAllocator* allocator() {
|
|
|
|
if (allocator_.is_empty()) {
|
|
|
|
allocator_.Reset(
|
2014-11-04 09:21:12 +00:00
|
|
|
new RegisterAllocator(config(), zone(), frame(), sequence()));
|
2014-11-03 13:26:46 +00:00
|
|
|
}
|
|
|
|
return allocator_.get();
|
|
|
|
}
|
|
|
|
|
2014-11-06 08:28:15 +00:00
|
|
|
void StartLoop(int loop_blocks) {
|
|
|
|
CHECK(current_block_ == nullptr);
|
|
|
|
if (!loop_blocks_.empty()) {
|
|
|
|
CHECK(!loop_blocks_.back().loop_header_.IsValid());
|
2014-11-03 13:26:46 +00:00
|
|
|
}
|
2014-11-06 08:28:15 +00:00
|
|
|
LoopData loop_data = {Rpo::Invalid(), loop_blocks};
|
|
|
|
loop_blocks_.push_back(loop_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EndLoop() {
|
|
|
|
CHECK(current_block_ == nullptr);
|
|
|
|
CHECK(!loop_blocks_.empty());
|
|
|
|
CHECK_EQ(0, loop_blocks_.back().expected_blocks_);
|
|
|
|
loop_blocks_.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
void StartBlock() {
|
2014-11-12 14:53:30 +00:00
|
|
|
block_returns_ = false;
|
2014-11-06 08:28:15 +00:00
|
|
|
NewBlock();
|
2014-11-03 13:26:46 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 09:41:58 +00:00
|
|
|
int EndBlock(BlockCompletion completion = FallThrough()) {
|
|
|
|
int instruction_index = kMinInt;
|
|
|
|
if (block_returns_) {
|
|
|
|
CHECK(completion.type_ == kBlockEnd || completion.type_ == kFallThrough);
|
|
|
|
completion.type_ = kBlockEnd;
|
|
|
|
}
|
2014-11-06 08:28:15 +00:00
|
|
|
switch (completion.type_) {
|
2014-11-12 14:53:30 +00:00
|
|
|
case kBlockEnd:
|
|
|
|
break;
|
2014-11-06 08:28:15 +00:00
|
|
|
case kFallThrough:
|
2014-11-13 09:41:58 +00:00
|
|
|
instruction_index = EmitFallThrough();
|
2014-11-06 08:28:15 +00:00
|
|
|
break;
|
|
|
|
case kJump:
|
2014-11-12 14:53:30 +00:00
|
|
|
CHECK(!block_returns_);
|
2014-11-13 09:41:58 +00:00
|
|
|
instruction_index = EmitJump();
|
2014-11-06 08:28:15 +00:00
|
|
|
break;
|
|
|
|
case kBranch:
|
2014-11-12 14:53:30 +00:00
|
|
|
CHECK(!block_returns_);
|
2014-11-13 09:41:58 +00:00
|
|
|
instruction_index = EmitBranch(completion.op_);
|
2014-11-06 08:28:15 +00:00
|
|
|
break;
|
|
|
|
}
|
2014-11-13 09:41:58 +00:00
|
|
|
completions_.push_back(completion);
|
2014-11-06 08:28:15 +00:00
|
|
|
CHECK(current_block_ != nullptr);
|
|
|
|
sequence()->EndBlock(current_block_->rpo_number());
|
|
|
|
current_block_ = nullptr;
|
2014-11-13 09:41:58 +00:00
|
|
|
return instruction_index;
|
2014-11-03 13:26:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Allocate() {
|
2014-11-06 08:28:15 +00:00
|
|
|
CHECK_EQ(nullptr, current_block_);
|
|
|
|
WireBlocks();
|
2014-11-12 14:53:30 +00:00
|
|
|
RegisterAllocatorVerifier verifier(zone(), config(), sequence());
|
2014-11-06 08:28:15 +00:00
|
|
|
if (FLAG_trace_alloc || FLAG_trace_turbo) {
|
2014-11-03 13:26:46 +00:00
|
|
|
OFStream os(stdout);
|
2014-11-04 09:21:12 +00:00
|
|
|
PrintableInstructionSequence printable = {config(), sequence()};
|
|
|
|
os << "Before: " << std::endl << printable << std::endl;
|
2014-11-03 13:26:46 +00:00
|
|
|
}
|
|
|
|
allocator()->Allocate();
|
2014-11-06 08:28:15 +00:00
|
|
|
if (FLAG_trace_alloc || FLAG_trace_turbo) {
|
2014-11-03 13:26:46 +00:00
|
|
|
OFStream os(stdout);
|
2014-11-04 09:21:12 +00:00
|
|
|
PrintableInstructionSequence printable = {config(), sequence()};
|
|
|
|
os << "After: " << std::endl << printable << std::endl;
|
2014-11-03 13:26:46 +00:00
|
|
|
}
|
2014-11-12 14:53:30 +00:00
|
|
|
verifier.VerifyAssignment();
|
|
|
|
verifier.VerifyGapMoves();
|
2014-11-03 13:26:46 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 09:41:58 +00:00
|
|
|
TestOperand Imm(int32_t imm = 0) {
|
|
|
|
int index = sequence()->AddImmediate(Constant(imm));
|
|
|
|
return TestOperand(kImmediate, index);
|
|
|
|
}
|
2014-11-03 13:26:46 +00:00
|
|
|
|
2014-11-13 09:41:58 +00:00
|
|
|
VReg Parameter(TestOperand output_op = Reg()) {
|
|
|
|
VReg vreg = NewReg();
|
|
|
|
InstructionOperand* outputs[1]{ConvertOutputOp(vreg, output_op)};
|
|
|
|
Emit(vreg.value_, kArchNop, 1, outputs);
|
2014-11-03 13:26:46 +00:00
|
|
|
return vreg;
|
|
|
|
}
|
|
|
|
|
2014-11-13 09:41:58 +00:00
|
|
|
int Return(TestOperand input_op_0) {
|
2014-11-12 14:53:30 +00:00
|
|
|
block_returns_ = true;
|
2014-11-13 09:41:58 +00:00
|
|
|
InstructionOperand* inputs[1]{ConvertInputOp(input_op_0)};
|
|
|
|
return Emit(NewIndex(), kArchRet, 0, nullptr, 1, inputs);
|
2014-11-06 08:28:15 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 09:41:58 +00:00
|
|
|
int Return(VReg vreg) { return Return(Reg(vreg, 0)); }
|
|
|
|
|
|
|
|
PhiInstruction* Phi(VReg incoming_vreg) {
|
|
|
|
PhiInstruction* phi = new (zone()) PhiInstruction(zone(), NewReg().value_);
|
|
|
|
phi->operands().push_back(incoming_vreg.value_);
|
2014-11-06 08:28:15 +00:00
|
|
|
current_block_->AddPhi(phi);
|
|
|
|
return phi;
|
2014-11-03 13:26:46 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 09:41:58 +00:00
|
|
|
PhiInstruction* Phi(VReg incoming_vreg_0, VReg incoming_vreg_1) {
|
|
|
|
auto phi = Phi(incoming_vreg_0);
|
|
|
|
Extend(phi, incoming_vreg_1);
|
|
|
|
return phi;
|
2014-11-06 08:28:15 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 09:41:58 +00:00
|
|
|
static void Extend(PhiInstruction* phi, VReg vreg) {
|
|
|
|
phi->operands().push_back(vreg.value_);
|
2014-11-06 08:28:15 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 09:41:58 +00:00
|
|
|
VReg DefineConstant(int32_t imm = 0) {
|
|
|
|
VReg vreg = NewReg();
|
|
|
|
sequence()->AddConstant(vreg.value_, Constant(imm));
|
|
|
|
InstructionOperand* outputs[1]{
|
|
|
|
ConstantOperand::Create(vreg.value_, zone())};
|
|
|
|
Emit(vreg.value_, kArchNop, 1, outputs);
|
|
|
|
return vreg;
|
2014-11-03 13:26:46 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 09:41:58 +00:00
|
|
|
VReg EmitOII(TestOperand output_op, TestOperand input_op_0,
|
|
|
|
TestOperand input_op_1) {
|
|
|
|
VReg output_vreg = NewReg();
|
|
|
|
InstructionOperand* outputs[1]{ConvertOutputOp(output_vreg, output_op)};
|
|
|
|
InstructionOperand* inputs[2]{ConvertInputOp(input_op_0),
|
|
|
|
ConvertInputOp(input_op_1)};
|
|
|
|
Emit(output_vreg.value_, kArchNop, 1, outputs, 2, inputs);
|
|
|
|
return output_vreg;
|
2014-11-03 13:26:46 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 09:41:58 +00:00
|
|
|
VReg EmitCall(TestOperand output_op, size_t input_size, TestOperand* inputs) {
|
|
|
|
VReg output_vreg = NewReg();
|
|
|
|
InstructionOperand* outputs[1]{ConvertOutputOp(output_vreg, output_op)};
|
|
|
|
InstructionOperand** mapped_inputs =
|
|
|
|
zone()->NewArray<InstructionOperand*>(static_cast<int>(input_size));
|
|
|
|
for (size_t i = 0; i < input_size; ++i) {
|
|
|
|
mapped_inputs[i] = ConvertInputOp(inputs[i]);
|
|
|
|
}
|
|
|
|
Emit(output_vreg.value_, kArchCallCodeObject, 1, outputs, input_size,
|
|
|
|
mapped_inputs);
|
|
|
|
return output_vreg;
|
2014-11-06 08:28:15 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 09:41:58 +00:00
|
|
|
// Get defining instruction vreg or value returned at instruction creation
|
|
|
|
// time when there is no return value.
|
|
|
|
const Instruction* GetInstruction(int instruction_index) {
|
|
|
|
auto it = instructions_.find(instruction_index);
|
|
|
|
CHECK(it != instructions_.end());
|
|
|
|
return it->second;
|
2014-11-06 08:28:15 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 09:41:58 +00:00
|
|
|
private:
|
|
|
|
VReg NewReg() { return VReg(sequence()->NextVirtualRegister()); }
|
|
|
|
int NewIndex() { return current_instruction_index_--; }
|
2014-11-06 08:28:15 +00:00
|
|
|
|
2014-11-13 09:41:58 +00:00
|
|
|
static TestOperand Invalid() { return TestOperand(kInvalid, VReg()); }
|
2014-11-06 08:28:15 +00:00
|
|
|
|
2014-11-13 09:41:58 +00:00
|
|
|
int EmitBranch(TestOperand input_op) {
|
|
|
|
InstructionOperand* inputs[4]{ConvertInputOp(input_op),
|
|
|
|
ConvertInputOp(Imm()), ConvertInputOp(Imm()),
|
|
|
|
ConvertInputOp(Imm())};
|
2014-11-06 08:28:15 +00:00
|
|
|
InstructionCode opcode = kArchJmp | FlagsModeField::encode(kFlags_branch) |
|
|
|
|
FlagsConditionField::encode(kEqual);
|
2014-11-13 09:41:58 +00:00
|
|
|
auto instruction =
|
2014-11-06 08:28:15 +00:00
|
|
|
NewInstruction(opcode, 0, nullptr, 4, inputs)->MarkAsControl();
|
2014-11-13 09:41:58 +00:00
|
|
|
return AddInstruction(NewIndex(), instruction);
|
2014-11-06 08:28:15 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 09:41:58 +00:00
|
|
|
int EmitFallThrough() {
|
|
|
|
auto instruction = NewInstruction(kArchNop, 0, nullptr)->MarkAsControl();
|
|
|
|
return AddInstruction(NewIndex(), instruction);
|
2014-11-06 08:28:15 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 09:41:58 +00:00
|
|
|
int EmitJump() {
|
|
|
|
InstructionOperand* inputs[1]{ConvertInputOp(Imm())};
|
|
|
|
auto instruction =
|
2014-11-06 08:28:15 +00:00
|
|
|
NewInstruction(kArchJmp, 0, nullptr, 1, inputs)->MarkAsControl();
|
2014-11-13 09:41:58 +00:00
|
|
|
return AddInstruction(NewIndex(), instruction);
|
2014-11-06 08:28:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Instruction* NewInstruction(InstructionCode code, size_t outputs_size,
|
|
|
|
InstructionOperand** outputs,
|
|
|
|
size_t inputs_size = 0,
|
|
|
|
InstructionOperand* *inputs = nullptr,
|
|
|
|
size_t temps_size = 0,
|
|
|
|
InstructionOperand* *temps = nullptr) {
|
|
|
|
CHECK_NE(nullptr, current_block_);
|
|
|
|
return Instruction::New(zone(), code, outputs_size, outputs, inputs_size,
|
|
|
|
inputs, temps_size, temps);
|
|
|
|
}
|
|
|
|
|
2014-11-13 09:41:58 +00:00
|
|
|
InstructionOperand* Unallocated(TestOperand op,
|
|
|
|
UnallocatedOperand::ExtendedPolicy policy) {
|
|
|
|
auto unallocated = new (zone()) UnallocatedOperand(policy);
|
|
|
|
unallocated->set_virtual_register(op.vreg_.value_);
|
|
|
|
return unallocated;
|
|
|
|
}
|
|
|
|
|
|
|
|
InstructionOperand* Unallocated(TestOperand op,
|
|
|
|
UnallocatedOperand::ExtendedPolicy policy,
|
|
|
|
UnallocatedOperand::Lifetime lifetime) {
|
|
|
|
auto unallocated = new (zone()) UnallocatedOperand(policy, lifetime);
|
|
|
|
unallocated->set_virtual_register(op.vreg_.value_);
|
|
|
|
return unallocated;
|
|
|
|
}
|
|
|
|
|
|
|
|
InstructionOperand* Unallocated(TestOperand op,
|
|
|
|
UnallocatedOperand::ExtendedPolicy policy,
|
|
|
|
int index) {
|
|
|
|
auto unallocated = new (zone()) UnallocatedOperand(policy, index);
|
|
|
|
unallocated->set_virtual_register(op.vreg_.value_);
|
|
|
|
return unallocated;
|
|
|
|
}
|
|
|
|
|
|
|
|
InstructionOperand* Unallocated(TestOperand op,
|
|
|
|
UnallocatedOperand::BasicPolicy policy,
|
|
|
|
int index) {
|
|
|
|
auto unallocated = new (zone()) UnallocatedOperand(policy, index);
|
|
|
|
unallocated->set_virtual_register(op.vreg_.value_);
|
|
|
|
return unallocated;
|
|
|
|
}
|
|
|
|
|
|
|
|
InstructionOperand* ConvertInputOp(TestOperand op) {
|
|
|
|
if (op.type_ == kImmediate) {
|
|
|
|
CHECK_EQ(op.vreg_.value_, kNoValue);
|
|
|
|
return ImmediateOperand::Create(op.value_, zone());
|
|
|
|
}
|
|
|
|
CHECK_NE(op.vreg_.value_, kNoValue);
|
|
|
|
switch (op.type_) {
|
|
|
|
case kNone:
|
|
|
|
return Unallocated(op, UnallocatedOperand::NONE,
|
|
|
|
UnallocatedOperand::USED_AT_START);
|
|
|
|
case kRegister:
|
|
|
|
return Unallocated(op, UnallocatedOperand::MUST_HAVE_REGISTER,
|
|
|
|
UnallocatedOperand::USED_AT_START);
|
|
|
|
case kFixedRegister:
|
|
|
|
CHECK(0 <= op.value_ && op.value_ < num_general_registers_);
|
|
|
|
return Unallocated(op, UnallocatedOperand::FIXED_REGISTER, op.value_);
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
CHECK(false);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
InstructionOperand* ConvertOutputOp(VReg vreg, TestOperand op) {
|
|
|
|
CHECK_EQ(op.vreg_.value_, kNoValue);
|
|
|
|
op.vreg_ = vreg;
|
|
|
|
switch (op.type_) {
|
|
|
|
case kSameAsFirst:
|
|
|
|
return Unallocated(op, UnallocatedOperand::SAME_AS_FIRST_INPUT);
|
|
|
|
case kRegister:
|
|
|
|
return Unallocated(op, UnallocatedOperand::MUST_HAVE_REGISTER);
|
|
|
|
case kFixedSlot:
|
|
|
|
return Unallocated(op, UnallocatedOperand::FIXED_SLOT, op.value_);
|
|
|
|
case kFixedRegister:
|
|
|
|
CHECK(0 <= op.value_ && op.value_ < num_general_registers_);
|
|
|
|
return Unallocated(op, UnallocatedOperand::FIXED_REGISTER, op.value_);
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
CHECK(false);
|
|
|
|
return NULL;
|
2014-11-06 08:28:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
InstructionBlock* NewBlock() {
|
|
|
|
CHECK(current_block_ == nullptr);
|
2014-11-13 09:41:58 +00:00
|
|
|
auto block_id = BasicBlock::Id::FromSize(instruction_blocks_.size());
|
2014-11-06 08:28:15 +00:00
|
|
|
Rpo rpo = Rpo::FromInt(block_id.ToInt());
|
|
|
|
Rpo loop_header = Rpo::Invalid();
|
|
|
|
Rpo loop_end = Rpo::Invalid();
|
|
|
|
if (!loop_blocks_.empty()) {
|
|
|
|
auto& loop_data = loop_blocks_.back();
|
|
|
|
// This is a loop header.
|
|
|
|
if (!loop_data.loop_header_.IsValid()) {
|
|
|
|
loop_end = Rpo::FromInt(block_id.ToInt() + loop_data.expected_blocks_);
|
|
|
|
loop_data.expected_blocks_--;
|
|
|
|
loop_data.loop_header_ = rpo;
|
|
|
|
} else {
|
|
|
|
// This is a loop body.
|
|
|
|
CHECK_NE(0, loop_data.expected_blocks_);
|
|
|
|
// TODO(dcarney): handle nested loops.
|
|
|
|
loop_data.expected_blocks_--;
|
|
|
|
loop_header = loop_data.loop_header_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Construct instruction block.
|
2014-11-13 09:41:58 +00:00
|
|
|
auto instruction_block = new (zone()) InstructionBlock(
|
2014-11-06 08:28:15 +00:00
|
|
|
zone(), block_id, rpo, rpo, loop_header, loop_end, false);
|
|
|
|
instruction_blocks_.push_back(instruction_block);
|
|
|
|
current_block_ = instruction_block;
|
|
|
|
sequence()->StartBlock(rpo);
|
|
|
|
return instruction_block;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WireBlocks() {
|
|
|
|
CHECK(instruction_blocks_.size() == completions_.size());
|
|
|
|
size_t offset = 0;
|
|
|
|
for (const auto& completion : completions_) {
|
|
|
|
switch (completion.type_) {
|
2014-11-12 14:53:30 +00:00
|
|
|
case kBlockEnd:
|
|
|
|
break;
|
|
|
|
case kFallThrough: // Fallthrough.
|
2014-11-06 08:28:15 +00:00
|
|
|
case kJump:
|
|
|
|
WireBlock(offset, completion.offset_0_);
|
|
|
|
break;
|
|
|
|
case kBranch:
|
|
|
|
WireBlock(offset, completion.offset_0_);
|
|
|
|
WireBlock(offset, completion.offset_1_);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++offset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WireBlock(size_t block_offset, int jump_offset) {
|
|
|
|
size_t target_block_offset =
|
|
|
|
block_offset + static_cast<size_t>(jump_offset);
|
|
|
|
CHECK(block_offset < instruction_blocks_.size());
|
|
|
|
CHECK(target_block_offset < instruction_blocks_.size());
|
2014-11-13 09:41:58 +00:00
|
|
|
auto block = instruction_blocks_[block_offset];
|
|
|
|
auto target = instruction_blocks_[target_block_offset];
|
2014-11-06 08:28:15 +00:00
|
|
|
block->successors().push_back(target->rpo_number());
|
|
|
|
target->predecessors().push_back(block->rpo_number());
|
|
|
|
}
|
|
|
|
|
2014-11-13 09:41:58 +00:00
|
|
|
int Emit(int instruction_index, InstructionCode code, size_t outputs_size,
|
|
|
|
InstructionOperand** outputs, size_t inputs_size = 0,
|
|
|
|
InstructionOperand* *inputs = nullptr, size_t temps_size = 0,
|
|
|
|
InstructionOperand* *temps = nullptr) {
|
|
|
|
auto instruction = NewInstruction(code, outputs_size, outputs, inputs_size,
|
|
|
|
inputs, temps_size, temps);
|
|
|
|
return AddInstruction(instruction_index, instruction);
|
|
|
|
}
|
|
|
|
|
|
|
|
int AddInstruction(int instruction_index, Instruction* instruction) {
|
|
|
|
sequence()->AddInstruction(instruction);
|
|
|
|
return instruction_index;
|
|
|
|
}
|
|
|
|
|
2014-11-06 08:28:15 +00:00
|
|
|
struct LoopData {
|
|
|
|
Rpo loop_header_;
|
|
|
|
int expected_blocks_;
|
|
|
|
};
|
2014-11-13 09:41:58 +00:00
|
|
|
|
2014-11-06 08:28:15 +00:00
|
|
|
typedef std::vector<LoopData> LoopBlocks;
|
2014-11-13 09:41:58 +00:00
|
|
|
typedef std::map<int, const Instruction*> Instructions;
|
2014-11-06 08:28:15 +00:00
|
|
|
typedef std::vector<BlockCompletion> Completions;
|
|
|
|
|
2014-11-04 09:21:12 +00:00
|
|
|
SmartPointer<RegisterConfiguration> config_;
|
2014-11-14 16:44:38 +00:00
|
|
|
Frame* frame_;
|
2014-11-03 13:26:46 +00:00
|
|
|
SmartPointer<RegisterAllocator> allocator_;
|
2014-11-14 16:44:38 +00:00
|
|
|
InstructionSequence* sequence_;
|
2014-11-06 08:28:15 +00:00
|
|
|
int num_general_registers_;
|
|
|
|
int num_double_registers_;
|
|
|
|
|
|
|
|
// Block building state.
|
|
|
|
InstructionBlocks instruction_blocks_;
|
2014-11-13 09:41:58 +00:00
|
|
|
Instructions instructions_;
|
|
|
|
int current_instruction_index_;
|
2014-11-06 08:28:15 +00:00
|
|
|
Completions completions_;
|
|
|
|
LoopBlocks loop_blocks_;
|
|
|
|
InstructionBlock* current_block_;
|
2014-11-12 14:53:30 +00:00
|
|
|
bool block_returns_;
|
2014-11-03 13:26:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(RegisterAllocatorTest, CanAllocateThreeRegisters) {
|
2014-11-13 09:41:58 +00:00
|
|
|
// return p0 + p1;
|
|
|
|
StartBlock();
|
|
|
|
auto a_reg = Parameter();
|
|
|
|
auto b_reg = Parameter();
|
|
|
|
auto c_reg = EmitOII(Reg(1), Reg(a_reg, 1), Reg(b_reg, 0));
|
2014-11-03 13:26:46 +00:00
|
|
|
Return(c_reg);
|
2014-11-13 09:41:58 +00:00
|
|
|
EndBlock(Last());
|
2014-11-03 13:26:46 +00:00
|
|
|
|
|
|
|
Allocate();
|
|
|
|
}
|
|
|
|
|
2014-11-06 08:28:15 +00:00
|
|
|
|
|
|
|
TEST_F(RegisterAllocatorTest, SimpleLoop) {
|
|
|
|
// i = K;
|
|
|
|
// while(true) { i++ }
|
|
|
|
StartBlock();
|
2014-11-13 09:41:58 +00:00
|
|
|
auto i_reg = DefineConstant();
|
2014-11-06 08:28:15 +00:00
|
|
|
EndBlock();
|
|
|
|
|
|
|
|
{
|
|
|
|
StartLoop(1);
|
|
|
|
|
2014-11-13 09:41:58 +00:00
|
|
|
StartBlock();
|
|
|
|
auto phi = Phi(i_reg);
|
|
|
|
auto ipp = EmitOII(Same(), Reg(phi), Use(DefineConstant()));
|
|
|
|
Extend(phi, ipp);
|
2014-11-06 08:28:15 +00:00
|
|
|
EndBlock(Jump(0));
|
|
|
|
|
|
|
|
EndLoop();
|
|
|
|
}
|
|
|
|
|
|
|
|
Allocate();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(RegisterAllocatorTest, SimpleBranch) {
|
|
|
|
// return i ? K1 : K2
|
|
|
|
StartBlock();
|
2014-11-13 09:41:58 +00:00
|
|
|
auto i = DefineConstant();
|
|
|
|
EndBlock(Branch(Reg(i), 1, 2));
|
2014-11-06 08:28:15 +00:00
|
|
|
|
|
|
|
StartBlock();
|
|
|
|
Return(DefineConstant());
|
2014-11-13 09:41:58 +00:00
|
|
|
EndBlock(Last());
|
2014-11-06 08:28:15 +00:00
|
|
|
|
2014-11-13 09:41:58 +00:00
|
|
|
StartBlock();
|
2014-11-06 08:28:15 +00:00
|
|
|
Return(DefineConstant());
|
2014-11-13 09:41:58 +00:00
|
|
|
EndBlock(Last());
|
|
|
|
|
|
|
|
Allocate();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(RegisterAllocatorTest, SimpleDiamond) {
|
|
|
|
// return p0 ? p0 : p0
|
|
|
|
StartBlock();
|
|
|
|
auto param = Parameter();
|
|
|
|
EndBlock(Branch(Reg(param), 1, 2));
|
|
|
|
|
|
|
|
StartBlock();
|
|
|
|
EndBlock(Jump(2));
|
|
|
|
|
|
|
|
StartBlock();
|
|
|
|
EndBlock(Jump(1));
|
|
|
|
|
|
|
|
StartBlock();
|
|
|
|
Return(param);
|
|
|
|
EndBlock();
|
|
|
|
|
|
|
|
Allocate();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(RegisterAllocatorTest, SimpleDiamondPhi) {
|
|
|
|
// return i ? K1 : K2
|
|
|
|
StartBlock();
|
|
|
|
EndBlock(Branch(Reg(DefineConstant()), 1, 2));
|
|
|
|
|
|
|
|
StartBlock();
|
|
|
|
auto t_val = DefineConstant();
|
|
|
|
EndBlock(Jump(2));
|
|
|
|
|
|
|
|
StartBlock();
|
|
|
|
auto f_val = DefineConstant();
|
|
|
|
EndBlock(Jump(1));
|
|
|
|
|
|
|
|
StartBlock();
|
|
|
|
Return(Reg(Phi(t_val, f_val)));
|
|
|
|
EndBlock();
|
|
|
|
|
|
|
|
Allocate();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(RegisterAllocatorTest, DiamondManyPhis) {
|
|
|
|
const int kPhis = kDefaultNRegs * 2;
|
|
|
|
|
|
|
|
StartBlock();
|
|
|
|
EndBlock(Branch(Reg(DefineConstant()), 1, 2));
|
|
|
|
|
|
|
|
StartBlock();
|
|
|
|
VReg t_vals[kPhis];
|
|
|
|
for (int i = 0; i < kPhis; ++i) {
|
|
|
|
t_vals[i] = DefineConstant();
|
|
|
|
}
|
|
|
|
EndBlock(Jump(2));
|
|
|
|
|
|
|
|
StartBlock();
|
|
|
|
VReg f_vals[kPhis];
|
|
|
|
for (int i = 0; i < kPhis; ++i) {
|
|
|
|
f_vals[i] = DefineConstant();
|
|
|
|
}
|
|
|
|
EndBlock(Jump(1));
|
|
|
|
|
|
|
|
StartBlock();
|
|
|
|
TestOperand merged[kPhis];
|
|
|
|
for (int i = 0; i < kPhis; ++i) {
|
|
|
|
merged[i] = Use(Phi(t_vals[i], f_vals[i]));
|
|
|
|
}
|
|
|
|
Return(EmitCall(Reg(), kPhis, merged));
|
|
|
|
EndBlock();
|
|
|
|
|
|
|
|
Allocate();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(RegisterAllocatorTest, DoubleDiamondManyRedundantPhis) {
|
|
|
|
const int kPhis = kDefaultNRegs * 2;
|
|
|
|
|
|
|
|
// First diamond.
|
|
|
|
StartBlock();
|
|
|
|
VReg vals[kPhis];
|
|
|
|
for (int i = 0; i < kPhis; ++i) {
|
|
|
|
vals[i] = Parameter(Slot(i));
|
|
|
|
}
|
|
|
|
EndBlock(Branch(Reg(DefineConstant()), 1, 2));
|
|
|
|
|
|
|
|
StartBlock();
|
|
|
|
EndBlock(Jump(2));
|
|
|
|
|
|
|
|
StartBlock();
|
|
|
|
EndBlock(Jump(1));
|
|
|
|
|
|
|
|
// Second diamond.
|
|
|
|
StartBlock();
|
|
|
|
EndBlock(Branch(Reg(DefineConstant()), 1, 2));
|
|
|
|
|
|
|
|
StartBlock();
|
|
|
|
EndBlock(Jump(2));
|
|
|
|
|
|
|
|
StartBlock();
|
|
|
|
EndBlock(Jump(1));
|
|
|
|
|
|
|
|
StartBlock();
|
|
|
|
TestOperand merged[kPhis];
|
|
|
|
for (int i = 0; i < kPhis; ++i) {
|
|
|
|
merged[i] = Use(Phi(vals[i], vals[i]));
|
|
|
|
}
|
|
|
|
Return(EmitCall(Reg(), kPhis, merged));
|
2014-11-06 08:28:15 +00:00
|
|
|
EndBlock();
|
|
|
|
|
|
|
|
Allocate();
|
|
|
|
}
|
|
|
|
|
2014-11-06 12:56:18 +00:00
|
|
|
|
|
|
|
TEST_F(RegisterAllocatorTest, RegressionPhisNeedTooManyRegisters) {
|
|
|
|
const size_t kNumRegs = 3;
|
|
|
|
const size_t kParams = kNumRegs + 1;
|
|
|
|
// Override number of registers.
|
|
|
|
SetNumRegs(kNumRegs, kNumRegs);
|
|
|
|
|
|
|
|
StartBlock();
|
2014-11-13 09:41:58 +00:00
|
|
|
auto constant = DefineConstant();
|
|
|
|
VReg parameters[kParams];
|
2014-11-06 12:56:18 +00:00
|
|
|
for (size_t i = 0; i < arraysize(parameters); ++i) {
|
|
|
|
parameters[i] = DefineConstant();
|
|
|
|
}
|
|
|
|
EndBlock();
|
|
|
|
|
|
|
|
PhiInstruction* phis[kParams];
|
|
|
|
{
|
|
|
|
StartLoop(2);
|
|
|
|
|
|
|
|
// Loop header.
|
|
|
|
StartBlock();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < arraysize(parameters); ++i) {
|
|
|
|
phis[i] = Phi(parameters[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform some computations.
|
|
|
|
// something like phi[i] += const
|
|
|
|
for (size_t i = 0; i < arraysize(parameters); ++i) {
|
2014-11-13 09:41:58 +00:00
|
|
|
auto result = EmitOII(Same(), Reg(phis[i]), Use(constant));
|
|
|
|
Extend(phis[i], result);
|
2014-11-06 12:56:18 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 09:41:58 +00:00
|
|
|
EndBlock(Branch(Reg(DefineConstant()), 1, 2));
|
2014-11-06 12:56:18 +00:00
|
|
|
|
|
|
|
// Jump back to loop header.
|
|
|
|
StartBlock();
|
|
|
|
EndBlock(Jump(-1));
|
|
|
|
|
|
|
|
EndLoop();
|
|
|
|
}
|
|
|
|
|
2014-11-13 09:41:58 +00:00
|
|
|
StartBlock();
|
2014-11-06 12:56:18 +00:00
|
|
|
Return(DefineConstant());
|
|
|
|
EndBlock();
|
|
|
|
|
|
|
|
Allocate();
|
|
|
|
}
|
|
|
|
|
2014-11-03 13:26:46 +00:00
|
|
|
} // namespace compiler
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|