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