2014-11-26 12:41: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/compiler/instruction-codes.h"
|
2017-02-23 11:46:29 +00:00
|
|
|
#include "src/compiler/instruction.h"
|
2014-11-26 12:41:46 +00:00
|
|
|
#include "src/compiler/jump-threading.h"
|
2017-02-23 11:46:29 +00:00
|
|
|
#include "src/source-position.h"
|
2015-10-28 13:32:17 +00:00
|
|
|
#include "test/cctest/cctest.h"
|
2014-11-26 12:41:46 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace compiler {
|
|
|
|
|
|
|
|
class TestCode : public HandleAndZoneScope {
|
|
|
|
public:
|
|
|
|
TestCode()
|
|
|
|
: HandleAndZoneScope(),
|
|
|
|
blocks_(main_zone()),
|
2015-01-23 15:19:34 +00:00
|
|
|
sequence_(main_isolate(), main_zone(), &blocks_),
|
2014-11-26 12:41:46 +00:00
|
|
|
rpo_number_(RpoNumber::FromInt(0)),
|
2017-10-13 16:33:03 +00:00
|
|
|
current_(nullptr) {}
|
2014-11-26 12:41:46 +00:00
|
|
|
|
|
|
|
ZoneVector<InstructionBlock*> blocks_;
|
|
|
|
InstructionSequence sequence_;
|
|
|
|
RpoNumber rpo_number_;
|
|
|
|
InstructionBlock* current_;
|
|
|
|
|
|
|
|
int Jump(int target) {
|
|
|
|
Start();
|
2015-02-04 12:38:47 +00:00
|
|
|
InstructionOperand ops[] = {UseRpo(target)};
|
2017-10-13 16:33:03 +00:00
|
|
|
sequence_.AddInstruction(Instruction::New(main_zone(), kArchJmp, 0, nullptr,
|
|
|
|
1, ops, 0, nullptr));
|
2014-11-26 12:41:46 +00:00
|
|
|
int pos = static_cast<int>(sequence_.instructions().size() - 1);
|
|
|
|
End();
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
void Fallthru() {
|
|
|
|
Start();
|
|
|
|
End();
|
|
|
|
}
|
|
|
|
int Branch(int ttarget, int ftarget) {
|
|
|
|
Start();
|
2015-02-04 12:38:47 +00:00
|
|
|
InstructionOperand ops[] = {UseRpo(ttarget), UseRpo(ftarget)};
|
2014-11-26 12:41:46 +00:00
|
|
|
InstructionCode code = 119 | FlagsModeField::encode(kFlags_branch) |
|
|
|
|
FlagsConditionField::encode(kEqual);
|
2015-03-24 14:05:21 +00:00
|
|
|
sequence_.AddInstruction(
|
2017-10-13 16:33:03 +00:00
|
|
|
Instruction::New(main_zone(), code, 0, nullptr, 2, ops, 0, nullptr));
|
2014-11-26 12:41:46 +00:00
|
|
|
int pos = static_cast<int>(sequence_.instructions().size() - 1);
|
|
|
|
End();
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
void Nop() {
|
|
|
|
Start();
|
|
|
|
sequence_.AddInstruction(Instruction::New(main_zone(), kArchNop));
|
|
|
|
}
|
|
|
|
void RedundantMoves() {
|
|
|
|
Start();
|
|
|
|
sequence_.AddInstruction(Instruction::New(main_zone(), kArchNop));
|
2015-03-31 13:06:37 +00:00
|
|
|
int index = static_cast<int>(sequence_.instructions().size()) - 1;
|
2015-12-10 09:03:30 +00:00
|
|
|
AddGapMove(index, AllocatedOperand(LocationOperand::REGISTER,
|
|
|
|
MachineRepresentation::kWord32, 13),
|
|
|
|
AllocatedOperand(LocationOperand::REGISTER,
|
|
|
|
MachineRepresentation::kWord32, 13));
|
2014-11-26 12:41:46 +00:00
|
|
|
}
|
|
|
|
void NonRedundantMoves() {
|
|
|
|
Start();
|
|
|
|
sequence_.AddInstruction(Instruction::New(main_zone(), kArchNop));
|
2015-03-31 13:06:37 +00:00
|
|
|
int index = static_cast<int>(sequence_.instructions().size()) - 1;
|
[turbofan] Create ExplicitOperands to specify operands without virtual registers
Up until now, if one wanted to specify an explicit stack location or register as an operand for an instruction, it had to also be
explicitly associated with a virtual register as a so-called
FixedRegister or FixedStackSlot.
For the implementation of tail calls, the plan is to use the gap
resolver needs to shuffle stack locations from the caller to the
tail-called callee. In order to do this, it must be possible to
explicitly address operand locations on the stack that are not
associated with virtual registers.
This CL introduces ExplictOperands, which can specify a specific
register or stack location that is not associated with virtual
register. This will allow tail calls to specify the target
locations for the necessary stack moves in the gap for the tail
call without the core register allocation having to know about
the target of the stack moves at all.
In the process this CL:
* creates a new Operand kind, ExplicitOperand, with which
instructions can specify register and stack slots without an
associated virtual register.
* creates a LocationOperand class from which AllocatedOperand and
ExplicitOperand are derived and provides a common interface to
get Register, DoubleRegister and spill slot information.
* removes RegisterOperand, DoubleRegisterOperand,
StackSlotOperand and DoubleStackSlotOperand, they are subsumed
by LocationOperand.
* addresses a cleanup TODO in AllocatedOperand to reduce the
redundancy of AllocatedOperand::Kind by using machine_type() to
determine if an operand corresponds to a general purpose or
double register.
BUG=v8:4076
LOG=n
Review URL: https://codereview.chromium.org/1389373002
Cr-Commit-Position: refs/heads/master@{#31603}
2015-10-27 13:26:35 +00:00
|
|
|
AddGapMove(index, ConstantOperand(11),
|
2015-12-10 09:03:30 +00:00
|
|
|
AllocatedOperand(LocationOperand::REGISTER,
|
|
|
|
MachineRepresentation::kWord32, 11));
|
2014-11-26 12:41:46 +00:00
|
|
|
}
|
|
|
|
void Other() {
|
|
|
|
Start();
|
|
|
|
sequence_.AddInstruction(Instruction::New(main_zone(), 155));
|
|
|
|
}
|
|
|
|
void End() {
|
|
|
|
Start();
|
|
|
|
sequence_.EndBlock(current_->rpo_number());
|
2017-10-13 16:33:03 +00:00
|
|
|
current_ = nullptr;
|
2014-11-26 12:41:46 +00:00
|
|
|
rpo_number_ = RpoNumber::FromInt(rpo_number_.ToInt() + 1);
|
|
|
|
}
|
2015-02-04 12:38:47 +00:00
|
|
|
InstructionOperand UseRpo(int num) {
|
2015-04-09 14:06:19 +00:00
|
|
|
return sequence_.AddImmediate(Constant(RpoNumber::FromInt(num)));
|
2014-11-26 12:41:46 +00:00
|
|
|
}
|
|
|
|
void Start(bool deferred = false) {
|
2017-10-13 16:33:03 +00:00
|
|
|
if (current_ == nullptr) {
|
2015-02-25 16:37:49 +00:00
|
|
|
current_ = new (main_zone())
|
|
|
|
InstructionBlock(main_zone(), rpo_number_, RpoNumber::Invalid(),
|
2015-06-17 05:40:28 +00:00
|
|
|
RpoNumber::Invalid(), deferred, false);
|
2014-11-26 12:41:46 +00:00
|
|
|
blocks_.push_back(current_);
|
|
|
|
sequence_.StartBlock(rpo_number_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void Defer() {
|
2017-10-18 09:06:55 +00:00
|
|
|
CHECK_NULL(current_);
|
2014-11-26 12:41:46 +00:00
|
|
|
Start(true);
|
|
|
|
}
|
2015-04-15 12:36:36 +00:00
|
|
|
void AddGapMove(int index, const InstructionOperand& from,
|
|
|
|
const InstructionOperand& to) {
|
2015-03-31 13:06:37 +00:00
|
|
|
sequence_.InstructionAt(index)
|
|
|
|
->GetOrCreateParallelMove(Instruction::START, main_zone())
|
2015-04-15 12:36:36 +00:00
|
|
|
->AddMove(from, to);
|
2015-03-31 13:06:37 +00:00
|
|
|
}
|
2014-11-26 12:41:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void VerifyForwarding(TestCode& code, int count, int* expected) {
|
2016-09-20 16:07:25 +00:00
|
|
|
v8::internal::AccountingAllocator allocator;
|
2016-10-17 12:12:30 +00:00
|
|
|
Zone local_zone(&allocator, ZONE_NAME);
|
2014-11-26 12:41:46 +00:00
|
|
|
ZoneVector<RpoNumber> result(&local_zone);
|
2016-01-28 16:14:40 +00:00
|
|
|
JumpThreading::ComputeForwarding(&local_zone, result, &code.sequence_, true);
|
2014-11-26 12:41:46 +00:00
|
|
|
|
|
|
|
CHECK(count == static_cast<int>(result.size()));
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
CHECK(expected[i] == result[i].ToInt());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(FwEmpty1) {
|
|
|
|
TestCode code;
|
|
|
|
|
|
|
|
// B0
|
|
|
|
code.Jump(1);
|
|
|
|
// B1
|
|
|
|
code.Jump(2);
|
|
|
|
// B2
|
|
|
|
code.End();
|
|
|
|
|
|
|
|
static int expected[] = {2, 2, 2};
|
|
|
|
VerifyForwarding(code, 3, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(FwEmptyN) {
|
|
|
|
for (int i = 0; i < 9; i++) {
|
|
|
|
TestCode code;
|
|
|
|
|
|
|
|
// B0
|
|
|
|
code.Jump(1);
|
|
|
|
// B1
|
|
|
|
for (int j = 0; j < i; j++) code.Nop();
|
|
|
|
code.Jump(2);
|
|
|
|
// B2
|
|
|
|
code.End();
|
|
|
|
|
|
|
|
static int expected[] = {2, 2, 2};
|
|
|
|
VerifyForwarding(code, 3, expected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(FwNone1) {
|
|
|
|
TestCode code;
|
|
|
|
|
|
|
|
// B0
|
|
|
|
code.End();
|
|
|
|
|
|
|
|
static int expected[] = {0};
|
|
|
|
VerifyForwarding(code, 1, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(FwMoves1) {
|
|
|
|
TestCode code;
|
|
|
|
|
|
|
|
// B0
|
|
|
|
code.RedundantMoves();
|
|
|
|
code.End();
|
|
|
|
|
|
|
|
static int expected[] = {0};
|
|
|
|
VerifyForwarding(code, 1, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(FwMoves2) {
|
|
|
|
TestCode code;
|
|
|
|
|
|
|
|
// B0
|
|
|
|
code.RedundantMoves();
|
|
|
|
code.Fallthru();
|
|
|
|
// B1
|
|
|
|
code.End();
|
|
|
|
|
|
|
|
static int expected[] = {1, 1};
|
|
|
|
VerifyForwarding(code, 2, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(FwMoves2b) {
|
|
|
|
TestCode code;
|
|
|
|
|
|
|
|
// B0
|
|
|
|
code.NonRedundantMoves();
|
|
|
|
code.Fallthru();
|
|
|
|
// B1
|
|
|
|
code.End();
|
|
|
|
|
|
|
|
static int expected[] = {0, 1};
|
|
|
|
VerifyForwarding(code, 2, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(FwOther2) {
|
|
|
|
TestCode code;
|
|
|
|
|
|
|
|
// B0
|
|
|
|
code.Other();
|
|
|
|
code.Fallthru();
|
|
|
|
// B1
|
|
|
|
code.End();
|
|
|
|
|
|
|
|
static int expected[] = {0, 1};
|
|
|
|
VerifyForwarding(code, 2, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(FwNone2a) {
|
|
|
|
TestCode code;
|
|
|
|
|
|
|
|
// B0
|
|
|
|
code.Fallthru();
|
|
|
|
// B1
|
|
|
|
code.End();
|
|
|
|
|
|
|
|
static int expected[] = {1, 1};
|
|
|
|
VerifyForwarding(code, 2, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(FwNone2b) {
|
|
|
|
TestCode code;
|
|
|
|
|
|
|
|
// B0
|
|
|
|
code.Jump(1);
|
|
|
|
// B1
|
|
|
|
code.End();
|
|
|
|
|
|
|
|
static int expected[] = {1, 1};
|
|
|
|
VerifyForwarding(code, 2, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(FwLoop1) {
|
|
|
|
TestCode code;
|
|
|
|
|
|
|
|
// B0
|
|
|
|
code.Jump(0);
|
|
|
|
|
|
|
|
static int expected[] = {0};
|
|
|
|
VerifyForwarding(code, 1, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(FwLoop2) {
|
|
|
|
TestCode code;
|
|
|
|
|
|
|
|
// B0
|
|
|
|
code.Fallthru();
|
|
|
|
// B1
|
|
|
|
code.Jump(0);
|
|
|
|
|
|
|
|
static int expected[] = {0, 0};
|
|
|
|
VerifyForwarding(code, 2, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(FwLoop3) {
|
|
|
|
TestCode code;
|
|
|
|
|
|
|
|
// B0
|
|
|
|
code.Fallthru();
|
|
|
|
// B1
|
|
|
|
code.Fallthru();
|
|
|
|
// B2
|
|
|
|
code.Jump(0);
|
|
|
|
|
|
|
|
static int expected[] = {0, 0, 0};
|
|
|
|
VerifyForwarding(code, 3, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(FwLoop1b) {
|
|
|
|
TestCode code;
|
|
|
|
|
|
|
|
// B0
|
|
|
|
code.Fallthru();
|
|
|
|
// B1
|
|
|
|
code.Jump(1);
|
|
|
|
|
|
|
|
static int expected[] = {1, 1};
|
|
|
|
VerifyForwarding(code, 2, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(FwLoop2b) {
|
|
|
|
TestCode code;
|
|
|
|
|
|
|
|
// B0
|
|
|
|
code.Fallthru();
|
|
|
|
// B1
|
|
|
|
code.Fallthru();
|
|
|
|
// B2
|
|
|
|
code.Jump(1);
|
|
|
|
|
|
|
|
static int expected[] = {1, 1, 1};
|
|
|
|
VerifyForwarding(code, 3, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(FwLoop3b) {
|
|
|
|
TestCode code;
|
|
|
|
|
|
|
|
// B0
|
|
|
|
code.Fallthru();
|
|
|
|
// B1
|
|
|
|
code.Fallthru();
|
|
|
|
// B2
|
|
|
|
code.Fallthru();
|
|
|
|
// B3
|
|
|
|
code.Jump(1);
|
|
|
|
|
|
|
|
static int expected[] = {1, 1, 1, 1};
|
|
|
|
VerifyForwarding(code, 4, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(FwLoop2_1a) {
|
|
|
|
TestCode code;
|
|
|
|
|
|
|
|
// B0
|
|
|
|
code.Fallthru();
|
|
|
|
// B1
|
|
|
|
code.Fallthru();
|
|
|
|
// B2
|
|
|
|
code.Fallthru();
|
|
|
|
// B3
|
|
|
|
code.Jump(1);
|
|
|
|
// B4
|
|
|
|
code.Jump(2);
|
|
|
|
|
|
|
|
static int expected[] = {1, 1, 1, 1, 1};
|
|
|
|
VerifyForwarding(code, 5, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(FwLoop2_1b) {
|
|
|
|
TestCode code;
|
|
|
|
|
|
|
|
// B0
|
|
|
|
code.Fallthru();
|
|
|
|
// B1
|
|
|
|
code.Fallthru();
|
|
|
|
// B2
|
|
|
|
code.Jump(4);
|
|
|
|
// B3
|
|
|
|
code.Jump(1);
|
|
|
|
// B4
|
|
|
|
code.Jump(2);
|
|
|
|
|
|
|
|
static int expected[] = {2, 2, 2, 2, 2};
|
|
|
|
VerifyForwarding(code, 5, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(FwLoop2_1c) {
|
|
|
|
TestCode code;
|
|
|
|
|
|
|
|
// B0
|
|
|
|
code.Fallthru();
|
|
|
|
// B1
|
|
|
|
code.Fallthru();
|
|
|
|
// B2
|
|
|
|
code.Jump(4);
|
|
|
|
// B3
|
|
|
|
code.Jump(2);
|
|
|
|
// B4
|
|
|
|
code.Jump(1);
|
|
|
|
|
|
|
|
static int expected[] = {1, 1, 1, 1, 1};
|
|
|
|
VerifyForwarding(code, 5, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(FwLoop2_1d) {
|
|
|
|
TestCode code;
|
|
|
|
|
|
|
|
// B0
|
|
|
|
code.Fallthru();
|
|
|
|
// B1
|
|
|
|
code.Fallthru();
|
|
|
|
// B2
|
|
|
|
code.Jump(1);
|
|
|
|
// B3
|
|
|
|
code.Jump(1);
|
|
|
|
// B4
|
|
|
|
code.Jump(1);
|
|
|
|
|
|
|
|
static int expected[] = {1, 1, 1, 1, 1};
|
|
|
|
VerifyForwarding(code, 5, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(FwLoop3_1a) {
|
|
|
|
TestCode code;
|
|
|
|
|
|
|
|
// B0
|
|
|
|
code.Fallthru();
|
|
|
|
// B1
|
|
|
|
code.Fallthru();
|
|
|
|
// B2
|
|
|
|
code.Fallthru();
|
|
|
|
// B3
|
|
|
|
code.Jump(2);
|
|
|
|
// B4
|
|
|
|
code.Jump(1);
|
|
|
|
// B5
|
|
|
|
code.Jump(0);
|
|
|
|
|
|
|
|
static int expected[] = {2, 2, 2, 2, 2, 2};
|
|
|
|
VerifyForwarding(code, 6, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(FwDiamonds) {
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
for (int j = 0; j < 2; j++) {
|
|
|
|
TestCode code;
|
|
|
|
// B0
|
|
|
|
code.Branch(1, 2);
|
|
|
|
// B1
|
|
|
|
if (i) code.Other();
|
|
|
|
code.Jump(3);
|
|
|
|
// B2
|
|
|
|
if (j) code.Other();
|
|
|
|
code.Jump(3);
|
|
|
|
// B3
|
|
|
|
code.End();
|
|
|
|
|
|
|
|
int expected[] = {0, i ? 1 : 3, j ? 2 : 3, 3};
|
|
|
|
VerifyForwarding(code, 4, expected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(FwDiamonds2) {
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
for (int j = 0; j < 2; j++) {
|
|
|
|
for (int k = 0; k < 2; k++) {
|
|
|
|
TestCode code;
|
|
|
|
// B0
|
|
|
|
code.Branch(1, 2);
|
|
|
|
// B1
|
|
|
|
if (i) code.Other();
|
|
|
|
code.Jump(3);
|
|
|
|
// B2
|
|
|
|
if (j) code.Other();
|
|
|
|
code.Jump(3);
|
|
|
|
// B3
|
|
|
|
if (k) code.NonRedundantMoves();
|
|
|
|
code.Jump(4);
|
|
|
|
// B4
|
|
|
|
code.End();
|
|
|
|
|
|
|
|
int merge = k ? 3 : 4;
|
|
|
|
int expected[] = {0, i ? 1 : merge, j ? 2 : merge, merge, 4};
|
|
|
|
VerifyForwarding(code, 5, expected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(FwDoubleDiamonds) {
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
for (int j = 0; j < 2; j++) {
|
|
|
|
for (int x = 0; x < 2; x++) {
|
|
|
|
for (int y = 0; y < 2; y++) {
|
|
|
|
TestCode code;
|
|
|
|
// B0
|
|
|
|
code.Branch(1, 2);
|
|
|
|
// B1
|
|
|
|
if (i) code.Other();
|
|
|
|
code.Jump(3);
|
|
|
|
// B2
|
|
|
|
if (j) code.Other();
|
|
|
|
code.Jump(3);
|
|
|
|
// B3
|
|
|
|
code.Branch(4, 5);
|
|
|
|
// B4
|
|
|
|
if (x) code.Other();
|
|
|
|
code.Jump(6);
|
|
|
|
// B5
|
|
|
|
if (y) code.Other();
|
|
|
|
code.Jump(6);
|
|
|
|
// B6
|
|
|
|
code.End();
|
|
|
|
|
|
|
|
int expected[] = {0, i ? 1 : 3, j ? 2 : 3, 3,
|
|
|
|
x ? 4 : 6, y ? 5 : 6, 6};
|
|
|
|
VerifyForwarding(code, 7, expected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <int kSize>
|
|
|
|
void RunPermutationsRecursive(int outer[kSize], int start,
|
|
|
|
void (*run)(int*, int)) {
|
|
|
|
int permutation[kSize];
|
|
|
|
|
|
|
|
for (int i = 0; i < kSize; i++) permutation[i] = outer[i];
|
|
|
|
|
|
|
|
int count = kSize - start;
|
|
|
|
if (count == 0) return run(permutation, kSize);
|
|
|
|
for (int i = start; i < kSize; i++) {
|
|
|
|
permutation[start] = outer[i];
|
|
|
|
permutation[i] = outer[start];
|
|
|
|
RunPermutationsRecursive<kSize>(permutation, start + 1, run);
|
|
|
|
permutation[i] = outer[i];
|
|
|
|
permutation[start] = outer[start];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template <int kSize>
|
|
|
|
void RunAllPermutations(void (*run)(int*, int)) {
|
|
|
|
int permutation[kSize];
|
|
|
|
for (int i = 0; i < kSize; i++) permutation[i] = i;
|
|
|
|
RunPermutationsRecursive<kSize>(permutation, 0, run);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void PrintPermutation(int* permutation, int size) {
|
|
|
|
printf("{ ");
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
if (i > 0) printf(", ");
|
|
|
|
printf("%d", permutation[i]);
|
|
|
|
}
|
|
|
|
printf(" }\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int find(int x, int* permutation, int size) {
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
if (permutation[i] == x) return i;
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RunPermutedChain(int* permutation, int size) {
|
|
|
|
TestCode code;
|
|
|
|
int cur = -1;
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
code.Jump(find(cur + 1, permutation, size) + 1);
|
|
|
|
cur = permutation[i];
|
|
|
|
}
|
|
|
|
code.Jump(find(cur + 1, permutation, size) + 1);
|
|
|
|
code.End();
|
|
|
|
|
|
|
|
int expected[] = {size + 1, size + 1, size + 1, size + 1,
|
|
|
|
size + 1, size + 1, size + 1};
|
|
|
|
VerifyForwarding(code, size + 2, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(FwPermuted_chain) {
|
|
|
|
RunAllPermutations<3>(RunPermutedChain);
|
|
|
|
RunAllPermutations<4>(RunPermutedChain);
|
|
|
|
RunAllPermutations<5>(RunPermutedChain);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void RunPermutedDiamond(int* permutation, int size) {
|
|
|
|
TestCode code;
|
|
|
|
int br = 1 + find(0, permutation, size);
|
|
|
|
code.Jump(br);
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
switch (permutation[i]) {
|
|
|
|
case 0:
|
|
|
|
code.Branch(1 + find(1, permutation, size),
|
|
|
|
1 + find(2, permutation, size));
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
code.Jump(1 + find(3, permutation, size));
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
code.Jump(1 + find(3, permutation, size));
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
code.Jump(5);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
code.End();
|
|
|
|
|
|
|
|
int expected[] = {br, 5, 5, 5, 5, 5};
|
|
|
|
expected[br] = br;
|
|
|
|
VerifyForwarding(code, 6, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(FwPermuted_diamond) { RunAllPermutations<4>(RunPermutedDiamond); }
|
|
|
|
|
|
|
|
|
|
|
|
void ApplyForwarding(TestCode& code, int size, int* forward) {
|
|
|
|
ZoneVector<RpoNumber> vector(code.main_zone());
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
vector.push_back(RpoNumber::FromInt(forward[i]));
|
|
|
|
}
|
|
|
|
JumpThreading::ApplyForwarding(vector, &code.sequence_);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CheckJump(TestCode& code, int pos, int target) {
|
|
|
|
Instruction* instr = code.sequence_.InstructionAt(pos);
|
|
|
|
CHECK_EQ(kArchJmp, instr->arch_opcode());
|
|
|
|
CHECK_EQ(1, static_cast<int>(instr->InputCount()));
|
|
|
|
CHECK_EQ(0, static_cast<int>(instr->OutputCount()));
|
|
|
|
CHECK_EQ(0, static_cast<int>(instr->TempCount()));
|
|
|
|
CHECK_EQ(target, code.sequence_.InputRpo(instr, 0).ToInt());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CheckNop(TestCode& code, int pos) {
|
|
|
|
Instruction* instr = code.sequence_.InstructionAt(pos);
|
|
|
|
CHECK_EQ(kArchNop, instr->arch_opcode());
|
|
|
|
CHECK_EQ(0, static_cast<int>(instr->InputCount()));
|
|
|
|
CHECK_EQ(0, static_cast<int>(instr->OutputCount()));
|
|
|
|
CHECK_EQ(0, static_cast<int>(instr->TempCount()));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CheckBranch(TestCode& code, int pos, int t1, int t2) {
|
|
|
|
Instruction* instr = code.sequence_.InstructionAt(pos);
|
|
|
|
CHECK_EQ(2, static_cast<int>(instr->InputCount()));
|
|
|
|
CHECK_EQ(0, static_cast<int>(instr->OutputCount()));
|
|
|
|
CHECK_EQ(0, static_cast<int>(instr->TempCount()));
|
|
|
|
CHECK_EQ(t1, code.sequence_.InputRpo(instr, 0).ToInt());
|
|
|
|
CHECK_EQ(t2, code.sequence_.InputRpo(instr, 1).ToInt());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CheckAssemblyOrder(TestCode& code, int size, int* expected) {
|
|
|
|
int i = 0;
|
|
|
|
for (auto const block : code.sequence_.instruction_blocks()) {
|
|
|
|
CHECK_EQ(expected[i++], block->ao_number().ToInt());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(Rewire1) {
|
|
|
|
TestCode code;
|
|
|
|
|
|
|
|
// B0
|
|
|
|
int j1 = code.Jump(1);
|
|
|
|
// B1
|
|
|
|
int j2 = code.Jump(2);
|
|
|
|
// B2
|
|
|
|
code.End();
|
|
|
|
|
|
|
|
static int forward[] = {2, 2, 2};
|
|
|
|
ApplyForwarding(code, 3, forward);
|
|
|
|
CheckJump(code, j1, 2);
|
|
|
|
CheckNop(code, j2);
|
|
|
|
|
|
|
|
static int assembly[] = {0, 1, 1};
|
|
|
|
CheckAssemblyOrder(code, 3, assembly);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(Rewire1_deferred) {
|
|
|
|
TestCode code;
|
|
|
|
|
|
|
|
// B0
|
|
|
|
int j1 = code.Jump(1);
|
|
|
|
// B1
|
|
|
|
int j2 = code.Jump(2);
|
|
|
|
// B2
|
|
|
|
code.Defer();
|
|
|
|
int j3 = code.Jump(3);
|
|
|
|
// B3
|
|
|
|
code.End();
|
|
|
|
|
|
|
|
static int forward[] = {3, 3, 3, 3};
|
|
|
|
ApplyForwarding(code, 4, forward);
|
|
|
|
CheckJump(code, j1, 3);
|
|
|
|
CheckNop(code, j2);
|
|
|
|
CheckNop(code, j3);
|
|
|
|
|
|
|
|
static int assembly[] = {0, 1, 2, 1};
|
|
|
|
CheckAssemblyOrder(code, 4, assembly);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(Rewire2_deferred) {
|
|
|
|
TestCode code;
|
|
|
|
|
|
|
|
// B0
|
|
|
|
code.Other();
|
|
|
|
int j1 = code.Jump(1);
|
|
|
|
// B1
|
|
|
|
code.Defer();
|
|
|
|
code.Fallthru();
|
|
|
|
// B2
|
|
|
|
code.Defer();
|
|
|
|
int j2 = code.Jump(3);
|
|
|
|
// B3
|
|
|
|
code.End();
|
|
|
|
|
|
|
|
static int forward[] = {0, 1, 2, 3};
|
|
|
|
ApplyForwarding(code, 4, forward);
|
|
|
|
CheckJump(code, j1, 1);
|
|
|
|
CheckJump(code, j2, 3);
|
|
|
|
|
|
|
|
static int assembly[] = {0, 2, 3, 1};
|
|
|
|
CheckAssemblyOrder(code, 4, assembly);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(Rewire_diamond) {
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
for (int j = 0; j < 2; j++) {
|
|
|
|
TestCode code;
|
|
|
|
// B0
|
|
|
|
int j1 = code.Jump(1);
|
|
|
|
// B1
|
|
|
|
int b1 = code.Branch(2, 3);
|
|
|
|
// B2
|
|
|
|
int j2 = code.Jump(4);
|
|
|
|
// B3
|
|
|
|
int j3 = code.Jump(4);
|
|
|
|
// B5
|
|
|
|
code.End();
|
|
|
|
|
|
|
|
int forward[] = {0, 1, i ? 4 : 2, j ? 4 : 3, 4};
|
|
|
|
ApplyForwarding(code, 5, forward);
|
|
|
|
CheckJump(code, j1, 1);
|
|
|
|
CheckBranch(code, b1, i ? 4 : 2, j ? 4 : 3);
|
|
|
|
if (i) {
|
|
|
|
CheckNop(code, j2);
|
|
|
|
} else {
|
|
|
|
CheckJump(code, j2, 4);
|
|
|
|
}
|
|
|
|
if (j) {
|
|
|
|
CheckNop(code, j3);
|
|
|
|
} else {
|
|
|
|
CheckJump(code, j3, 4);
|
|
|
|
}
|
|
|
|
|
|
|
|
int assembly[] = {0, 1, 2, 3, 4};
|
|
|
|
if (i) {
|
|
|
|
for (int k = 3; k < 5; k++) assembly[k]--;
|
|
|
|
}
|
|
|
|
if (j) {
|
|
|
|
for (int k = 4; k < 5; k++) assembly[k]--;
|
|
|
|
}
|
|
|
|
CheckAssemblyOrder(code, 5, assembly);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace compiler
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|