Commit dcf193f1 authored by bmeurer's avatar bmeurer Committed by Commit bot

[turbofan] Strength reduction for inline comparisons.

Perform strength reduction on machine operators with inline comparisons:

  CMP & 1 => CMP
  1 & CMP => CMP
  CMP << 31 >> 31 => CMP

Also strength reduce the following constructs:

  x + (0 - y) => x - y
  (0 - y) + x => x - y

R=dcarney@chromium.org

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

Cr-Commit-Position: refs/heads/master@{#26817}
parent b33f552f
......@@ -589,6 +589,7 @@ source_set("v8_base") {
"src/compiler/node-cache.h",
"src/compiler/node-marker.cc",
"src/compiler/node-marker.h",
"src/compiler/node-matchers.cc",
"src/compiler/node-matchers.h",
"src/compiler/node-properties.cc",
"src/compiler/node-properties.h",
......
......@@ -160,29 +160,8 @@ Reduction MachineOperatorReducer::Reduce(Node* node) {
}
return ReduceWord32Shifts(node);
}
case IrOpcode::kWord32Sar: {
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());
}
if (m.left().IsWord32Shl()) {
Int32BinopMatcher mleft(m.left().node());
if (mleft.left().IsLoad()) {
LoadRepresentation const rep =
OpParameter<LoadRepresentation>(mleft.left().node());
if (m.right().Is(24) && mleft.right().Is(24) && rep == kMachInt8) {
// Load[kMachInt8] << 24 >> 24 => Load[kMachInt8]
return Replace(mleft.left().node());
}
if (m.right().Is(16) && mleft.right().Is(16) && rep == kMachInt16) {
// Load[kMachInt16] << 16 >> 16 => Load[kMachInt8]
return Replace(mleft.left().node());
}
}
}
return ReduceWord32Shifts(node);
}
case IrOpcode::kWord32Sar:
return ReduceWord32Sar(node);
case IrOpcode::kWord32Ror: {
Int32BinopMatcher m(node);
if (m.right().Is(0)) return Replace(m.left().node()); // x ror 0 => x
......@@ -471,6 +450,25 @@ Reduction MachineOperatorReducer::ReduceInt32Add(Node* node) {
return ReplaceUint32(bit_cast<uint32_t>(m.left().Value()) +
bit_cast<uint32_t>(m.right().Value()));
}
if (m.left().IsInt32Sub()) {
Int32BinopMatcher mleft(m.left().node());
if (mleft.left().Is(0)) { // (0 - x) + y => y - x
node->set_op(machine()->Int32Sub());
node->ReplaceInput(0, m.right().node());
node->ReplaceInput(1, mleft.right().node());
Reduction const reduction = ReduceInt32Sub(node);
return reduction.Changed() ? reduction : Changed(node);
}
}
if (m.right().IsInt32Sub()) {
Int32BinopMatcher mright(m.right().node());
if (mright.left().Is(0)) { // y + (0 - x) => y - x
node->set_op(machine()->Int32Sub());
node->ReplaceInput(1, mright.right().node());
Reduction const reduction = ReduceInt32Sub(node);
return reduction.Changed() ? reduction : Changed(node);
}
}
return NoChange();
}
......@@ -784,11 +782,48 @@ Reduction MachineOperatorReducer::ReduceWord32Shl(Node* node) {
}
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());
}
if (m.left().IsWord32Shl()) {
Int32BinopMatcher mleft(m.left().node());
if (mleft.left().IsComparison()) {
if (m.right().Is(31) && mleft.right().Is(31)) {
// Comparison << 31 >> 31 => 0 - Comparison
node->set_op(machine()->Int32Sub());
node->ReplaceInput(0, Int32Constant(0));
node->ReplaceInput(1, mleft.left().node());
Reduction const reduction = ReduceInt32Sub(node);
return reduction.Changed() ? reduction : Changed(node);
}
} else if (mleft.left().IsLoad()) {
LoadRepresentation const rep =
OpParameter<LoadRepresentation>(mleft.left().node());
if (m.right().Is(24) && mleft.right().Is(24) && rep == kMachInt8) {
// Load[kMachInt8] << 24 >> 24 => Load[kMachInt8]
return Replace(mleft.left().node());
}
if (m.right().Is(16) && mleft.right().Is(16) && rep == kMachInt16) {
// Load[kMachInt16] << 16 >> 16 => Load[kMachInt8]
return Replace(mleft.left().node());
}
}
}
return ReduceWord32Shifts(node);
}
Reduction MachineOperatorReducer::ReduceWord32And(Node* node) {
DCHECK_EQ(IrOpcode::kWord32And, node->opcode());
Int32BinopMatcher m(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.left().IsComparison() && m.right().Is(1)) { // CMP & 1 => CMP
return Replace(m.left().node());
}
if (m.IsFoldable()) { // K & K => K
return ReplaceInt32(m.left().Value() & m.right().Value());
}
......
......@@ -75,6 +75,7 @@ class MachineOperatorReducer FINAL : public Reducer {
Reduction ReduceProjection(size_t index, Node* node);
Reduction ReduceWord32Shifts(Node* node);
Reduction ReduceWord32Shl(Node* node);
Reduction ReduceWord32Sar(Node* node);
Reduction ReduceWord32And(Node* node);
Reduction ReduceWord32Or(Node* node);
......
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/compiler/node-matchers.h"
namespace v8 {
namespace internal {
namespace compiler {
bool NodeMatcher::IsComparison() const {
return IrOpcode::IsComparisonOpcode(opcode());
}
} // namespace compiler
} // namespace internal
} // namespace v8
......@@ -28,6 +28,8 @@ struct NodeMatcher {
}
Node* InputAt(int index) const { return node()->InputAt(index); }
bool IsComparison() const;
#define DEFINE_IS_OPCODE(Opcode) \
bool Is##Opcode() const { return opcode() == IrOpcode::k##Opcode; }
ALL_OP_LIST(DEFINE_IS_OPCODE)
......
......@@ -140,113 +140,119 @@
JS_OTHER_OP_LIST(V)
// Opcodes for VirtuaMachine-level operators.
#define SIMPLIFIED_OP_LIST(V) \
V(AnyToBoolean) \
V(BooleanNot) \
V(BooleanToNumber) \
V(NumberEqual) \
V(NumberLessThan) \
V(NumberLessThanOrEqual) \
V(NumberAdd) \
V(NumberSubtract) \
V(NumberMultiply) \
V(NumberDivide) \
V(NumberModulus) \
V(NumberToInt32) \
V(NumberToUint32) \
V(PlainPrimitiveToNumber) \
V(ReferenceEqual) \
V(StringEqual) \
V(StringLessThan) \
V(StringLessThanOrEqual) \
V(StringAdd) \
V(ChangeTaggedToInt32) \
V(ChangeTaggedToUint32) \
V(ChangeTaggedToFloat64) \
V(ChangeInt32ToTagged) \
V(ChangeUint32ToTagged) \
V(ChangeFloat64ToTagged) \
V(ChangeBoolToBit) \
V(ChangeBitToBool) \
V(LoadField) \
V(LoadBuffer) \
V(LoadElement) \
V(StoreField) \
V(StoreBuffer) \
V(StoreElement) \
V(ObjectIsSmi) \
#define SIMPLIFIED_COMPARE_BINOP_LIST(V) \
V(NumberEqual) \
V(NumberLessThan) \
V(NumberLessThanOrEqual) \
V(ReferenceEqual) \
V(StringEqual) \
V(StringLessThan) \
V(StringLessThanOrEqual)
#define SIMPLIFIED_OP_LIST(V) \
SIMPLIFIED_COMPARE_BINOP_LIST(V) \
V(AnyToBoolean) \
V(BooleanNot) \
V(BooleanToNumber) \
V(NumberAdd) \
V(NumberSubtract) \
V(NumberMultiply) \
V(NumberDivide) \
V(NumberModulus) \
V(NumberToInt32) \
V(NumberToUint32) \
V(PlainPrimitiveToNumber) \
V(StringAdd) \
V(ChangeTaggedToInt32) \
V(ChangeTaggedToUint32) \
V(ChangeTaggedToFloat64) \
V(ChangeInt32ToTagged) \
V(ChangeUint32ToTagged) \
V(ChangeFloat64ToTagged) \
V(ChangeBoolToBit) \
V(ChangeBitToBool) \
V(LoadField) \
V(LoadBuffer) \
V(LoadElement) \
V(StoreField) \
V(StoreBuffer) \
V(StoreElement) \
V(ObjectIsSmi) \
V(ObjectIsNonNegativeSmi)
// Opcodes for Machine-level operators.
#define MACHINE_OP_LIST(V) \
V(Load) \
V(Store) \
V(Word32And) \
V(Word32Or) \
V(Word32Xor) \
V(Word32Shl) \
V(Word32Shr) \
V(Word32Sar) \
V(Word32Ror) \
V(Word32Equal) \
V(Word64And) \
V(Word64Or) \
V(Word64Xor) \
V(Word64Shl) \
V(Word64Shr) \
V(Word64Sar) \
V(Word64Ror) \
V(Word64Equal) \
V(Int32Add) \
V(Int32AddWithOverflow) \
V(Int32Sub) \
V(Int32SubWithOverflow) \
V(Int32Mul) \
V(Int32MulHigh) \
V(Int32Div) \
V(Int32Mod) \
V(Int32LessThan) \
V(Int32LessThanOrEqual) \
V(Uint32Div) \
V(Uint32LessThan) \
V(Uint32LessThanOrEqual) \
V(Uint32Mod) \
V(Uint32MulHigh) \
V(Int64Add) \
V(Int64Sub) \
V(Int64Mul) \
V(Int64Div) \
V(Int64Mod) \
V(Int64LessThan) \
V(Int64LessThanOrEqual) \
V(Uint64Div) \
V(Uint64LessThan) \
V(Uint64Mod) \
V(ChangeFloat32ToFloat64) \
V(ChangeFloat64ToInt32) \
V(ChangeFloat64ToUint32) \
V(ChangeInt32ToFloat64) \
V(ChangeInt32ToInt64) \
V(ChangeUint32ToFloat64) \
V(ChangeUint32ToUint64) \
V(TruncateFloat64ToFloat32) \
V(TruncateFloat64ToInt32) \
V(TruncateInt64ToInt32) \
V(Float64Add) \
V(Float64Sub) \
V(Float64Mul) \
V(Float64Div) \
V(Float64Mod) \
V(Float64Sqrt) \
V(Float64Equal) \
V(Float64LessThan) \
V(Float64LessThanOrEqual) \
V(Float64Floor) \
V(Float64Ceil) \
V(Float64RoundTruncate) \
V(Float64RoundTiesAway) \
V(LoadStackPointer) \
V(CheckedLoad) \
#define MACHINE_COMPARE_BINOP_LIST(V) \
V(Word32Equal) \
V(Word64Equal) \
V(Int32LessThan) \
V(Int32LessThanOrEqual) \
V(Uint32LessThan) \
V(Uint32LessThanOrEqual) \
V(Int64LessThan) \
V(Int64LessThanOrEqual) \
V(Uint64LessThan) \
V(Float64Equal) \
V(Float64LessThan) \
V(Float64LessThanOrEqual)
#define MACHINE_OP_LIST(V) \
MACHINE_COMPARE_BINOP_LIST(V) \
V(Load) \
V(Store) \
V(Word32And) \
V(Word32Or) \
V(Word32Xor) \
V(Word32Shl) \
V(Word32Shr) \
V(Word32Sar) \
V(Word32Ror) \
V(Word64And) \
V(Word64Or) \
V(Word64Xor) \
V(Word64Shl) \
V(Word64Shr) \
V(Word64Sar) \
V(Word64Ror) \
V(Int32Add) \
V(Int32AddWithOverflow) \
V(Int32Sub) \
V(Int32SubWithOverflow) \
V(Int32Mul) \
V(Int32MulHigh) \
V(Int32Div) \
V(Int32Mod) \
V(Uint32Div) \
V(Uint32Mod) \
V(Uint32MulHigh) \
V(Int64Add) \
V(Int64Sub) \
V(Int64Mul) \
V(Int64Div) \
V(Int64Mod) \
V(Uint64Div) \
V(Uint64Mod) \
V(ChangeFloat32ToFloat64) \
V(ChangeFloat64ToInt32) \
V(ChangeFloat64ToUint32) \
V(ChangeInt32ToFloat64) \
V(ChangeInt32ToInt64) \
V(ChangeUint32ToFloat64) \
V(ChangeUint32ToUint64) \
V(TruncateFloat64ToFloat32) \
V(TruncateFloat64ToInt32) \
V(TruncateInt64ToInt32) \
V(Float64Add) \
V(Float64Sub) \
V(Float64Mul) \
V(Float64Div) \
V(Float64Mod) \
V(Float64Sqrt) \
V(Float64Floor) \
V(Float64Ceil) \
V(Float64RoundTruncate) \
V(Float64RoundTiesAway) \
V(LoadStackPointer) \
V(CheckedLoad) \
V(CheckedStore)
#define VALUE_OP_LIST(V) \
......@@ -308,6 +314,13 @@ class IrOpcode {
static bool IsMergeOpcode(Value value) {
return value == kMerge || value == kLoop;
}
// Returns true if opcode for comparison operator.
static bool IsComparisonOpcode(Value value) {
return (kJSEqual <= value && value <= kJSGreaterThanOrEqual) ||
(kNumberEqual <= value && value <= kStringLessThanOrEqual) ||
(kWord32Equal <= value && value <= kFloat64LessThanOrEqual);
}
};
} // namespace compiler
......
......@@ -233,6 +233,27 @@ const uint32_t kUint32Values[] = {
0x000fffff, 0x0007ffff, 0x0003ffff, 0x0001ffff, 0x0000ffff, 0x00007fff,
0x00003fff, 0x00001fff, 0x00000fff, 0x000007ff, 0x000003ff, 0x000001ff};
struct ComparisonBinaryOperator {
const Operator* (MachineOperatorBuilder::*constructor)();
const char* constructor_name;
};
std::ostream& operator<<(std::ostream& os,
ComparisonBinaryOperator const& cbop) {
return os << cbop.constructor_name;
}
const ComparisonBinaryOperator kComparisonBinaryOperators[] = {
#define OPCODE(Opcode) \
{ &MachineOperatorBuilder::Opcode, #Opcode } \
,
MACHINE_COMPARE_BINOP_LIST(OPCODE)
#undef OPCODE
};
} // namespace
......@@ -632,6 +653,27 @@ TEST_F(MachineOperatorReducerTest,
}
TEST_F(MachineOperatorReducerTest, Word32AndWithComparisonAndConstantOne) {
Node* const p0 = Parameter(0);
Node* const p1 = Parameter(1);
TRACED_FOREACH(ComparisonBinaryOperator, cbop, kComparisonBinaryOperators) {
Node* cmp = graph()->NewNode((machine()->*cbop.constructor)(), p0, p1);
// cmp & 1 => cmp
Reduction const r1 =
Reduce(graph()->NewNode(machine()->Word32And(), cmp, Int32Constant(1)));
ASSERT_TRUE(r1.Changed());
EXPECT_EQ(cmp, r1.replacement());
// 1 & cmp => cmp
Reduction const r2 =
Reduce(graph()->NewNode(machine()->Word32And(), Int32Constant(1), cmp));
ASSERT_TRUE(r2.Changed());
EXPECT_EQ(cmp, r2.replacement());
}
}
// -----------------------------------------------------------------------------
// Word32Xor
......@@ -773,6 +815,24 @@ TEST_F(MachineOperatorReducerTest, Word32RorWithConstants) {
// Word32Sar
TEST_F(MachineOperatorReducerTest, Word32SarWithWord32ShlAndComparison) {
Node* const p0 = Parameter(0);
Node* const p1 = Parameter(1);
TRACED_FOREACH(ComparisonBinaryOperator, cbop, kComparisonBinaryOperators) {
Node* cmp = graph()->NewNode((machine()->*cbop.constructor)(), p0, p1);
// cmp << 31 >> 31 => 0 - cmp
Reduction const r = Reduce(graph()->NewNode(
machine()->Word32Sar(),
graph()->NewNode(machine()->Word32Shl(), cmp, Int32Constant(31)),
Int32Constant(31)));
ASSERT_TRUE(r.Changed());
EXPECT_THAT(r.replacement(), IsInt32Sub(IsInt32Constant(0), cmp));
}
}
TEST_F(MachineOperatorReducerTest, Word32SarWithWord32ShlAndLoad) {
Node* const p0 = Parameter(0);
Node* const p1 = Parameter(1);
......@@ -1189,6 +1249,28 @@ TEST_F(MachineOperatorReducerTest, Uint32ModWithParameters) {
}
// -----------------------------------------------------------------------------
// Int32Add
TEST_F(MachineOperatorReducerTest, Int32AddWithInt32SubWithConstantZero) {
Node* const p0 = Parameter(0);
Node* const p1 = Parameter(1);
Reduction const r1 = Reduce(graph()->NewNode(
machine()->Int32Add(),
graph()->NewNode(machine()->Int32Sub(), Int32Constant(0), p0), p1));
ASSERT_TRUE(r1.Changed());
EXPECT_THAT(r1.replacement(), IsInt32Sub(p1, p0));
Reduction const r2 = Reduce(graph()->NewNode(
machine()->Int32Add(), p0,
graph()->NewNode(machine()->Int32Sub(), Int32Constant(0), p1)));
ASSERT_TRUE(r2.Changed());
EXPECT_THAT(r2.replacement(), IsInt32Sub(p0, p1));
}
// -----------------------------------------------------------------------------
// Int32AddWithOverflow
......
......@@ -64,6 +64,21 @@ bool IsConstantOpcode(IrOpcode::Value opcode) {
}
bool IsComparisonOpcode(IrOpcode::Value opcode) {
switch (opcode) {
#define OPCODE(Opcode) \
case IrOpcode::k##Opcode: \
return true;
JS_COMPARE_BINOP_LIST(OPCODE)
SIMPLIFIED_COMPARE_BINOP_LIST(OPCODE)
MACHINE_COMPARE_BINOP_LIST(OPCODE)
#undef OPCODE
default:
return false;
}
}
const IrOpcode::Value kInvalidOpcode = static_cast<IrOpcode::Value>(123456789);
} // namespace
......@@ -109,6 +124,16 @@ TEST(IrOpcodeTest, IsConstantOpcode) {
}
TEST(IrOpcodeTest, IsComparisonOpcode) {
EXPECT_FALSE(IrOpcode::IsComparisonOpcode(kInvalidOpcode));
#define OPCODE(Opcode) \
EXPECT_EQ(IsComparisonOpcode(IrOpcode::k##Opcode), \
IrOpcode::IsComparisonOpcode(IrOpcode::k##Opcode));
ALL_OP_LIST(OPCODE)
#undef OPCODE
}
TEST(IrOpcodeTest, Mnemonic) {
EXPECT_STREQ("UnknownOpcode", IrOpcode::Mnemonic(kInvalidOpcode));
#define OPCODE(Opcode) \
......
......@@ -497,6 +497,7 @@
'../../src/compiler/node-cache.h',
'../../src/compiler/node-marker.cc',
'../../src/compiler/node-marker.h',
'../../src/compiler/node-matchers.cc',
'../../src/compiler/node-matchers.h',
'../../src/compiler/node-properties.cc',
'../../src/compiler/node-properties.h',
......
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