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 "src/compiler/machine-operator-reducer.h" | 5 #include "src/compiler/machine-operator-reducer.h" |
6 | 6 |
| 7 #include "src/base/bits.h" |
7 #include "src/compiler/common-node-cache.h" | 8 #include "src/compiler/common-node-cache.h" |
8 #include "src/compiler/generic-node-inl.h" | 9 #include "src/compiler/generic-node-inl.h" |
9 #include "src/compiler/graph.h" | 10 #include "src/compiler/graph.h" |
10 #include "src/compiler/node-matchers.h" | 11 #include "src/compiler/node-matchers.h" |
11 | 12 |
12 namespace v8 { | 13 namespace v8 { |
13 namespace internal { | 14 namespace internal { |
14 namespace compiler { | 15 namespace compiler { |
15 | 16 |
16 MachineOperatorReducer::MachineOperatorReducer(Graph* graph) | 17 MachineOperatorReducer::MachineOperatorReducer(Graph* graph) |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 break; | 61 break; |
61 } | 62 } |
62 case IrOpcode::kWord32Or: { | 63 case IrOpcode::kWord32Or: { |
63 Int32BinopMatcher m(node); | 64 Int32BinopMatcher m(node); |
64 if (m.right().Is(0)) return Replace(m.left().node()); // x | 0 => x | 65 if (m.right().Is(0)) return Replace(m.left().node()); // x | 0 => x |
65 if (m.right().Is(-1)) return Replace(m.right().node()); // x | -1 => -1 | 66 if (m.right().Is(-1)) return Replace(m.right().node()); // x | -1 => -1 |
66 if (m.IsFoldable()) { // K | K => K | 67 if (m.IsFoldable()) { // K | K => K |
67 return ReplaceInt32(m.left().Value() | m.right().Value()); | 68 return ReplaceInt32(m.left().Value() | m.right().Value()); |
68 } | 69 } |
69 if (m.LeftEqualsRight()) return Replace(m.left().node()); // x | x => x | 70 if (m.LeftEqualsRight()) return Replace(m.left().node()); // x | x => x |
| 71 if (m.left().IsWord32Shl() && m.right().IsWord32Shr()) { |
| 72 Int32BinopMatcher mleft(m.left().node()); |
| 73 Int32BinopMatcher mright(m.right().node()); |
| 74 if (mleft.left().node() == mright.left().node()) { |
| 75 // (x << y) | (x >> (32 - y)) => x ror y |
| 76 if (mright.right().IsInt32Sub()) { |
| 77 Int32BinopMatcher mrightright(mright.right().node()); |
| 78 if (mrightright.left().Is(32) && |
| 79 mrightright.right().node() == mleft.right().node()) { |
| 80 graph_->ChangeOperator(node, machine_.Word32Ror()); |
| 81 node->ReplaceInput(0, mleft.left().node()); |
| 82 node->ReplaceInput(1, mleft.right().node()); |
| 83 return Changed(node); |
| 84 } |
| 85 } |
| 86 // (x << K) | (x >> (32 - K)) => x ror K |
| 87 if (mleft.right().IsInRange(0, 31) && |
| 88 mright.right().Is(32 - mleft.right().Value())) { |
| 89 graph_->ChangeOperator(node, machine_.Word32Ror()); |
| 90 node->ReplaceInput(0, mleft.left().node()); |
| 91 node->ReplaceInput(1, mleft.right().node()); |
| 92 return Changed(node); |
| 93 } |
| 94 } |
| 95 } |
| 96 if (m.left().IsWord32Shr() && m.right().IsWord32Shl()) { |
| 97 // (x >> (32 - y)) | (x << y) => x ror y |
| 98 Int32BinopMatcher mleft(m.left().node()); |
| 99 Int32BinopMatcher mright(m.right().node()); |
| 100 if (mleft.left().node() == mright.left().node()) { |
| 101 if (mleft.right().IsInt32Sub()) { |
| 102 Int32BinopMatcher mleftright(mleft.right().node()); |
| 103 if (mleftright.left().Is(32) && |
| 104 mleftright.right().node() == mright.right().node()) { |
| 105 graph_->ChangeOperator(node, machine_.Word32Ror()); |
| 106 node->ReplaceInput(0, mright.left().node()); |
| 107 node->ReplaceInput(1, mright.right().node()); |
| 108 return Changed(node); |
| 109 } |
| 110 } |
| 111 // (x >> (32 - K)) | (x << K) => x ror K |
| 112 if (mright.right().IsInRange(0, 31) && |
| 113 mleft.right().Is(32 - mright.right().Value())) { |
| 114 graph_->ChangeOperator(node, machine_.Word32Ror()); |
| 115 node->ReplaceInput(0, mright.left().node()); |
| 116 node->ReplaceInput(1, mright.right().node()); |
| 117 return Changed(node); |
| 118 } |
| 119 } |
| 120 } |
70 break; | 121 break; |
71 } | 122 } |
72 case IrOpcode::kWord32Xor: { | 123 case IrOpcode::kWord32Xor: { |
73 Int32BinopMatcher m(node); | 124 Int32BinopMatcher m(node); |
74 if (m.right().Is(0)) return Replace(m.left().node()); // x ^ 0 => x | 125 if (m.right().Is(0)) return Replace(m.left().node()); // x ^ 0 => x |
75 if (m.IsFoldable()) { // K ^ K => K | 126 if (m.IsFoldable()) { // K ^ K => K |
76 return ReplaceInt32(m.left().Value() ^ m.right().Value()); | 127 return ReplaceInt32(m.left().Value() ^ m.right().Value()); |
77 } | 128 } |
78 if (m.LeftEqualsRight()) return ReplaceInt32(0); // x ^ x => 0 | 129 if (m.LeftEqualsRight()) return ReplaceInt32(0); // x ^ x => 0 |
79 break; | 130 break; |
(...skipping 15 matching lines...) Expand all Loading... |
95 break; | 146 break; |
96 } | 147 } |
97 case IrOpcode::kWord32Sar: { | 148 case IrOpcode::kWord32Sar: { |
98 Int32BinopMatcher m(node); | 149 Int32BinopMatcher m(node); |
99 if (m.right().Is(0)) return Replace(m.left().node()); // x >> 0 => x | 150 if (m.right().Is(0)) return Replace(m.left().node()); // x >> 0 => x |
100 if (m.IsFoldable()) { // K >> K => K | 151 if (m.IsFoldable()) { // K >> K => K |
101 return ReplaceInt32(m.left().Value() >> m.right().Value()); | 152 return ReplaceInt32(m.left().Value() >> m.right().Value()); |
102 } | 153 } |
103 break; | 154 break; |
104 } | 155 } |
| 156 case IrOpcode::kWord32Ror: { |
| 157 Int32BinopMatcher m(node); |
| 158 if (m.right().Is(0)) return Replace(m.left().node()); // x ror 0 => x |
| 159 if (m.IsFoldable()) { // K ror K => K |
| 160 return ReplaceInt32( |
| 161 base::bits::RotateRight32(m.left().Value(), m.right().Value())); |
| 162 } |
| 163 break; |
| 164 } |
105 case IrOpcode::kWord32Equal: { | 165 case IrOpcode::kWord32Equal: { |
106 Int32BinopMatcher m(node); | 166 Int32BinopMatcher m(node); |
107 if (m.IsFoldable()) { // K == K => K | 167 if (m.IsFoldable()) { // K == K => K |
108 return ReplaceBool(m.left().Value() == m.right().Value()); | 168 return ReplaceBool(m.left().Value() == m.right().Value()); |
109 } | 169 } |
110 if (m.left().IsInt32Sub() && m.right().Is(0)) { // x - y == 0 => x == y | 170 if (m.left().IsInt32Sub() && m.right().Is(0)) { // x - y == 0 => x == y |
111 Int32BinopMatcher msub(m.left().node()); | 171 Int32BinopMatcher msub(m.left().node()); |
112 node->ReplaceInput(0, msub.left().node()); | 172 node->ReplaceInput(0, msub.left().node()); |
113 node->ReplaceInput(1, msub.right().node()); | 173 node->ReplaceInput(1, msub.right().node()); |
114 return Changed(node); | 174 return Changed(node); |
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 } | 394 } |
335 // TODO(turbofan): strength-reduce and fold floating point operations. | 395 // TODO(turbofan): strength-reduce and fold floating point operations. |
336 default: | 396 default: |
337 break; | 397 break; |
338 } | 398 } |
339 return NoChange(); | 399 return NoChange(); |
340 } | 400 } |
341 } | 401 } |
342 } | 402 } |
343 } // namespace v8::internal::compiler | 403 } // namespace v8::internal::compiler |
OLD | NEW |