[turbofan] Simplifying (x+k1)==k2 into x==(k2-k1)

Change-Id: I234da79e1f53fa0fc15494fe6d31742d4e6eea97
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/4055393
Commit-Queue: Jianxiao Lu <jianxiao.lu@intel.com>
Reviewed-by: Tobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/main@{#84947}
This commit is contained in:
JianxiaoLuIntel 2022-12-20 09:32:00 +08:00 committed by V8 LUCI CQ
parent 99eba76742
commit e9333ebd3c
2 changed files with 101 additions and 0 deletions

View File

@ -2551,6 +2551,19 @@ Reduction MachineOperatorReducer::ReduceWord32Equal(Node* node) {
node->ReplaceInput(1, Uint32Constant(replacements->second)); node->ReplaceInput(1, Uint32Constant(replacements->second));
return Changed(node); return Changed(node);
} }
// Simplifying (x+k1)==k2 into x==k2-k1.
if (m.left().IsInt32Add() && m.right().IsInt32Constant()) {
Int32AddMatcher m_add(m.left().node());
if (m_add.right().IsInt32Constant()) {
int32_t lte_right = m.right().ResolvedValue();
int32_t add_right = m_add.right().ResolvedValue();
// No need to consider overflow in this condition (==).
node->ReplaceInput(0, m_add.left().node());
node->ReplaceInput(1, Int32Constant(lte_right - add_right));
return Changed(node);
}
}
} }
return NoChange(); return NoChange();
@ -2579,6 +2592,19 @@ Reduction MachineOperatorReducer::ReduceWord64Equal(Node* node) {
return Changed(node); return Changed(node);
} }
// Simplifying (x+k1)==k2 into x==k2-k1.
if (m.left().IsInt64Add() && m.right().IsInt64Constant()) {
Int64AddMatcher m_add(m.left().node());
if (m_add.right().IsInt64Constant()) {
int64_t lte_right = m.right().ResolvedValue();
int64_t add_right = m_add.right().ResolvedValue();
// No need to consider overflow in this condition (==).
node->ReplaceInput(0, m_add.left().node());
node->ReplaceInput(1, Int64Constant(lte_right - add_right));
return Changed(node);
}
}
/* /*
If Int64Constant(c) can be casted from an Int32Constant: If Int64Constant(c) can be casted from an Int32Constant:
------------------------------------------------- -------------------------------------------------

View File

@ -4,6 +4,7 @@
#include "src/compiler/machine-operator-reducer.h" #include "src/compiler/machine-operator-reducer.h"
#include <cstdint>
#include <limits> #include <limits>
#include "src/base/bits.h" #include "src/base/bits.h"
@ -11,6 +12,7 @@
#include "src/base/ieee754.h" #include "src/base/ieee754.h"
#include "src/base/overflowing-math.h" #include "src/base/overflowing-math.h"
#include "src/builtins/builtins.h" #include "src/builtins/builtins.h"
#include "src/common/globals.h"
#include "src/compiler/js-graph.h" #include "src/compiler/js-graph.h"
#include "src/compiler/machine-operator.h" #include "src/compiler/machine-operator.h"
#include "src/numbers/conversions-inl.h" #include "src/numbers/conversions-inl.h"
@ -1398,6 +1400,21 @@ TEST_F(MachineOperatorReducerTest,
} }
} }
TEST_F(MachineOperatorReducerTest, Word32EqualWithAddAndConstant) {
// (x+k1)==k2 => x==(k2-k1)
Node* const p0 = Parameter(0);
TRACED_FOREACH(int32_t, k1, kInt32Values) {
TRACED_FOREACH(int32_t, k2, kInt32Values) {
Node* node = graph()->NewNode(
machine()->Word32Equal(),
graph()->NewNode(machine()->Int32Add(), p0, Int32Constant(k1)),
Int32Constant(k2));
Reduction r = Reduce(node);
ASSERT_TRUE(r.Changed());
}
}
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Word64Equal // Word64Equal
@ -1436,6 +1453,21 @@ TEST_F(MachineOperatorReducerTest,
} }
} }
TEST_F(MachineOperatorReducerTest, Word64EqualWithAddAndConstant) {
// (x+k1)==k2 => x==(k2-k1)
Node* const p0 = Parameter(0);
TRACED_FOREACH(int64_t, k1, kInt64Values) {
TRACED_FOREACH(int64_t, k2, kInt64Values) {
Node* node = graph()->NewNode(
machine()->Word64Equal(),
graph()->NewNode(machine()->Int64Add(), p0, Int64Constant(k1)),
Int64Constant(k2));
Reduction r = Reduce(node);
ASSERT_TRUE(r.Changed());
}
}
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Branch // Branch
@ -2584,6 +2616,49 @@ TEST_F(MachineOperatorReducerTest, Uint64LessThanWithUint32Reduction) {
} }
} }
TEST_F(MachineOperatorReducerTest, Uint64LessThanWithInt64AddDontReduce) {
Node* const p0 = Parameter(0);
TRACED_FOREACH(uint64_t, k1, kUint64Values) {
TRACED_FOREACH(uint64_t, k2, kUint64Values) {
Node* node = graph()->NewNode(
machine()->Uint64LessThan(),
graph()->NewNode(machine()->Int64Add(), p0, Int64Constant(k1)),
Int64Constant(k2));
Reduction r = Reduce(node);
// Don't reduce because of potential overflow
ASSERT_FALSE(r.Changed());
}
}
}
TEST_F(MachineOperatorReducerTest,
Uint64LessThanOrEqualWithInt64AddDontReduce) {
Node* const p0 = Parameter(0);
TRACED_FOREACH(uint64_t, k1, kUint64Values) {
TRACED_FOREACH(uint64_t, k2, kUint64Values) {
uint64_t k1 = 0;
uint64_t k2 = 18446744073709551615u;
Node* node = graph()->NewNode(
machine()->Uint64LessThanOrEqual(),
graph()->NewNode(machine()->Int64Add(), p0, Int64Constant(k1)),
Int64Constant(k2));
Reduction r = Reduce(node);
if (k2 == 0) {
// x <= 0 => x == 0
ASSERT_TRUE(r.Changed());
} else if (k2 == std::numeric_limits<uint64_t>::max()) {
// x <= Max => true
ASSERT_TRUE(r.Changed());
} else {
// Don't reduce because of potential overflow
ASSERT_FALSE(r.Changed());
}
}
}
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Int64LessThan // Int64LessThan