Commit c875a643 authored by Sigurd Schneider's avatar Sigurd Schneider Committed by Commit Bot

[turbofan] Reduce consecutive machine additions with constants

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.

This helps reducing awkward sequences like
  leaq r12,[r9*8+0x8]
  movq r12,[rbx+r12*1-0x1]
to
  movq r12,[rax+r9*8+0x7]


Change-Id: Iaa3a1cb9136a3f905ba33c62e16c3cb3c117605c
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1627544
Auto-Submit: Sigurd Schneider <sigurds@chromium.org>
Reviewed-by: 's avatarBenedikt Meurer <bmeurer@chromium.org>
Reviewed-by: 's avatarJaroslav Sevcik <jarin@chromium.org>
Commit-Queue: Sigurd Schneider <sigurds@chromium.org>
Cr-Commit-Position: refs/heads/master@{#61817}
parent 35b611e0
......@@ -751,6 +751,16 @@ Reduction MachineOperatorReducer::ReduceInt32Add(Node* node) {
return reduction.Changed() ? reduction : Changed(node);
}
}
// (x + Int32Constant(a)) + Int32Constant(b)) => x + Int32Constant(a + b)
if (m.right().HasValue() && m.left().IsInt32Add()) {
Int32BinopMatcher n(m.left().node());
if (n.right().HasValue()) {
node->ReplaceInput(1, Int32Constant(base::AddWithWraparound(
m.right().Value(), n.right().Value())));
node->ReplaceInput(0, n.left().node());
return Changed(node);
}
}
return NoChange();
}
......@@ -762,6 +772,16 @@ Reduction MachineOperatorReducer::ReduceInt64Add(Node* node) {
return ReplaceInt64(
base::AddWithWraparound(m.left().Value(), m.right().Value()));
}
// (x + Int64Constant(a)) + Int64Constant(b)) => x + Int64Constant(a + b)
if (m.right().HasValue() && m.left().IsInt64Add()) {
Int64BinopMatcher n(m.left().node());
if (n.right().HasValue()) {
node->ReplaceInput(1, Int64Constant(base::AddWithWraparound(
m.right().Value(), n.right().Value())));
node->ReplaceInput(0, n.left().node());
return Changed(node);
}
}
return NoChange();
}
......
......@@ -1414,6 +1414,39 @@ TEST_F(MachineOperatorReducerTest, Int32AddWithInt32SubWithConstantZero) {
EXPECT_THAT(r2.replacement(), IsInt32Sub(p0, p1));
}
TEST_F(MachineOperatorReducerTest, Int32AddMergeConstants) {
Node* const p0 = Parameter(0);
Reduction const r1 = Reduce(graph()->NewNode(
machine()->Int32Add(),
graph()->NewNode(machine()->Int32Add(), p0, Int32Constant(1)),
Int32Constant(2)));
ASSERT_TRUE(r1.Changed());
EXPECT_THAT(r1.replacement(), IsInt32Add(p0, IsInt32Constant(3)));
Reduction const r2 = Reduce(graph()->NewNode(
machine()->Int32Add(), Int32Constant(2),
graph()->NewNode(machine()->Int32Add(), p0, Int32Constant(1))));
ASSERT_TRUE(r2.Changed());
EXPECT_THAT(r2.replacement(), IsInt32Add(p0, IsInt32Constant(3)));
}
TEST_F(MachineOperatorReducerTest, Int64AddMergeConstants) {
Node* const p0 = Parameter(0);
Reduction const r1 = Reduce(graph()->NewNode(
machine()->Int64Add(),
graph()->NewNode(machine()->Int64Add(), p0, Int64Constant(1)),
Int64Constant(2)));
ASSERT_TRUE(r1.Changed());
EXPECT_THAT(r1.replacement(), IsInt64Add(p0, IsInt64Constant(3)));
Reduction const r2 = Reduce(graph()->NewNode(
machine()->Int64Add(), Int64Constant(2),
graph()->NewNode(machine()->Int64Add(), p0, Int64Constant(1))));
ASSERT_TRUE(r2.Changed());
EXPECT_THAT(r2.replacement(), IsInt64Add(p0, IsInt64Constant(3)));
}
// -----------------------------------------------------------------------------
// 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