2016-05-12 19:18:07 +00:00
|
|
|
// Copyright 2016 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/v8.h"
|
|
|
|
|
|
|
|
#include "src/factory.h"
|
2016-06-03 14:52:59 +00:00
|
|
|
#include "src/interpreter/bytecode-label.h"
|
2016-05-12 19:18:07 +00:00
|
|
|
#include "src/interpreter/bytecode-peephole-optimizer.h"
|
|
|
|
#include "src/objects-inl.h"
|
|
|
|
#include "src/objects.h"
|
|
|
|
#include "test/unittests/test-utils.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace interpreter {
|
|
|
|
|
|
|
|
class BytecodePeepholeOptimizerTest : public BytecodePipelineStage,
|
|
|
|
public TestWithIsolateAndZone {
|
|
|
|
public:
|
2016-09-22 16:34:16 +00:00
|
|
|
BytecodePeepholeOptimizerTest()
|
2016-12-15 07:55:10 +00:00
|
|
|
: peephole_optimizer_(this),
|
|
|
|
last_written_(BytecodeNode::Illegal(BytecodeSourceInfo())) {}
|
2016-05-12 19:18:07 +00:00
|
|
|
~BytecodePeepholeOptimizerTest() override {}
|
|
|
|
|
2016-07-05 13:44:05 +00:00
|
|
|
void Reset() {
|
2016-12-15 07:55:10 +00:00
|
|
|
last_written_ = BytecodeNode::Illegal(BytecodeSourceInfo());
|
2016-07-05 13:44:05 +00:00
|
|
|
write_count_ = 0;
|
|
|
|
}
|
|
|
|
|
2016-05-12 19:18:07 +00:00
|
|
|
void Write(BytecodeNode* node) override {
|
|
|
|
write_count_++;
|
2016-10-24 20:47:41 +00:00
|
|
|
last_written_ = *node;
|
2016-05-12 19:18:07 +00:00
|
|
|
}
|
|
|
|
|
2016-06-03 14:52:59 +00:00
|
|
|
void WriteJump(BytecodeNode* node, BytecodeLabel* label) override {
|
|
|
|
write_count_++;
|
2016-10-24 20:47:41 +00:00
|
|
|
last_written_ = *node;
|
2016-06-03 14:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BindLabel(BytecodeLabel* label) override {}
|
|
|
|
void BindLabel(const BytecodeLabel& target, BytecodeLabel* label) override {}
|
|
|
|
Handle<BytecodeArray> ToBytecodeArray(
|
2016-08-18 13:42:05 +00:00
|
|
|
Isolate* isolate, int fixed_register_count, int parameter_count,
|
2016-06-03 14:52:59 +00:00
|
|
|
Handle<FixedArray> handle_table) override {
|
|
|
|
return Handle<BytecodeArray>();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Flush() {
|
2016-08-18 13:42:05 +00:00
|
|
|
optimizer()->ToBytecodeArray(isolate(), 0, 0,
|
|
|
|
factory()->empty_fixed_array());
|
2016-06-03 14:52:59 +00:00
|
|
|
}
|
|
|
|
|
2016-05-12 19:18:07 +00:00
|
|
|
BytecodePeepholeOptimizer* optimizer() { return &peephole_optimizer_; }
|
|
|
|
|
|
|
|
int write_count() const { return write_count_; }
|
|
|
|
const BytecodeNode& last_written() const { return last_written_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
BytecodePeepholeOptimizer peephole_optimizer_;
|
|
|
|
|
|
|
|
int write_count_ = 0;
|
|
|
|
BytecodeNode last_written_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Sanity tests.
|
|
|
|
|
2016-06-03 14:52:59 +00:00
|
|
|
TEST_F(BytecodePeepholeOptimizerTest, FlushOnJump) {
|
2016-05-12 19:18:07 +00:00
|
|
|
CHECK_EQ(write_count(), 0);
|
|
|
|
|
2016-08-08 01:15:22 +00:00
|
|
|
BytecodeNode add(Bytecode::kAdd, Register(0).ToOperand(), 1);
|
2016-06-03 14:52:59 +00:00
|
|
|
optimizer()->Write(&add);
|
2016-05-12 19:18:07 +00:00
|
|
|
CHECK_EQ(write_count(), 0);
|
2016-06-03 14:52:59 +00:00
|
|
|
|
|
|
|
BytecodeLabel target;
|
2016-10-24 20:47:41 +00:00
|
|
|
BytecodeNode jump(Bytecode::kJump, 0);
|
2016-06-03 14:52:59 +00:00
|
|
|
optimizer()->WriteJump(&jump, &target);
|
|
|
|
CHECK_EQ(write_count(), 2);
|
|
|
|
CHECK_EQ(jump, last_written());
|
2016-05-12 19:18:07 +00:00
|
|
|
}
|
|
|
|
|
2016-06-03 14:52:59 +00:00
|
|
|
TEST_F(BytecodePeepholeOptimizerTest, FlushOnBind) {
|
2016-05-12 19:18:07 +00:00
|
|
|
CHECK_EQ(write_count(), 0);
|
|
|
|
|
2016-08-08 01:15:22 +00:00
|
|
|
BytecodeNode add(Bytecode::kAdd, Register(0).ToOperand(), 1);
|
2016-06-03 14:52:59 +00:00
|
|
|
optimizer()->Write(&add);
|
2016-05-12 19:18:07 +00:00
|
|
|
CHECK_EQ(write_count(), 0);
|
2016-06-03 14:52:59 +00:00
|
|
|
|
|
|
|
BytecodeLabel target;
|
|
|
|
optimizer()->BindLabel(&target);
|
|
|
|
CHECK_EQ(write_count(), 1);
|
|
|
|
CHECK_EQ(add, last_written());
|
2016-05-12 19:18:07 +00:00
|
|
|
}
|
|
|
|
|
2016-06-03 14:52:59 +00:00
|
|
|
// Nop elimination tests.
|
|
|
|
|
|
|
|
TEST_F(BytecodePeepholeOptimizerTest, ElideEmptyNop) {
|
|
|
|
BytecodeNode nop(Bytecode::kNop);
|
|
|
|
optimizer()->Write(&nop);
|
2016-08-08 01:15:22 +00:00
|
|
|
BytecodeNode add(Bytecode::kAdd, Register(0).ToOperand(), 1);
|
2016-06-03 14:52:59 +00:00
|
|
|
optimizer()->Write(&add);
|
|
|
|
Flush();
|
|
|
|
CHECK_EQ(write_count(), 1);
|
|
|
|
CHECK_EQ(add, last_written());
|
2016-05-12 19:18:07 +00:00
|
|
|
}
|
|
|
|
|
2016-06-03 14:52:59 +00:00
|
|
|
TEST_F(BytecodePeepholeOptimizerTest, ElideExpressionNop) {
|
2016-09-22 16:34:16 +00:00
|
|
|
BytecodeSourceInfo source_info(3, false);
|
2016-10-24 20:47:41 +00:00
|
|
|
BytecodeNode nop(Bytecode::kNop, source_info);
|
2016-06-03 14:52:59 +00:00
|
|
|
optimizer()->Write(&nop);
|
2016-08-08 01:15:22 +00:00
|
|
|
BytecodeNode add(Bytecode::kAdd, Register(0).ToOperand(), 1);
|
2016-06-03 14:52:59 +00:00
|
|
|
optimizer()->Write(&add);
|
|
|
|
Flush();
|
2016-05-12 19:18:07 +00:00
|
|
|
CHECK_EQ(write_count(), 1);
|
2016-06-03 14:52:59 +00:00
|
|
|
CHECK_EQ(add, last_written());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BytecodePeepholeOptimizerTest, KeepStatementNop) {
|
2016-10-24 20:47:41 +00:00
|
|
|
BytecodeSourceInfo source_info_statement(3, true);
|
|
|
|
BytecodeNode nop(Bytecode::kNop, source_info_statement);
|
2016-06-03 14:52:59 +00:00
|
|
|
optimizer()->Write(&nop);
|
2016-10-24 20:47:41 +00:00
|
|
|
BytecodeSourceInfo source_info_expression(3, false);
|
|
|
|
BytecodeNode add(Bytecode::kAdd, Register(0).ToOperand(), 1,
|
|
|
|
source_info_expression);
|
2016-06-03 14:52:59 +00:00
|
|
|
optimizer()->Write(&add);
|
|
|
|
Flush();
|
|
|
|
CHECK_EQ(write_count(), 2);
|
|
|
|
CHECK_EQ(add, last_written());
|
2016-05-12 19:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests covering BytecodePeepholeOptimizer::UpdateCurrentBytecode().
|
|
|
|
|
|
|
|
TEST_F(BytecodePeepholeOptimizerTest, KeepJumpIfToBooleanTrue) {
|
|
|
|
BytecodeNode first(Bytecode::kLdaNull);
|
2016-06-10 10:34:50 +00:00
|
|
|
BytecodeNode second(Bytecode::kJumpIfToBooleanTrue, 3);
|
2016-07-19 11:54:21 +00:00
|
|
|
BytecodeLabel label;
|
2016-05-12 19:18:07 +00:00
|
|
|
optimizer()->Write(&first);
|
|
|
|
CHECK_EQ(write_count(), 0);
|
2016-07-19 11:54:21 +00:00
|
|
|
optimizer()->WriteJump(&second, &label);
|
2016-05-12 19:18:07 +00:00
|
|
|
CHECK_EQ(write_count(), 2);
|
|
|
|
CHECK_EQ(last_written(), second);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BytecodePeepholeOptimizerTest, ElideJumpIfToBooleanTrue) {
|
|
|
|
BytecodeNode first(Bytecode::kLdaTrue);
|
2016-06-10 10:34:50 +00:00
|
|
|
BytecodeNode second(Bytecode::kJumpIfToBooleanTrue, 3);
|
2016-07-19 11:54:21 +00:00
|
|
|
BytecodeLabel label;
|
2016-05-12 19:18:07 +00:00
|
|
|
optimizer()->Write(&first);
|
|
|
|
CHECK_EQ(write_count(), 0);
|
2016-07-19 11:54:21 +00:00
|
|
|
optimizer()->WriteJump(&second, &label);
|
2016-05-12 19:18:07 +00:00
|
|
|
CHECK_EQ(write_count(), 2);
|
2016-07-19 11:54:21 +00:00
|
|
|
CHECK_EQ(last_written(), second);
|
2016-05-12 19:18:07 +00:00
|
|
|
}
|
|
|
|
|
2016-05-17 20:39:45 +00:00
|
|
|
TEST_F(BytecodePeepholeOptimizerTest, KeepToBooleanLogicalNot) {
|
|
|
|
BytecodeNode first(Bytecode::kLdaNull);
|
|
|
|
BytecodeNode second(Bytecode::kToBooleanLogicalNot);
|
|
|
|
optimizer()->Write(&first);
|
|
|
|
CHECK_EQ(write_count(), 0);
|
|
|
|
optimizer()->Write(&second);
|
|
|
|
CHECK_EQ(write_count(), 1);
|
|
|
|
CHECK_EQ(last_written(), first);
|
2016-06-03 14:52:59 +00:00
|
|
|
Flush();
|
2016-05-17 20:39:45 +00:00
|
|
|
CHECK_EQ(write_count(), 2);
|
|
|
|
CHECK_EQ(last_written(), second);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BytecodePeepholeOptimizerTest, ElideToBooleanLogicalNot) {
|
|
|
|
BytecodeNode first(Bytecode::kLdaTrue);
|
|
|
|
BytecodeNode second(Bytecode::kToBooleanLogicalNot);
|
|
|
|
optimizer()->Write(&first);
|
|
|
|
CHECK_EQ(write_count(), 0);
|
|
|
|
optimizer()->Write(&second);
|
|
|
|
CHECK_EQ(write_count(), 1);
|
|
|
|
CHECK_EQ(last_written(), first);
|
2016-06-03 14:52:59 +00:00
|
|
|
Flush();
|
2016-05-17 20:39:45 +00:00
|
|
|
CHECK_EQ(write_count(), 2);
|
|
|
|
CHECK_EQ(last_written().bytecode(), Bytecode::kLogicalNot);
|
|
|
|
}
|
|
|
|
|
2016-05-12 19:18:07 +00:00
|
|
|
// Tests covering BytecodePeepholeOptimizer::CanElideCurrent().
|
|
|
|
|
2016-06-03 14:52:59 +00:00
|
|
|
TEST_F(BytecodePeepholeOptimizerTest, StarRxLdarRy) {
|
2016-06-10 10:34:50 +00:00
|
|
|
BytecodeNode first(Bytecode::kStar, Register(0).ToOperand());
|
|
|
|
BytecodeNode second(Bytecode::kLdar, Register(1).ToOperand());
|
2016-05-12 19:18:07 +00:00
|
|
|
optimizer()->Write(&first);
|
|
|
|
CHECK_EQ(write_count(), 0);
|
|
|
|
optimizer()->Write(&second);
|
|
|
|
CHECK_EQ(write_count(), 1);
|
|
|
|
CHECK_EQ(last_written(), first);
|
2016-06-03 14:52:59 +00:00
|
|
|
Flush();
|
2016-05-12 19:18:07 +00:00
|
|
|
CHECK_EQ(write_count(), 2);
|
|
|
|
CHECK_EQ(last_written(), second);
|
|
|
|
}
|
|
|
|
|
2016-06-03 14:52:59 +00:00
|
|
|
TEST_F(BytecodePeepholeOptimizerTest, StarRxLdarRx) {
|
|
|
|
BytecodeLabel label;
|
2016-06-10 10:34:50 +00:00
|
|
|
BytecodeNode first(Bytecode::kStar, Register(0).ToOperand());
|
|
|
|
BytecodeNode second(Bytecode::kLdar, Register(0).ToOperand());
|
2016-05-12 19:18:07 +00:00
|
|
|
optimizer()->Write(&first);
|
2016-07-18 08:51:47 +00:00
|
|
|
optimizer()->Write(&second);
|
2016-07-19 11:54:21 +00:00
|
|
|
CHECK_EQ(write_count(), 0);
|
2016-07-18 08:51:47 +00:00
|
|
|
Flush();
|
|
|
|
CHECK_EQ(write_count(), 1);
|
2016-07-19 11:54:21 +00:00
|
|
|
CHECK_EQ(last_written(), first);
|
2016-05-12 19:18:07 +00:00
|
|
|
}
|
|
|
|
|
2016-06-03 14:52:59 +00:00
|
|
|
TEST_F(BytecodePeepholeOptimizerTest, StarRxLdarRxStatement) {
|
2016-06-10 10:34:50 +00:00
|
|
|
BytecodeNode first(Bytecode::kStar, Register(0).ToOperand());
|
2016-09-22 16:34:16 +00:00
|
|
|
BytecodeSourceInfo source_info(3, true);
|
2016-10-24 20:47:41 +00:00
|
|
|
BytecodeNode second(Bytecode::kLdar, Register(0).ToOperand(), source_info);
|
2016-05-12 19:18:07 +00:00
|
|
|
optimizer()->Write(&first);
|
|
|
|
CHECK_EQ(write_count(), 0);
|
|
|
|
optimizer()->Write(&second);
|
|
|
|
CHECK_EQ(write_count(), 1);
|
|
|
|
CHECK_EQ(last_written(), first);
|
2016-06-03 14:52:59 +00:00
|
|
|
Flush();
|
2016-05-12 19:18:07 +00:00
|
|
|
CHECK_EQ(write_count(), 2);
|
|
|
|
CHECK_EQ(last_written().bytecode(), Bytecode::kNop);
|
2016-10-24 20:47:41 +00:00
|
|
|
CHECK_EQ(last_written().source_info(), source_info);
|
2016-05-12 19:18:07 +00:00
|
|
|
}
|
|
|
|
|
2016-06-03 14:52:59 +00:00
|
|
|
TEST_F(BytecodePeepholeOptimizerTest, StarRxLdarRxStatementStarRy) {
|
|
|
|
BytecodeLabel label;
|
2016-06-10 10:34:50 +00:00
|
|
|
BytecodeNode first(Bytecode::kStar, Register(0).ToOperand());
|
2016-09-22 16:34:16 +00:00
|
|
|
BytecodeSourceInfo source_info(0, true);
|
2016-10-24 20:47:41 +00:00
|
|
|
BytecodeNode second(Bytecode::kLdar, Register(0).ToOperand(), source_info);
|
2016-06-10 10:34:50 +00:00
|
|
|
BytecodeNode third(Bytecode::kStar, Register(3).ToOperand());
|
2016-05-12 19:18:07 +00:00
|
|
|
optimizer()->Write(&first);
|
|
|
|
CHECK_EQ(write_count(), 0);
|
|
|
|
optimizer()->Write(&second);
|
|
|
|
CHECK_EQ(write_count(), 1);
|
|
|
|
CHECK_EQ(last_written(), first);
|
|
|
|
optimizer()->Write(&third);
|
|
|
|
CHECK_EQ(write_count(), 1);
|
2016-06-03 14:52:59 +00:00
|
|
|
Flush();
|
2016-05-12 19:18:07 +00:00
|
|
|
CHECK_EQ(write_count(), 2);
|
|
|
|
CHECK_EQ(last_written(), third);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BytecodePeepholeOptimizerTest, LdarToName) {
|
2016-06-10 10:34:50 +00:00
|
|
|
BytecodeNode first(Bytecode::kLdar, Register(0).ToOperand());
|
2016-07-25 12:51:39 +00:00
|
|
|
BytecodeNode second(Bytecode::kToName, Register(0).ToOperand());
|
2016-05-12 19:18:07 +00:00
|
|
|
optimizer()->Write(&first);
|
|
|
|
CHECK_EQ(write_count(), 0);
|
|
|
|
optimizer()->Write(&second);
|
|
|
|
CHECK_EQ(write_count(), 1);
|
|
|
|
CHECK_EQ(last_written(), first);
|
2016-06-03 14:52:59 +00:00
|
|
|
Flush();
|
2016-05-12 19:18:07 +00:00
|
|
|
CHECK_EQ(write_count(), 2);
|
|
|
|
CHECK_EQ(last_written(), second);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BytecodePeepholeOptimizerTest, TypeOfToName) {
|
|
|
|
BytecodeNode first(Bytecode::kTypeOf);
|
2016-07-25 12:51:39 +00:00
|
|
|
BytecodeNode second(Bytecode::kToName, Register(0).ToOperand());
|
2016-05-12 19:18:07 +00:00
|
|
|
optimizer()->Write(&first);
|
2016-07-19 11:54:21 +00:00
|
|
|
CHECK_EQ(write_count(), 0);
|
2016-07-25 12:51:39 +00:00
|
|
|
optimizer()->Write(&second);
|
2016-07-18 08:51:47 +00:00
|
|
|
CHECK_EQ(write_count(), 1);
|
2016-07-19 11:54:21 +00:00
|
|
|
CHECK_EQ(last_written(), first);
|
2016-07-25 12:51:39 +00:00
|
|
|
Flush();
|
|
|
|
CHECK_EQ(write_count(), 2);
|
|
|
|
CHECK_EQ(last_written(), second);
|
|
|
|
CHECK_EQ(last_written().bytecode(), Bytecode::kStar);
|
2016-05-12 19:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests covering BytecodePeepholeOptimizer::CanElideLast().
|
|
|
|
|
|
|
|
TEST_F(BytecodePeepholeOptimizerTest, LdaTrueLdaFalse) {
|
|
|
|
BytecodeNode first(Bytecode::kLdaTrue);
|
|
|
|
BytecodeNode second(Bytecode::kLdaFalse);
|
|
|
|
optimizer()->Write(&first);
|
|
|
|
CHECK_EQ(write_count(), 0);
|
|
|
|
optimizer()->Write(&second);
|
|
|
|
CHECK_EQ(write_count(), 0);
|
2016-06-03 14:52:59 +00:00
|
|
|
Flush();
|
2016-05-12 19:18:07 +00:00
|
|
|
CHECK_EQ(write_count(), 1);
|
|
|
|
CHECK_EQ(last_written(), second);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BytecodePeepholeOptimizerTest, LdaTrueStatementLdaFalse) {
|
2016-09-22 16:34:16 +00:00
|
|
|
BytecodeSourceInfo source_info(3, true);
|
2016-10-24 20:47:41 +00:00
|
|
|
BytecodeNode first(Bytecode::kLdaTrue, source_info);
|
2016-05-12 19:18:07 +00:00
|
|
|
BytecodeNode second(Bytecode::kLdaFalse);
|
|
|
|
optimizer()->Write(&first);
|
|
|
|
CHECK_EQ(write_count(), 0);
|
|
|
|
optimizer()->Write(&second);
|
|
|
|
CHECK_EQ(write_count(), 0);
|
2016-06-03 14:52:59 +00:00
|
|
|
Flush();
|
2016-05-12 19:18:07 +00:00
|
|
|
CHECK_EQ(write_count(), 1);
|
|
|
|
CHECK_EQ(last_written(), second);
|
2016-09-22 16:34:16 +00:00
|
|
|
CHECK(second.source_info().is_statement());
|
2016-06-21 11:11:15 +00:00
|
|
|
CHECK_EQ(second.source_info().source_position(), 3);
|
2016-05-12 19:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BytecodePeepholeOptimizerTest, NopStackCheck) {
|
|
|
|
BytecodeNode first(Bytecode::kNop);
|
2016-10-24 20:47:41 +00:00
|
|
|
BytecodeNode second(Bytecode::kStackCheck);
|
2016-05-12 19:18:07 +00:00
|
|
|
optimizer()->Write(&first);
|
|
|
|
CHECK_EQ(write_count(), 0);
|
|
|
|
optimizer()->Write(&second);
|
|
|
|
CHECK_EQ(write_count(), 0);
|
2016-06-03 14:52:59 +00:00
|
|
|
Flush();
|
2016-05-12 19:18:07 +00:00
|
|
|
CHECK_EQ(write_count(), 1);
|
|
|
|
CHECK_EQ(last_written(), second);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BytecodePeepholeOptimizerTest, NopStatementStackCheck) {
|
2016-09-22 16:34:16 +00:00
|
|
|
BytecodeSourceInfo source_info(3, true);
|
2016-10-24 20:47:41 +00:00
|
|
|
BytecodeNode first(Bytecode::kNop, source_info);
|
2016-05-12 19:18:07 +00:00
|
|
|
BytecodeNode second(Bytecode::kStackCheck);
|
|
|
|
optimizer()->Write(&first);
|
|
|
|
CHECK_EQ(write_count(), 0);
|
|
|
|
optimizer()->Write(&second);
|
|
|
|
CHECK_EQ(write_count(), 0);
|
2016-06-03 14:52:59 +00:00
|
|
|
Flush();
|
2016-05-12 19:18:07 +00:00
|
|
|
CHECK_EQ(write_count(), 1);
|
2016-10-24 20:47:41 +00:00
|
|
|
BytecodeNode expected(Bytecode::kStackCheck, source_info);
|
2016-09-22 16:34:16 +00:00
|
|
|
CHECK_EQ(last_written(), expected);
|
2016-05-12 19:18:07 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 09:55:26 +00:00
|
|
|
// Tests covering BytecodePeepholeOptimizer::UpdateLastAndCurrentBytecodes().
|
|
|
|
|
2016-07-05 13:44:05 +00:00
|
|
|
TEST_F(BytecodePeepholeOptimizerTest, MergeLdaSmiWithBinaryOp) {
|
|
|
|
Bytecode operator_replacement_pairs[][2] = {
|
|
|
|
{Bytecode::kAdd, Bytecode::kAddSmi},
|
|
|
|
{Bytecode::kSub, Bytecode::kSubSmi},
|
|
|
|
{Bytecode::kBitwiseAnd, Bytecode::kBitwiseAndSmi},
|
|
|
|
{Bytecode::kBitwiseOr, Bytecode::kBitwiseOrSmi},
|
|
|
|
{Bytecode::kShiftLeft, Bytecode::kShiftLeftSmi},
|
|
|
|
{Bytecode::kShiftRight, Bytecode::kShiftRightSmi}};
|
|
|
|
|
|
|
|
for (auto operator_replacement : operator_replacement_pairs) {
|
|
|
|
uint32_t imm_operand = 17;
|
2016-09-22 16:34:16 +00:00
|
|
|
BytecodeSourceInfo source_info(3, true);
|
2016-10-24 20:47:41 +00:00
|
|
|
BytecodeNode first(Bytecode::kLdaSmi, imm_operand, source_info);
|
2016-07-05 13:44:05 +00:00
|
|
|
uint32_t reg_operand = Register(0).ToOperand();
|
2016-08-23 14:59:10 +00:00
|
|
|
uint32_t idx_operand = 1;
|
|
|
|
BytecodeNode second(operator_replacement[0], reg_operand, idx_operand);
|
2016-07-05 13:44:05 +00:00
|
|
|
optimizer()->Write(&first);
|
|
|
|
optimizer()->Write(&second);
|
|
|
|
Flush();
|
|
|
|
CHECK_EQ(write_count(), 1);
|
|
|
|
CHECK_EQ(last_written().bytecode(), operator_replacement[1]);
|
2016-08-23 14:59:10 +00:00
|
|
|
CHECK_EQ(last_written().operand_count(), 3);
|
2016-07-05 13:44:05 +00:00
|
|
|
CHECK_EQ(last_written().operand(0), imm_operand);
|
|
|
|
CHECK_EQ(last_written().operand(1), reg_operand);
|
2016-08-23 14:59:10 +00:00
|
|
|
CHECK_EQ(last_written().operand(2), idx_operand);
|
2016-10-24 20:47:41 +00:00
|
|
|
CHECK_EQ(last_written().source_info(), source_info);
|
2016-07-05 13:44:05 +00:00
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BytecodePeepholeOptimizerTest, NotMergingLdaSmiWithBinaryOp) {
|
|
|
|
Bytecode operator_replacement_pairs[][2] = {
|
|
|
|
{Bytecode::kAdd, Bytecode::kAddSmi},
|
|
|
|
{Bytecode::kSub, Bytecode::kSubSmi},
|
|
|
|
{Bytecode::kBitwiseAnd, Bytecode::kBitwiseAndSmi},
|
|
|
|
{Bytecode::kBitwiseOr, Bytecode::kBitwiseOrSmi},
|
|
|
|
{Bytecode::kShiftLeft, Bytecode::kShiftLeftSmi},
|
|
|
|
{Bytecode::kShiftRight, Bytecode::kShiftRightSmi}};
|
|
|
|
|
|
|
|
for (auto operator_replacement : operator_replacement_pairs) {
|
|
|
|
uint32_t imm_operand = 17;
|
2016-09-22 16:34:16 +00:00
|
|
|
BytecodeSourceInfo source_info(3, true);
|
2016-10-24 20:47:41 +00:00
|
|
|
BytecodeNode first(Bytecode::kLdaSmi, imm_operand, source_info);
|
2016-07-05 13:44:05 +00:00
|
|
|
uint32_t reg_operand = Register(0).ToOperand();
|
2016-09-22 16:34:16 +00:00
|
|
|
source_info.MakeStatementPosition(4);
|
2016-10-24 20:47:41 +00:00
|
|
|
BytecodeNode second(operator_replacement[0], reg_operand, 1, source_info);
|
2016-07-05 13:44:05 +00:00
|
|
|
optimizer()->Write(&first);
|
|
|
|
optimizer()->Write(&second);
|
|
|
|
CHECK_EQ(last_written(), first);
|
|
|
|
Flush();
|
|
|
|
CHECK_EQ(last_written(), second);
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(BytecodePeepholeOptimizerTest, MergeLdaZeroWithBinaryOp) {
|
|
|
|
Bytecode operator_replacement_pairs[][2] = {
|
|
|
|
{Bytecode::kAdd, Bytecode::kAddSmi},
|
|
|
|
{Bytecode::kSub, Bytecode::kSubSmi},
|
|
|
|
{Bytecode::kBitwiseAnd, Bytecode::kBitwiseAndSmi},
|
|
|
|
{Bytecode::kBitwiseOr, Bytecode::kBitwiseOrSmi},
|
|
|
|
{Bytecode::kShiftLeft, Bytecode::kShiftLeftSmi},
|
|
|
|
{Bytecode::kShiftRight, Bytecode::kShiftRightSmi}};
|
|
|
|
|
|
|
|
for (auto operator_replacement : operator_replacement_pairs) {
|
|
|
|
BytecodeNode first(Bytecode::kLdaZero);
|
|
|
|
uint32_t reg_operand = Register(0).ToOperand();
|
2016-08-23 14:59:10 +00:00
|
|
|
uint32_t idx_operand = 1;
|
|
|
|
BytecodeNode second(operator_replacement[0], reg_operand, idx_operand);
|
2016-07-05 13:44:05 +00:00
|
|
|
optimizer()->Write(&first);
|
|
|
|
optimizer()->Write(&second);
|
|
|
|
Flush();
|
|
|
|
CHECK_EQ(write_count(), 1);
|
|
|
|
CHECK_EQ(last_written().bytecode(), operator_replacement[1]);
|
2016-08-23 14:59:10 +00:00
|
|
|
CHECK_EQ(last_written().operand_count(), 3);
|
2016-11-11 12:12:31 +00:00
|
|
|
CHECK_EQ(last_written().operand(0), 0u);
|
2016-07-05 13:44:05 +00:00
|
|
|
CHECK_EQ(last_written().operand(1), reg_operand);
|
2016-08-23 14:59:10 +00:00
|
|
|
CHECK_EQ(last_written().operand(2), idx_operand);
|
2016-07-05 13:44:05 +00:00
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-06 11:32:09 +00:00
|
|
|
TEST_F(BytecodePeepholeOptimizerTest, MergeLdaNullOrUndefinedWithCompareOp) {
|
|
|
|
Bytecode first_bytecodes[] = {Bytecode::kLdaUndefined, Bytecode::kLdaNull};
|
|
|
|
|
|
|
|
for (auto first_bytecode : first_bytecodes) {
|
|
|
|
uint32_t reg_operand = Register(0).ToOperand();
|
|
|
|
uint32_t idx_operand = 1;
|
|
|
|
BytecodeNode first(first_bytecode);
|
|
|
|
BytecodeNode second(Bytecode::kTestEqual, reg_operand, idx_operand);
|
|
|
|
optimizer()->Write(&first);
|
|
|
|
optimizer()->Write(&second);
|
|
|
|
Flush();
|
|
|
|
CHECK_EQ(write_count(), 1);
|
|
|
|
CHECK_EQ(last_written().bytecode(), Bytecode::kTestUndetectable);
|
|
|
|
CHECK_EQ(last_written().operand_count(), 1);
|
|
|
|
CHECK_EQ(last_written().operand(0), reg_operand);
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-12 19:18:07 +00:00
|
|
|
} // namespace interpreter
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|