2014-09-04 11:13:35 +00:00
|
|
|
// 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 <limits>
|
|
|
|
|
|
|
|
#include "src/compiler/graph.h"
|
2015-01-07 14:42:38 +00:00
|
|
|
#include "src/compiler/node.h"
|
|
|
|
#include "src/compiler/operator.h"
|
2014-09-04 11:13:35 +00:00
|
|
|
#include "src/compiler/value-numbering-reducer.h"
|
2014-10-01 08:34:25 +00:00
|
|
|
#include "test/unittests/test-utils.h"
|
2014-09-04 11:13:35 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace compiler {
|
|
|
|
|
2014-10-29 14:40:47 +00:00
|
|
|
struct TestOperator : public Operator {
|
|
|
|
TestOperator(Operator::Opcode opcode, Operator::Properties properties,
|
|
|
|
size_t value_in, size_t value_out)
|
|
|
|
: Operator(opcode, properties, "TestOp", value_in, 0, 0, value_out, 0,
|
|
|
|
0) {}
|
|
|
|
};
|
2014-09-04 11:13:35 +00:00
|
|
|
|
|
|
|
|
2015-02-11 15:02:32 +00:00
|
|
|
static const TestOperator kOp0(0, Operator::kIdempotent, 0, 1);
|
|
|
|
static const TestOperator kOp1(1, Operator::kIdempotent, 1, 1);
|
2014-09-04 11:13:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ValueNumberingReducerTest : public TestWithZone {
|
|
|
|
public:
|
2016-07-14 08:09:19 +00:00
|
|
|
ValueNumberingReducerTest()
|
|
|
|
: graph_(zone()), reducer_(zone(), graph()->zone()) {}
|
2014-09-04 11:13:35 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
Reduction Reduce(Node* node) { return reducer_.Reduce(node); }
|
|
|
|
|
|
|
|
Graph* graph() { return &graph_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
Graph graph_;
|
|
|
|
ValueNumberingReducer reducer_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(ValueNumberingReducerTest, AllInputsAreChecked) {
|
|
|
|
Node* na = graph()->NewNode(&kOp0);
|
|
|
|
Node* nb = graph()->NewNode(&kOp0);
|
2015-09-23 09:08:15 +00:00
|
|
|
Node* n1 = graph()->NewNode(&kOp1, na);
|
|
|
|
Node* n2 = graph()->NewNode(&kOp1, nb);
|
2014-09-04 11:13:35 +00:00
|
|
|
EXPECT_FALSE(Reduce(n1).Changed());
|
|
|
|
EXPECT_FALSE(Reduce(n2).Changed());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-05 11:10:28 +00:00
|
|
|
TEST_F(ValueNumberingReducerTest, DeadNodesAreNeverReturned) {
|
|
|
|
Node* n0 = graph()->NewNode(&kOp0);
|
|
|
|
Node* n1 = graph()->NewNode(&kOp1, n0);
|
2014-09-04 11:13:35 +00:00
|
|
|
EXPECT_FALSE(Reduce(n1).Changed());
|
|
|
|
n1->Kill();
|
2014-09-05 11:10:28 +00:00
|
|
|
EXPECT_FALSE(Reduce(graph()->NewNode(&kOp1, n0)).Changed());
|
2014-09-04 11:13:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-10-08 08:06:59 +00:00
|
|
|
TEST_F(ValueNumberingReducerTest, OnlyEliminatableNodesAreReduced) {
|
2014-10-29 14:40:47 +00:00
|
|
|
TestOperator op(0, Operator::kNoProperties, 0, 1);
|
2014-10-08 08:06:59 +00:00
|
|
|
Node* n0 = graph()->NewNode(&op);
|
|
|
|
Node* n1 = graph()->NewNode(&op);
|
|
|
|
EXPECT_FALSE(Reduce(n0).Changed());
|
|
|
|
EXPECT_FALSE(Reduce(n1).Changed());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-04 11:13:35 +00:00
|
|
|
TEST_F(ValueNumberingReducerTest, OperatorEqualityNotIdentity) {
|
|
|
|
static const size_t kMaxInputCount = 16;
|
|
|
|
Node* inputs[kMaxInputCount];
|
|
|
|
for (size_t i = 0; i < arraysize(inputs); ++i) {
|
2015-09-25 08:42:51 +00:00
|
|
|
Operator::Opcode opcode = static_cast<Operator::Opcode>(kMaxInputCount + i);
|
2014-10-29 14:40:47 +00:00
|
|
|
inputs[i] = graph()->NewNode(
|
2015-02-11 15:02:32 +00:00
|
|
|
new (zone()) TestOperator(opcode, Operator::kIdempotent, 0, 1));
|
2014-09-04 11:13:35 +00:00
|
|
|
}
|
|
|
|
TRACED_FORRANGE(size_t, input_count, 0, arraysize(inputs)) {
|
2014-10-29 14:40:47 +00:00
|
|
|
const TestOperator op1(static_cast<Operator::Opcode>(input_count),
|
2015-02-11 15:02:32 +00:00
|
|
|
Operator::kIdempotent, input_count, 1);
|
2014-09-04 11:43:20 +00:00
|
|
|
Node* n1 = graph()->NewNode(&op1, static_cast<int>(input_count), inputs);
|
2014-09-04 11:13:35 +00:00
|
|
|
Reduction r1 = Reduce(n1);
|
|
|
|
EXPECT_FALSE(r1.Changed());
|
|
|
|
|
2014-10-29 14:40:47 +00:00
|
|
|
const TestOperator op2(static_cast<Operator::Opcode>(input_count),
|
2015-02-11 15:02:32 +00:00
|
|
|
Operator::kIdempotent, input_count, 1);
|
2014-09-04 11:43:20 +00:00
|
|
|
Node* n2 = graph()->NewNode(&op2, static_cast<int>(input_count), inputs);
|
2014-09-04 11:13:35 +00:00
|
|
|
Reduction r2 = Reduce(n2);
|
|
|
|
EXPECT_TRUE(r2.Changed());
|
|
|
|
EXPECT_EQ(n1, r2.replacement());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(ValueNumberingReducerTest, SubsequentReductionsYieldTheSameNode) {
|
|
|
|
static const size_t kMaxInputCount = 16;
|
|
|
|
Node* inputs[kMaxInputCount];
|
|
|
|
for (size_t i = 0; i < arraysize(inputs); ++i) {
|
2015-09-25 08:42:51 +00:00
|
|
|
Operator::Opcode opcode = static_cast<Operator::Opcode>(2 + i);
|
2014-10-29 14:40:47 +00:00
|
|
|
inputs[i] = graph()->NewNode(
|
2015-02-11 15:02:32 +00:00
|
|
|
new (zone()) TestOperator(opcode, Operator::kIdempotent, 0, 1));
|
2014-09-04 11:13:35 +00:00
|
|
|
}
|
|
|
|
TRACED_FORRANGE(size_t, input_count, 0, arraysize(inputs)) {
|
2015-02-11 15:02:32 +00:00
|
|
|
const TestOperator op1(1, Operator::kIdempotent, input_count, 1);
|
2014-09-04 11:43:20 +00:00
|
|
|
Node* n = graph()->NewNode(&op1, static_cast<int>(input_count), inputs);
|
2014-09-04 11:13:35 +00:00
|
|
|
Reduction r = Reduce(n);
|
|
|
|
EXPECT_FALSE(r.Changed());
|
|
|
|
|
2014-09-04 11:43:20 +00:00
|
|
|
r = Reduce(graph()->NewNode(&op1, static_cast<int>(input_count), inputs));
|
2014-09-04 11:13:35 +00:00
|
|
|
ASSERT_TRUE(r.Changed());
|
|
|
|
EXPECT_EQ(n, r.replacement());
|
|
|
|
|
2014-09-04 11:43:20 +00:00
|
|
|
r = Reduce(graph()->NewNode(&op1, static_cast<int>(input_count), inputs));
|
2014-09-04 11:13:35 +00:00
|
|
|
ASSERT_TRUE(r.Changed());
|
|
|
|
EXPECT_EQ(n, r.replacement());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-05 10:41:18 +00:00
|
|
|
|
|
|
|
TEST_F(ValueNumberingReducerTest, WontReplaceNodeWithItself) {
|
|
|
|
Node* n = graph()->NewNode(&kOp0);
|
|
|
|
EXPECT_FALSE(Reduce(n).Changed());
|
|
|
|
EXPECT_FALSE(Reduce(n).Changed());
|
|
|
|
}
|
|
|
|
|
2014-09-04 11:13:35 +00:00
|
|
|
} // namespace compiler
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|