summaryrefslogtreecommitdiff
path: root/deps/v8/test/unittests/compiler/move-optimizer-unittest.cc
blob: b8375fab10ad5267db5889fd03d227c75070ac2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// 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/move-optimizer.h"
#include "test/unittests/compiler/instruction-sequence-unittest.h"

namespace v8 {
namespace internal {
namespace compiler {

class MoveOptimizerTest : public InstructionSequenceTest {
 public:
  GapInstruction* LastGap() {
    return GapInstruction::cast(*(sequence()->instructions().rbegin() + 1));
  }

  void AddMove(GapInstruction* gap, TestOperand from, TestOperand to,
               GapInstruction::InnerPosition pos = GapInstruction::START) {
    auto parallel_move = gap->GetOrCreateParallelMove(pos, zone());
    parallel_move->AddMove(ConvertMoveArg(from), ConvertMoveArg(to), zone());
  }

  int NonRedundantSize(ParallelMove* move) {
    int i = 0;
    auto ops = move->move_operands();
    for (auto op = ops->begin(); op != ops->end(); ++op) {
      if (op->IsRedundant()) continue;
      i++;
    }
    return i;
  }

  bool Contains(ParallelMove* move, TestOperand from_op, TestOperand to_op) {
    auto from = ConvertMoveArg(from_op);
    auto to = ConvertMoveArg(to_op);
    auto ops = move->move_operands();
    for (auto op = ops->begin(); op != ops->end(); ++op) {
      if (op->IsRedundant()) continue;
      if (op->source()->Equals(from) && op->destination()->Equals(to)) {
        return true;
      }
    }
    return false;
  }

  // TODO(dcarney): add a verifier.
  void Optimize() {
    WireBlocks();
    if (FLAG_trace_turbo) {
      OFStream os(stdout);
      PrintableInstructionSequence printable = {config(), sequence()};
      os << "----- Instruction sequence before move optimization -----\n"
         << printable;
    }
    MoveOptimizer move_optimizer(zone(), sequence());
    move_optimizer.Run();
    if (FLAG_trace_turbo) {
      OFStream os(stdout);
      PrintableInstructionSequence printable = {config(), sequence()};
      os << "----- Instruction sequence after move optimization -----\n"
         << printable;
    }
  }

 private:
  InstructionOperand* ConvertMoveArg(TestOperand op) {
    CHECK_EQ(kNoValue, op.vreg_.value_);
    CHECK_NE(kNoValue, op.value_);
    switch (op.type_) {
      case kConstant:
        return ConstantOperand::New(op.value_, zone());
      case kFixedSlot:
        return StackSlotOperand::New(op.value_, zone());
      case kFixedRegister:
        CHECK(0 <= op.value_ && op.value_ < num_general_registers());
        return RegisterOperand::New(op.value_, zone());
      default:
        break;
    }
    CHECK(false);
    return nullptr;
  }
};


TEST_F(MoveOptimizerTest, RemovesRedundant) {
  StartBlock();
  EmitNop();
  AddMove(LastGap(), Reg(0), Reg(1));
  EmitNop();
  AddMove(LastGap(), Reg(1), Reg(0));
  EndBlock(Last());

  Optimize();

  auto gap = LastGap();
  auto move = gap->parallel_moves()[0];
  CHECK_EQ(1, NonRedundantSize(move));
  CHECK(Contains(move, Reg(0), Reg(1)));
}


TEST_F(MoveOptimizerTest, SplitsConstants) {
  StartBlock();
  EndBlock(Last());

  auto gap = LastGap();
  AddMove(gap, Const(1), Slot(0));
  AddMove(gap, Const(1), Slot(1));
  AddMove(gap, Const(1), Reg(0));
  AddMove(gap, Const(1), Slot(2));

  Optimize();

  auto move = gap->parallel_moves()[0];
  CHECK_EQ(1, NonRedundantSize(move));
  CHECK(Contains(move, Const(1), Reg(0)));

  move = gap->parallel_moves()[1];
  CHECK_EQ(3, NonRedundantSize(move));
  CHECK(Contains(move, Reg(0), Slot(0)));
  CHECK(Contains(move, Reg(0), Slot(1)));
  CHECK(Contains(move, Reg(0), Slot(2)));
}

}  // namespace compiler
}  // namespace internal
}  // namespace v8