| OLD | NEW |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 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 <limits> | 5 #include <limits> |
| 6 | 6 |
| 7 #include "src/compiler/graph.h" | 7 #include "src/compiler/graph.h" |
| 8 #include "src/compiler/node.h" | 8 #include "src/compiler/node.h" |
| 9 #include "src/compiler/operator.h" | 9 #include "src/compiler/operator.h" |
| 10 #include "src/compiler/value-numbering-reducer.h" | 10 #include "src/compiler/value-numbering-reducer.h" |
| 11 #include "test/unittests/test-utils.h" | 11 #include "test/unittests/test-utils.h" |
| 12 | 12 |
| 13 namespace v8 { | 13 namespace v8 { |
| 14 namespace internal { | 14 namespace internal { |
| 15 namespace compiler { | 15 namespace compiler { |
| 16 | 16 |
| 17 struct TestOperator : public Operator { | 17 struct TestOperator : public Operator { |
| 18 TestOperator(Operator::Opcode opcode, Operator::Properties properties, | 18 TestOperator(Operator::Opcode opcode, Operator::Properties properties, |
| 19 size_t value_in, size_t value_out) | 19 size_t value_in, size_t value_out) |
| 20 : Operator(opcode, properties, "TestOp", value_in, 0, 0, value_out, 0, | 20 : Operator(opcode, properties, "TestOp", value_in, 0, 0, value_out, 0, |
| 21 0) {} | 21 0) {} |
| 22 }; | 22 }; |
| 23 | 23 |
| 24 | 24 |
| 25 static const TestOperator kOp0(0, Operator::kEliminatable, 0, 1); | 25 static const TestOperator kOp0(0, Operator::kIdempotent, 0, 1); |
| 26 static const TestOperator kOp1(1, Operator::kEliminatable, 1, 1); | 26 static const TestOperator kOp1(1, Operator::kIdempotent, 1, 1); |
| 27 | 27 |
| 28 | 28 |
| 29 class ValueNumberingReducerTest : public TestWithZone { | 29 class ValueNumberingReducerTest : public TestWithZone { |
| 30 public: | 30 public: |
| 31 ValueNumberingReducerTest() : graph_(zone()), reducer_(zone()) {} | 31 ValueNumberingReducerTest() : graph_(zone()), reducer_(zone()) {} |
| 32 | 32 |
| 33 protected: | 33 protected: |
| 34 Reduction Reduce(Node* node) { return reducer_.Reduce(node); } | 34 Reduction Reduce(Node* node) { return reducer_.Reduce(node); } |
| 35 | 35 |
| 36 Graph* graph() { return &graph_; } | 36 Graph* graph() { return &graph_; } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 } | 69 } |
| 70 | 70 |
| 71 | 71 |
| 72 TEST_F(ValueNumberingReducerTest, OperatorEqualityNotIdentity) { | 72 TEST_F(ValueNumberingReducerTest, OperatorEqualityNotIdentity) { |
| 73 static const size_t kMaxInputCount = 16; | 73 static const size_t kMaxInputCount = 16; |
| 74 Node* inputs[kMaxInputCount]; | 74 Node* inputs[kMaxInputCount]; |
| 75 for (size_t i = 0; i < arraysize(inputs); ++i) { | 75 for (size_t i = 0; i < arraysize(inputs); ++i) { |
| 76 Operator::Opcode opcode = static_cast<Operator::Opcode>( | 76 Operator::Opcode opcode = static_cast<Operator::Opcode>( |
| 77 std::numeric_limits<Operator::Opcode>::max() - i); | 77 std::numeric_limits<Operator::Opcode>::max() - i); |
| 78 inputs[i] = graph()->NewNode( | 78 inputs[i] = graph()->NewNode( |
| 79 new (zone()) TestOperator(opcode, Operator::kEliminatable, 0, 1)); | 79 new (zone()) TestOperator(opcode, Operator::kIdempotent, 0, 1)); |
| 80 } | 80 } |
| 81 TRACED_FORRANGE(size_t, input_count, 0, arraysize(inputs)) { | 81 TRACED_FORRANGE(size_t, input_count, 0, arraysize(inputs)) { |
| 82 const TestOperator op1(static_cast<Operator::Opcode>(input_count), | 82 const TestOperator op1(static_cast<Operator::Opcode>(input_count), |
| 83 Operator::kEliminatable, input_count, 1); | 83 Operator::kIdempotent, input_count, 1); |
| 84 Node* n1 = graph()->NewNode(&op1, static_cast<int>(input_count), inputs); | 84 Node* n1 = graph()->NewNode(&op1, static_cast<int>(input_count), inputs); |
| 85 Reduction r1 = Reduce(n1); | 85 Reduction r1 = Reduce(n1); |
| 86 EXPECT_FALSE(r1.Changed()); | 86 EXPECT_FALSE(r1.Changed()); |
| 87 | 87 |
| 88 const TestOperator op2(static_cast<Operator::Opcode>(input_count), | 88 const TestOperator op2(static_cast<Operator::Opcode>(input_count), |
| 89 Operator::kEliminatable, input_count, 1); | 89 Operator::kIdempotent, input_count, 1); |
| 90 Node* n2 = graph()->NewNode(&op2, static_cast<int>(input_count), inputs); | 90 Node* n2 = graph()->NewNode(&op2, static_cast<int>(input_count), inputs); |
| 91 Reduction r2 = Reduce(n2); | 91 Reduction r2 = Reduce(n2); |
| 92 EXPECT_TRUE(r2.Changed()); | 92 EXPECT_TRUE(r2.Changed()); |
| 93 EXPECT_EQ(n1, r2.replacement()); | 93 EXPECT_EQ(n1, r2.replacement()); |
| 94 } | 94 } |
| 95 } | 95 } |
| 96 | 96 |
| 97 | 97 |
| 98 TEST_F(ValueNumberingReducerTest, SubsequentReductionsYieldTheSameNode) { | 98 TEST_F(ValueNumberingReducerTest, SubsequentReductionsYieldTheSameNode) { |
| 99 static const size_t kMaxInputCount = 16; | 99 static const size_t kMaxInputCount = 16; |
| 100 Node* inputs[kMaxInputCount]; | 100 Node* inputs[kMaxInputCount]; |
| 101 for (size_t i = 0; i < arraysize(inputs); ++i) { | 101 for (size_t i = 0; i < arraysize(inputs); ++i) { |
| 102 Operator::Opcode opcode = static_cast<Operator::Opcode>( | 102 Operator::Opcode opcode = static_cast<Operator::Opcode>( |
| 103 std::numeric_limits<Operator::Opcode>::max() - i); | 103 std::numeric_limits<Operator::Opcode>::max() - i); |
| 104 inputs[i] = graph()->NewNode( | 104 inputs[i] = graph()->NewNode( |
| 105 new (zone()) TestOperator(opcode, Operator::kEliminatable, 0, 1)); | 105 new (zone()) TestOperator(opcode, Operator::kIdempotent, 0, 1)); |
| 106 } | 106 } |
| 107 TRACED_FORRANGE(size_t, input_count, 0, arraysize(inputs)) { | 107 TRACED_FORRANGE(size_t, input_count, 0, arraysize(inputs)) { |
| 108 const TestOperator op1(1, Operator::kEliminatable, input_count, 1); | 108 const TestOperator op1(1, Operator::kIdempotent, input_count, 1); |
| 109 Node* n = graph()->NewNode(&op1, static_cast<int>(input_count), inputs); | 109 Node* n = graph()->NewNode(&op1, static_cast<int>(input_count), inputs); |
| 110 Reduction r = Reduce(n); | 110 Reduction r = Reduce(n); |
| 111 EXPECT_FALSE(r.Changed()); | 111 EXPECT_FALSE(r.Changed()); |
| 112 | 112 |
| 113 r = Reduce(graph()->NewNode(&op1, static_cast<int>(input_count), inputs)); | 113 r = Reduce(graph()->NewNode(&op1, static_cast<int>(input_count), inputs)); |
| 114 ASSERT_TRUE(r.Changed()); | 114 ASSERT_TRUE(r.Changed()); |
| 115 EXPECT_EQ(n, r.replacement()); | 115 EXPECT_EQ(n, r.replacement()); |
| 116 | 116 |
| 117 r = Reduce(graph()->NewNode(&op1, static_cast<int>(input_count), inputs)); | 117 r = Reduce(graph()->NewNode(&op1, static_cast<int>(input_count), inputs)); |
| 118 ASSERT_TRUE(r.Changed()); | 118 ASSERT_TRUE(r.Changed()); |
| 119 EXPECT_EQ(n, r.replacement()); | 119 EXPECT_EQ(n, r.replacement()); |
| 120 } | 120 } |
| 121 } | 121 } |
| 122 | 122 |
| 123 | 123 |
| 124 TEST_F(ValueNumberingReducerTest, WontReplaceNodeWithItself) { | 124 TEST_F(ValueNumberingReducerTest, WontReplaceNodeWithItself) { |
| 125 Node* n = graph()->NewNode(&kOp0); | 125 Node* n = graph()->NewNode(&kOp0); |
| 126 EXPECT_FALSE(Reduce(n).Changed()); | 126 EXPECT_FALSE(Reduce(n).Changed()); |
| 127 EXPECT_FALSE(Reduce(n).Changed()); | 127 EXPECT_FALSE(Reduce(n).Changed()); |
| 128 } | 128 } |
| 129 | 129 |
| 130 } // namespace compiler | 130 } // namespace compiler |
| 131 } // namespace internal | 131 } // namespace internal |
| 132 } // namespace v8 | 132 } // namespace v8 |
| OLD | NEW |