 Chromium Code Reviews
 Chromium Code Reviews Issue 1948453002:
  [turbofan] Take the immediate size in account when narrowing ia32/x64 word comparison operators.  (Closed) 
  Base URL: https://chromium.googlesource.com/v8/v8.git@master
    
  
    Issue 1948453002:
  [turbofan] Take the immediate size in account when narrowing ia32/x64 word comparison operators.  (Closed) 
  Base URL: https://chromium.googlesource.com/v8/v8.git@master| 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 GetMachineRepresentation(Node* node, | |
| 
Benedikt Meurer
2016/05/04 04:05:47
See ia32 comment.
 
epertoso
2016/05/04 08:58:03
Done.
 | |
| 1504 MachineRepresentation* representation) { | |
| 1505 if (node->opcode() == IrOpcode::kLoad) { | |
| 1506 *representation = LoadRepresentationOf(node->op()).representation(); | |
| 1507 return true; | |
| 1508 } | |
| 1509 uint64_t value = 0; | |
| 1510 switch (node->opcode()) { | |
| 1511 case IrOpcode::kInt32Constant: | |
| 1512 value = OpParameter<uint32_t>(node->op()); | |
| 
Benedikt Meurer
2016/05/04 04:05:47
See ia32 comment.
 
epertoso
2016/05/04 08:58:03
Done.
 | |
| 1513 break; | |
| 1514 case IrOpcode::kInt64Constant: | |
| 1515 value = OpParameter<uint64_t>(node->op()); | |
| 
Benedikt Meurer
2016/05/04 04:05:47
See ia32 comment.
 
epertoso
2016/05/04 08:58:03
Done.
 | |
| 1516 break; | |
| 1517 default: | |
| 1518 return false; | |
| 1519 } | |
| 1520 if (is_uint8(value)) { | |
| 1521 *representation = MachineRepresentation::kWord8; | |
| 1522 } else if (is_uint16(value)) { | |
| 1523 *representation = MachineRepresentation::kWord16; | |
| 1524 } else if (is_uint32(value)) { | |
| 1525 *representation = MachineRepresentation::kWord32; | |
| 1526 } else { | |
| 1527 return false; | |
| 1528 } | |
| 1529 return true; | |
| 1530 } | |
| 1531 | |
| 1503 // Tries to match the size of the given opcode to that of the operands, if | 1532 // Tries to match the size of the given opcode to that of the operands, if | 
| 1504 // possible. | 1533 // possible. | 
| 1505 InstructionCode TryNarrowOpcodeSize(InstructionCode opcode, Node* left, | 1534 InstructionCode TryNarrowOpcodeSize(InstructionCode opcode, Node* left, | 
| 1506 Node* right) { | 1535 Node* right) { | 
| 1507 if (opcode != kX64Cmp32 && opcode != kX64Test32) { | 1536 if (opcode != kX64Cmp32 && opcode != kX64Test32) { | 
| 1508 return opcode; | 1537 return opcode; | 
| 1509 } | 1538 } | 
| 1510 // Currently, if one of the two operands is not a Load, we don't know what its | 1539 // We only do this if at least one of the two operands is a load. | 
| 1511 // machine representation is, so we bail out. | 1540 // TODO(epertoso): we can probably get some size information out of phi nodes. | 
| 1512 // TODO(epertoso): we can probably get some size information out of immediates | 1541 if (left->opcode() != IrOpcode::kLoad && right->opcode() != IrOpcode::kLoad) { | 
| 1513 // and phi nodes. | |
| 1514 if (left->opcode() != IrOpcode::kLoad || right->opcode() != IrOpcode::kLoad) { | |
| 1515 return opcode; | 1542 return opcode; | 
| 1516 } | 1543 } | 
| 1517 // If the load representations don't match, both operands will be | 1544 MachineRepresentation left_representation, right_representation; | 
| 1518 // zero/sign-extended to 32bit. | 1545 if (!GetMachineRepresentation(left, &left_representation) || | 
| 1519 LoadRepresentation left_representation = LoadRepresentationOf(left->op()); | 1546 !GetMachineRepresentation(right, &right_representation)) { | 
| 1520 if (left_representation != LoadRepresentationOf(right->op())) { | |
| 1521 return opcode; | 1547 return opcode; | 
| 1522 } | 1548 } | 
| 1523 switch (left_representation.representation()) { | 1549 // If the representations don't match, both operands will be | 
| 1550 // zero/sign-extended to 32bit. | |
| 1551 if (left_representation != right_representation) { | |
| 1552 return opcode; | |
| 1553 } | |
| 1554 switch (left_representation) { | |
| 1524 case MachineRepresentation::kBit: | 1555 case MachineRepresentation::kBit: | 
| 1525 case MachineRepresentation::kWord8: | 1556 case MachineRepresentation::kWord8: | 
| 1526 return opcode == kX64Cmp32 ? kX64Cmp8 : kX64Test8; | 1557 return opcode == kX64Cmp32 ? kX64Cmp8 : kX64Test8; | 
| 1527 case MachineRepresentation::kWord16: | 1558 case MachineRepresentation::kWord16: | 
| 1528 return opcode == kX64Cmp32 ? kX64Cmp16 : kX64Test16; | 1559 return opcode == kX64Cmp32 ? kX64Cmp16 : kX64Test16; | 
| 1529 default: | 1560 default: | 
| 1530 return opcode; | 1561 return opcode; | 
| 1531 } | 1562 } | 
| 1532 } | 1563 } | 
| 1533 | 1564 | 
| 1534 // Shared routine for multiple word compare operations. | 1565 // Shared routine for multiple word compare operations. | 
| 1535 void VisitWordCompare(InstructionSelector* selector, Node* node, | 1566 void VisitWordCompare(InstructionSelector* selector, Node* node, | 
| 1536 InstructionCode opcode, FlagsContinuation* cont) { | 1567 InstructionCode opcode, FlagsContinuation* cont) { | 
| 1537 X64OperandGenerator g(selector); | 1568 X64OperandGenerator g(selector); | 
| 1538 Node* left = node->InputAt(0); | 1569 Node* left = node->InputAt(0); | 
| 1539 Node* right = node->InputAt(1); | 1570 Node* right = node->InputAt(1); | 
| 1540 | 1571 | 
| 1541 opcode = TryNarrowOpcodeSize(opcode, left, right); | 1572 InstructionCode narrowed_opcode = TryNarrowOpcodeSize(opcode, left, right); | 
| 1542 | 1573 | 
| 1543 // If one of the two inputs is an immediate, make sure it's on the right, or | 1574 // If one of the two inputs is an immediate, make sure it's on the right, or | 
| 1544 // if one of the two inputs is a memory operand, make sure it's on the left. | 1575 // if one of the two inputs is a memory operand, make sure it's on the left. | 
| 1545 if ((!g.CanBeImmediate(right) && g.CanBeImmediate(left)) || | 1576 if ((!g.CanBeImmediate(right) && g.CanBeImmediate(left)) || | 
| 1546 (g.CanBeMemoryOperand(opcode, node, right) && | 1577 (g.CanBeMemoryOperand(narrowed_opcode, node, right) && | 
| 1547 !g.CanBeMemoryOperand(opcode, node, left))) { | 1578 !g.CanBeMemoryOperand(narrowed_opcode, node, left))) { | 
| 1548 if (!node->op()->HasProperty(Operator::kCommutative)) cont->Commute(); | 1579 if (!node->op()->HasProperty(Operator::kCommutative)) cont->Commute(); | 
| 1549 std::swap(left, right); | 1580 std::swap(left, right); | 
| 1550 } | 1581 } | 
| 1551 | 1582 | 
| 1552 // Match immediates on right side of comparison. | 1583 // Match immediates on right side of comparison. | 
| 1553 if (g.CanBeImmediate(right)) { | 1584 if (g.CanBeImmediate(right)) { | 
| 1554 if (g.CanBeMemoryOperand(opcode, node, left)) { | 1585 if (g.CanBeMemoryOperand(narrowed_opcode, node, left)) { | 
| 1555 return VisitCompareWithMemoryOperand(selector, opcode, left, | 1586 // If we're truncating the immediate (32 bits to 16 or 8), comparison | 
| 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, | |
| 1556 g.UseImmediate(right), cont); | 1612 g.UseImmediate(right), cont); | 
| 1557 } | 1613 } | 
| 1558 return VisitCompare(selector, opcode, g.Use(left), g.UseImmediate(right), | 1614 return VisitCompare(selector, opcode, g.Use(left), g.UseImmediate(right), | 
| 1559 cont); | 1615 cont); | 
| 1560 } | 1616 } | 
| 1561 | 1617 | 
| 1562 // Match memory operands on left side of comparison. | 1618 // Match memory operands on left side of comparison. | 
| 1563 if (g.CanBeMemoryOperand(opcode, node, left)) { | 1619 if (g.CanBeMemoryOperand(narrowed_opcode, node, left)) { | 
| 1564 return VisitCompareWithMemoryOperand(selector, opcode, left, | 1620 return VisitCompareWithMemoryOperand(selector, narrowed_opcode, left, | 
| 1565 g.UseRegister(right), cont); | 1621 g.UseRegister(right), cont); | 
| 1566 } | 1622 } | 
| 1567 | 1623 | 
| 1568 if (g.CanBeBetterLeftOperand(right)) { | 1624 if (g.CanBeBetterLeftOperand(right)) { | 
| 1569 if (!node->op()->HasProperty(Operator::kCommutative)) cont->Commute(); | 1625 if (!node->op()->HasProperty(Operator::kCommutative)) cont->Commute(); | 
| 1570 std::swap(left, right); | 1626 std::swap(left, right); | 
| 1571 } | 1627 } | 
| 1572 | 1628 | 
| 1573 return VisitCompare(selector, opcode, left, right, cont, | 1629 return VisitCompare(selector, opcode, left, right, cont, | 
| 1574 node->op()->HasProperty(Operator::kCommutative)); | 1630 node->op()->HasProperty(Operator::kCommutative)); | 
| (...skipping 477 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2052 MachineOperatorBuilder::kFloat64RoundTruncate | | 2108 MachineOperatorBuilder::kFloat64RoundTruncate | | 
| 2053 MachineOperatorBuilder::kFloat32RoundTiesEven | | 2109 MachineOperatorBuilder::kFloat32RoundTiesEven | | 
| 2054 MachineOperatorBuilder::kFloat64RoundTiesEven; | 2110 MachineOperatorBuilder::kFloat64RoundTiesEven; | 
| 2055 } | 2111 } | 
| 2056 return flags; | 2112 return flags; | 
| 2057 } | 2113 } | 
| 2058 | 2114 | 
| 2059 } // namespace compiler | 2115 } // namespace compiler | 
| 2060 } // namespace internal | 2116 } // namespace internal | 
| 2061 } // namespace v8 | 2117 } // namespace v8 | 
| OLD | NEW |