OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/v8.h" | 5 #include "src/v8.h" |
6 | 6 |
7 #include "src/factory.h" | 7 #include "src/factory.h" |
| 8 #include "src/interpreter/bytecode-label.h" |
8 #include "src/interpreter/bytecode-register-optimizer.h" | 9 #include "src/interpreter/bytecode-register-optimizer.h" |
9 #include "src/objects-inl.h" | 10 #include "src/objects-inl.h" |
10 #include "src/objects.h" | 11 #include "src/objects.h" |
11 #include "test/unittests/test-utils.h" | 12 #include "test/unittests/test-utils.h" |
12 | 13 |
13 namespace v8 { | 14 namespace v8 { |
14 namespace internal { | 15 namespace internal { |
15 namespace interpreter { | 16 namespace interpreter { |
16 | 17 |
17 class BytecodeRegisterOptimizerTest : public BytecodePipelineStage, | 18 class BytecodeRegisterOptimizerTest : public BytecodePipelineStage, |
18 public TestWithIsolateAndZone { | 19 public TestWithIsolateAndZone { |
19 public: | 20 public: |
20 BytecodeRegisterOptimizerTest() {} | 21 BytecodeRegisterOptimizerTest() {} |
21 ~BytecodeRegisterOptimizerTest() override { delete register_allocator_; } | 22 ~BytecodeRegisterOptimizerTest() override { delete register_allocator_; } |
22 | 23 |
23 void Initialize(int number_of_parameters, int number_of_locals) { | 24 void Initialize(int number_of_parameters, int number_of_locals) { |
24 register_allocator_ = | 25 register_allocator_ = |
25 new TemporaryRegisterAllocator(zone(), number_of_locals); | 26 new TemporaryRegisterAllocator(zone(), number_of_locals); |
26 register_optimizer_ = new (zone()) BytecodeRegisterOptimizer( | 27 register_optimizer_ = new (zone()) BytecodeRegisterOptimizer( |
27 zone(), register_allocator_, number_of_parameters, this); | 28 zone(), register_allocator_, number_of_parameters, this); |
28 } | 29 } |
29 | 30 |
30 size_t FlushForOffset() override { | |
31 flush_for_offset_count_++; | |
32 return 0; | |
33 }; | |
34 | |
35 void FlushBasicBlock() override { flush_basic_block_count_++; } | |
36 | |
37 void Write(BytecodeNode* node) override { output_.push_back(*node); } | 31 void Write(BytecodeNode* node) override { output_.push_back(*node); } |
| 32 void WriteJump(BytecodeNode* node, BytecodeLabel* label) override { |
| 33 output_.push_back(*node); |
| 34 } |
| 35 void BindLabel(BytecodeLabel* label) override {} |
| 36 void BindLabel(const BytecodeLabel& target, BytecodeLabel* label) override {} |
| 37 Handle<BytecodeArray> ToBytecodeArray( |
| 38 int fixed_register_count, int parameter_count, |
| 39 Handle<FixedArray> handle_table) override { |
| 40 return Handle<BytecodeArray>(); |
| 41 } |
38 | 42 |
39 TemporaryRegisterAllocator* allocator() { return register_allocator_; } | 43 TemporaryRegisterAllocator* allocator() { return register_allocator_; } |
40 BytecodeRegisterOptimizer* optimizer() { return register_optimizer_; } | 44 BytecodeRegisterOptimizer* optimizer() { return register_optimizer_; } |
41 | 45 |
42 Register NewTemporary() { | 46 Register NewTemporary() { |
43 return Register(allocator()->BorrowTemporaryRegister()); | 47 return Register(allocator()->BorrowTemporaryRegister()); |
44 } | 48 } |
45 | 49 |
46 void KillTemporary(Register reg) { | 50 void KillTemporary(Register reg) { |
47 allocator()->ReturnTemporaryRegister(reg.index()); | 51 allocator()->ReturnTemporaryRegister(reg.index()); |
48 } | 52 } |
49 | 53 |
50 int flush_for_offset_count() const { return flush_for_offset_count_; } | |
51 int flush_basic_block_count() const { return flush_basic_block_count_; } | |
52 size_t write_count() const { return output_.size(); } | 54 size_t write_count() const { return output_.size(); } |
53 const BytecodeNode& last_written() const { return output_.back(); } | 55 const BytecodeNode& last_written() const { return output_.back(); } |
54 const std::vector<BytecodeNode>* output() { return &output_; } | 56 const std::vector<BytecodeNode>* output() { return &output_; } |
55 | 57 |
56 private: | 58 private: |
57 TemporaryRegisterAllocator* register_allocator_; | 59 TemporaryRegisterAllocator* register_allocator_; |
58 BytecodeRegisterOptimizer* register_optimizer_; | 60 BytecodeRegisterOptimizer* register_optimizer_; |
59 | 61 |
60 int flush_for_offset_count_ = 0; | |
61 int flush_basic_block_count_ = 0; | |
62 std::vector<BytecodeNode> output_; | 62 std::vector<BytecodeNode> output_; |
63 }; | 63 }; |
64 | 64 |
65 // Sanity tests. | 65 // Sanity tests. |
66 | 66 |
67 TEST_F(BytecodeRegisterOptimizerTest, FlushForOffsetPassThrough) { | 67 TEST_F(BytecodeRegisterOptimizerTest, WriteNop) { |
68 Initialize(1, 1); | |
69 CHECK_EQ(flush_for_offset_count(), 0); | |
70 CHECK_EQ(optimizer()->FlushForOffset(), 0); | |
71 CHECK_EQ(flush_for_offset_count(), 1); | |
72 } | |
73 | |
74 TEST_F(BytecodeRegisterOptimizerTest, FlushForOffsetRightSize) { | |
75 Initialize(1, 1); | |
76 BytecodeNode node(Bytecode::kAdd, Register(0).ToOperand(), | |
77 OperandScale::kQuadruple); | |
78 optimizer()->Write(&node); | |
79 CHECK_EQ(optimizer()->FlushForOffset(), 0); | |
80 CHECK_EQ(flush_for_offset_count(), 1); | |
81 CHECK_EQ(write_count(), 1); | |
82 } | |
83 | |
84 TEST_F(BytecodeRegisterOptimizerTest, FlushForOffsetNop) { | |
85 Initialize(1, 1); | 68 Initialize(1, 1); |
86 BytecodeNode node(Bytecode::kNop); | 69 BytecodeNode node(Bytecode::kNop); |
87 optimizer()->Write(&node); | 70 optimizer()->Write(&node); |
88 CHECK_EQ(optimizer()->FlushForOffset(), 0); | |
89 CHECK_EQ(flush_for_offset_count(), 1); | |
90 CHECK_EQ(write_count(), 1); | 71 CHECK_EQ(write_count(), 1); |
| 72 CHECK_EQ(node, last_written()); |
91 } | 73 } |
92 | 74 |
93 TEST_F(BytecodeRegisterOptimizerTest, FlushForOffsetNopExpression) { | 75 TEST_F(BytecodeRegisterOptimizerTest, WriteNopExpression) { |
94 Initialize(1, 1); | 76 Initialize(1, 1); |
95 BytecodeNode node(Bytecode::kNop); | 77 BytecodeNode node(Bytecode::kNop); |
96 node.source_info().Update({3, false}); | 78 node.source_info().Update({3, false}); |
97 optimizer()->Write(&node); | 79 optimizer()->Write(&node); |
98 CHECK_EQ(optimizer()->FlushForOffset(), 0); | |
99 CHECK_EQ(flush_for_offset_count(), 1); | |
100 CHECK_EQ(write_count(), 1); | 80 CHECK_EQ(write_count(), 1); |
| 81 CHECK_EQ(node, last_written()); |
101 } | 82 } |
102 | 83 |
103 TEST_F(BytecodeRegisterOptimizerTest, FlushForOffsetNopStatement) { | 84 TEST_F(BytecodeRegisterOptimizerTest, WriteNopStatement) { |
104 Initialize(1, 1); | 85 Initialize(1, 1); |
105 BytecodeNode node(Bytecode::kNop); | 86 BytecodeNode node(Bytecode::kNop); |
106 node.source_info().Update({3, true}); | 87 node.source_info().Update({3, true}); |
107 optimizer()->Write(&node); | 88 optimizer()->Write(&node); |
108 CHECK_EQ(optimizer()->FlushForOffset(), 0); | |
109 CHECK_EQ(flush_for_offset_count(), 1); | |
110 CHECK_EQ(write_count(), 1); | |
111 } | |
112 | |
113 TEST_F(BytecodeRegisterOptimizerTest, FlushBasicBlockPassThrough) { | |
114 Initialize(1, 1); | |
115 CHECK_EQ(flush_basic_block_count(), 0); | |
116 optimizer()->FlushBasicBlock(); | |
117 CHECK_EQ(flush_basic_block_count(), 1); | |
118 CHECK_EQ(write_count(), 0); | |
119 } | |
120 | |
121 TEST_F(BytecodeRegisterOptimizerTest, WriteOneFlushBasicBlock) { | |
122 Initialize(1, 1); | |
123 BytecodeNode node(Bytecode::kAdd, Register(0).ToOperand(), | |
124 OperandScale::kQuadruple); | |
125 optimizer()->Write(&node); | |
126 CHECK_EQ(write_count(), 1); | |
127 optimizer()->FlushBasicBlock(); | |
128 CHECK_EQ(write_count(), 1); | 89 CHECK_EQ(write_count(), 1); |
129 CHECK_EQ(node, last_written()); | 90 CHECK_EQ(node, last_written()); |
130 } | 91 } |
131 | 92 |
| 93 TEST_F(BytecodeRegisterOptimizerTest, TemporaryMaterializedForJump) { |
| 94 Initialize(1, 1); |
| 95 Register temp = NewTemporary(); |
| 96 BytecodeNode node(Bytecode::kStar, temp.ToOperand(), OperandScale::kSingle); |
| 97 optimizer()->Write(&node); |
| 98 CHECK_EQ(write_count(), 0); |
| 99 BytecodeLabel label; |
| 100 BytecodeNode jump(Bytecode::kJump, 0, OperandScale::kSingle); |
| 101 optimizer()->WriteJump(&jump, &label); |
| 102 CHECK_EQ(write_count(), 2); |
| 103 CHECK_EQ(output()->at(0).bytecode(), Bytecode::kStar); |
| 104 CHECK_EQ(output()->at(0).operand(0), temp.ToOperand()); |
| 105 CHECK_EQ(output()->at(0).operand_scale(), OperandScale::kSingle); |
| 106 CHECK_EQ(output()->at(1).bytecode(), Bytecode::kJump); |
| 107 } |
| 108 |
| 109 TEST_F(BytecodeRegisterOptimizerTest, TemporaryMaterializedForBind) { |
| 110 Initialize(1, 1); |
| 111 Register temp = NewTemporary(); |
| 112 BytecodeNode node(Bytecode::kStar, temp.ToOperand(), OperandScale::kSingle); |
| 113 optimizer()->Write(&node); |
| 114 CHECK_EQ(write_count(), 0); |
| 115 BytecodeLabel label; |
| 116 optimizer()->BindLabel(&label); |
| 117 CHECK_EQ(write_count(), 1); |
| 118 CHECK_EQ(output()->at(0).bytecode(), Bytecode::kStar); |
| 119 CHECK_EQ(output()->at(0).operand(0), temp.ToOperand()); |
| 120 CHECK_EQ(output()->at(0).operand_scale(), OperandScale::kSingle); |
| 121 } |
| 122 |
132 // Basic Register Optimizations | 123 // Basic Register Optimizations |
133 | 124 |
134 TEST_F(BytecodeRegisterOptimizerTest, TemporaryNotEmitted) { | 125 TEST_F(BytecodeRegisterOptimizerTest, TemporaryNotEmitted) { |
135 Initialize(3, 1); | 126 Initialize(3, 1); |
136 Register parameter = Register::FromParameterIndex(1, 3); | 127 Register parameter = Register::FromParameterIndex(1, 3); |
137 BytecodeNode node0(Bytecode::kLdar, parameter.ToOperand(), | 128 BytecodeNode node0(Bytecode::kLdar, parameter.ToOperand(), |
138 OperandScale::kSingle); | 129 OperandScale::kSingle); |
139 optimizer()->Write(&node0); | 130 optimizer()->Write(&node0); |
140 CHECK_EQ(write_count(), 0); | 131 CHECK_EQ(write_count(), 0); |
141 Register temp = NewTemporary(); | 132 Register temp = NewTemporary(); |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 CHECK_EQ(output()->at(3).bytecode(), Bytecode::kCallJSRuntime); | 228 CHECK_EQ(output()->at(3).bytecode(), Bytecode::kCallJSRuntime); |
238 CHECK_EQ(output()->at(3).operand(0), 0); | 229 CHECK_EQ(output()->at(3).operand(0), 0); |
239 CHECK_EQ(output()->at(3).operand(1), temp0.ToOperand()); | 230 CHECK_EQ(output()->at(3).operand(1), temp0.ToOperand()); |
240 CHECK_EQ(output()->at(3).operand(2), 2); | 231 CHECK_EQ(output()->at(3).operand(2), 2); |
241 CHECK_EQ(output()->at(3).operand_scale(), OperandScale::kSingle); | 232 CHECK_EQ(output()->at(3).operand_scale(), OperandScale::kSingle); |
242 } | 233 } |
243 | 234 |
244 } // namespace interpreter | 235 } // namespace interpreter |
245 } // namespace internal | 236 } // namespace internal |
246 } // namespace v8 | 237 } // namespace v8 |
OLD | NEW |