Commit e3bbf2bf authored by Z Nguyen-Huu's avatar Z Nguyen-Huu Committed by Commit Bot

[turbofan] Reduce consecutive machine multiplication with constants

There exists such optimization for additions but not for multiplication.

This adds optimizations that apply the reductions
  (x * Int32Constant(a)) * Int32Constant(b)) => x * Int32Constant(a * b)
  (x * Int64Constant(a)) * Int64Constant(b)) => x * Int64Constant(a * b)
to the TurboFan graph.

Bug: v8:10305
Change-Id: I28f72c2b7d8ff0f758a0a08b69fb3763557a6241
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2360327
Commit-Queue: Z Nguyen-Huu <duongn@microsoft.com>
Reviewed-by: 's avatarGeorg Neis <neis@chromium.org>
Cr-Commit-Position: refs/heads/master@{#69462}
parent 9b317d2d
......@@ -356,6 +356,16 @@ Reduction MachineOperatorReducer::Reduce(Node* node) {
NodeProperties::ChangeOp(node, machine()->Word32Shl());
return Changed(node).FollowedBy(ReduceWord32Shl(node));
}
// (x * Int32Constant(a)) * Int32Constant(b)) => x * Int32Constant(a * b)
if (m.right().HasValue() && m.left().IsInt32Mul()) {
Int32BinopMatcher n(m.left().node());
if (n.right().HasValue() && m.OwnsInput(m.left().node())) {
node->ReplaceInput(1, Int32Constant(base::MulWithWraparound(
m.right().Value(), n.right().Value())));
node->ReplaceInput(0, n.left().node());
return Changed(node);
}
}
break;
}
case IrOpcode::kInt32MulWithOverflow: {
......@@ -1005,6 +1015,16 @@ Reduction MachineOperatorReducer::ReduceInt64Mul(Node* node) {
NodeProperties::ChangeOp(node, machine()->Word64Shl());
return Changed(node).FollowedBy(ReduceWord64Shl(node));
}
// (x * Int64Constant(a)) * Int64Constant(b)) => x * Int64Constant(a * b)
if (m.right().HasValue() && m.left().IsInt64Mul()) {
Int64BinopMatcher n(m.left().node());
if (n.right().HasValue() && m.OwnsInput(m.left().node())) {
node->ReplaceInput(1, Int64Constant(base::MulWithWraparound(
m.right().Value(), n.right().Value())));
node->ReplaceInput(0, n.left().node());
return Changed(node);
}
}
return NoChange();
}
......
......@@ -1686,10 +1686,8 @@ TEST_F(MachineOperatorReducerTest, Uint32ModWithParameters) {
EXPECT_THAT(r.replacement(), IsInt32Constant(0));
}
// -----------------------------------------------------------------------------
// Int32Add
// Int32Add, Int64Add
TEST_F(MachineOperatorReducerTest, Int32AddWithInt32SubWithConstantZero) {
Node* const p0 = Parameter(0);
......@@ -1742,6 +1740,43 @@ TEST_F(MachineOperatorReducerTest, Int64AddMergeConstants) {
EXPECT_THAT(r2.replacement(), IsInt64Add(p0, IsInt64Constant(3)));
}
// -----------------------------------------------------------------------------
// Int32Mul, Int64Mul
TEST_F(MachineOperatorReducerTest, Int32MulMergeConstants) {
Node* const p0 = Parameter(0);
Reduction const r1 = Reduce(graph()->NewNode(
machine()->Int32Mul(),
graph()->NewNode(machine()->Int32Mul(), p0, Int32Constant(5)),
Int32Constant(3)));
ASSERT_TRUE(r1.Changed());
EXPECT_THAT(r1.replacement(), IsInt32Mul(p0, IsInt32Constant(15)));
Reduction const r2 = Reduce(graph()->NewNode(
machine()->Int32Mul(), Int32Constant(5),
graph()->NewNode(machine()->Int32Mul(), p0, Int32Constant(3))));
ASSERT_TRUE(r2.Changed());
EXPECT_THAT(r2.replacement(), IsInt32Mul(p0, IsInt32Constant(15)));
}
TEST_F(MachineOperatorReducerTest, Int64MulMergeConstants) {
Node* const p0 = Parameter(0);
Reduction const r1 = Reduce(graph()->NewNode(
machine()->Int64Mul(),
graph()->NewNode(machine()->Int64Mul(), p0, Int64Constant(5)),
Int64Constant(3)));
ASSERT_TRUE(r1.Changed());
EXPECT_THAT(r1.replacement(), IsInt64Mul(p0, IsInt64Constant(15)));
Reduction const r2 = Reduce(graph()->NewNode(
machine()->Int64Mul(), Int64Constant(5),
graph()->NewNode(machine()->Int64Mul(), p0, Int64Constant(3))));
ASSERT_TRUE(r2.Changed());
EXPECT_THAT(r2.replacement(), IsInt64Mul(p0, IsInt64Constant(15)));
}
// -----------------------------------------------------------------------------
// Int32AddWithOverflow
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment