OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "src/base/bits.h" |
| 6 #include "src/compiler/common-operator.h" |
| 7 #include "src/compiler/graph.h" |
| 8 #include "src/compiler/machine-operator-reducer.h" |
| 9 #include "test/compiler-unittests/compiler-unittests.h" |
| 10 #include "test/compiler-unittests/node-matchers.h" |
| 11 |
| 12 namespace v8 { |
| 13 namespace internal { |
| 14 namespace compiler { |
| 15 |
| 16 class MachineOperatorReducerTest : public CompilerTest { |
| 17 public: |
| 18 explicit MachineOperatorReducerTest(int num_parameters = 2) |
| 19 : graph_(zone()), common_(zone()), machine_(zone()) { |
| 20 graph()->SetStart(graph()->NewNode(common()->Start(num_parameters))); |
| 21 } |
| 22 virtual ~MachineOperatorReducerTest() {} |
| 23 |
| 24 protected: |
| 25 Node* Parameter(int32_t index) { |
| 26 return graph()->NewNode(common()->Parameter(index), graph()->start()); |
| 27 } |
| 28 Node* Int32Constant(int32_t value) { |
| 29 return graph()->NewNode(common()->Int32Constant(value)); |
| 30 } |
| 31 |
| 32 Reduction Reduce(Node* node) { |
| 33 MachineOperatorReducer reducer(graph()); |
| 34 return reducer.Reduce(node); |
| 35 } |
| 36 |
| 37 Graph* graph() { return &graph_; } |
| 38 CommonOperatorBuilder* common() { return &common_; } |
| 39 MachineOperatorBuilder* machine() { return &machine_; } |
| 40 |
| 41 private: |
| 42 Graph graph_; |
| 43 CommonOperatorBuilder common_; |
| 44 MachineOperatorBuilder machine_; |
| 45 }; |
| 46 |
| 47 |
| 48 namespace { |
| 49 |
| 50 static const uint32_t kConstants[] = { |
| 51 0x00000000, 0x00000001, 0xffffffff, 0x1b09788b, 0x04c5fce8, 0xcc0de5bf, |
| 52 0x273a798e, 0x187937a3, 0xece3af83, 0x5495a16b, 0x0b668ecc, 0x11223344, |
| 53 0x0000009e, 0x00000043, 0x0000af73, 0x0000116b, 0x00658ecc, 0x002b3b4c, |
| 54 0x88776655, 0x70000000, 0x07200000, 0x7fffffff, 0x56123761, 0x7fffff00, |
| 55 0x761c4761, 0x80000000, 0x88888888, 0xa0000000, 0xdddddddd, 0xe0000000, |
| 56 0xeeeeeeee, 0xfffffffd, 0xf0000000, 0x007fffff, 0x003fffff, 0x001fffff, |
| 57 0x000fffff, 0x0007ffff, 0x0003ffff, 0x0001ffff, 0x0000ffff, 0x00007fff, |
| 58 0x00003fff, 0x00001fff, 0x00000fff, 0x000007ff, 0x000003ff, 0x000001ff}; |
| 59 |
| 60 } // namespace |
| 61 |
| 62 |
| 63 TEST_F(MachineOperatorReducerTest, ReduceToWord32RorWithParameters) { |
| 64 Node* value = Parameter(0); |
| 65 Node* shift = Parameter(1); |
| 66 Node* shl = graph()->NewNode(machine()->Word32Shl(), value, shift); |
| 67 Node* shr = graph()->NewNode( |
| 68 machine()->Word32Shr(), value, |
| 69 graph()->NewNode(machine()->Int32Sub(), Int32Constant(32), shift)); |
| 70 |
| 71 // (x << y) | (x >> (32 - y)) => x ror y |
| 72 Node* node1 = graph()->NewNode(machine()->Word32Or(), shl, shr); |
| 73 Reduction reduction1 = Reduce(node1); |
| 74 EXPECT_TRUE(reduction1.Changed()); |
| 75 EXPECT_EQ(reduction1.replacement(), node1); |
| 76 EXPECT_THAT(reduction1.replacement(), IsWord32Ror(value, shift)); |
| 77 |
| 78 // (x >> (32 - y)) | (x << y) => x ror y |
| 79 Node* node2 = graph()->NewNode(machine()->Word32Or(), shr, shl); |
| 80 Reduction reduction2 = Reduce(node2); |
| 81 EXPECT_TRUE(reduction2.Changed()); |
| 82 EXPECT_EQ(reduction2.replacement(), node2); |
| 83 EXPECT_THAT(reduction2.replacement(), IsWord32Ror(value, shift)); |
| 84 } |
| 85 |
| 86 |
| 87 TEST_F(MachineOperatorReducerTest, ReduceToWord32RorWithConstant) { |
| 88 Node* value = Parameter(0); |
| 89 TRACED_FORRANGE(int32_t, k, 0, 31) { |
| 90 Node* shl = |
| 91 graph()->NewNode(machine()->Word32Shl(), value, Int32Constant(k)); |
| 92 Node* shr = |
| 93 graph()->NewNode(machine()->Word32Shr(), value, Int32Constant(32 - k)); |
| 94 |
| 95 // (x << K) | (x >> ((32 - K) - y)) => x ror K |
| 96 Node* node1 = graph()->NewNode(machine()->Word32Or(), shl, shr); |
| 97 Reduction reduction1 = Reduce(node1); |
| 98 EXPECT_TRUE(reduction1.Changed()); |
| 99 EXPECT_EQ(reduction1.replacement(), node1); |
| 100 EXPECT_THAT(reduction1.replacement(), |
| 101 IsWord32Ror(value, IsInt32Constant(k))); |
| 102 |
| 103 // (x >> (32 - K)) | (x << K) => x ror K |
| 104 Node* node2 = graph()->NewNode(machine()->Word32Or(), shr, shl); |
| 105 Reduction reduction2 = Reduce(node2); |
| 106 EXPECT_TRUE(reduction2.Changed()); |
| 107 EXPECT_EQ(reduction2.replacement(), node2); |
| 108 EXPECT_THAT(reduction2.replacement(), |
| 109 IsWord32Ror(value, IsInt32Constant(k))); |
| 110 } |
| 111 } |
| 112 |
| 113 |
| 114 TEST_F(MachineOperatorReducerTest, Word32RorWithZeroShift) { |
| 115 Node* value = Parameter(0); |
| 116 Node* node = |
| 117 graph()->NewNode(machine()->Word32Ror(), value, Int32Constant(0)); |
| 118 Reduction reduction = Reduce(node); |
| 119 EXPECT_TRUE(reduction.Changed()); |
| 120 EXPECT_EQ(reduction.replacement(), value); |
| 121 } |
| 122 |
| 123 |
| 124 TEST_F(MachineOperatorReducerTest, Word32RorWithConstants) { |
| 125 TRACED_FOREACH(int32_t, x, kConstants) { |
| 126 TRACED_FORRANGE(int32_t, y, 0, 31) { |
| 127 Node* node = graph()->NewNode(machine()->Word32Ror(), Int32Constant(x), |
| 128 Int32Constant(y)); |
| 129 Reduction reduction = Reduce(node); |
| 130 EXPECT_TRUE(reduction.Changed()); |
| 131 EXPECT_THAT(reduction.replacement(), |
| 132 IsInt32Constant(base::bits::RotateRight32(x, y))); |
| 133 } |
| 134 } |
| 135 } |
| 136 |
| 137 } // namespace compiler |
| 138 } // namespace internal |
| 139 } // namespace v8 |
OLD | NEW |