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 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 DCHECK_LT(index, GetJSCallArity()); | 88 DCHECK_LT(index, GetJSCallArity()); |
89 // Skip first (i.e. callee) and second (i.e. receiver) operand. | 89 // Skip first (i.e. callee) and second (i.e. receiver) operand. |
90 return NodeProperties::GetValueInput(node_, index + 2); | 90 return NodeProperties::GetValueInput(node_, index + 2); |
91 } | 91 } |
92 | 92 |
93 private: | 93 private: |
94 Node* node_; | 94 Node* node_; |
95 }; | 95 }; |
96 | 96 |
97 | 97 |
| 98 // ECMA-262, section 15.8.2.1. |
| 99 Reduction JSBuiltinReducer::ReduceMathAbs(Node* node) { |
| 100 JSCallReduction r(node); |
| 101 if (r.InputsMatchOne(Type::Unsigned32())) { |
| 102 // Math.abs(a:uint32) -> a |
| 103 return Replace(r.left()); |
| 104 } |
| 105 if (r.InputsMatchOne(Type::Number())) { |
| 106 // Math.abs(a:number) -> (a > 0 ? a : 0 - a) |
| 107 Node* value = r.left(); |
| 108 Node* zero = jsgraph()->ZeroConstant(); |
| 109 Node* control = graph()->start(); |
| 110 Node* tag = graph()->NewNode(simplified()->NumberLessThan(), zero, value); |
| 111 |
| 112 Node* branch = graph()->NewNode(common()->Branch(), tag, control); |
| 113 Node* if_true = graph()->NewNode(common()->IfTrue(), branch); |
| 114 Node* if_false = graph()->NewNode(common()->IfFalse(), branch); |
| 115 Node* merge = graph()->NewNode(common()->Merge(2), if_true, if_false); |
| 116 |
| 117 Node* neg = graph()->NewNode(simplified()->NumberSubtract(), zero, value); |
| 118 value = graph()->NewNode(common()->Phi(kMachNone, 2), value, neg, merge); |
| 119 return Replace(value); |
| 120 } |
| 121 return NoChange(); |
| 122 } |
| 123 |
| 124 |
98 // ECMA-262, section 15.8.2.17. | 125 // ECMA-262, section 15.8.2.17. |
99 Reduction JSBuiltinReducer::ReduceMathSqrt(Node* node) { | 126 Reduction JSBuiltinReducer::ReduceMathSqrt(Node* node) { |
100 JSCallReduction r(node); | 127 JSCallReduction r(node); |
101 if (r.InputsMatchOne(Type::Number())) { | 128 if (r.InputsMatchOne(Type::Number())) { |
102 // Math.sqrt(a:number) -> Float64Sqrt(a) | 129 // Math.sqrt(a:number) -> Float64Sqrt(a) |
103 Node* value = graph()->NewNode(machine()->Float64Sqrt(), r.left()); | 130 Node* value = graph()->NewNode(machine()->Float64Sqrt(), r.left()); |
104 return Replace(value); | 131 return Replace(value); |
105 } | 132 } |
106 return NoChange(); | 133 return NoChange(); |
107 } | 134 } |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 return NoChange(); | 190 return NoChange(); |
164 } | 191 } |
165 | 192 |
166 | 193 |
167 Reduction JSBuiltinReducer::Reduce(Node* node) { | 194 Reduction JSBuiltinReducer::Reduce(Node* node) { |
168 JSCallReduction r(node); | 195 JSCallReduction r(node); |
169 | 196 |
170 // Dispatch according to the BuiltinFunctionId if present. | 197 // Dispatch according to the BuiltinFunctionId if present. |
171 if (!r.HasBuiltinFunctionId()) return NoChange(); | 198 if (!r.HasBuiltinFunctionId()) return NoChange(); |
172 switch (r.GetBuiltinFunctionId()) { | 199 switch (r.GetBuiltinFunctionId()) { |
| 200 case kMathAbs: |
| 201 return ReplaceWithPureReduction(node, ReduceMathAbs(node)); |
173 case kMathSqrt: | 202 case kMathSqrt: |
174 return ReplaceWithPureReduction(node, ReduceMathSqrt(node)); | 203 return ReplaceWithPureReduction(node, ReduceMathSqrt(node)); |
175 case kMathMax: | 204 case kMathMax: |
176 return ReplaceWithPureReduction(node, ReduceMathMax(node)); | 205 return ReplaceWithPureReduction(node, ReduceMathMax(node)); |
177 case kMathImul: | 206 case kMathImul: |
178 return ReplaceWithPureReduction(node, ReduceMathImul(node)); | 207 return ReplaceWithPureReduction(node, ReduceMathImul(node)); |
179 case kMathFround: | 208 case kMathFround: |
180 return ReplaceWithPureReduction(node, ReduceMathFround(node)); | 209 return ReplaceWithPureReduction(node, ReduceMathFround(node)); |
181 default: | 210 default: |
182 break; | 211 break; |
183 } | 212 } |
184 return NoChange(); | 213 return NoChange(); |
185 } | 214 } |
186 | 215 |
187 } // namespace compiler | 216 } // namespace compiler |
188 } // namespace internal | 217 } // namespace internal |
189 } // namespace v8 | 218 } // namespace v8 |
OLD | NEW |