2015-02-17 13:29:31 +00:00
|
|
|
// 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/control-flow-optimizer.h"
|
|
|
|
#include "src/compiler/js-operator.h"
|
|
|
|
#include "src/compiler/machine-operator.h"
|
|
|
|
#include "test/unittests/compiler/graph-unittest.h"
|
|
|
|
#include "test/unittests/compiler/node-test-utils.h"
|
|
|
|
#include "testing/gmock-support.h"
|
|
|
|
|
|
|
|
using testing::AllOf;
|
|
|
|
using testing::Capture;
|
|
|
|
using testing::CaptureEq;
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace compiler {
|
|
|
|
|
|
|
|
class ControlFlowOptimizerTest : public GraphTest {
|
|
|
|
public:
|
|
|
|
explicit ControlFlowOptimizerTest(int num_parameters = 3)
|
2015-05-28 09:12:50 +00:00
|
|
|
: GraphTest(num_parameters), machine_(zone()), javascript_(zone()) {}
|
2015-04-20 13:08:11 +00:00
|
|
|
~ControlFlowOptimizerTest() override {}
|
2015-02-17 13:29:31 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
void Optimize() {
|
2015-05-28 09:12:50 +00:00
|
|
|
ControlFlowOptimizer optimizer(graph(), common(), machine(), zone());
|
2015-02-17 13:29:31 +00:00
|
|
|
optimizer.Optimize();
|
|
|
|
}
|
|
|
|
|
2015-03-03 06:11:37 +00:00
|
|
|
JSOperatorBuilder* javascript() { return &javascript_; }
|
2015-02-17 13:29:31 +00:00
|
|
|
MachineOperatorBuilder* machine() { return &machine_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
MachineOperatorBuilder machine_;
|
2015-03-03 06:11:37 +00:00
|
|
|
JSOperatorBuilder javascript_;
|
2015-02-17 13:29:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-03-03 06:11:37 +00:00
|
|
|
TEST_F(ControlFlowOptimizerTest, BuildSwitch1) {
|
2015-02-17 13:29:31 +00:00
|
|
|
Node* index = Parameter(0);
|
|
|
|
Node* branch0 = graph()->NewNode(
|
|
|
|
common()->Branch(),
|
|
|
|
graph()->NewNode(machine()->Word32Equal(), index, Int32Constant(0)),
|
|
|
|
start());
|
|
|
|
Node* if_true0 = graph()->NewNode(common()->IfTrue(), branch0);
|
|
|
|
Node* if_false0 = graph()->NewNode(common()->IfFalse(), branch0);
|
|
|
|
Node* branch1 = graph()->NewNode(
|
|
|
|
common()->Branch(),
|
|
|
|
graph()->NewNode(machine()->Word32Equal(), index, Int32Constant(1)),
|
|
|
|
if_false0);
|
|
|
|
Node* if_true1 = graph()->NewNode(common()->IfTrue(), branch1);
|
|
|
|
Node* if_false1 = graph()->NewNode(common()->IfFalse(), branch1);
|
|
|
|
Node* merge =
|
|
|
|
graph()->NewNode(common()->Merge(3), if_true0, if_true1, if_false1);
|
2015-05-26 10:31:55 +00:00
|
|
|
graph()->SetEnd(graph()->NewNode(common()->End(1), merge));
|
2015-02-17 13:29:31 +00:00
|
|
|
Optimize();
|
|
|
|
Capture<Node*> switch_capture;
|
2018-03-02 10:54:30 +00:00
|
|
|
EXPECT_THAT(
|
|
|
|
end(), IsEnd(IsMerge(
|
|
|
|
IsIfValue(IfValueParameters(0, 1), CaptureEq(&switch_capture)),
|
|
|
|
IsIfValue(IfValueParameters(1, 2), CaptureEq(&switch_capture)),
|
|
|
|
IsIfDefault(AllOf(CaptureEq(&switch_capture),
|
|
|
|
IsSwitch(index, start()))))));
|
2015-02-17 13:29:31 +00:00
|
|
|
}
|
|
|
|
|
2015-02-24 07:41:25 +00:00
|
|
|
|
2015-03-03 06:11:37 +00:00
|
|
|
TEST_F(ControlFlowOptimizerTest, BuildSwitch2) {
|
|
|
|
Node* input = Parameter(0);
|
|
|
|
Node* context = Parameter(1);
|
2015-04-23 09:04:37 +00:00
|
|
|
Node* index = graph()->NewNode(javascript()->ToNumber(), input, context,
|
2015-05-28 09:12:50 +00:00
|
|
|
start(), start(), start());
|
2015-03-03 06:11:37 +00:00
|
|
|
Node* if_success = graph()->NewNode(common()->IfSuccess(), index);
|
|
|
|
Node* branch0 = graph()->NewNode(
|
|
|
|
common()->Branch(),
|
|
|
|
graph()->NewNode(machine()->Word32Equal(), index, Int32Constant(0)),
|
|
|
|
if_success);
|
|
|
|
Node* if_true0 = graph()->NewNode(common()->IfTrue(), branch0);
|
|
|
|
Node* if_false0 = graph()->NewNode(common()->IfFalse(), branch0);
|
|
|
|
Node* branch1 = graph()->NewNode(
|
|
|
|
common()->Branch(),
|
|
|
|
graph()->NewNode(machine()->Word32Equal(), index, Int32Constant(1)),
|
|
|
|
if_false0);
|
|
|
|
Node* if_true1 = graph()->NewNode(common()->IfTrue(), branch1);
|
|
|
|
Node* if_false1 = graph()->NewNode(common()->IfFalse(), branch1);
|
|
|
|
Node* merge =
|
|
|
|
graph()->NewNode(common()->Merge(3), if_true0, if_true1, if_false1);
|
2015-05-26 10:31:55 +00:00
|
|
|
graph()->SetEnd(graph()->NewNode(common()->End(1), merge));
|
2015-03-03 06:11:37 +00:00
|
|
|
Optimize();
|
|
|
|
Capture<Node*> switch_capture;
|
|
|
|
EXPECT_THAT(
|
2018-03-02 10:54:30 +00:00
|
|
|
end(), IsEnd(IsMerge(
|
|
|
|
IsIfValue(IfValueParameters(0, 1), CaptureEq(&switch_capture)),
|
|
|
|
IsIfValue(IfValueParameters(1, 2), CaptureEq(&switch_capture)),
|
|
|
|
IsIfDefault(AllOf(CaptureEq(&switch_capture),
|
|
|
|
IsSwitch(index, IsIfSuccess(index)))))));
|
2015-03-03 06:11:37 +00:00
|
|
|
}
|
|
|
|
|
2015-02-17 13:29:31 +00:00
|
|
|
} // namespace compiler
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|