#include "src/compiler/machine-operator-reducer.h"
#include "src/base/bits.h"
+#include "src/base/division-by-constant.h"
+#include "src/codegen.h"
+#include "src/compiler/diamond.h"
#include "src/compiler/generic-node-inl.h"
#include "src/compiler/graph.h"
#include "src/compiler/js-graph.h"
}
+Node* MachineOperatorReducer::Word32And(Node* lhs, uint32_t rhs) {
+ return graph()->NewNode(machine()->Word32And(), lhs, Uint32Constant(rhs));
+}
+
+
+Node* MachineOperatorReducer::Word32Sar(Node* lhs, uint32_t rhs) {
+ if (rhs == 0) return lhs;
+ return graph()->NewNode(machine()->Word32Sar(), lhs, Uint32Constant(rhs));
+}
+
+
+Node* MachineOperatorReducer::Word32Shr(Node* lhs, uint32_t rhs) {
+ if (rhs == 0) return lhs;
+ return graph()->NewNode(machine()->Word32Shr(), lhs, Uint32Constant(rhs));
+}
+
+
+Node* MachineOperatorReducer::Word32Equal(Node* lhs, Node* rhs) {
+ return graph()->NewNode(machine()->Word32Equal(), lhs, rhs);
+}
+
+
+Node* MachineOperatorReducer::Int32Add(Node* lhs, Node* rhs) {
+ return graph()->NewNode(machine()->Int32Add(), lhs, rhs);
+}
+
+
+Node* MachineOperatorReducer::Int32Sub(Node* lhs, Node* rhs) {
+ return graph()->NewNode(machine()->Int32Sub(), lhs, rhs);
+}
+
+
+Node* MachineOperatorReducer::Int32Mul(Node* lhs, Node* rhs) {
+ return graph()->NewNode(machine()->Int32Mul(), lhs, rhs);
+}
+
+
+Node* MachineOperatorReducer::Int32Div(Node* dividend, int32_t divisor) {
+ DCHECK_NE(0, divisor);
+ DCHECK_NE(std::numeric_limits<int32_t>::min(), divisor);
+ base::MagicNumbersForDivision<uint32_t> const mag =
+ base::SignedDivisionByConstant(bit_cast<uint32_t>(divisor));
+ Node* quotient = graph()->NewNode(machine()->Int32MulHigh(), dividend,
+ Uint32Constant(mag.multiplier));
+ if (divisor > 0 && bit_cast<int32_t>(mag.multiplier) < 0) {
+ quotient = Int32Add(quotient, dividend);
+ } else if (divisor < 0 && bit_cast<int32_t>(mag.multiplier) > 0) {
+ quotient = Int32Sub(quotient, dividend);
+ }
+ return Int32Add(Word32Sar(quotient, mag.shift), Word32Shr(dividend, 31));
+}
+
+
+Node* MachineOperatorReducer::Uint32Div(Node* dividend, uint32_t divisor) {
+ DCHECK_LT(0, divisor);
+ base::MagicNumbersForDivision<uint32_t> const mag =
+ base::UnsignedDivisionByConstant(bit_cast<uint32_t>(divisor));
+ Node* quotient = graph()->NewNode(machine()->Uint32MulHigh(), dividend,
+ Uint32Constant(mag.multiplier));
+ if (mag.add) {
+ DCHECK_LE(1, mag.shift);
+ quotient = Word32Shr(
+ Int32Add(Word32Shr(Int32Sub(dividend, quotient), 1), quotient),
+ mag.shift - 1);
+ } else {
+ quotient = Word32Shr(quotient, mag.shift);
+ }
+ return quotient;
+}
+
+
// Perform constant folding and strength reduction on machine operators.
Reduction MachineOperatorReducer::Reduce(Node* node) {
switch (node->opcode()) {
return ReplaceInt32(m.left().Value() & m.right().Value());
}
if (m.LeftEqualsRight()) return Replace(m.left().node()); // x & x => x
+ if (m.left().IsWord32And() && m.right().HasValue()) {
+ Int32BinopMatcher mleft(m.left().node());
+ if (mleft.right().HasValue()) { // (x & K) & K => x & K
+ node->ReplaceInput(0, mleft.left().node());
+ node->ReplaceInput(
+ 1, Int32Constant(m.right().Value() & mleft.right().Value()));
+ return Changed(node);
+ }
+ }
break;
}
case IrOpcode::kWord32Or: {
return ReplaceInt32(m.left().Value() ^ m.right().Value());
}
if (m.LeftEqualsRight()) return ReplaceInt32(0); // x ^ x => 0
+ if (m.left().IsWord32Xor() && m.right().Is(-1)) {
+ Int32BinopMatcher mleft(m.left().node());
+ if (mleft.right().Is(-1)) { // (x ^ -1) ^ -1 => x
+ return Replace(mleft.left().node());
+ }
+ }
break;
}
case IrOpcode::kWord32Shl: {
if (m.IsFoldable()) { // K << K => K
return ReplaceInt32(m.left().Value() << m.right().Value());
}
+ if (m.right().IsInRange(1, 31)) {
+ // (x >>> K) << K => x & ~(2^K - 1)
+ // (x >> K) << K => x & ~(2^K - 1)
+ if (m.left().IsWord32Sar() || m.left().IsWord32Shr()) {
+ Int32BinopMatcher mleft(m.left().node());
+ if (mleft.right().Is(m.right().Value())) {
+ node->set_op(machine()->Word32And());
+ node->ReplaceInput(0, mleft.left().node());
+ node->ReplaceInput(
+ 1, Uint32Constant(~((1U << m.right().Value()) - 1U)));
+ return Changed(node);
+ }
+ }
+ }
break;
}
case IrOpcode::kWord32Shr: {
if (m.LeftEqualsRight()) return ReplaceBool(true); // x == x => true
break;
}
+ case IrOpcode::kWord64Equal: {
+ Int64BinopMatcher m(node);
+ if (m.IsFoldable()) { // K == K => K
+ return ReplaceBool(m.left().Value() == m.right().Value());
+ }
+ if (m.left().IsInt64Sub() && m.right().Is(0)) { // x - y == 0 => x == y
+ Int64BinopMatcher msub(m.left().node());
+ node->ReplaceInput(0, msub.left().node());
+ node->ReplaceInput(1, msub.right().node());
+ return Changed(node);
+ }
+ // TODO(turbofan): fold HeapConstant, ExternalReference, pointer compares
+ if (m.LeftEqualsRight()) return ReplaceBool(true); // x == x => true
+ break;
+ }
case IrOpcode::kInt32Add: {
Int32BinopMatcher m(node);
if (m.right().Is(0)) return Replace(m.left().node()); // x + 0 => x
}
break;
}
- case IrOpcode::kInt32Div: {
- Int32BinopMatcher m(node);
- if (m.right().Is(1)) return Replace(m.left().node()); // x / 1 => x
- // TODO(turbofan): if (m.left().Is(0))
- // TODO(turbofan): if (m.right().IsPowerOf2())
- // TODO(turbofan): if (m.right().Is(0))
- // TODO(turbofan): if (m.LeftEqualsRight())
- if (m.IsFoldable() && !m.right().Is(0)) { // K / K => K
- if (m.right().Is(-1)) return ReplaceInt32(-m.left().Value());
- return ReplaceInt32(m.left().Value() / m.right().Value());
- }
- if (m.right().Is(-1)) { // x / -1 => 0 - x
- node->set_op(machine()->Int32Sub());
- node->ReplaceInput(0, Int32Constant(0));
- node->ReplaceInput(1, m.left().node());
- return Changed(node);
- }
- break;
- }
- case IrOpcode::kInt32UDiv: {
- Uint32BinopMatcher m(node);
- if (m.right().Is(1)) return Replace(m.left().node()); // x / 1 => x
- // TODO(turbofan): if (m.left().Is(0))
- // TODO(turbofan): if (m.right().Is(0))
- // TODO(turbofan): if (m.LeftEqualsRight())
- if (m.IsFoldable() && !m.right().Is(0)) { // K / K => K
- return ReplaceInt32(m.left().Value() / m.right().Value());
- }
- if (m.right().IsPowerOf2()) { // x / 2^n => x >> n
- node->set_op(machine()->Word32Shr());
- node->ReplaceInput(1, Int32Constant(WhichPowerOf2(m.right().Value())));
- return Changed(node);
- }
- break;
- }
- case IrOpcode::kInt32Mod: {
- Int32BinopMatcher m(node);
- if (m.right().Is(1)) return ReplaceInt32(0); // x % 1 => 0
- if (m.right().Is(-1)) return ReplaceInt32(0); // x % -1 => 0
- // TODO(turbofan): if (m.left().Is(0))
- // TODO(turbofan): if (m.right().IsPowerOf2())
- // TODO(turbofan): if (m.right().Is(0))
- // TODO(turbofan): if (m.LeftEqualsRight())
- if (m.IsFoldable() && !m.right().Is(0)) { // K % K => K
- return ReplaceInt32(m.left().Value() % m.right().Value());
- }
- break;
- }
- case IrOpcode::kInt32UMod: {
- Uint32BinopMatcher m(node);
- if (m.right().Is(1)) return ReplaceInt32(0); // x % 1 => 0
- // TODO(turbofan): if (m.left().Is(0))
- // TODO(turbofan): if (m.right().Is(0))
- // TODO(turbofan): if (m.LeftEqualsRight())
- if (m.IsFoldable() && !m.right().Is(0)) { // K % K => K
- return ReplaceInt32(m.left().Value() % m.right().Value());
- }
- if (m.right().IsPowerOf2()) { // x % 2^n => x & 2^n-1
- node->set_op(machine()->Word32And());
- node->ReplaceInput(1, Int32Constant(m.right().Value() - 1));
- return Changed(node);
- }
- break;
- }
+ case IrOpcode::kInt32Div:
+ return ReduceInt32Div(node);
+ case IrOpcode::kUint32Div:
+ return ReduceUint32Div(node);
+ case IrOpcode::kInt32Mod:
+ return ReduceInt32Mod(node);
+ case IrOpcode::kUint32Mod:
+ return ReduceUint32Mod(node);
case IrOpcode::kInt32LessThan: {
Int32BinopMatcher m(node);
if (m.IsFoldable()) { // K < K => K
return ReplaceBool(m.left().Value() < m.right().Value());
}
if (m.LeftEqualsRight()) return ReplaceBool(false); // x < x => false
+ if (m.left().IsWord32Sar() && m.right().HasValue()) {
+ Int32BinopMatcher mleft(m.left().node());
+ if (mleft.right().HasValue()) {
+ // (x >> K) < C => x < (C << K)
+ // when C < (M >> K)
+ const uint32_t c = m.right().Value();
+ const uint32_t k = mleft.right().Value() & 0x1f;
+ if (c < static_cast<uint32_t>(kMaxInt >> k)) {
+ node->ReplaceInput(0, mleft.left().node());
+ node->ReplaceInput(1, Uint32Constant(c << k));
+ return Changed(node);
+ }
+ // TODO(turbofan): else the comparison is always true.
+ }
+ }
break;
}
case IrOpcode::kUint32LessThanOrEqual: {
}
case IrOpcode::kFloat64Add: {
Float64BinopMatcher m(node);
+ if (m.right().IsNaN()) { // x + NaN => NaN
+ return Replace(m.right().node());
+ }
if (m.IsFoldable()) { // K + K => K
return ReplaceFloat64(m.left().Value() + m.right().Value());
}
}
case IrOpcode::kFloat64Sub: {
Float64BinopMatcher m(node);
+ if (m.right().Is(0) && (Double(m.right().Value()).Sign() > 0)) {
+ return Replace(m.left().node()); // x - 0 => x
+ }
+ if (m.right().IsNaN()) { // x - NaN => NaN
+ return Replace(m.right().node());
+ }
+ if (m.left().IsNaN()) { // NaN - x => NaN
+ return Replace(m.left().node());
+ }
if (m.IsFoldable()) { // K - K => K
return ReplaceFloat64(m.left().Value() - m.right().Value());
}
}
case IrOpcode::kFloat64Mod: {
Float64BinopMatcher m(node);
+ if (m.right().Is(0)) { // x % 0 => NaN
+ return ReplaceFloat64(base::OS::nan_value());
+ }
if (m.right().IsNaN()) { // x % NaN => NaN
return Replace(m.right().node());
}
if (m.IsChangeFloat32ToFloat64()) return Replace(m.node()->InputAt(0));
break;
}
+ case IrOpcode::kStore:
+ return ReduceStore(node);
+ default:
+ break;
+ }
+ return NoChange();
+}
+
+
+Reduction MachineOperatorReducer::ReduceInt32Div(Node* node) {
+ Int32BinopMatcher m(node);
+ if (m.left().Is(0)) return Replace(m.left().node()); // 0 / x => 0
+ 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(
+ base::bits::SignedDiv32(m.left().Value(), m.right().Value()));
+ }
+ if (m.LeftEqualsRight()) { // x / x => x != 0
+ Node* const zero = Int32Constant(0);
+ return Replace(Word32Equal(Word32Equal(m.left().node(), zero), zero));
+ }
+ if (m.right().Is(-1)) { // x / -1 => 0 - x
+ node->set_op(machine()->Int32Sub());
+ node->ReplaceInput(0, Int32Constant(0));
+ node->ReplaceInput(1, m.left().node());
+ node->TrimInputCount(2);
+ return Changed(node);
+ }
+ if (m.right().HasValue()) {
+ int32_t const divisor = m.right().Value();
+ Node* const dividend = m.left().node();
+ Node* quotient = dividend;
+ if (base::bits::IsPowerOfTwo32(Abs(divisor))) {
+ uint32_t const shift = WhichPowerOf2Abs(divisor);
+ DCHECK_NE(0, shift);
+ if (shift > 1) {
+ quotient = Word32Sar(quotient, 31);
+ }
+ quotient = Int32Add(Word32Shr(quotient, 32u - shift), dividend);
+ quotient = Word32Sar(quotient, shift);
+ } else {
+ quotient = Int32Div(quotient, Abs(divisor));
+ }
+ if (divisor < 0) {
+ node->set_op(machine()->Int32Sub());
+ node->ReplaceInput(0, Int32Constant(0));
+ node->ReplaceInput(1, quotient);
+ node->TrimInputCount(2);
+ return Changed(node);
+ }
+ return Replace(quotient);
+ }
+ return NoChange();
+}
+
+
+Reduction MachineOperatorReducer::ReduceUint32Div(Node* node) {
+ Uint32BinopMatcher m(node);
+ if (m.left().Is(0)) return Replace(m.left().node()); // 0 / x => 0
+ 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 ReplaceUint32(
+ base::bits::UnsignedDiv32(m.left().Value(), m.right().Value()));
+ }
+ if (m.LeftEqualsRight()) { // x / x => x != 0
+ Node* const zero = Int32Constant(0);
+ return Replace(Word32Equal(Word32Equal(m.left().node(), zero), zero));
+ }
+ if (m.right().HasValue()) {
+ Node* const dividend = m.left().node();
+ uint32_t const divisor = m.right().Value();
+ if (base::bits::IsPowerOfTwo32(divisor)) { // x / 2^n => x >> n
+ node->set_op(machine()->Word32Shr());
+ node->ReplaceInput(1, Uint32Constant(WhichPowerOf2(m.right().Value())));
+ node->TrimInputCount(2);
+ return Changed(node);
+ } else {
+ return Replace(Uint32Div(dividend, divisor));
+ }
+ }
+ return NoChange();
+}
+
+
+Reduction MachineOperatorReducer::ReduceInt32Mod(Node* node) {
+ Int32BinopMatcher m(node);
+ if (m.left().Is(0)) return Replace(m.left().node()); // 0 % x => 0
+ if (m.right().Is(0)) return Replace(m.right().node()); // x % 0 => 0
+ if (m.right().Is(1)) return ReplaceInt32(0); // x % 1 => 0
+ if (m.right().Is(-1)) return ReplaceInt32(0); // x % -1 => 0
+ if (m.LeftEqualsRight()) return ReplaceInt32(0); // x % x => 0
+ if (m.IsFoldable()) { // K % K => K
+ return ReplaceInt32(
+ base::bits::SignedMod32(m.left().Value(), m.right().Value()));
+ }
+ if (m.right().HasValue()) {
+ Node* const dividend = m.left().node();
+ int32_t const divisor = Abs(m.right().Value());
+ if (base::bits::IsPowerOfTwo32(divisor)) {
+ uint32_t const mask = divisor - 1;
+ Node* const zero = Int32Constant(0);
+
+ Node* check =
+ graph()->NewNode(machine()->Int32LessThan(), dividend, zero);
+ Diamond d(graph(), common(), check, BranchHint::kFalse);
+ Node* neg = Int32Sub(zero, Word32And(Int32Sub(zero, dividend), mask));
+ Node* pos = Word32And(dividend, mask);
+ d.OverwriteWithPhi(node, kMachInt32, neg, pos);
+ } else {
+ Node* quotient = Int32Div(dividend, divisor);
+ node->set_op(machine()->Int32Sub());
+ DCHECK_EQ(dividend, node->InputAt(0));
+ node->ReplaceInput(1, Int32Mul(quotient, Int32Constant(divisor)));
+ node->TrimInputCount(2);
+ }
+ return Changed(node);
+ }
+ return NoChange();
+}
+
+
+Reduction MachineOperatorReducer::ReduceUint32Mod(Node* node) {
+ Uint32BinopMatcher m(node);
+ if (m.left().Is(0)) return Replace(m.left().node()); // 0 % x => 0
+ if (m.right().Is(0)) return Replace(m.right().node()); // x % 0 => 0
+ if (m.right().Is(1)) return ReplaceUint32(0); // x % 1 => 0
+ if (m.LeftEqualsRight()) return ReplaceInt32(0); // x % x => 0
+ if (m.IsFoldable()) { // K % K => K
+ return ReplaceUint32(
+ base::bits::UnsignedMod32(m.left().Value(), m.right().Value()));
+ }
+ if (m.right().HasValue()) {
+ Node* const dividend = m.left().node();
+ uint32_t const divisor = m.right().Value();
+ if (base::bits::IsPowerOfTwo32(divisor)) { // x % 2^n => x & 2^n-1
+ node->set_op(machine()->Word32And());
+ node->ReplaceInput(1, Uint32Constant(m.right().Value() - 1));
+ } else {
+ Node* quotient = Uint32Div(dividend, divisor);
+ node->set_op(machine()->Int32Sub());
+ DCHECK_EQ(dividend, node->InputAt(0));
+ node->ReplaceInput(1, Int32Mul(quotient, Uint32Constant(divisor)));
+ }
+ node->TrimInputCount(2);
+ return Changed(node);
+ }
+ return NoChange();
+}
+
+
+Reduction MachineOperatorReducer::ReduceStore(Node* node) {
+ MachineType const rep =
+ RepresentationOf(StoreRepresentationOf(node->op()).machine_type());
+ Node* const value = node->InputAt(2);
+ switch (value->opcode()) {
+ case IrOpcode::kWord32And: {
+ Uint32BinopMatcher m(value);
+ if (m.right().HasValue() &&
+ ((rep == kRepWord8 && (m.right().Value() & 0xff) == 0xff) ||
+ (rep == kRepWord16 && (m.right().Value() & 0xffff) == 0xffff))) {
+ node->ReplaceInput(2, m.left().node());
+ return Changed(node);
+ }
+ break;
+ }
+ case IrOpcode::kWord32Sar: {
+ Int32BinopMatcher m(value);
+ if (m.left().IsWord32Shl() &&
+ ((rep == kRepWord8 && m.right().IsInRange(1, 24)) ||
+ (rep == kRepWord16 && m.right().IsInRange(1, 16)))) {
+ Int32BinopMatcher mleft(m.left().node());
+ if (mleft.right().Is(m.right().Value())) {
+ node->ReplaceInput(2, mleft.left().node());
+ return Changed(node);
+ }
+ }
+ break;
+ }
default:
break;
}