| 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-label.h" |
| 9 #include "src/interpreter/bytecode-register-optimizer.h" | 9 #include "src/interpreter/bytecode-register-optimizer.h" |
| 10 #include "src/objects-inl.h" | 10 #include "src/objects-inl.h" |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 | 59 |
| 60 std::vector<BytecodeNode> output_; | 60 std::vector<BytecodeNode> output_; |
| 61 }; | 61 }; |
| 62 | 62 |
| 63 // Sanity tests. | 63 // Sanity tests. |
| 64 | 64 |
| 65 TEST_F(BytecodeRegisterOptimizerTest, TemporaryMaterializedForFlush) { | 65 TEST_F(BytecodeRegisterOptimizerTest, TemporaryMaterializedForFlush) { |
| 66 Initialize(1, 1); | 66 Initialize(1, 1); |
| 67 Register temp = NewTemporary(); | 67 Register temp = NewTemporary(); |
| 68 optimizer()->DoStar(temp, BytecodeSourceInfo()); | 68 optimizer()->DoStar(temp, BytecodeSourceInfo()); |
| 69 CHECK_EQ(write_count(), 0); | 69 CHECK_EQ(write_count(), 0u); |
| 70 optimizer()->Flush(); | 70 optimizer()->Flush(); |
| 71 CHECK_EQ(write_count(), 1); | 71 CHECK_EQ(write_count(), 1u); |
| 72 CHECK_EQ(output()->at(0).bytecode(), Bytecode::kStar); | 72 CHECK_EQ(output()->at(0).bytecode(), Bytecode::kStar); |
| 73 CHECK_EQ(output()->at(0).operand(0), static_cast<uint32_t>(temp.ToOperand())); | 73 CHECK_EQ(output()->at(0).operand(0), static_cast<uint32_t>(temp.ToOperand())); |
| 74 } | 74 } |
| 75 | 75 |
| 76 TEST_F(BytecodeRegisterOptimizerTest, TemporaryMaterializedForJump) { | 76 TEST_F(BytecodeRegisterOptimizerTest, TemporaryMaterializedForJump) { |
| 77 Initialize(1, 1); | 77 Initialize(1, 1); |
| 78 Register temp = NewTemporary(); | 78 Register temp = NewTemporary(); |
| 79 optimizer()->DoStar(temp, BytecodeSourceInfo()); | 79 optimizer()->DoStar(temp, BytecodeSourceInfo()); |
| 80 CHECK_EQ(write_count(), 0); | 80 CHECK_EQ(write_count(), 0u); |
| 81 optimizer()->PrepareForBytecode(Bytecode::kJump); | 81 optimizer()->PrepareForBytecode(Bytecode::kJump); |
| 82 CHECK_EQ(write_count(), 1); | 82 CHECK_EQ(write_count(), 1u); |
| 83 CHECK_EQ(output()->at(0).bytecode(), Bytecode::kStar); | 83 CHECK_EQ(output()->at(0).bytecode(), Bytecode::kStar); |
| 84 CHECK_EQ(output()->at(0).operand(0), static_cast<uint32_t>(temp.ToOperand())); | 84 CHECK_EQ(output()->at(0).operand(0), static_cast<uint32_t>(temp.ToOperand())); |
| 85 } | 85 } |
| 86 | 86 |
| 87 // Basic Register Optimizations | 87 // Basic Register Optimizations |
| 88 | 88 |
| 89 TEST_F(BytecodeRegisterOptimizerTest, TemporaryNotEmitted) { | 89 TEST_F(BytecodeRegisterOptimizerTest, TemporaryNotEmitted) { |
| 90 Initialize(3, 1); | 90 Initialize(3, 1); |
| 91 Register parameter = Register::FromParameterIndex(1, 3); | 91 Register parameter = Register::FromParameterIndex(1, 3); |
| 92 optimizer()->DoLdar(parameter, BytecodeSourceInfo()); | 92 optimizer()->DoLdar(parameter, BytecodeSourceInfo()); |
| 93 CHECK_EQ(write_count(), 0); | 93 CHECK_EQ(write_count(), 0u); |
| 94 Register temp = NewTemporary(); | 94 Register temp = NewTemporary(); |
| 95 optimizer()->DoStar(temp, BytecodeSourceInfo()); | 95 optimizer()->DoStar(temp, BytecodeSourceInfo()); |
| 96 BytecodeNode node1(Bytecode::kStar, NewTemporary().ToOperand()); | 96 BytecodeNode node1(Bytecode::kStar, NewTemporary().ToOperand()); |
| 97 ReleaseTemporaries(temp); | 97 ReleaseTemporaries(temp); |
| 98 CHECK_EQ(write_count(), 0); | 98 CHECK_EQ(write_count(), 0u); |
| 99 optimizer()->PrepareForBytecode(Bytecode::kReturn); | 99 optimizer()->PrepareForBytecode(Bytecode::kReturn); |
| 100 CHECK_EQ(output()->at(0).bytecode(), Bytecode::kLdar); | 100 CHECK_EQ(output()->at(0).bytecode(), Bytecode::kLdar); |
| 101 CHECK_EQ(output()->at(0).operand(0), | 101 CHECK_EQ(output()->at(0).operand(0), |
| 102 static_cast<uint32_t>(parameter.ToOperand())); | 102 static_cast<uint32_t>(parameter.ToOperand())); |
| 103 } | 103 } |
| 104 | 104 |
| 105 TEST_F(BytecodeRegisterOptimizerTest, ReleasedRegisterUsed) { | 105 TEST_F(BytecodeRegisterOptimizerTest, ReleasedRegisterUsed) { |
| 106 Initialize(3, 1); | 106 Initialize(3, 1); |
| 107 optimizer()->PrepareForBytecode(Bytecode::kLdaSmi); | 107 optimizer()->PrepareForBytecode(Bytecode::kLdaSmi); |
| 108 Register temp0 = NewTemporary(); | 108 Register temp0 = NewTemporary(); |
| 109 Register temp1 = NewTemporary(); | 109 Register temp1 = NewTemporary(); |
| 110 optimizer()->DoStar(temp1, BytecodeSourceInfo()); | 110 optimizer()->DoStar(temp1, BytecodeSourceInfo()); |
| 111 CHECK_EQ(write_count(), 0); | 111 CHECK_EQ(write_count(), 0u); |
| 112 optimizer()->PrepareForBytecode(Bytecode::kLdaSmi); | 112 optimizer()->PrepareForBytecode(Bytecode::kLdaSmi); |
| 113 CHECK_EQ(write_count(), 1); | 113 CHECK_EQ(write_count(), 1u); |
| 114 CHECK_EQ(output()->at(0).bytecode(), Bytecode::kStar); | 114 CHECK_EQ(output()->at(0).bytecode(), Bytecode::kStar); |
| 115 CHECK_EQ(output()->at(0).operand(0), | 115 CHECK_EQ(output()->at(0).operand(0), |
| 116 static_cast<uint32_t>(temp1.ToOperand())); | 116 static_cast<uint32_t>(temp1.ToOperand())); |
| 117 optimizer()->DoMov(temp1, temp0, BytecodeSourceInfo()); | 117 optimizer()->DoMov(temp1, temp0, BytecodeSourceInfo()); |
| 118 CHECK_EQ(write_count(), 1); | 118 CHECK_EQ(write_count(), 1u); |
| 119 ReleaseTemporaries(temp1); | 119 ReleaseTemporaries(temp1); |
| 120 CHECK_EQ(write_count(), 1); | 120 CHECK_EQ(write_count(), 1u); |
| 121 optimizer()->DoLdar(temp0, BytecodeSourceInfo()); | 121 optimizer()->DoLdar(temp0, BytecodeSourceInfo()); |
| 122 CHECK_EQ(write_count(), 1); | 122 CHECK_EQ(write_count(), 1u); |
| 123 optimizer()->PrepareForBytecode(Bytecode::kReturn); | 123 optimizer()->PrepareForBytecode(Bytecode::kReturn); |
| 124 CHECK_EQ(write_count(), 2); | 124 CHECK_EQ(write_count(), 2u); |
| 125 CHECK_EQ(output()->at(1).bytecode(), Bytecode::kLdar); | 125 CHECK_EQ(output()->at(1).bytecode(), Bytecode::kLdar); |
| 126 CHECK_EQ(output()->at(1).operand(0), | 126 CHECK_EQ(output()->at(1).operand(0), |
| 127 static_cast<uint32_t>(temp1.ToOperand())); | 127 static_cast<uint32_t>(temp1.ToOperand())); |
| 128 } | 128 } |
| 129 | 129 |
| 130 TEST_F(BytecodeRegisterOptimizerTest, ReleasedRegisterNotFlushed) { | 130 TEST_F(BytecodeRegisterOptimizerTest, ReleasedRegisterNotFlushed) { |
| 131 Initialize(3, 1); | 131 Initialize(3, 1); |
| 132 optimizer()->PrepareForBytecode(Bytecode::kLdaSmi); | 132 optimizer()->PrepareForBytecode(Bytecode::kLdaSmi); |
| 133 Register temp0 = NewTemporary(); | 133 Register temp0 = NewTemporary(); |
| 134 Register temp1 = NewTemporary(); | 134 Register temp1 = NewTemporary(); |
| 135 optimizer()->DoStar(temp0, BytecodeSourceInfo()); | 135 optimizer()->DoStar(temp0, BytecodeSourceInfo()); |
| 136 CHECK_EQ(write_count(), 0); | 136 CHECK_EQ(write_count(), 0u); |
| 137 optimizer()->DoStar(temp1, BytecodeSourceInfo()); | 137 optimizer()->DoStar(temp1, BytecodeSourceInfo()); |
| 138 CHECK_EQ(write_count(), 0); | 138 CHECK_EQ(write_count(), 0u); |
| 139 ReleaseTemporaries(temp1); | 139 ReleaseTemporaries(temp1); |
| 140 optimizer()->Flush(); | 140 optimizer()->Flush(); |
| 141 CHECK_EQ(write_count(), 1); | 141 CHECK_EQ(write_count(), 1u); |
| 142 CHECK_EQ(output()->at(0).bytecode(), Bytecode::kStar); | 142 CHECK_EQ(output()->at(0).bytecode(), Bytecode::kStar); |
| 143 CHECK_EQ(output()->at(0).operand(0), | 143 CHECK_EQ(output()->at(0).operand(0), |
| 144 static_cast<uint32_t>(temp0.ToOperand())); | 144 static_cast<uint32_t>(temp0.ToOperand())); |
| 145 } | 145 } |
| 146 | 146 |
| 147 TEST_F(BytecodeRegisterOptimizerTest, StoresToLocalsImmediate) { | 147 TEST_F(BytecodeRegisterOptimizerTest, StoresToLocalsImmediate) { |
| 148 Initialize(3, 1); | 148 Initialize(3, 1); |
| 149 Register parameter = Register::FromParameterIndex(1, 3); | 149 Register parameter = Register::FromParameterIndex(1, 3); |
| 150 optimizer()->DoLdar(parameter, BytecodeSourceInfo()); | 150 optimizer()->DoLdar(parameter, BytecodeSourceInfo()); |
| 151 CHECK_EQ(write_count(), 0); | 151 CHECK_EQ(write_count(), 0u); |
| 152 Register local = Register(0); | 152 Register local = Register(0); |
| 153 optimizer()->DoStar(local, BytecodeSourceInfo()); | 153 optimizer()->DoStar(local, BytecodeSourceInfo()); |
| 154 CHECK_EQ(write_count(), 1); | 154 CHECK_EQ(write_count(), 1u); |
| 155 CHECK_EQ(output()->at(0).bytecode(), Bytecode::kMov); | 155 CHECK_EQ(output()->at(0).bytecode(), Bytecode::kMov); |
| 156 CHECK_EQ(output()->at(0).operand(0), | 156 CHECK_EQ(output()->at(0).operand(0), |
| 157 static_cast<uint32_t>(parameter.ToOperand())); | 157 static_cast<uint32_t>(parameter.ToOperand())); |
| 158 CHECK_EQ(output()->at(0).operand(1), | 158 CHECK_EQ(output()->at(0).operand(1), |
| 159 static_cast<uint32_t>(local.ToOperand())); | 159 static_cast<uint32_t>(local.ToOperand())); |
| 160 | 160 |
| 161 optimizer()->PrepareForBytecode(Bytecode::kReturn); | 161 optimizer()->PrepareForBytecode(Bytecode::kReturn); |
| 162 CHECK_EQ(write_count(), 2); | 162 CHECK_EQ(write_count(), 2u); |
| 163 CHECK_EQ(output()->at(1).bytecode(), Bytecode::kLdar); | 163 CHECK_EQ(output()->at(1).bytecode(), Bytecode::kLdar); |
| 164 CHECK_EQ(output()->at(1).operand(0), | 164 CHECK_EQ(output()->at(1).operand(0), |
| 165 static_cast<uint32_t>(local.ToOperand())); | 165 static_cast<uint32_t>(local.ToOperand())); |
| 166 } | 166 } |
| 167 | 167 |
| 168 TEST_F(BytecodeRegisterOptimizerTest, SingleTemporaryNotMaterializedForInput) { | 168 TEST_F(BytecodeRegisterOptimizerTest, SingleTemporaryNotMaterializedForInput) { |
| 169 Initialize(3, 1); | 169 Initialize(3, 1); |
| 170 Register parameter = Register::FromParameterIndex(1, 3); | 170 Register parameter = Register::FromParameterIndex(1, 3); |
| 171 Register temp0 = NewTemporary(); | 171 Register temp0 = NewTemporary(); |
| 172 Register temp1 = NewTemporary(); | 172 Register temp1 = NewTemporary(); |
| 173 optimizer()->DoMov(parameter, temp0, BytecodeSourceInfo()); | 173 optimizer()->DoMov(parameter, temp0, BytecodeSourceInfo()); |
| 174 optimizer()->DoMov(parameter, temp1, BytecodeSourceInfo()); | 174 optimizer()->DoMov(parameter, temp1, BytecodeSourceInfo()); |
| 175 CHECK_EQ(write_count(), 0); | 175 CHECK_EQ(write_count(), 0u); |
| 176 | 176 |
| 177 Register reg = optimizer()->GetInputRegister(temp0); | 177 Register reg = optimizer()->GetInputRegister(temp0); |
| 178 RegisterList reg_list = | 178 RegisterList reg_list = |
| 179 optimizer()->GetInputRegisterList(RegisterList(temp0.index(), 1)); | 179 optimizer()->GetInputRegisterList(RegisterList(temp0.index(), 1)); |
| 180 CHECK_EQ(write_count(), 0); | 180 CHECK_EQ(write_count(), 0u); |
| 181 CHECK_EQ(parameter.index(), reg.index()); | 181 CHECK_EQ(parameter.index(), reg.index()); |
| 182 CHECK_EQ(parameter.index(), reg_list.first_register().index()); | 182 CHECK_EQ(parameter.index(), reg_list.first_register().index()); |
| 183 CHECK_EQ(1, reg_list.register_count()); | 183 CHECK_EQ(1, reg_list.register_count()); |
| 184 } | 184 } |
| 185 | 185 |
| 186 TEST_F(BytecodeRegisterOptimizerTest, RangeOfTemporariesMaterializedForInput) { | 186 TEST_F(BytecodeRegisterOptimizerTest, RangeOfTemporariesMaterializedForInput) { |
| 187 Initialize(3, 1); | 187 Initialize(3, 1); |
| 188 Register parameter = Register::FromParameterIndex(1, 3); | 188 Register parameter = Register::FromParameterIndex(1, 3); |
| 189 Register temp0 = NewTemporary(); | 189 Register temp0 = NewTemporary(); |
| 190 Register temp1 = NewTemporary(); | 190 Register temp1 = NewTemporary(); |
| 191 optimizer()->PrepareForBytecode(Bytecode::kLdaSmi); | 191 optimizer()->PrepareForBytecode(Bytecode::kLdaSmi); |
| 192 optimizer()->DoStar(temp0, BytecodeSourceInfo()); | 192 optimizer()->DoStar(temp0, BytecodeSourceInfo()); |
| 193 optimizer()->DoMov(parameter, temp1, BytecodeSourceInfo()); | 193 optimizer()->DoMov(parameter, temp1, BytecodeSourceInfo()); |
| 194 CHECK_EQ(write_count(), 0); | 194 CHECK_EQ(write_count(), 0u); |
| 195 | 195 |
| 196 optimizer()->PrepareForBytecode(Bytecode::kCallJSRuntime); | 196 optimizer()->PrepareForBytecode(Bytecode::kCallJSRuntime); |
| 197 RegisterList reg_list = | 197 RegisterList reg_list = |
| 198 optimizer()->GetInputRegisterList(RegisterList(temp0.index(), 2)); | 198 optimizer()->GetInputRegisterList(RegisterList(temp0.index(), 2)); |
| 199 CHECK_EQ(temp0.index(), reg_list.first_register().index()); | 199 CHECK_EQ(temp0.index(), reg_list.first_register().index()); |
| 200 CHECK_EQ(2, reg_list.register_count()); | 200 CHECK_EQ(2, reg_list.register_count()); |
| 201 CHECK_EQ(write_count(), 2); | 201 CHECK_EQ(write_count(), 2u); |
| 202 CHECK_EQ(output()->at(0).bytecode(), Bytecode::kStar); | 202 CHECK_EQ(output()->at(0).bytecode(), Bytecode::kStar); |
| 203 CHECK_EQ(output()->at(0).operand(0), | 203 CHECK_EQ(output()->at(0).operand(0), |
| 204 static_cast<uint32_t>(temp0.ToOperand())); | 204 static_cast<uint32_t>(temp0.ToOperand())); |
| 205 CHECK_EQ(output()->at(1).bytecode(), Bytecode::kMov); | 205 CHECK_EQ(output()->at(1).bytecode(), Bytecode::kMov); |
| 206 CHECK_EQ(output()->at(1).operand(0), | 206 CHECK_EQ(output()->at(1).operand(0), |
| 207 static_cast<uint32_t>(parameter.ToOperand())); | 207 static_cast<uint32_t>(parameter.ToOperand())); |
| 208 CHECK_EQ(output()->at(1).operand(1), | 208 CHECK_EQ(output()->at(1).operand(1), |
| 209 static_cast<uint32_t>(temp1.ToOperand())); | 209 static_cast<uint32_t>(temp1.ToOperand())); |
| 210 } | 210 } |
| 211 | 211 |
| 212 } // namespace interpreter | 212 } // namespace interpreter |
| 213 } // namespace internal | 213 } // namespace internal |
| 214 } // namespace v8 | 214 } // namespace v8 |
| OLD | NEW |