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/common-operator-reducer.h" | 5 #include "src/compiler/common-operator-reducer.h" |
6 | 6 |
| 7 #include <algorithm> |
| 8 |
7 #include "src/compiler/common-operator.h" | 9 #include "src/compiler/common-operator.h" |
| 10 #include "src/compiler/js-graph.h" |
8 #include "src/compiler/node.h" | 11 #include "src/compiler/node.h" |
| 12 #include "src/compiler/node-matchers.h" |
9 | 13 |
10 namespace v8 { | 14 namespace v8 { |
11 namespace internal { | 15 namespace internal { |
12 namespace compiler { | 16 namespace compiler { |
13 | 17 |
14 Reduction CommonOperatorReducer::Reduce(Node* node) { | 18 Reduction CommonOperatorReducer::Reduce(Node* node) { |
15 switch (node->opcode()) { | 19 switch (node->opcode()) { |
16 case IrOpcode::kEffectPhi: | 20 case IrOpcode::kEffectPhi: |
17 case IrOpcode::kPhi: { | 21 return ReduceEffectPhi(node); |
18 int const input_count = node->InputCount(); | 22 case IrOpcode::kPhi: |
19 if (input_count > 1) { | 23 return ReducePhi(node); |
20 Node* const replacement = node->InputAt(0); | 24 case IrOpcode::kSelect: |
21 for (int i = 1; i < input_count - 1; ++i) { | 25 return ReduceSelect(node); |
22 if (node->InputAt(i) != replacement) return NoChange(); | |
23 } | |
24 return Replace(replacement); | |
25 } | |
26 break; | |
27 } | |
28 case IrOpcode::kSelect: { | |
29 if (node->InputAt(1) == node->InputAt(2)) { | |
30 return Replace(node->InputAt(1)); | |
31 } | |
32 break; | |
33 } | |
34 default: | 26 default: |
35 break; | 27 break; |
36 } | 28 } |
37 return NoChange(); | 29 return NoChange(); |
38 } | 30 } |
39 | 31 |
| 32 |
| 33 Reduction CommonOperatorReducer::ReduceEffectPhi(Node* node) { |
| 34 DCHECK_EQ(IrOpcode::kEffectPhi, node->opcode()); |
| 35 int const input_count = node->InputCount(); |
| 36 if (input_count > 1) { |
| 37 Node* const replacement = node->InputAt(0); |
| 38 for (int i = 1; i < input_count - 1; ++i) { |
| 39 if (node->InputAt(i) != replacement) return NoChange(); |
| 40 } |
| 41 return Replace(replacement); |
| 42 } |
| 43 return NoChange(); |
| 44 } |
| 45 |
| 46 |
| 47 Reduction CommonOperatorReducer::ReducePhi(Node* node) { |
| 48 DCHECK_EQ(IrOpcode::kPhi, node->opcode()); |
| 49 int const input_count = node->InputCount(); |
| 50 if (input_count == 3) { |
| 51 Node* vtrue = NodeProperties::GetValueInput(node, 0); |
| 52 Node* vfalse = NodeProperties::GetValueInput(node, 1); |
| 53 Node* merge = NodeProperties::GetControlInput(node); |
| 54 Node* if_true = NodeProperties::GetControlInput(merge, 0); |
| 55 Node* if_false = NodeProperties::GetControlInput(merge, 1); |
| 56 if (if_true->opcode() != IrOpcode::kIfTrue) { |
| 57 std::swap(if_true, if_false); |
| 58 std::swap(vtrue, vfalse); |
| 59 } |
| 60 if (if_true->opcode() == IrOpcode::kIfTrue && |
| 61 if_false->opcode() == IrOpcode::kIfFalse && |
| 62 if_true->InputAt(0) == if_false->InputAt(0)) { |
| 63 Node* branch = if_true->InputAt(0); |
| 64 Node* cond = branch->InputAt(0); |
| 65 if (cond->opcode() == IrOpcode::kFloat64LessThan) { |
| 66 if (cond->InputAt(0) == vtrue && cond->InputAt(1) == vfalse && |
| 67 machine()->HasFloat64Min()) { |
| 68 node->set_op(machine()->Float64Min()); |
| 69 node->ReplaceInput(0, vtrue); |
| 70 node->ReplaceInput(1, vfalse); |
| 71 node->TrimInputCount(2); |
| 72 return Changed(node); |
| 73 } else if (cond->InputAt(0) == vfalse && cond->InputAt(1) == vtrue && |
| 74 machine()->HasFloat64Max()) { |
| 75 node->set_op(machine()->Float64Max()); |
| 76 node->ReplaceInput(0, vtrue); |
| 77 node->ReplaceInput(1, vfalse); |
| 78 node->TrimInputCount(2); |
| 79 return Changed(node); |
| 80 } |
| 81 } |
| 82 } |
| 83 } |
| 84 if (input_count > 1) { |
| 85 Node* const replacement = node->InputAt(0); |
| 86 for (int i = 1; i < input_count - 1; ++i) { |
| 87 if (node->InputAt(i) != replacement) return NoChange(); |
| 88 } |
| 89 return Replace(replacement); |
| 90 } |
| 91 return NoChange(); |
| 92 } |
| 93 |
| 94 |
| 95 Reduction CommonOperatorReducer::ReduceSelect(Node* node) { |
| 96 DCHECK_EQ(IrOpcode::kSelect, node->opcode()); |
| 97 Node* cond = NodeProperties::GetValueInput(node, 0); |
| 98 Node* vtrue = NodeProperties::GetValueInput(node, 1); |
| 99 Node* vfalse = NodeProperties::GetValueInput(node, 2); |
| 100 if (vtrue == vfalse) return Replace(vtrue); |
| 101 if (cond->opcode() == IrOpcode::kFloat64LessThan) { |
| 102 if (cond->InputAt(0) == vtrue && cond->InputAt(1) == vfalse && |
| 103 machine()->HasFloat64Min()) { |
| 104 node->set_op(machine()->Float64Min()); |
| 105 node->ReplaceInput(0, vtrue); |
| 106 node->ReplaceInput(1, vfalse); |
| 107 node->TrimInputCount(2); |
| 108 return Changed(node); |
| 109 } else if (cond->InputAt(0) == vfalse && cond->InputAt(1) == vtrue && |
| 110 machine()->HasFloat64Max()) { |
| 111 node->set_op(machine()->Float64Max()); |
| 112 node->ReplaceInput(0, vtrue); |
| 113 node->ReplaceInput(1, vfalse); |
| 114 node->TrimInputCount(2); |
| 115 return Changed(node); |
| 116 } |
| 117 } |
| 118 return NoChange(); |
| 119 } |
| 120 |
| 121 |
| 122 CommonOperatorBuilder* CommonOperatorReducer::common() const { |
| 123 return jsgraph()->common(); |
| 124 } |
| 125 |
| 126 |
| 127 Graph* CommonOperatorReducer::graph() const { return jsgraph()->graph(); } |
| 128 |
| 129 |
| 130 MachineOperatorBuilder* CommonOperatorReducer::machine() const { |
| 131 return jsgraph()->machine(); |
| 132 } |
| 133 |
40 } // namespace compiler | 134 } // namespace compiler |
41 } // namespace internal | 135 } // namespace internal |
42 } // namespace v8 | 136 } // namespace v8 |
OLD | NEW |