2015-03-16 13:43:02 +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 "src/compiler/state-values-utils.h"
|
2019-05-23 13:27:57 +00:00
|
|
|
#include "src/utils/bit-vector.h"
|
2015-03-16 13:43:02 +00:00
|
|
|
#include "test/unittests/compiler/graph-unittest.h"
|
|
|
|
#include "test/unittests/compiler/node-test-utils.h"
|
|
|
|
#include "test/unittests/test-utils.h"
|
|
|
|
#include "testing/gmock/include/gmock/gmock.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace compiler {
|
|
|
|
|
|
|
|
class StateValuesIteratorTest : public GraphTest {
|
|
|
|
public:
|
|
|
|
StateValuesIteratorTest() : GraphTest(3) {}
|
|
|
|
|
|
|
|
Node* StateValuesFromVector(NodeVector* nodes) {
|
|
|
|
int count = static_cast<int>(nodes->size());
|
2017-01-05 10:44:44 +00:00
|
|
|
return graph()->NewNode(
|
|
|
|
common()->StateValues(count, SparseInputMask::Dense()), count,
|
|
|
|
count == 0 ? nullptr : &(nodes->front()));
|
2015-03-16 13:43:02 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(StateValuesIteratorTest, SimpleIteration) {
|
|
|
|
NodeVector inputs(zone());
|
|
|
|
const int count = 10;
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
inputs.push_back(Int32Constant(i));
|
|
|
|
}
|
|
|
|
Node* state_values = StateValuesFromVector(&inputs);
|
|
|
|
int i = 0;
|
2015-03-19 14:00:28 +00:00
|
|
|
for (StateValuesAccess::TypedNode node : StateValuesAccess(state_values)) {
|
|
|
|
EXPECT_THAT(node.node, IsInt32Constant(i));
|
2015-03-16 13:43:02 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
EXPECT_EQ(count, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(StateValuesIteratorTest, EmptyIteration) {
|
|
|
|
NodeVector inputs(zone());
|
|
|
|
Node* state_values = StateValuesFromVector(&inputs);
|
2017-12-01 11:00:00 +00:00
|
|
|
bool empty = true;
|
2015-03-19 14:00:28 +00:00
|
|
|
for (auto node : StateValuesAccess(state_values)) {
|
2015-03-16 13:43:02 +00:00
|
|
|
USE(node);
|
2017-12-01 11:00:00 +00:00
|
|
|
empty = false;
|
2015-03-16 13:43:02 +00:00
|
|
|
}
|
2017-12-01 11:00:00 +00:00
|
|
|
EXPECT_TRUE(empty);
|
2015-03-16 13:43:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(StateValuesIteratorTest, NestedIteration) {
|
|
|
|
NodeVector inputs(zone());
|
|
|
|
int count = 0;
|
|
|
|
for (int i = 0; i < 8; i++) {
|
|
|
|
if (i == 2) {
|
|
|
|
// Single nested in index 2.
|
|
|
|
NodeVector nested_inputs(zone());
|
|
|
|
for (int j = 0; j < 8; j++) {
|
|
|
|
nested_inputs.push_back(Int32Constant(count++));
|
|
|
|
}
|
|
|
|
inputs.push_back(StateValuesFromVector(&nested_inputs));
|
|
|
|
} else if (i == 5) {
|
|
|
|
// Double nested at index 5.
|
|
|
|
NodeVector nested_inputs(zone());
|
|
|
|
for (int j = 0; j < 8; j++) {
|
|
|
|
if (j == 7) {
|
|
|
|
NodeVector doubly_nested_inputs(zone());
|
|
|
|
for (int k = 0; k < 2; k++) {
|
|
|
|
doubly_nested_inputs.push_back(Int32Constant(count++));
|
|
|
|
}
|
|
|
|
nested_inputs.push_back(StateValuesFromVector(&doubly_nested_inputs));
|
|
|
|
} else {
|
|
|
|
nested_inputs.push_back(Int32Constant(count++));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inputs.push_back(StateValuesFromVector(&nested_inputs));
|
|
|
|
} else {
|
|
|
|
inputs.push_back(Int32Constant(count++));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Node* state_values = StateValuesFromVector(&inputs);
|
|
|
|
int i = 0;
|
2015-03-19 14:00:28 +00:00
|
|
|
for (StateValuesAccess::TypedNode node : StateValuesAccess(state_values)) {
|
|
|
|
EXPECT_THAT(node.node, IsInt32Constant(i));
|
2015-03-16 13:43:02 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
EXPECT_EQ(count, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(StateValuesIteratorTest, TreeFromVector) {
|
|
|
|
int sizes[] = {0, 1, 2, 100, 5000, 30000};
|
|
|
|
TRACED_FOREACH(int, count, sizes) {
|
|
|
|
JSOperatorBuilder javascript(zone());
|
|
|
|
MachineOperatorBuilder machine(zone());
|
2015-10-16 12:38:46 +00:00
|
|
|
JSGraph jsgraph(isolate(), graph(), common(), &javascript, nullptr,
|
|
|
|
&machine);
|
2015-03-16 13:43:02 +00:00
|
|
|
|
|
|
|
// Generate the input vector.
|
|
|
|
NodeVector inputs(zone());
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
inputs.push_back(Int32Constant(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the tree.
|
|
|
|
StateValuesCache builder(&jsgraph);
|
|
|
|
Node* values_node = builder.GetNodeForValues(
|
2017-01-05 10:44:44 +00:00
|
|
|
inputs.size() == 0 ? nullptr : &(inputs.front()), inputs.size(),
|
|
|
|
nullptr);
|
2015-03-16 13:43:02 +00:00
|
|
|
|
|
|
|
// Check the tree contents with vector.
|
|
|
|
int i = 0;
|
2015-03-19 14:00:28 +00:00
|
|
|
for (StateValuesAccess::TypedNode node : StateValuesAccess(values_node)) {
|
|
|
|
EXPECT_THAT(node.node, IsInt32Constant(i));
|
2015-03-16 13:43:02 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
EXPECT_EQ(inputs.size(), static_cast<size_t>(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-05 10:44:44 +00:00
|
|
|
TEST_F(StateValuesIteratorTest, TreeFromVectorWithLiveness) {
|
|
|
|
int sizes[] = {0, 1, 2, 100, 5000, 30000};
|
|
|
|
TRACED_FOREACH(int, count, sizes) {
|
|
|
|
JSOperatorBuilder javascript(zone());
|
|
|
|
MachineOperatorBuilder machine(zone());
|
|
|
|
JSGraph jsgraph(isolate(), graph(), common(), &javascript, nullptr,
|
|
|
|
&machine);
|
|
|
|
|
|
|
|
// Generate the input vector.
|
|
|
|
NodeVector inputs(zone());
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
inputs.push_back(Int32Constant(i));
|
|
|
|
}
|
|
|
|
// Generate the input liveness.
|
|
|
|
BitVector liveness(count, zone());
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
if (i % 3 == 0) {
|
|
|
|
liveness.Add(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build the tree.
|
|
|
|
StateValuesCache builder(&jsgraph);
|
|
|
|
Node* values_node = builder.GetNodeForValues(
|
|
|
|
inputs.size() == 0 ? nullptr : &(inputs.front()), inputs.size(),
|
|
|
|
&liveness);
|
|
|
|
|
|
|
|
// Check the tree contents with vector.
|
|
|
|
int i = 0;
|
2020-03-05 10:51:13 +00:00
|
|
|
for (StateValuesAccess::iterator it =
|
|
|
|
StateValuesAccess(values_node).begin();
|
|
|
|
!it.done(); ++it) {
|
2017-01-05 10:44:44 +00:00
|
|
|
if (liveness.Contains(i)) {
|
2020-03-05 10:51:13 +00:00
|
|
|
EXPECT_THAT(it.node(), IsInt32Constant(i));
|
2017-01-05 10:44:44 +00:00
|
|
|
} else {
|
2020-03-05 10:51:13 +00:00
|
|
|
EXPECT_EQ(it.node(), nullptr);
|
2017-01-05 10:44:44 +00:00
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
EXPECT_EQ(inputs.size(), static_cast<size_t>(i));
|
|
|
|
}
|
|
|
|
}
|
2015-03-16 13:43:02 +00:00
|
|
|
|
|
|
|
TEST_F(StateValuesIteratorTest, BuildTreeIdentical) {
|
|
|
|
int sizes[] = {0, 1, 2, 100, 5000, 30000};
|
|
|
|
TRACED_FOREACH(int, count, sizes) {
|
|
|
|
JSOperatorBuilder javascript(zone());
|
|
|
|
MachineOperatorBuilder machine(zone());
|
2015-10-16 12:38:46 +00:00
|
|
|
JSGraph jsgraph(isolate(), graph(), common(), &javascript, nullptr,
|
|
|
|
&machine);
|
2015-03-16 13:43:02 +00:00
|
|
|
|
|
|
|
// Generate the input vector.
|
|
|
|
NodeVector inputs(zone());
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
inputs.push_back(Int32Constant(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build two trees from the same data.
|
|
|
|
StateValuesCache builder(&jsgraph);
|
|
|
|
Node* node1 = builder.GetNodeForValues(
|
2017-01-05 10:44:44 +00:00
|
|
|
inputs.size() == 0 ? nullptr : &(inputs.front()), inputs.size(),
|
|
|
|
nullptr);
|
|
|
|
Node* node2 = builder.GetNodeForValues(
|
|
|
|
inputs.size() == 0 ? nullptr : &(inputs.front()), inputs.size(),
|
|
|
|
nullptr);
|
|
|
|
|
|
|
|
// The trees should be equal since the data was the same.
|
|
|
|
EXPECT_EQ(node1, node2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(StateValuesIteratorTest, BuildTreeWithLivenessIdentical) {
|
|
|
|
int sizes[] = {0, 1, 2, 100, 5000, 30000};
|
|
|
|
TRACED_FOREACH(int, count, sizes) {
|
|
|
|
JSOperatorBuilder javascript(zone());
|
|
|
|
MachineOperatorBuilder machine(zone());
|
|
|
|
JSGraph jsgraph(isolate(), graph(), common(), &javascript, nullptr,
|
|
|
|
&machine);
|
|
|
|
|
|
|
|
// Generate the input vector.
|
|
|
|
NodeVector inputs(zone());
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
inputs.push_back(Int32Constant(i));
|
|
|
|
}
|
|
|
|
// Generate the input liveness.
|
|
|
|
BitVector liveness(count, zone());
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
if (i % 3 == 0) {
|
|
|
|
liveness.Add(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Build two trees from the same data.
|
|
|
|
StateValuesCache builder(&jsgraph);
|
|
|
|
Node* node1 = builder.GetNodeForValues(
|
|
|
|
inputs.size() == 0 ? nullptr : &(inputs.front()), inputs.size(),
|
|
|
|
&liveness);
|
2015-03-16 13:43:02 +00:00
|
|
|
Node* node2 = builder.GetNodeForValues(
|
2017-01-05 10:44:44 +00:00
|
|
|
inputs.size() == 0 ? nullptr : &(inputs.front()), inputs.size(),
|
|
|
|
&liveness);
|
2015-03-16 13:43:02 +00:00
|
|
|
|
|
|
|
// The trees should be equal since the data was the same.
|
|
|
|
EXPECT_EQ(node1, node2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace compiler
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|