Commit 2983d84e authored by Benedikt Meurer's avatar Benedikt Meurer

[turbofan] Combine additional Word32And with Int32Add and negative power of two.

- (y << L + x) & (-1 << L) => (x & (-1 << L)) + y << L
- (x + y << L) & (-1 << L) => (x & (-1 << L)) + y << L

TEST=unittests
R=jarin@chromium.org

Review URL: https://codereview.chromium.org/758603003

Cr-Commit-Position: refs/heads/master@{#25627}
parent a07045af
......@@ -779,13 +779,37 @@ Reduction MachineOperatorReducer::ReduceWord32And(Node* node) {
Int32BinopMatcher mleft(m.left().node());
if (mleft.right().HasValue() &&
(mleft.right().Value() & m.right().Value()) == mleft.right().Value()) {
// (x + K) & K => (x & K) + K
// (x + (K << L)) & (-1 << L) => (x & (-1 << L)) + (K << L)
return Replace(graph()->NewNode(
machine()->Int32Add(),
graph()->NewNode(machine()->Word32And(), mleft.left().node(),
m.right().node()),
mleft.right().node()));
}
if (mleft.left().IsWord32Shl()) {
Int32BinopMatcher mleftleft(mleft.left().node());
if (mleftleft.right().Is(
base::bits::CountTrailingZeros32(m.right().Value()))) {
// (y << L + x) & (-1 << L) => (x & (-1 << L)) + y << L
return Replace(graph()->NewNode(
machine()->Int32Add(),
graph()->NewNode(machine()->Word32And(), mleft.right().node(),
m.right().node()),
mleftleft.node()));
}
}
if (mleft.right().IsWord32Shl()) {
Int32BinopMatcher mleftright(mleft.right().node());
if (mleftright.right().Is(
base::bits::CountTrailingZeros32(m.right().Value()))) {
// (x + y << L) & (-1 << L) => (x & (-1 << L)) + y << L
return Replace(graph()->NewNode(
machine()->Int32Add(),
graph()->NewNode(machine()->Word32And(), mleft.left().node(),
m.right().node()),
mleftright.node()));
}
}
}
return NoChange();
}
......
......@@ -532,9 +532,10 @@ TEST_F(MachineOperatorReducerTest, Word32AndWithWord32AndWithConstant) {
TEST_F(MachineOperatorReducerTest, Word32AndWithInt32AddAndConstant) {
Node* const p0 = Parameter(0);
Node* const p1 = Parameter(1);
TRACED_FOREACH(int32_t, k, kInt32Values) {
TRACED_FORRANGE(int32_t, l, 1, 31) {
TRACED_FORRANGE(int32_t, l, 1, 31) {
TRACED_FOREACH(int32_t, k, kInt32Values) {
// (x + (K << L)) & (-1 << L) => (x & (-1 << L)) + (K << L)
Reduction const r = Reduce(graph()->NewNode(
machine()->Word32And(),
......@@ -545,6 +546,24 @@ TEST_F(MachineOperatorReducerTest, Word32AndWithInt32AddAndConstant) {
IsInt32Add(IsWord32And(p0, IsInt32Constant(-1 << l)),
IsInt32Constant(k << l)));
}
Node* s1 = graph()->NewNode(machine()->Word32Shl(), p1, Int32Constant(l));
// (y << L + x) & (-1 << L) => (x & (-1 << L)) + y << L
Reduction const r1 = Reduce(graph()->NewNode(
machine()->Word32And(), graph()->NewNode(machine()->Int32Add(), s1, p0),
Int32Constant(-1 << l)));
ASSERT_TRUE(r1.Changed());
EXPECT_THAT(r1.replacement(),
IsInt32Add(IsWord32And(p0, IsInt32Constant(-1 << l)), s1));
// (x + y << L) & (-1 << L) => (x & (-1 << L)) + y << L
Reduction const r2 = Reduce(graph()->NewNode(
machine()->Word32And(), graph()->NewNode(machine()->Int32Add(), p0, s1),
Int32Constant(-1 << l)));
ASSERT_TRUE(r2.Changed());
EXPECT_THAT(r2.replacement(),
IsInt32Add(IsWord32And(p0, IsInt32Constant(-1 << l)), s1));
}
}
......
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