| 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/graph-inl.h" | 5 #include "src/compiler/graph-inl.h" |
| 6 #include "src/compiler/js-builtin-reducer.h" | 6 #include "src/compiler/js-builtin-reducer.h" |
| 7 #include "src/compiler/node-matchers.h" | 7 #include "src/compiler/node-matchers.h" |
| 8 #include "src/compiler/node-properties-inl.h" | 8 #include "src/compiler/node-properties-inl.h" |
| 9 #include "src/types.h" | 9 #include "src/types.h" |
| 10 | 10 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 } | 41 } |
| 42 | 42 |
| 43 // Retrieves the BuiltinFunctionId as described above. | 43 // Retrieves the BuiltinFunctionId as described above. |
| 44 BuiltinFunctionId GetBuiltinFunctionId() { | 44 BuiltinFunctionId GetBuiltinFunctionId() { |
| 45 DCHECK_EQ(IrOpcode::kJSCallFunction, node_->opcode()); | 45 DCHECK_EQ(IrOpcode::kJSCallFunction, node_->opcode()); |
| 46 HeapObjectMatcher<Object> m(NodeProperties::GetValueInput(node_, 0)); | 46 HeapObjectMatcher<Object> m(NodeProperties::GetValueInput(node_, 0)); |
| 47 Handle<JSFunction> function = Handle<JSFunction>::cast(m.Value().handle()); | 47 Handle<JSFunction> function = Handle<JSFunction>::cast(m.Value().handle()); |
| 48 return function->shared()->builtin_function_id(); | 48 return function->shared()->builtin_function_id(); |
| 49 } | 49 } |
| 50 | 50 |
| 51 // Determines whether the call takes zero inputs. |
| 52 bool InputsMatchZero() { return GetJSCallArity() == 0; } |
| 53 |
| 51 // Determines whether the call takes one input of the given type. | 54 // Determines whether the call takes one input of the given type. |
| 52 bool InputsMatch(Type* t1) { | 55 bool InputsMatchOne(Type* t1) { |
| 53 return GetJSCallArity() == 1 && | 56 return GetJSCallArity() == 1 && |
| 54 NodeProperties::GetBounds(GetJSCallInput(0)).upper->Is(t1); | 57 NodeProperties::GetBounds(GetJSCallInput(0)).upper->Is(t1); |
| 55 } | 58 } |
| 56 | 59 |
| 57 // Determines whether the call takes two inputs of the given types. | 60 // Determines whether the call takes two inputs of the given types. |
| 58 bool InputsMatch(Type* t1, Type* t2) { | 61 bool InputsMatchTwo(Type* t1, Type* t2) { |
| 59 return GetJSCallArity() == 2 && | 62 return GetJSCallArity() == 2 && |
| 60 NodeProperties::GetBounds(GetJSCallInput(0)).upper->Is(t1) && | 63 NodeProperties::GetBounds(GetJSCallInput(0)).upper->Is(t1) && |
| 61 NodeProperties::GetBounds(GetJSCallInput(1)).upper->Is(t2); | 64 NodeProperties::GetBounds(GetJSCallInput(1)).upper->Is(t2); |
| 62 } | 65 } |
| 63 | 66 |
| 67 // Determines whether the call takes inputs all of the given type. |
| 68 bool InputsMatchAll(Type* t) { |
| 69 for (int i = 0; i < GetJSCallArity(); i++) { |
| 70 if (!NodeProperties::GetBounds(GetJSCallInput(i)).upper->Is(t)) { |
| 71 return false; |
| 72 } |
| 73 } |
| 74 return true; |
| 75 } |
| 76 |
| 64 Node* left() { return GetJSCallInput(0); } | 77 Node* left() { return GetJSCallInput(0); } |
| 65 Node* right() { return GetJSCallInput(1); } | 78 Node* right() { return GetJSCallInput(1); } |
| 66 | 79 |
| 67 protected: | |
| 68 int GetJSCallArity() { | 80 int GetJSCallArity() { |
| 69 DCHECK_EQ(IrOpcode::kJSCallFunction, node_->opcode()); | 81 DCHECK_EQ(IrOpcode::kJSCallFunction, node_->opcode()); |
| 70 // Skip first (i.e. callee) and second (i.e. receiver) operand. | 82 // Skip first (i.e. callee) and second (i.e. receiver) operand. |
| 71 return OperatorProperties::GetValueInputCount(node_->op()) - 2; | 83 return OperatorProperties::GetValueInputCount(node_->op()) - 2; |
| 72 } | 84 } |
| 73 | 85 |
| 74 Node* GetJSCallInput(int index) { | 86 Node* GetJSCallInput(int index) { |
| 75 DCHECK_EQ(IrOpcode::kJSCallFunction, node_->opcode()); | 87 DCHECK_EQ(IrOpcode::kJSCallFunction, node_->opcode()); |
| 76 DCHECK_LT(index, GetJSCallArity()); | 88 DCHECK_LT(index, GetJSCallArity()); |
| 77 // Skip first (i.e. callee) and second (i.e. receiver) operand. | 89 // Skip first (i.e. callee) and second (i.e. receiver) operand. |
| 78 return NodeProperties::GetValueInput(node_, index + 2); | 90 return NodeProperties::GetValueInput(node_, index + 2); |
| 79 } | 91 } |
| 80 | 92 |
| 81 private: | 93 private: |
| 82 Node* node_; | 94 Node* node_; |
| 83 }; | 95 }; |
| 84 | 96 |
| 85 | 97 |
| 98 // ECMA-262, section 15.8.2.11. |
| 99 Reduction JSBuiltinReducer::ReduceMathMax(Node* node) { |
| 100 JSCallReduction r(node); |
| 101 if (r.InputsMatchZero()) { |
| 102 // Math.max() -> -Infinity |
| 103 return Replace(jsgraph()->Constant(-V8_INFINITY)); |
| 104 } |
| 105 if (r.InputsMatchOne(Type::Number())) { |
| 106 // Math.max(a:number) -> a |
| 107 return Replace(r.left()); |
| 108 } |
| 109 if (r.InputsMatchAll(Type::Integral32())) { |
| 110 // Math.max(a:int32, b:int32, ...) |
| 111 Node* value = r.GetJSCallInput(0); |
| 112 for (int i = 1; i < r.GetJSCallArity(); i++) { |
| 113 Node* p = r.GetJSCallInput(i); |
| 114 Node* control = graph()->start(); |
| 115 Node* tag = graph()->NewNode(simplified()->NumberLessThan(), value, p); |
| 116 |
| 117 Node* branch = graph()->NewNode(common()->Branch(), tag, control); |
| 118 Node* if_true = graph()->NewNode(common()->IfTrue(), branch); |
| 119 Node* if_false = graph()->NewNode(common()->IfFalse(), branch); |
| 120 Node* merge = graph()->NewNode(common()->Merge(2), if_true, if_false); |
| 121 |
| 122 value = graph()->NewNode(common()->Phi(kMachNone, 2), p, value, merge); |
| 123 } |
| 124 return Replace(value); |
| 125 } |
| 126 return NoChange(); |
| 127 } |
| 128 |
| 129 |
| 86 // ES6 draft 08-24-14, section 20.2.2.19. | 130 // ES6 draft 08-24-14, section 20.2.2.19. |
| 87 Reduction JSBuiltinReducer::ReduceMathImul(Node* node) { | 131 Reduction JSBuiltinReducer::ReduceMathImul(Node* node) { |
| 88 JSCallReduction r(node); | 132 JSCallReduction r(node); |
| 89 if (r.InputsMatch(Type::Integral32(), Type::Integral32())) { | 133 if (r.InputsMatchTwo(Type::Integral32(), Type::Integral32())) { |
| 90 // Math.imul(a:int32, b:int32) -> Int32Mul(a, b) | 134 // Math.imul(a:int32, b:int32) -> Int32Mul(a, b) |
| 91 Node* value = graph()->NewNode(machine()->Int32Mul(), r.left(), r.right()); | 135 Node* value = graph()->NewNode(machine()->Int32Mul(), r.left(), r.right()); |
| 92 return Replace(value); | 136 return Replace(value); |
| 93 } | 137 } |
| 94 return NoChange(); | 138 return NoChange(); |
| 95 } | 139 } |
| 96 | 140 |
| 97 | 141 |
| 98 Reduction JSBuiltinReducer::Reduce(Node* node) { | 142 Reduction JSBuiltinReducer::Reduce(Node* node) { |
| 99 JSCallReduction r(node); | 143 JSCallReduction r(node); |
| 100 | 144 |
| 101 // Dispatch according to the BuiltinFunctionId if present. | 145 // Dispatch according to the BuiltinFunctionId if present. |
| 102 if (!r.HasBuiltinFunctionId()) return NoChange(); | 146 if (!r.HasBuiltinFunctionId()) return NoChange(); |
| 103 switch (r.GetBuiltinFunctionId()) { | 147 switch (r.GetBuiltinFunctionId()) { |
| 148 case kMathMax: |
| 149 return ReplaceWithPureReduction(node, ReduceMathMax(node)); |
| 104 case kMathImul: | 150 case kMathImul: |
| 105 return ReplaceWithPureReduction(node, ReduceMathImul(node)); | 151 return ReplaceWithPureReduction(node, ReduceMathImul(node)); |
| 106 default: | 152 default: |
| 107 break; | 153 break; |
| 108 } | 154 } |
| 109 return NoChange(); | 155 return NoChange(); |
| 110 } | 156 } |
| 111 | 157 |
| 112 } // namespace compiler | 158 } // namespace compiler |
| 113 } // namespace internal | 159 } // namespace internal |
| 114 } // namespace v8 | 160 } // namespace v8 |
| OLD | NEW |