Node* MachineOperatorReducer::Int32Sub(Node* lhs, Node* rhs) {
- return graph()->NewNode(machine()->Int32Sub(), lhs, rhs);
+ Node* const node = graph()->NewNode(machine()->Int32Sub(), lhs, rhs);
+ Reduction const reduction = ReduceInt32Sub(node);
+ return reduction.Changed() ? reduction.replacement() : node;
}
}
case IrOpcode::kInt32Add:
return ReduceInt32Add(node);
- case IrOpcode::kInt32Sub: {
- 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()));
- }
- if (m.LeftEqualsRight()) return ReplaceInt32(0); // x - x => 0
- break;
- }
+ case IrOpcode::kInt32Sub:
+ return ReduceInt32Sub(node);
case IrOpcode::kInt32Mul: {
Int32BinopMatcher m(node);
if (m.right().Is(0)) return Replace(m.right().node()); // x * 0 => 0
}
+Reduction MachineOperatorReducer::ReduceInt32Sub(Node* node) {
+ DCHECK_EQ(IrOpcode::kInt32Sub, node->opcode());
+ 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()));
+ }
+ if (m.LeftEqualsRight()) return ReplaceInt32(0); // x - x => 0
+ if (m.right().HasValue()) { // x - K => x + -K
+ node->set_op(machine()->Int32Add());
+ node->ReplaceInput(1, Int32Constant(-m.right().Value()));
+ Reduction const reduction = ReduceInt32Add(node);
+ return reduction.Changed() ? reduction : Changed(node);
+ }
+ return NoChange();
+}
+
+
Reduction MachineOperatorReducer::ReduceInt32Div(Node* node) {
Int32BinopMatcher m(node);
if (m.left().Is(0)) return Replace(m.left().node()); // 0 / x => 0
// -----------------------------------------------------------------------------
+// Int32Sub
+
+
+TEST_F(MachineOperatorReducerTest, Int32SubWithConstant) {
+ Node* const p0 = Parameter(0);
+ TRACED_FOREACH(int32_t, k, kInt32Values) {
+ Reduction const r =
+ Reduce(graph()->NewNode(machine()->Int32Sub(), p0, Int32Constant(k)));
+ ASSERT_TRUE(r.Changed());
+ if (k == 0) {
+ EXPECT_EQ(p0, r.replacement());
+ } else {
+ EXPECT_THAT(r.replacement(), IsInt32Add(p0, IsInt32Constant(-k)));
+ }
+ }
+}
+
+
+// -----------------------------------------------------------------------------
// Int32Div