aboutsummaryrefslogtreecommitdiff
path: root/deps/v8/src/compiler/machine-operator-reducer.cc
diff options
context:
space:
mode:
Diffstat (limited to 'deps/v8/src/compiler/machine-operator-reducer.cc')
-rw-r--r--deps/v8/src/compiler/machine-operator-reducer.cc67
1 files changed, 43 insertions, 24 deletions
diff --git a/deps/v8/src/compiler/machine-operator-reducer.cc b/deps/v8/src/compiler/machine-operator-reducer.cc
index 8ef7e7ce08..751cdacca6 100644
--- a/deps/v8/src/compiler/machine-operator-reducer.cc
+++ b/deps/v8/src/compiler/machine-operator-reducer.cc
@@ -7,6 +7,7 @@
#include "src/base/bits.h"
#include "src/base/division-by-constant.h"
#include "src/base/ieee754.h"
+#include "src/base/overflowing-math.h"
#include "src/compiler/diamond.h"
#include "src/compiler/graph.h"
#include "src/compiler/machine-graph.h"
@@ -18,9 +19,12 @@ namespace v8 {
namespace internal {
namespace compiler {
-MachineOperatorReducer::MachineOperatorReducer(MachineGraph* mcgraph,
+MachineOperatorReducer::MachineOperatorReducer(Editor* editor,
+ MachineGraph* mcgraph,
bool allow_signalling_nan)
- : mcgraph_(mcgraph), allow_signalling_nan_(allow_signalling_nan) {}
+ : AdvancedReducer(editor),
+ mcgraph_(mcgraph),
+ allow_signalling_nan_(allow_signalling_nan) {}
MachineOperatorReducer::~MachineOperatorReducer() = default;
@@ -217,7 +221,8 @@ Reduction MachineOperatorReducer::Reduce(Node* node) {
if (m.right().Is(0)) return Replace(m.right().node()); // x * 0 => 0
if (m.right().Is(1)) return Replace(m.left().node()); // x * 1 => x
if (m.IsFoldable()) { // K * K => K
- return ReplaceInt32(m.left().Value() * m.right().Value());
+ return ReplaceInt32(
+ base::MulWithWraparound(m.left().Value(), m.right().Value()));
}
if (m.right().Is(-1)) { // x * -1 => 0 - x
node->ReplaceInput(0, Int32Constant(0));
@@ -432,7 +437,8 @@ Reduction MachineOperatorReducer::Reduce(Node* node) {
return ReplaceFloat64(m.left().Value() - m.left().Value());
}
if (m.IsFoldable()) { // K / K => K
- return ReplaceFloat64(m.left().Value() / m.right().Value());
+ return ReplaceFloat64(
+ base::Divide(m.left().Value(), m.right().Value()));
}
if (allow_signalling_nan_ && m.right().Is(-1)) { // x / -1.0 => -x
node->RemoveInput(1);
@@ -702,6 +708,14 @@ Reduction MachineOperatorReducer::Reduce(Node* node) {
return ReduceFloat64Compare(node);
case IrOpcode::kFloat64RoundDown:
return ReduceFloat64RoundDown(node);
+ case IrOpcode::kBitcastTaggedToWord: {
+ NodeMatcher m(node->InputAt(0));
+ if (m.IsBitcastWordToTaggedSigned()) {
+ RelaxEffectsAndControls(node);
+ return Replace(m.InputAt(0));
+ }
+ break;
+ }
default:
break;
}
@@ -713,8 +727,8 @@ Reduction MachineOperatorReducer::ReduceInt32Add(Node* node) {
Int32BinopMatcher m(node);
if (m.right().Is(0)) return Replace(m.left().node()); // x + 0 => x
if (m.IsFoldable()) { // K + K => K
- return ReplaceUint32(bit_cast<uint32_t>(m.left().Value()) +
- bit_cast<uint32_t>(m.right().Value()));
+ return ReplaceInt32(
+ base::AddWithWraparound(m.left().Value(), m.right().Value()));
}
if (m.left().IsInt32Sub()) {
Int32BinopMatcher mleft(m.left().node());
@@ -743,8 +757,8 @@ Reduction MachineOperatorReducer::ReduceInt64Add(Node* node) {
Int64BinopMatcher m(node);
if (m.right().Is(0)) return Replace(m.left().node()); // x + 0 => 0
if (m.IsFoldable()) {
- return Replace(Uint64Constant(bit_cast<uint64_t>(m.left().Value()) +
- bit_cast<uint64_t>(m.right().Value())));
+ return ReplaceInt64(
+ base::AddWithWraparound(m.left().Value(), m.right().Value()));
}
return NoChange();
}
@@ -754,12 +768,13 @@ Reduction MachineOperatorReducer::ReduceInt32Sub(Node* node) {
Int32BinopMatcher m(node);
if (m.right().Is(0)) return Replace(m.left().node()); // x - 0 => x
if (m.IsFoldable()) { // K - K => K
- return ReplaceInt32(static_cast<uint32_t>(m.left().Value()) -
- static_cast<uint32_t>(m.right().Value()));
+ return ReplaceInt32(
+ base::SubWithWraparound(m.left().Value(), m.right().Value()));
}
if (m.LeftEqualsRight()) return ReplaceInt32(0); // x - x => 0
if (m.right().HasValue()) { // x - K => x + -K
- node->ReplaceInput(1, Int32Constant(-m.right().Value()));
+ node->ReplaceInput(
+ 1, Int32Constant(base::NegateWithWraparound(m.right().Value())));
NodeProperties::ChangeOp(node, machine()->Int32Add());
Reduction const reduction = ReduceInt32Add(node);
return reduction.Changed() ? reduction : Changed(node);
@@ -772,12 +787,13 @@ Reduction MachineOperatorReducer::ReduceInt64Sub(Node* node) {
Int64BinopMatcher m(node);
if (m.right().Is(0)) return Replace(m.left().node()); // x - 0 => x
if (m.IsFoldable()) { // K - K => K
- return Replace(Uint64Constant(bit_cast<uint64_t>(m.left().Value()) -
- bit_cast<uint64_t>(m.right().Value())));
+ return ReplaceInt64(
+ base::SubWithWraparound(m.left().Value(), m.right().Value()));
}
if (m.LeftEqualsRight()) return Replace(Int64Constant(0)); // x - x => 0
if (m.right().HasValue()) { // x - K => x + -K
- node->ReplaceInput(1, Int64Constant(-m.right().Value()));
+ node->ReplaceInput(
+ 1, Int64Constant(base::NegateWithWraparound(m.right().Value())));
NodeProperties::ChangeOp(node, machine()->Int64Add());
Reduction const reduction = ReduceInt64Add(node);
return reduction.Changed() ? reduction : Changed(node);
@@ -1056,7 +1072,8 @@ Reduction MachineOperatorReducer::ReduceWord32Shl(Node* node) {
Int32BinopMatcher m(node);
if (m.right().Is(0)) return Replace(m.left().node()); // x << 0 => x
if (m.IsFoldable()) { // K << K => K
- return ReplaceInt32(m.left().Value() << m.right().Value());
+ return ReplaceInt32(
+ base::ShlWithWraparound(m.left().Value(), m.right().Value()));
}
if (m.right().IsInRange(1, 31)) {
// (x >>> K) << K => x & ~(2^K - 1)
@@ -1081,7 +1098,8 @@ Reduction MachineOperatorReducer::ReduceWord64Shl(Node* node) {
Int64BinopMatcher m(node);
if (m.right().Is(0)) return Replace(m.left().node()); // x << 0 => x
if (m.IsFoldable()) { // K << K => K
- return ReplaceInt64(m.left().Value() << m.right().Value());
+ return ReplaceInt64(
+ base::ShlWithWraparound(m.left().Value(), m.right().Value()));
}
return NoChange();
}
@@ -1090,12 +1108,12 @@ Reduction MachineOperatorReducer::ReduceWord32Shr(Node* node) {
Uint32BinopMatcher m(node);
if (m.right().Is(0)) return Replace(m.left().node()); // x >>> 0 => x
if (m.IsFoldable()) { // K >>> K => K
- return ReplaceInt32(m.left().Value() >> m.right().Value());
+ return ReplaceInt32(m.left().Value() >> (m.right().Value() & 31));
}
if (m.left().IsWord32And() && m.right().HasValue()) {
Uint32BinopMatcher mleft(m.left().node());
if (mleft.right().HasValue()) {
- uint32_t shift = m.right().Value() & 0x1F;
+ uint32_t shift = m.right().Value() & 31;
uint32_t mask = mleft.right().Value();
if ((mask >> shift) == 0) {
// (m >>> s) == 0 implies ((x & m) >>> s) == 0
@@ -1111,7 +1129,7 @@ Reduction MachineOperatorReducer::ReduceWord64Shr(Node* node) {
Uint64BinopMatcher m(node);
if (m.right().Is(0)) return Replace(m.left().node()); // x >>> 0 => x
if (m.IsFoldable()) { // K >> K => K
- return ReplaceInt64(m.left().Value() >> m.right().Value());
+ return ReplaceInt64(m.left().Value() >> (m.right().Value() & 63));
}
return NoChange();
}
@@ -1120,7 +1138,7 @@ Reduction MachineOperatorReducer::ReduceWord32Sar(Node* node) {
Int32BinopMatcher m(node);
if (m.right().Is(0)) return Replace(m.left().node()); // x >> 0 => x
if (m.IsFoldable()) { // K >> K => K
- return ReplaceInt32(m.left().Value() >> m.right().Value());
+ return ReplaceInt32(m.left().Value() >> (m.right().Value() & 31));
}
if (m.left().IsWord32Shl()) {
Int32BinopMatcher mleft(m.left().node());
@@ -1155,7 +1173,7 @@ Reduction MachineOperatorReducer::ReduceWord64Sar(Node* node) {
Int64BinopMatcher m(node);
if (m.right().Is(0)) return Replace(m.left().node()); // x >> 0 => x
if (m.IsFoldable()) {
- return ReplaceInt64(m.left().Value() >> m.right().Value());
+ return ReplaceInt64(m.left().Value() >> (m.right().Value() & 63));
}
return NoChange();
}
@@ -1184,6 +1202,7 @@ Reduction MachineOperatorReducer::ReduceWord32And(Node* node) {
}
if (m.right().IsNegativePowerOf2()) {
int32_t const mask = m.right().Value();
+ int32_t const neg_mask = base::NegateWithWraparound(mask);
if (m.left().IsWord32Shl()) {
Uint32BinopMatcher mleft(m.left().node());
if (mleft.right().HasValue() &&
@@ -1205,7 +1224,7 @@ Reduction MachineOperatorReducer::ReduceWord32And(Node* node) {
}
if (mleft.left().IsInt32Mul()) {
Int32BinopMatcher mleftleft(mleft.left().node());
- if (mleftleft.right().IsMultipleOf(-mask)) {
+ if (mleftleft.right().IsMultipleOf(neg_mask)) {
// (y * (K << L) + x) & (-1 << L) => (x & (-1 << L)) + y * (K << L)
node->ReplaceInput(0,
Word32And(mleft.right().node(), m.right().node()));
@@ -1217,7 +1236,7 @@ Reduction MachineOperatorReducer::ReduceWord32And(Node* node) {
}
if (mleft.right().IsInt32Mul()) {
Int32BinopMatcher mleftright(mleft.right().node());
- if (mleftright.right().IsMultipleOf(-mask)) {
+ if (mleftright.right().IsMultipleOf(neg_mask)) {
// (x + y * (K << L)) & (-1 << L) => (x & (-1 << L)) + y * (K << L)
node->ReplaceInput(0,
Word32And(mleft.left().node(), m.right().node()));
@@ -1253,7 +1272,7 @@ Reduction MachineOperatorReducer::ReduceWord32And(Node* node) {
}
} else if (m.left().IsInt32Mul()) {
Int32BinopMatcher mleft(m.left().node());
- if (mleft.right().IsMultipleOf(-mask)) {
+ if (mleft.right().IsMultipleOf(neg_mask)) {
// (x * (K << L)) & (-1 << L) => x * (K << L)
return Replace(mleft.node());
}