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 <algorithm> | 5 #include <algorithm> |
6 | 6 |
7 #include "src/base/adapters.h" | 7 #include "src/base/adapters.h" |
8 #include "src/compiler/instruction-selector-impl.h" | 8 #include "src/compiler/instruction-selector-impl.h" |
9 #include "src/compiler/node-matchers.h" | 9 #include "src/compiler/node-matchers.h" |
10 #include "src/compiler/node-properties.h" | 10 #include "src/compiler/node-properties.h" |
(...skipping 1482 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1493 void VisitCompare(InstructionSelector* selector, InstructionCode opcode, | 1493 void VisitCompare(InstructionSelector* selector, InstructionCode opcode, |
1494 Node* left, Node* right, FlagsContinuation* cont, | 1494 Node* left, Node* right, FlagsContinuation* cont, |
1495 bool commutative) { | 1495 bool commutative) { |
1496 X64OperandGenerator g(selector); | 1496 X64OperandGenerator g(selector); |
1497 if (commutative && g.CanBeBetterLeftOperand(right)) { | 1497 if (commutative && g.CanBeBetterLeftOperand(right)) { |
1498 std::swap(left, right); | 1498 std::swap(left, right); |
1499 } | 1499 } |
1500 VisitCompare(selector, opcode, g.UseRegister(left), g.Use(right), cont); | 1500 VisitCompare(selector, opcode, g.UseRegister(left), g.Use(right), cont); |
1501 } | 1501 } |
1502 | 1502 |
1503 bool InferMachineRepresentation(Node* node, | |
1504 MachineRepresentation* representation) { | |
1505 if (node->opcode() == IrOpcode::kLoad) { | |
1506 *representation = LoadRepresentationOf(node->op()).representation(); | |
1507 return true; | |
1508 } | |
1509 int64_t value = 0; | |
1510 switch (node->opcode()) { | |
1511 case IrOpcode::kInt32Constant: | |
1512 value = OpParameter<int32_t>(node->op()); | |
1513 break; | |
1514 case IrOpcode::kInt64Constant: | |
1515 value = OpParameter<int64_t>(node->op()); | |
1516 break; | |
1517 default: | |
1518 return false; | |
1519 } | |
1520 if (is_int8(value)) { | |
1521 *representation = MachineRepresentation::kWord8; | |
1522 } else if (is_int16(value)) { | |
1523 *representation = MachineRepresentation::kWord16; | |
1524 } else if (is_int32(value)) { | |
1525 *representation = MachineRepresentation::kWord32; | |
1526 } else { | |
1527 return false; | |
1528 } | |
1529 return true; | |
1530 } | |
1531 | |
1532 // Tries to match the size of the given opcode to that of the operands, if | 1503 // Tries to match the size of the given opcode to that of the operands, if |
1533 // possible. | 1504 // possible. |
1534 InstructionCode TryNarrowOpcodeSize(InstructionCode opcode, Node* left, | 1505 InstructionCode TryNarrowOpcodeSize(InstructionCode opcode, Node* left, |
1535 Node* right) { | 1506 Node* right) { |
1536 if (opcode != kX64Cmp32 && opcode != kX64Test32) { | 1507 if (opcode != kX64Cmp32 && opcode != kX64Test32) { |
1537 return opcode; | 1508 return opcode; |
1538 } | 1509 } |
1539 // We only do this if at least one of the two operands is a load. | 1510 // Currently, if one of the two operands is not a Load, we don't know what its |
1540 // TODO(epertoso): we can probably get some size information out of phi nodes. | 1511 // machine representation is, so we bail out. |
1541 if (left->opcode() != IrOpcode::kLoad && right->opcode() != IrOpcode::kLoad) { | 1512 // TODO(epertoso): we can probably get some size information out of immediates |
| 1513 // and phi nodes. |
| 1514 if (left->opcode() != IrOpcode::kLoad || right->opcode() != IrOpcode::kLoad) { |
1542 return opcode; | 1515 return opcode; |
1543 } | 1516 } |
1544 MachineRepresentation left_representation, right_representation; | 1517 // If the load representations don't match, both operands will be |
1545 if (!InferMachineRepresentation(left, &left_representation) || | 1518 // zero/sign-extended to 32bit. |
1546 !InferMachineRepresentation(right, &right_representation)) { | 1519 LoadRepresentation left_representation = LoadRepresentationOf(left->op()); |
| 1520 if (left_representation != LoadRepresentationOf(right->op())) { |
1547 return opcode; | 1521 return opcode; |
1548 } | 1522 } |
1549 // If the representations don't match, both operands will be | 1523 switch (left_representation.representation()) { |
1550 // zero/sign-extended to 32bit. | |
1551 if (left_representation != right_representation) { | |
1552 return opcode; | |
1553 } | |
1554 switch (left_representation) { | |
1555 case MachineRepresentation::kBit: | 1524 case MachineRepresentation::kBit: |
1556 case MachineRepresentation::kWord8: | 1525 case MachineRepresentation::kWord8: |
1557 return opcode == kX64Cmp32 ? kX64Cmp8 : kX64Test8; | 1526 return opcode == kX64Cmp32 ? kX64Cmp8 : kX64Test8; |
1558 case MachineRepresentation::kWord16: | 1527 case MachineRepresentation::kWord16: |
1559 return opcode == kX64Cmp32 ? kX64Cmp16 : kX64Test16; | 1528 return opcode == kX64Cmp32 ? kX64Cmp16 : kX64Test16; |
1560 default: | 1529 default: |
1561 return opcode; | 1530 return opcode; |
1562 } | 1531 } |
1563 } | 1532 } |
1564 | 1533 |
1565 // Shared routine for multiple word compare operations. | 1534 // Shared routine for multiple word compare operations. |
1566 void VisitWordCompare(InstructionSelector* selector, Node* node, | 1535 void VisitWordCompare(InstructionSelector* selector, Node* node, |
1567 InstructionCode opcode, FlagsContinuation* cont) { | 1536 InstructionCode opcode, FlagsContinuation* cont) { |
1568 X64OperandGenerator g(selector); | 1537 X64OperandGenerator g(selector); |
1569 Node* left = node->InputAt(0); | 1538 Node* left = node->InputAt(0); |
1570 Node* right = node->InputAt(1); | 1539 Node* right = node->InputAt(1); |
1571 | 1540 |
1572 InstructionCode narrowed_opcode = TryNarrowOpcodeSize(opcode, left, right); | 1541 opcode = TryNarrowOpcodeSize(opcode, left, right); |
1573 | 1542 |
1574 // If one of the two inputs is an immediate, make sure it's on the right, or | 1543 // If one of the two inputs is an immediate, make sure it's on the right, or |
1575 // if one of the two inputs is a memory operand, make sure it's on the left. | 1544 // if one of the two inputs is a memory operand, make sure it's on the left. |
1576 if ((!g.CanBeImmediate(right) && g.CanBeImmediate(left)) || | 1545 if ((!g.CanBeImmediate(right) && g.CanBeImmediate(left)) || |
1577 (g.CanBeMemoryOperand(narrowed_opcode, node, right) && | 1546 (g.CanBeMemoryOperand(opcode, node, right) && |
1578 !g.CanBeMemoryOperand(narrowed_opcode, node, left))) { | 1547 !g.CanBeMemoryOperand(opcode, node, left))) { |
1579 if (!node->op()->HasProperty(Operator::kCommutative)) cont->Commute(); | 1548 if (!node->op()->HasProperty(Operator::kCommutative)) cont->Commute(); |
1580 std::swap(left, right); | 1549 std::swap(left, right); |
1581 } | 1550 } |
1582 | 1551 |
1583 // Match immediates on right side of comparison. | 1552 // Match immediates on right side of comparison. |
1584 if (g.CanBeImmediate(right)) { | 1553 if (g.CanBeImmediate(right)) { |
1585 if (g.CanBeMemoryOperand(narrowed_opcode, node, left)) { | 1554 if (g.CanBeMemoryOperand(opcode, node, left)) { |
1586 // If we're truncating the immediate (32 bits to 16 or 8), comparison | 1555 return VisitCompareWithMemoryOperand(selector, opcode, left, |
1587 // semantics should take the signedness/unsignedness of the op into | |
1588 // account. | |
1589 if (narrowed_opcode != opcode && | |
1590 LoadRepresentationOf(left->op()).IsUnsigned()) { | |
1591 switch (cont->condition()) { | |
1592 case FlagsCondition::kSignedLessThan: | |
1593 cont->OverwriteAndNegateIfEqual(FlagsCondition::kUnsignedLessThan); | |
1594 break; | |
1595 case FlagsCondition::kSignedGreaterThan: | |
1596 cont->OverwriteAndNegateIfEqual( | |
1597 FlagsCondition::kUnsignedGreaterThan); | |
1598 break; | |
1599 case FlagsCondition::kSignedLessThanOrEqual: | |
1600 cont->OverwriteAndNegateIfEqual( | |
1601 FlagsCondition::kUnsignedLessThanOrEqual); | |
1602 break; | |
1603 case FlagsCondition::kSignedGreaterThanOrEqual: | |
1604 cont->OverwriteAndNegateIfEqual( | |
1605 FlagsCondition::kUnsignedGreaterThanOrEqual); | |
1606 break; | |
1607 default: | |
1608 break; | |
1609 } | |
1610 } | |
1611 return VisitCompareWithMemoryOperand(selector, narrowed_opcode, left, | |
1612 g.UseImmediate(right), cont); | 1556 g.UseImmediate(right), cont); |
1613 } | 1557 } |
1614 return VisitCompare(selector, opcode, g.Use(left), g.UseImmediate(right), | 1558 return VisitCompare(selector, opcode, g.Use(left), g.UseImmediate(right), |
1615 cont); | 1559 cont); |
1616 } | 1560 } |
1617 | 1561 |
1618 // Match memory operands on left side of comparison. | 1562 // Match memory operands on left side of comparison. |
1619 if (g.CanBeMemoryOperand(narrowed_opcode, node, left)) { | 1563 if (g.CanBeMemoryOperand(opcode, node, left)) { |
1620 return VisitCompareWithMemoryOperand(selector, narrowed_opcode, left, | 1564 return VisitCompareWithMemoryOperand(selector, opcode, left, |
1621 g.UseRegister(right), cont); | 1565 g.UseRegister(right), cont); |
1622 } | 1566 } |
1623 | 1567 |
1624 if (g.CanBeBetterLeftOperand(right)) { | 1568 if (g.CanBeBetterLeftOperand(right)) { |
1625 if (!node->op()->HasProperty(Operator::kCommutative)) cont->Commute(); | 1569 if (!node->op()->HasProperty(Operator::kCommutative)) cont->Commute(); |
1626 std::swap(left, right); | 1570 std::swap(left, right); |
1627 } | 1571 } |
1628 | 1572 |
1629 return VisitCompare(selector, opcode, left, right, cont, | 1573 return VisitCompare(selector, opcode, left, right, cont, |
1630 node->op()->HasProperty(Operator::kCommutative)); | 1574 node->op()->HasProperty(Operator::kCommutative)); |
(...skipping 515 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2146 MachineOperatorBuilder::kFloat64RoundTruncate | | 2090 MachineOperatorBuilder::kFloat64RoundTruncate | |
2147 MachineOperatorBuilder::kFloat32RoundTiesEven | | 2091 MachineOperatorBuilder::kFloat32RoundTiesEven | |
2148 MachineOperatorBuilder::kFloat64RoundTiesEven; | 2092 MachineOperatorBuilder::kFloat64RoundTiesEven; |
2149 } | 2093 } |
2150 return flags; | 2094 return flags; |
2151 } | 2095 } |
2152 | 2096 |
2153 } // namespace compiler | 2097 } // namespace compiler |
2154 } // namespace internal | 2098 } // namespace internal |
2155 } // namespace v8 | 2099 } // namespace v8 |
OLD | NEW |