v8/test/unittests/compiler/common-operator-reducer-unittest.cc
Darius M 0719ace66e Reland^2 [compiler] Simplify "==0" branches in MachineOperatorReducer
This is a reland of 6b690a6b48.

The previous version of this CL was a bit too aggressive in the
duplication of branch conditions. This caused an increase in
register pressure in some cases, thus reducing performance.

In fact, duplicating branch conditions that require an "== 0" to be
added provides no benefits. We are thus now a bit less aggressive, and
only duplicate comparisons.

Original change's description:
> Reland [compiler] Simplify "==0" branches in MachineOperatorReducer
>
> This is a reland of 48b443f692.
>
> While fixing the initial CL, we stumbled upon a few bugs that
> we had to fix:
>
>  - CommonOperatorReducer and SimplifiedOperatorReducer were applied
>    before and after SimplifiedLowering, but always assumed that it
>    was before SimplifiedLowering, and thus had the wrong semantics
>    for branches in some cases. They now have an added parameter to
>    know which semantics of branch they should use.
>
>  - The lowering of StaticAssert was wrong and could leave kHeapConstant
>    in the assert (instead of machine Booleans).
>
> Original change's description:
> > [compiler] Simplify "==0" branches in MachineOperatorReducer
> >
> > Bug: v8:12484
> > Change-Id: I0667c7464c0dd71338bc199a24a69248a7a0a525
> > Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3497303
> > Reviewed-by: Tobias Tebbi <tebbi@chromium.org>
> > Owners-Override: Tobias Tebbi <tebbi@chromium.org>
> > Commit-Queue: Darius Mercadier <dmercadier@chromium.org>
> > Cr-Commit-Position: refs/heads/main@{#79379}
>
> Bug: v8:12484
> Change-Id: Ibbf5df96fce5ccb04868dc517539479bf69f5703
> Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3516869
> Reviewed-by: Tobias Tebbi <tebbi@chromium.org>
> Commit-Queue: Darius Mercadier <dmercadier@chromium.org>
> Cr-Commit-Position: refs/heads/main@{#79528}

Bug: v8:12484
Change-Id: I31f575a59811a83c7c1acb4c14bf5ded63a8f536
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3540102
Reviewed-by: Tobias Tebbi <tebbi@chromium.org>
Commit-Queue: Darius Mercadier <dmercadier@chromium.org>
Cr-Commit-Position: refs/heads/main@{#79560}
2022-03-22 20:40:41 +00:00

556 lines
21 KiB
C++

// Copyright 2014 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/common-operator-reducer.h"
#include "src/codegen/machine-type.h"
#include "src/compiler/common-operator.h"
#include "src/compiler/machine-operator.h"
#include "src/compiler/operator.h"
#include "src/compiler/simplified-operator.h"
#include "test/unittests/compiler/graph-reducer-unittest.h"
#include "test/unittests/compiler/graph-unittest.h"
#include "test/unittests/compiler/node-test-utils.h"
using testing::StrictMock;
namespace v8 {
namespace internal {
namespace compiler {
namespace common_operator_reducer_unittest {
class CommonOperatorReducerTest : public GraphTest {
public:
explicit CommonOperatorReducerTest(int num_parameters = 1)
: GraphTest(num_parameters), machine_(zone()), simplified_(zone()) {}
~CommonOperatorReducerTest() override = default;
protected:
Reduction Reduce(
AdvancedReducer::Editor* editor, Node* node,
BranchSemantics branch_semantics,
MachineOperatorBuilder::Flags flags = MachineOperatorBuilder::kNoFlags) {
JSHeapBroker broker(isolate(), zone());
MachineOperatorBuilder machine(zone(), MachineType::PointerRepresentation(),
flags);
CommonOperatorReducer reducer(editor, graph(), &broker, common(), &machine,
zone(), branch_semantics);
return reducer.Reduce(node);
}
Reduction Reduce(
Node* node, BranchSemantics branch_semantics,
MachineOperatorBuilder::Flags flags = MachineOperatorBuilder::kNoFlags) {
StrictMock<MockAdvancedReducerEditor> editor;
return Reduce(&editor, node, branch_semantics, flags);
}
MachineOperatorBuilder* machine() { return &machine_; }
SimplifiedOperatorBuilder* simplified() { return &simplified_; }
private:
MachineOperatorBuilder machine_;
SimplifiedOperatorBuilder simplified_;
};
namespace {
const BranchHint kBranchHints[] = {BranchHint::kNone, BranchHint::kFalse,
BranchHint::kTrue};
const MachineRepresentation kMachineRepresentations[] = {
MachineRepresentation::kBit, MachineRepresentation::kWord8,
MachineRepresentation::kWord16, MachineRepresentation::kWord32,
MachineRepresentation::kWord64, MachineRepresentation::kFloat32,
MachineRepresentation::kFloat64, MachineRepresentation::kTagged};
const Operator kOp0(0, Operator::kNoProperties, "Op0", 0, 0, 0, 1, 1, 0);
} // namespace
// -----------------------------------------------------------------------------
// Branch
TEST_F(CommonOperatorReducerTest, BranchWithInt32ZeroConstant) {
TRACED_FOREACH(BranchHint, hint, kBranchHints) {
Node* const control = graph()->start();
Node* const branch =
graph()->NewNode(common()->Branch(hint), Int32Constant(0), control);
Node* const if_true = graph()->NewNode(common()->IfTrue(), branch);
Node* const if_false = graph()->NewNode(common()->IfFalse(), branch);
StrictMock<MockAdvancedReducerEditor> editor;
EXPECT_CALL(editor, Replace(if_true, IsDead()));
EXPECT_CALL(editor, Replace(if_false, control));
Reduction const r = Reduce(&editor, branch, BranchSemantics::kMachine);
ASSERT_TRUE(r.Changed());
EXPECT_THAT(r.replacement(), IsDead());
}
}
TEST_F(CommonOperatorReducerTest, BranchWithInt32OneConstant) {
TRACED_FOREACH(BranchHint, hint, kBranchHints) {
Node* const control = graph()->start();
Node* const branch =
graph()->NewNode(common()->Branch(hint), Int32Constant(1), control);
Node* const if_true = graph()->NewNode(common()->IfTrue(), branch);
Node* const if_false = graph()->NewNode(common()->IfFalse(), branch);
StrictMock<MockAdvancedReducerEditor> editor;
EXPECT_CALL(editor, Replace(if_true, control));
EXPECT_CALL(editor, Replace(if_false, IsDead()));
Reduction const r = Reduce(&editor, branch, BranchSemantics::kMachine);
ASSERT_TRUE(r.Changed());
EXPECT_THAT(r.replacement(), IsDead());
}
}
TEST_F(CommonOperatorReducerTest, BranchWithFalseConstant) {
TRACED_FOREACH(BranchHint, hint, kBranchHints) {
Node* const control = graph()->start();
Node* const branch =
graph()->NewNode(common()->Branch(hint), FalseConstant(), control);
Node* const if_true = graph()->NewNode(common()->IfTrue(), branch);
Node* const if_false = graph()->NewNode(common()->IfFalse(), branch);
StrictMock<MockAdvancedReducerEditor> editor;
EXPECT_CALL(editor, Replace(if_true, IsDead()));
EXPECT_CALL(editor, Replace(if_false, control));
Reduction const r = Reduce(&editor, branch, BranchSemantics::kJS);
ASSERT_TRUE(r.Changed());
EXPECT_THAT(r.replacement(), IsDead());
}
}
TEST_F(CommonOperatorReducerTest, BranchWithTrueConstant) {
TRACED_FOREACH(BranchHint, hint, kBranchHints) {
Node* const control = graph()->start();
Node* const branch =
graph()->NewNode(common()->Branch(hint), TrueConstant(), control);
Node* const if_true = graph()->NewNode(common()->IfTrue(), branch);
Node* const if_false = graph()->NewNode(common()->IfFalse(), branch);
StrictMock<MockAdvancedReducerEditor> editor;
EXPECT_CALL(editor, Replace(if_true, control));
EXPECT_CALL(editor, Replace(if_false, IsDead()));
Reduction const r = Reduce(&editor, branch, BranchSemantics::kJS);
ASSERT_TRUE(r.Changed());
EXPECT_THAT(r.replacement(), IsDead());
}
}
TEST_F(CommonOperatorReducerTest, BranchWithBooleanNot) {
Node* const value = Parameter(0);
TRACED_FOREACH(BranchHint, hint, kBranchHints) {
Node* const control = graph()->start();
Node* const branch = graph()->NewNode(
common()->Branch(hint),
graph()->NewNode(simplified()->BooleanNot(), value), control);
Node* const if_true = graph()->NewNode(common()->IfTrue(), branch);
Node* const if_false = graph()->NewNode(common()->IfFalse(), branch);
Reduction const r = Reduce(branch, BranchSemantics::kJS);
ASSERT_TRUE(r.Changed());
EXPECT_EQ(branch, r.replacement());
EXPECT_THAT(branch, IsBranch(value, control));
EXPECT_THAT(if_false, IsIfTrue(branch));
EXPECT_THAT(if_true, IsIfFalse(branch));
EXPECT_EQ(NegateBranchHint(hint), BranchHintOf(branch->op()));
}
}
TEST_F(CommonOperatorReducerTest, BranchWithSelect) {
Node* const value = Parameter(0);
TRACED_FOREACH(BranchHint, hint, kBranchHints) {
Node* const control = graph()->start();
Node* const branch = graph()->NewNode(
common()->Branch(hint),
graph()->NewNode(common()->Select(MachineRepresentation::kTagged),
value, FalseConstant(), TrueConstant()),
control);
Node* const if_true = graph()->NewNode(common()->IfTrue(), branch);
Node* const if_false = graph()->NewNode(common()->IfFalse(), branch);
Reduction const r = Reduce(branch, BranchSemantics::kJS);
ASSERT_TRUE(r.Changed());
EXPECT_EQ(branch, r.replacement());
EXPECT_THAT(branch, IsBranch(value, control));
EXPECT_THAT(if_false, IsIfTrue(branch));
EXPECT_THAT(if_true, IsIfFalse(branch));
EXPECT_EQ(NegateBranchHint(hint), BranchHintOf(branch->op()));
}
}
// -----------------------------------------------------------------------------
// Merge
TEST_F(CommonOperatorReducerTest, MergeOfUnusedDiamond0) {
Node* const value = Parameter(0);
Node* const control = graph()->start();
Node* const branch = graph()->NewNode(common()->Branch(), value, control);
Node* const if_true = graph()->NewNode(common()->IfTrue(), branch);
Node* const if_false = graph()->NewNode(common()->IfFalse(), branch);
Reduction const r =
Reduce(graph()->NewNode(common()->Merge(2), if_true, if_false),
BranchSemantics::kJS);
ASSERT_TRUE(r.Changed());
EXPECT_EQ(control, r.replacement());
EXPECT_THAT(branch, IsDead());
}
TEST_F(CommonOperatorReducerTest, MergeOfUnusedDiamond1) {
Node* const value = Parameter(0);
Node* const control = graph()->start();
Node* const branch = graph()->NewNode(common()->Branch(), value, control);
Node* const if_true = graph()->NewNode(common()->IfTrue(), branch);
Node* const if_false = graph()->NewNode(common()->IfFalse(), branch);
Reduction const r =
Reduce(graph()->NewNode(common()->Merge(2), if_false, if_true),
BranchSemantics::kJS);
ASSERT_TRUE(r.Changed());
EXPECT_EQ(control, r.replacement());
EXPECT_THAT(branch, IsDead());
}
// -----------------------------------------------------------------------------
// EffectPhi
TEST_F(CommonOperatorReducerTest, EffectPhiWithMerge) {
const int kMaxInputs = 64;
Node* inputs[kMaxInputs];
Node* const input = graph()->NewNode(&kOp0);
TRACED_FORRANGE(int, input_count, 2, kMaxInputs - 1) {
int const value_input_count = input_count - 1;
for (int i = 0; i < value_input_count; ++i) {
inputs[i] = graph()->start();
}
Node* const merge = graph()->NewNode(common()->Merge(value_input_count),
value_input_count, inputs);
for (int i = 0; i < value_input_count; ++i) {
inputs[i] = input;
}
inputs[value_input_count] = merge;
StrictMock<MockAdvancedReducerEditor> editor;
EXPECT_CALL(editor, Revisit(merge));
Reduction r =
Reduce(&editor,
graph()->NewNode(common()->EffectPhi(value_input_count),
input_count, inputs),
BranchSemantics::kJS);
ASSERT_TRUE(r.Changed());
EXPECT_EQ(input, r.replacement());
}
}
TEST_F(CommonOperatorReducerTest, EffectPhiWithLoop) {
Node* const e0 = graph()->NewNode(&kOp0);
Node* const loop =
graph()->NewNode(common()->Loop(2), graph()->start(), graph()->start());
loop->ReplaceInput(1, loop);
Node* const ephi = graph()->NewNode(common()->EffectPhi(2), e0, e0, loop);
ephi->ReplaceInput(1, ephi);
StrictMock<MockAdvancedReducerEditor> editor;
EXPECT_CALL(editor, Revisit(loop));
Reduction const r = Reduce(&editor, ephi, BranchSemantics::kJS);
ASSERT_TRUE(r.Changed());
EXPECT_EQ(e0, r.replacement());
}
// -----------------------------------------------------------------------------
// Phi
TEST_F(CommonOperatorReducerTest, PhiWithMerge) {
const int kMaxInputs = 64;
Node* inputs[kMaxInputs];
Node* const input = graph()->NewNode(&kOp0);
TRACED_FORRANGE(int, input_count, 2, kMaxInputs - 1) {
int const value_input_count = input_count - 1;
TRACED_FOREACH(MachineRepresentation, rep, kMachineRepresentations) {
for (int i = 0; i < value_input_count; ++i) {
inputs[i] = graph()->start();
}
Node* const merge = graph()->NewNode(common()->Merge(value_input_count),
value_input_count, inputs);
for (int i = 0; i < value_input_count; ++i) {
inputs[i] = input;
}
inputs[value_input_count] = merge;
StrictMock<MockAdvancedReducerEditor> editor;
EXPECT_CALL(editor, Revisit(merge));
Reduction r =
Reduce(&editor,
graph()->NewNode(common()->Phi(rep, value_input_count),
input_count, inputs),
BranchSemantics::kJS);
ASSERT_TRUE(r.Changed());
EXPECT_EQ(input, r.replacement());
}
}
}
TEST_F(CommonOperatorReducerTest, PhiWithLoop) {
Node* const p0 = Parameter(0);
Node* const loop =
graph()->NewNode(common()->Loop(2), graph()->start(), graph()->start());
loop->ReplaceInput(1, loop);
Node* const phi = graph()->NewNode(
common()->Phi(MachineRepresentation::kTagged, 2), p0, p0, loop);
phi->ReplaceInput(1, phi);
StrictMock<MockAdvancedReducerEditor> editor;
EXPECT_CALL(editor, Revisit(loop));
Reduction const r = Reduce(&editor, phi, BranchSemantics::kMachine);
ASSERT_TRUE(r.Changed());
EXPECT_EQ(p0, r.replacement());
}
TEST_F(CommonOperatorReducerTest, PhiToFloat32Abs) {
Node* p0 = Parameter(0);
Node* c0 = Float32Constant(0.0);
Node* check = graph()->NewNode(machine()->Float32LessThan(), c0, p0);
Node* branch = graph()->NewNode(common()->Branch(), check, graph()->start());
Node* if_true = graph()->NewNode(common()->IfTrue(), branch);
Node* vtrue = p0;
Node* if_false = graph()->NewNode(common()->IfFalse(), branch);
Node* vfalse = graph()->NewNode(machine()->Float32Sub(), c0, p0);
Node* merge = graph()->NewNode(common()->Merge(2), if_true, if_false);
Node* phi = graph()->NewNode(
common()->Phi(MachineRepresentation::kFloat32, 2), vtrue, vfalse, merge);
StrictMock<MockAdvancedReducerEditor> editor;
EXPECT_CALL(editor, Revisit(merge));
Reduction r = Reduce(&editor, phi, BranchSemantics::kMachine);
ASSERT_TRUE(r.Changed());
EXPECT_THAT(r.replacement(), IsFloat32Abs(p0));
}
TEST_F(CommonOperatorReducerTest, PhiToFloat64Abs) {
Node* p0 = Parameter(0);
Node* c0 = Float64Constant(0.0);
Node* check = graph()->NewNode(machine()->Float64LessThan(), c0, p0);
Node* branch = graph()->NewNode(common()->Branch(), check, graph()->start());
Node* if_true = graph()->NewNode(common()->IfTrue(), branch);
Node* vtrue = p0;
Node* if_false = graph()->NewNode(common()->IfFalse(), branch);
Node* vfalse = graph()->NewNode(machine()->Float64Sub(), c0, p0);
Node* merge = graph()->NewNode(common()->Merge(2), if_true, if_false);
Node* phi = graph()->NewNode(
common()->Phi(MachineRepresentation::kFloat64, 2), vtrue, vfalse, merge);
StrictMock<MockAdvancedReducerEditor> editor;
EXPECT_CALL(editor, Revisit(merge));
Reduction r = Reduce(&editor, phi, BranchSemantics::kMachine);
ASSERT_TRUE(r.Changed());
EXPECT_THAT(r.replacement(), IsFloat64Abs(p0));
}
// -----------------------------------------------------------------------------
// Return
TEST_F(CommonOperatorReducerTest, ReturnWithPhiAndEffectPhiAndMerge) {
Node* cond = Parameter(2);
Node* branch = graph()->NewNode(common()->Branch(), cond, graph()->start());
Node* if_true = graph()->NewNode(common()->IfTrue(), branch);
Node* etrue = graph()->start();
Node* vtrue = Parameter(0);
Node* if_false = graph()->NewNode(common()->IfFalse(), branch);
Node* efalse = graph()->start();
Node* vfalse = Parameter(1);
Node* merge = graph()->NewNode(common()->Merge(2), if_true, if_false);
Node* ephi = graph()->NewNode(common()->EffectPhi(2), etrue, efalse, merge);
Node* phi = graph()->NewNode(common()->Phi(MachineRepresentation::kTagged, 2),
vtrue, vfalse, merge);
Node* zero = graph()->NewNode(common()->Int32Constant(0));
Node* ret = graph()->NewNode(common()->Return(), zero, phi, ephi, merge);
graph()->SetEnd(graph()->NewNode(common()->End(1), ret));
StrictMock<MockAdvancedReducerEditor> editor;
EXPECT_CALL(editor, Replace(merge, IsDead()));
Reduction const r = Reduce(&editor, ret, BranchSemantics::kJS);
ASSERT_TRUE(r.Changed());
EXPECT_THAT(r.replacement(), IsDead());
EXPECT_THAT(graph()->end(), IsEnd(ret, IsReturn(vtrue, etrue, if_true),
IsReturn(vfalse, efalse, if_false)));
}
TEST_F(CommonOperatorReducerTest, MultiReturnWithPhiAndEffectPhiAndMerge) {
Node* cond = Parameter(2);
Node* branch = graph()->NewNode(common()->Branch(), cond, graph()->start());
Node* if_true = graph()->NewNode(common()->IfTrue(), branch);
Node* etrue = graph()->start();
Node* vtrue1 = Parameter(0);
Node* vtrue2 = Parameter(1);
Node* if_false = graph()->NewNode(common()->IfFalse(), branch);
Node* efalse = graph()->start();
Node* vfalse1 = Parameter(1);
Node* vfalse2 = Parameter(0);
Node* merge = graph()->NewNode(common()->Merge(2), if_true, if_false);
Node* ephi = graph()->NewNode(common()->EffectPhi(2), etrue, efalse, merge);
Node* phi1 = graph()->NewNode(
common()->Phi(MachineRepresentation::kTagged, 2), vtrue1, vfalse1, merge);
Node* phi2 = graph()->NewNode(
common()->Phi(MachineRepresentation::kTagged, 2), vtrue2, vfalse2, merge);
Node* zero = graph()->NewNode(common()->Int32Constant(0));
Node* ret =
graph()->NewNode(common()->Return(2), zero, phi1, phi2, ephi, merge);
graph()->SetEnd(graph()->NewNode(common()->End(1), ret));
StrictMock<MockAdvancedReducerEditor> editor;
Reduction const r = Reduce(&editor, ret, BranchSemantics::kJS);
// For now a return with multiple return values should not be reduced.
ASSERT_TRUE(!r.Changed());
}
// -----------------------------------------------------------------------------
// Select
TEST_F(CommonOperatorReducerTest, SelectWithSameThenAndElse) {
Node* const input = graph()->NewNode(&kOp0);
TRACED_FOREACH(BranchHint, hint, kBranchHints) {
TRACED_FOREACH(MachineRepresentation, rep, kMachineRepresentations) {
Reduction r = Reduce(
graph()->NewNode(common()->Select(rep, hint), input, input, input),
BranchSemantics::kJS);
ASSERT_TRUE(r.Changed());
EXPECT_EQ(input, r.replacement());
}
}
}
TEST_F(CommonOperatorReducerTest, SelectWithInt32ZeroConstant) {
Node* p0 = Parameter(0);
Node* p1 = Parameter(1);
Node* select =
graph()->NewNode(common()->Select(MachineRepresentation::kTagged),
Int32Constant(0), p0, p1);
Reduction r = Reduce(select, BranchSemantics::kMachine);
ASSERT_TRUE(r.Changed());
EXPECT_EQ(p1, r.replacement());
}
TEST_F(CommonOperatorReducerTest, SelectWithInt32OneConstant) {
Node* p0 = Parameter(0);
Node* p1 = Parameter(1);
Node* select =
graph()->NewNode(common()->Select(MachineRepresentation::kTagged),
Int32Constant(1), p0, p1);
Reduction r = Reduce(select, BranchSemantics::kMachine);
ASSERT_TRUE(r.Changed());
EXPECT_EQ(p0, r.replacement());
}
TEST_F(CommonOperatorReducerTest, SelectWithFalseConstant) {
Node* p0 = Parameter(0);
Node* p1 = Parameter(1);
Node* select =
graph()->NewNode(common()->Select(MachineRepresentation::kTagged),
FalseConstant(), p0, p1);
Reduction r = Reduce(select, BranchSemantics::kJS);
ASSERT_TRUE(r.Changed());
EXPECT_EQ(p1, r.replacement());
}
TEST_F(CommonOperatorReducerTest, SelectWithTrueConstant) {
Node* p0 = Parameter(0);
Node* p1 = Parameter(1);
Node* select = graph()->NewNode(
common()->Select(MachineRepresentation::kTagged), TrueConstant(), p0, p1);
Reduction r = Reduce(select, BranchSemantics::kJS);
ASSERT_TRUE(r.Changed());
EXPECT_EQ(p0, r.replacement());
}
TEST_F(CommonOperatorReducerTest, SelectToFloat32Abs) {
Node* p0 = Parameter(0);
Node* c0 = Float32Constant(0.0);
Node* check = graph()->NewNode(machine()->Float32LessThan(), c0, p0);
Node* select =
graph()->NewNode(common()->Select(MachineRepresentation::kFloat32), check,
p0, graph()->NewNode(machine()->Float32Sub(), c0, p0));
Reduction r = Reduce(select, BranchSemantics::kMachine);
ASSERT_TRUE(r.Changed());
EXPECT_THAT(r.replacement(), IsFloat32Abs(p0));
}
TEST_F(CommonOperatorReducerTest, SelectToFloat64Abs) {
Node* p0 = Parameter(0);
Node* c0 = Float64Constant(0.0);
Node* check = graph()->NewNode(machine()->Float64LessThan(), c0, p0);
Node* select =
graph()->NewNode(common()->Select(MachineRepresentation::kFloat64), check,
p0, graph()->NewNode(machine()->Float64Sub(), c0, p0));
Reduction r = Reduce(select, BranchSemantics::kMachine);
ASSERT_TRUE(r.Changed());
EXPECT_THAT(r.replacement(), IsFloat64Abs(p0));
}
// -----------------------------------------------------------------------------
// Switch
TEST_F(CommonOperatorReducerTest, SwitchInputMatchesCaseWithDefault) {
Node* const control = graph()->start();
Node* sw = graph()->NewNode(common()->Switch(2), Int32Constant(1), control);
Node* const if_1 = graph()->NewNode(common()->IfValue(1), sw);
graph()->NewNode(common()->IfDefault(), sw);
StrictMock<MockAdvancedReducerEditor> editor;
EXPECT_CALL(editor, Replace(if_1, control));
Reduction r = Reduce(&editor, sw, BranchSemantics::kMachine);
ASSERT_TRUE(r.Changed());
EXPECT_THAT(r.replacement(), IsDead());
}
TEST_F(CommonOperatorReducerTest, SwitchInputMatchesDefaultWithCase) {
Node* const control = graph()->start();
Node* sw = graph()->NewNode(common()->Switch(2), Int32Constant(0), control);
graph()->NewNode(common()->IfValue(1), sw);
Node* const if_default = graph()->NewNode(common()->IfDefault(), sw);
StrictMock<MockAdvancedReducerEditor> editor;
EXPECT_CALL(editor, Replace(if_default, control));
Reduction r = Reduce(&editor, sw, BranchSemantics::kMachine);
ASSERT_TRUE(r.Changed());
EXPECT_THAT(r.replacement(), IsDead());
}
TEST_F(CommonOperatorReducerTest, SwitchInputMatchesCaseExtraCaseWithDefault) {
Node* const control = graph()->start();
Node* sw = graph()->NewNode(common()->Switch(3), Int32Constant(0), control);
Node* const if_0 = graph()->NewNode(common()->IfValue(0), sw);
graph()->NewNode(common()->IfValue(1), sw);
graph()->NewNode(common()->IfDefault(), sw);
StrictMock<MockAdvancedReducerEditor> editor;
EXPECT_CALL(editor, Replace(if_0, control));
Reduction r = Reduce(&editor, sw, BranchSemantics::kMachine);
ASSERT_TRUE(r.Changed());
EXPECT_THAT(r.replacement(), IsDead());
}
} // namespace common_operator_reducer_unittest
} // namespace compiler
} // namespace internal
} // namespace v8