| 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/base/bits.h" |
| 8 #include "src/base/division-by-constant.h" | 8 #include "src/base/division-by-constant.h" |
| 9 #include "src/codegen.h" | 9 #include "src/codegen.h" |
| 10 #include "src/compiler/diamond.h" | 10 #include "src/compiler/diamond.h" |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 | 68 |
| 69 | 69 |
| 70 Node* MachineOperatorReducer::Int32Add(Node* lhs, Node* rhs) { | 70 Node* MachineOperatorReducer::Int32Add(Node* lhs, Node* rhs) { |
| 71 Node* const node = graph()->NewNode(machine()->Int32Add(), lhs, rhs); | 71 Node* const node = graph()->NewNode(machine()->Int32Add(), lhs, rhs); |
| 72 Reduction const reduction = ReduceInt32Add(node); | 72 Reduction const reduction = ReduceInt32Add(node); |
| 73 return reduction.Changed() ? reduction.replacement() : node; | 73 return reduction.Changed() ? reduction.replacement() : node; |
| 74 } | 74 } |
| 75 | 75 |
| 76 | 76 |
| 77 Node* MachineOperatorReducer::Int32Sub(Node* lhs, Node* rhs) { | 77 Node* MachineOperatorReducer::Int32Sub(Node* lhs, Node* rhs) { |
| 78 return graph()->NewNode(machine()->Int32Sub(), lhs, rhs); | 78 Node* const node = graph()->NewNode(machine()->Int32Sub(), lhs, rhs); |
| 79 Reduction const reduction = ReduceInt32Sub(node); |
| 80 return reduction.Changed() ? reduction.replacement() : node; |
| 79 } | 81 } |
| 80 | 82 |
| 81 | 83 |
| 82 Node* MachineOperatorReducer::Int32Mul(Node* lhs, Node* rhs) { | 84 Node* MachineOperatorReducer::Int32Mul(Node* lhs, Node* rhs) { |
| 83 return graph()->NewNode(machine()->Int32Mul(), lhs, rhs); | 85 return graph()->NewNode(machine()->Int32Mul(), lhs, rhs); |
| 84 } | 86 } |
| 85 | 87 |
| 86 | 88 |
| 87 Node* MachineOperatorReducer::Int32Div(Node* dividend, int32_t divisor) { | 89 Node* MachineOperatorReducer::Int32Div(Node* dividend, int32_t divisor) { |
| 88 DCHECK_NE(0, divisor); | 90 DCHECK_NE(0, divisor); |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 209 node->ReplaceInput(0, msub.left().node()); | 211 node->ReplaceInput(0, msub.left().node()); |
| 210 node->ReplaceInput(1, msub.right().node()); | 212 node->ReplaceInput(1, msub.right().node()); |
| 211 return Changed(node); | 213 return Changed(node); |
| 212 } | 214 } |
| 213 // TODO(turbofan): fold HeapConstant, ExternalReference, pointer compares | 215 // TODO(turbofan): fold HeapConstant, ExternalReference, pointer compares |
| 214 if (m.LeftEqualsRight()) return ReplaceBool(true); // x == x => true | 216 if (m.LeftEqualsRight()) return ReplaceBool(true); // x == x => true |
| 215 break; | 217 break; |
| 216 } | 218 } |
| 217 case IrOpcode::kInt32Add: | 219 case IrOpcode::kInt32Add: |
| 218 return ReduceInt32Add(node); | 220 return ReduceInt32Add(node); |
| 219 case IrOpcode::kInt32Sub: { | 221 case IrOpcode::kInt32Sub: |
| 220 Int32BinopMatcher m(node); | 222 return ReduceInt32Sub(node); |
| 221 if (m.right().Is(0)) return Replace(m.left().node()); // x - 0 => x | |
| 222 if (m.IsFoldable()) { // K - K => K | |
| 223 return ReplaceInt32(static_cast<uint32_t>(m.left().Value()) - | |
| 224 static_cast<uint32_t>(m.right().Value())); | |
| 225 } | |
| 226 if (m.LeftEqualsRight()) return ReplaceInt32(0); // x - x => 0 | |
| 227 break; | |
| 228 } | |
| 229 case IrOpcode::kInt32Mul: { | 223 case IrOpcode::kInt32Mul: { |
| 230 Int32BinopMatcher m(node); | 224 Int32BinopMatcher m(node); |
| 231 if (m.right().Is(0)) return Replace(m.right().node()); // x * 0 => 0 | 225 if (m.right().Is(0)) return Replace(m.right().node()); // x * 0 => 0 |
| 232 if (m.right().Is(1)) return Replace(m.left().node()); // x * 1 => x | 226 if (m.right().Is(1)) return Replace(m.left().node()); // x * 1 => x |
| 233 if (m.IsFoldable()) { // K * K => K | 227 if (m.IsFoldable()) { // K * K => K |
| 234 return ReplaceInt32(m.left().Value() * m.right().Value()); | 228 return ReplaceInt32(m.left().Value() * m.right().Value()); |
| 235 } | 229 } |
| 236 if (m.right().Is(-1)) { // x * -1 => 0 - x | 230 if (m.right().Is(-1)) { // x * -1 => 0 - x |
| 237 node->set_op(machine()->Int32Sub()); | 231 node->set_op(machine()->Int32Sub()); |
| 238 node->ReplaceInput(0, Int32Constant(0)); | 232 node->ReplaceInput(0, Int32Constant(0)); |
| (...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 468 Int32BinopMatcher m(node); | 462 Int32BinopMatcher m(node); |
| 469 if (m.right().Is(0)) return Replace(m.left().node()); // x + 0 => x | 463 if (m.right().Is(0)) return Replace(m.left().node()); // x + 0 => x |
| 470 if (m.IsFoldable()) { // K + K => K | 464 if (m.IsFoldable()) { // K + K => K |
| 471 return ReplaceUint32(bit_cast<uint32_t>(m.left().Value()) + | 465 return ReplaceUint32(bit_cast<uint32_t>(m.left().Value()) + |
| 472 bit_cast<uint32_t>(m.right().Value())); | 466 bit_cast<uint32_t>(m.right().Value())); |
| 473 } | 467 } |
| 474 return NoChange(); | 468 return NoChange(); |
| 475 } | 469 } |
| 476 | 470 |
| 477 | 471 |
| 472 Reduction MachineOperatorReducer::ReduceInt32Sub(Node* node) { |
| 473 DCHECK_EQ(IrOpcode::kInt32Sub, node->opcode()); |
| 474 Int32BinopMatcher m(node); |
| 475 if (m.right().Is(0)) return Replace(m.left().node()); // x - 0 => x |
| 476 if (m.IsFoldable()) { // K - K => K |
| 477 return ReplaceInt32(static_cast<uint32_t>(m.left().Value()) - |
| 478 static_cast<uint32_t>(m.right().Value())); |
| 479 } |
| 480 if (m.LeftEqualsRight()) return ReplaceInt32(0); // x - x => 0 |
| 481 if (m.right().HasValue()) { // x - K => x + -K |
| 482 node->set_op(machine()->Int32Add()); |
| 483 node->ReplaceInput(1, Int32Constant(-m.right().Value())); |
| 484 Reduction const reduction = ReduceInt32Add(node); |
| 485 return reduction.Changed() ? reduction : Changed(node); |
| 486 } |
| 487 return NoChange(); |
| 488 } |
| 489 |
| 490 |
| 478 Reduction MachineOperatorReducer::ReduceInt32Div(Node* node) { | 491 Reduction MachineOperatorReducer::ReduceInt32Div(Node* node) { |
| 479 Int32BinopMatcher m(node); | 492 Int32BinopMatcher m(node); |
| 480 if (m.left().Is(0)) return Replace(m.left().node()); // 0 / x => 0 | 493 if (m.left().Is(0)) return Replace(m.left().node()); // 0 / x => 0 |
| 481 if (m.right().Is(0)) return Replace(m.right().node()); // x / 0 => 0 | 494 if (m.right().Is(0)) return Replace(m.right().node()); // x / 0 => 0 |
| 482 if (m.right().Is(1)) return Replace(m.left().node()); // x / 1 => x | 495 if (m.right().Is(1)) return Replace(m.left().node()); // x / 1 => x |
| 483 if (m.IsFoldable()) { // K / K => K | 496 if (m.IsFoldable()) { // K / K => K |
| 484 return ReplaceInt32( | 497 return ReplaceInt32( |
| 485 base::bits::SignedDiv32(m.left().Value(), m.right().Value())); | 498 base::bits::SignedDiv32(m.left().Value(), m.right().Value())); |
| 486 } | 499 } |
| 487 if (m.LeftEqualsRight()) { // x / x => x != 0 | 500 if (m.LeftEqualsRight()) { // x / x => x != 0 |
| (...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 913 MachineOperatorBuilder* MachineOperatorReducer::machine() const { | 926 MachineOperatorBuilder* MachineOperatorReducer::machine() const { |
| 914 return jsgraph()->machine(); | 927 return jsgraph()->machine(); |
| 915 } | 928 } |
| 916 | 929 |
| 917 | 930 |
| 918 Graph* MachineOperatorReducer::graph() const { return jsgraph()->graph(); } | 931 Graph* MachineOperatorReducer::graph() const { return jsgraph()->graph(); } |
| 919 | 932 |
| 920 } // namespace compiler | 933 } // namespace compiler |
| 921 } // namespace internal | 934 } // namespace internal |
| 922 } // namespace v8 | 935 } // namespace v8 |
| OLD | NEW |