| 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/base/ieee754.h" | 9 #include "src/base/ieee754.h" |
| 10 #include "src/codegen.h" | 10 #include "src/codegen.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 | 36 |
| 37 Node* MachineOperatorReducer::Int32Constant(int32_t value) { | 37 Node* MachineOperatorReducer::Int32Constant(int32_t value) { |
| 38 return jsgraph()->Int32Constant(value); | 38 return jsgraph()->Int32Constant(value); |
| 39 } | 39 } |
| 40 | 40 |
| 41 | 41 |
| 42 Node* MachineOperatorReducer::Int64Constant(int64_t value) { | 42 Node* MachineOperatorReducer::Int64Constant(int64_t value) { |
| 43 return graph()->NewNode(common()->Int64Constant(value)); | 43 return graph()->NewNode(common()->Int64Constant(value)); |
| 44 } | 44 } |
| 45 | 45 |
| 46 Node* MachineOperatorReducer::Float64Mul(Node* lhs, Node* rhs) { |
| 47 return graph()->NewNode(machine()->Float64Mul(), lhs, rhs); |
| 48 } |
| 49 |
| 50 Node* MachineOperatorReducer::Float64PowHalf(Node* value) { |
| 51 value = |
| 52 graph()->NewNode(machine()->Float64Add(), Float64Constant(0.0), value); |
| 53 return graph()->NewNode( |
| 54 common()->Select(MachineRepresentation::kFloat64, BranchHint::kFalse), |
| 55 graph()->NewNode(machine()->Float64LessThanOrEqual(), value, |
| 56 Float64Constant(-V8_INFINITY)), |
| 57 Float64Constant(V8_INFINITY), |
| 58 graph()->NewNode(machine()->Float64Sqrt(), value)); |
| 59 } |
| 46 | 60 |
| 47 Node* MachineOperatorReducer::Word32And(Node* lhs, Node* rhs) { | 61 Node* MachineOperatorReducer::Word32And(Node* lhs, Node* rhs) { |
| 48 Node* const node = graph()->NewNode(machine()->Word32And(), lhs, rhs); | 62 Node* const node = graph()->NewNode(machine()->Word32And(), lhs, rhs); |
| 49 Reduction const reduction = ReduceWord32And(node); | 63 Reduction const reduction = ReduceWord32And(node); |
| 50 return reduction.Changed() ? reduction.replacement() : node; | 64 return reduction.Changed() ? reduction.replacement() : node; |
| 51 } | 65 } |
| 52 | 66 |
| 53 | 67 |
| 54 Node* MachineOperatorReducer::Word32Sar(Node* lhs, uint32_t rhs) { | 68 Node* MachineOperatorReducer::Word32Sar(Node* lhs, uint32_t rhs) { |
| 55 if (rhs == 0) return lhs; | 69 if (rhs == 0) return lhs; |
| (...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 370 return ReplaceFloat64( | 384 return ReplaceFloat64( |
| 371 base::ieee754::atan2(m.left().Value(), m.right().Value())); | 385 base::ieee754::atan2(m.left().Value(), m.right().Value())); |
| 372 } | 386 } |
| 373 break; | 387 break; |
| 374 } | 388 } |
| 375 case IrOpcode::kFloat64Atanh: { | 389 case IrOpcode::kFloat64Atanh: { |
| 376 Float64Matcher m(node->InputAt(0)); | 390 Float64Matcher m(node->InputAt(0)); |
| 377 if (m.HasValue()) return ReplaceFloat64(base::ieee754::atanh(m.Value())); | 391 if (m.HasValue()) return ReplaceFloat64(base::ieee754::atanh(m.Value())); |
| 378 break; | 392 break; |
| 379 } | 393 } |
| 394 case IrOpcode::kFloat64Cbrt: { |
| 395 Float64Matcher m(node->InputAt(0)); |
| 396 if (m.HasValue()) return ReplaceFloat64(base::ieee754::cbrt(m.Value())); |
| 397 break; |
| 398 } |
| 380 case IrOpcode::kFloat64Cos: { | 399 case IrOpcode::kFloat64Cos: { |
| 381 Float64Matcher m(node->InputAt(0)); | 400 Float64Matcher m(node->InputAt(0)); |
| 382 if (m.HasValue()) return ReplaceFloat64(base::ieee754::cos(m.Value())); | 401 if (m.HasValue()) return ReplaceFloat64(base::ieee754::cos(m.Value())); |
| 383 break; | 402 break; |
| 384 } | 403 } |
| 385 case IrOpcode::kFloat64Exp: { | 404 case IrOpcode::kFloat64Exp: { |
| 386 Float64Matcher m(node->InputAt(0)); | 405 Float64Matcher m(node->InputAt(0)); |
| 387 if (m.HasValue()) return ReplaceFloat64(base::ieee754::exp(m.Value())); | 406 if (m.HasValue()) return ReplaceFloat64(base::ieee754::exp(m.Value())); |
| 388 break; | 407 break; |
| 389 } | 408 } |
| 390 case IrOpcode::kFloat64Expm1: { | 409 case IrOpcode::kFloat64Expm1: { |
| 391 Float64Matcher m(node->InputAt(0)); | 410 Float64Matcher m(node->InputAt(0)); |
| 392 if (m.HasValue()) return ReplaceFloat64(base::ieee754::expm1(m.Value())); | 411 if (m.HasValue()) return ReplaceFloat64(base::ieee754::expm1(m.Value())); |
| 393 break; | 412 break; |
| 394 } | 413 } |
| 395 case IrOpcode::kFloat64Log: { | 414 case IrOpcode::kFloat64Log: { |
| 396 Float64Matcher m(node->InputAt(0)); | 415 Float64Matcher m(node->InputAt(0)); |
| 397 if (m.HasValue()) return ReplaceFloat64(base::ieee754::log(m.Value())); | 416 if (m.HasValue()) return ReplaceFloat64(base::ieee754::log(m.Value())); |
| 398 break; | 417 break; |
| 399 } | 418 } |
| 400 case IrOpcode::kFloat64Log1p: { | 419 case IrOpcode::kFloat64Log1p: { |
| 401 Float64Matcher m(node->InputAt(0)); | 420 Float64Matcher m(node->InputAt(0)); |
| 402 if (m.HasValue()) return ReplaceFloat64(base::ieee754::log1p(m.Value())); | 421 if (m.HasValue()) return ReplaceFloat64(base::ieee754::log1p(m.Value())); |
| 403 break; | 422 break; |
| 404 } | 423 } |
| 424 case IrOpcode::kFloat64Log10: { |
| 425 Float64Matcher m(node->InputAt(0)); |
| 426 if (m.HasValue()) return ReplaceFloat64(base::ieee754::log10(m.Value())); |
| 427 break; |
| 428 } |
| 405 case IrOpcode::kFloat64Log2: { | 429 case IrOpcode::kFloat64Log2: { |
| 406 Float64Matcher m(node->InputAt(0)); | 430 Float64Matcher m(node->InputAt(0)); |
| 407 if (m.HasValue()) return ReplaceFloat64(base::ieee754::log2(m.Value())); | 431 if (m.HasValue()) return ReplaceFloat64(base::ieee754::log2(m.Value())); |
| 408 break; | 432 break; |
| 409 } | 433 } |
| 410 case IrOpcode::kFloat64Log10: { | 434 case IrOpcode::kFloat64Pow: { |
| 411 Float64Matcher m(node->InputAt(0)); | 435 Float64BinopMatcher m(node); |
| 412 if (m.HasValue()) return ReplaceFloat64(base::ieee754::log10(m.Value())); | 436 // TODO(bmeurer): Constant fold once we have a unified pow implementation. |
| 413 break; | 437 if (m.right().Is(0.0)) { // x ** +-0.0 => 1.0 |
| 414 } | 438 return ReplaceFloat64(1.0); |
| 415 case IrOpcode::kFloat64Cbrt: { | 439 } else if (m.right().Is(-2.0)) { // x ** -2.0 => 1 / (x * x) |
| 416 Float64Matcher m(node->InputAt(0)); | 440 node->ReplaceInput(0, Float64Constant(1.0)); |
| 417 if (m.HasValue()) return ReplaceFloat64(base::ieee754::cbrt(m.Value())); | 441 node->ReplaceInput(1, Float64Mul(m.left().node(), m.left().node())); |
| 442 NodeProperties::ChangeOp(node, machine()->Float64Div()); |
| 443 return Changed(node); |
| 444 } else if (m.right().Is(2.0)) { // x ** 2.0 => x * x |
| 445 node->ReplaceInput(1, m.left().node()); |
| 446 NodeProperties::ChangeOp(node, machine()->Float64Mul()); |
| 447 return Changed(node); |
| 448 } else if (m.right().Is(-0.5)) { |
| 449 // x ** 0.5 => 1 / (if x <= -Infinity then Infinity else sqrt(0.0 + x)) |
| 450 node->ReplaceInput(0, Float64Constant(1.0)); |
| 451 node->ReplaceInput(1, Float64PowHalf(m.left().node())); |
| 452 NodeProperties::ChangeOp(node, machine()->Float64Div()); |
| 453 return Changed(node); |
| 454 } else if (m.right().Is(0.5)) { |
| 455 // x ** 0.5 => if x <= -Infinity then Infinity else sqrt(0.0 + x) |
| 456 return Replace(Float64PowHalf(m.left().node())); |
| 457 } |
| 418 break; | 458 break; |
| 419 } | 459 } |
| 420 case IrOpcode::kFloat64Sin: { | 460 case IrOpcode::kFloat64Sin: { |
| 421 Float64Matcher m(node->InputAt(0)); | 461 Float64Matcher m(node->InputAt(0)); |
| 422 if (m.HasValue()) return ReplaceFloat64(base::ieee754::sin(m.Value())); | 462 if (m.HasValue()) return ReplaceFloat64(base::ieee754::sin(m.Value())); |
| 423 break; | 463 break; |
| 424 } | 464 } |
| 425 case IrOpcode::kFloat64Tan: { | 465 case IrOpcode::kFloat64Tan: { |
| 426 Float64Matcher m(node->InputAt(0)); | 466 Float64Matcher m(node->InputAt(0)); |
| 427 if (m.HasValue()) return ReplaceFloat64(base::ieee754::tan(m.Value())); | 467 if (m.HasValue()) return ReplaceFloat64(base::ieee754::tan(m.Value())); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 498 case IrOpcode::kFloat64Equal: | 538 case IrOpcode::kFloat64Equal: |
| 499 case IrOpcode::kFloat64LessThan: | 539 case IrOpcode::kFloat64LessThan: |
| 500 case IrOpcode::kFloat64LessThanOrEqual: | 540 case IrOpcode::kFloat64LessThanOrEqual: |
| 501 return ReduceFloat64Compare(node); | 541 return ReduceFloat64Compare(node); |
| 502 default: | 542 default: |
| 503 break; | 543 break; |
| 504 } | 544 } |
| 505 return NoChange(); | 545 return NoChange(); |
| 506 } | 546 } |
| 507 | 547 |
| 508 | |
| 509 Reduction MachineOperatorReducer::ReduceInt32Add(Node* node) { | 548 Reduction MachineOperatorReducer::ReduceInt32Add(Node* node) { |
| 510 DCHECK_EQ(IrOpcode::kInt32Add, node->opcode()); | 549 DCHECK_EQ(IrOpcode::kInt32Add, node->opcode()); |
| 511 Int32BinopMatcher m(node); | 550 Int32BinopMatcher m(node); |
| 512 if (m.right().Is(0)) return Replace(m.left().node()); // x + 0 => x | 551 if (m.right().Is(0)) return Replace(m.left().node()); // x + 0 => x |
| 513 if (m.IsFoldable()) { // K + K => K | 552 if (m.IsFoldable()) { // K + K => K |
| 514 return ReplaceUint32(bit_cast<uint32_t>(m.left().Value()) + | 553 return ReplaceUint32(bit_cast<uint32_t>(m.left().Value()) + |
| 515 bit_cast<uint32_t>(m.right().Value())); | 554 bit_cast<uint32_t>(m.right().Value())); |
| 516 } | 555 } |
| 517 if (m.left().IsInt32Sub()) { | 556 if (m.left().IsInt32Sub()) { |
| 518 Int32BinopMatcher mleft(m.left().node()); | 557 Int32BinopMatcher mleft(m.left().node()); |
| (...skipping 617 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1136 MachineOperatorBuilder* MachineOperatorReducer::machine() const { | 1175 MachineOperatorBuilder* MachineOperatorReducer::machine() const { |
| 1137 return jsgraph()->machine(); | 1176 return jsgraph()->machine(); |
| 1138 } | 1177 } |
| 1139 | 1178 |
| 1140 | 1179 |
| 1141 Graph* MachineOperatorReducer::graph() const { return jsgraph()->graph(); } | 1180 Graph* MachineOperatorReducer::graph() const { return jsgraph()->graph(); } |
| 1142 | 1181 |
| 1143 } // namespace compiler | 1182 } // namespace compiler |
| 1144 } // namespace internal | 1183 } // namespace internal |
| 1145 } // namespace v8 | 1184 } // namespace v8 |
| OLD | NEW |