1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "src/compiler/move-optimizer.h"
6 #include "test/unittests/compiler/instruction-sequence-unittest.h"
12 class MoveOptimizerTest : public InstructionSequenceTest {
14 GapInstruction* LastGap() {
15 return GapInstruction::cast(*(sequence()->instructions().rbegin() + 1));
18 void AddMove(GapInstruction* gap, TestOperand from, TestOperand to,
19 GapInstruction::InnerPosition pos = GapInstruction::START) {
20 auto parallel_move = gap->GetOrCreateParallelMove(pos, zone());
21 parallel_move->AddMove(ConvertMoveArg(from), ConvertMoveArg(to), zone());
24 int NonRedundantSize(ParallelMove* move) {
26 auto ops = move->move_operands();
27 for (auto op = ops->begin(); op != ops->end(); ++op) {
28 if (op->IsRedundant()) continue;
34 bool Contains(ParallelMove* move, TestOperand from_op, TestOperand to_op) {
35 auto from = ConvertMoveArg(from_op);
36 auto to = ConvertMoveArg(to_op);
37 auto ops = move->move_operands();
38 for (auto op = ops->begin(); op != ops->end(); ++op) {
39 if (op->IsRedundant()) continue;
40 if (op->source()->Equals(from) && op->destination()->Equals(to)) {
47 // TODO(dcarney): add a verifier.
50 if (FLAG_trace_turbo) {
52 PrintableInstructionSequence printable = {config(), sequence()};
53 os << "----- Instruction sequence before move optimization -----\n"
56 MoveOptimizer move_optimizer(zone(), sequence());
58 if (FLAG_trace_turbo) {
60 PrintableInstructionSequence printable = {config(), sequence()};
61 os << "----- Instruction sequence after move optimization -----\n"
67 InstructionOperand* ConvertMoveArg(TestOperand op) {
68 CHECK_EQ(kNoValue, op.vreg_.value_);
69 CHECK_NE(kNoValue, op.value_);
72 return ConstantOperand::New(op.value_, zone());
74 return StackSlotOperand::New(op.value_, zone());
76 CHECK(0 <= op.value_ && op.value_ < num_general_registers());
77 return RegisterOperand::New(op.value_, zone());
87 TEST_F(MoveOptimizerTest, RemovesRedundant) {
90 AddMove(LastGap(), Reg(0), Reg(1));
92 AddMove(LastGap(), Reg(1), Reg(0));
98 auto move = gap->parallel_moves()[0];
99 CHECK_EQ(1, NonRedundantSize(move));
100 CHECK(Contains(move, Reg(0), Reg(1)));
104 TEST_F(MoveOptimizerTest, SplitsConstants) {
108 auto gap = LastGap();
109 AddMove(gap, Const(1), Slot(0));
110 AddMove(gap, Const(1), Slot(1));
111 AddMove(gap, Const(1), Reg(0));
112 AddMove(gap, Const(1), Slot(2));
116 auto move = gap->parallel_moves()[0];
117 CHECK_EQ(1, NonRedundantSize(move));
118 CHECK(Contains(move, Const(1), Reg(0)));
120 move = gap->parallel_moves()[1];
121 CHECK_EQ(3, NonRedundantSize(move));
122 CHECK(Contains(move, Reg(0), Slot(0)));
123 CHECK(Contains(move, Reg(0), Slot(1)));
124 CHECK(Contains(move, Reg(0), Slot(2)));
127 } // namespace compiler
128 } // namespace internal