blob: 43cfd8484a712e0ad84c721e746c71cd46a99989 [file] [log] [blame]
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001// Copyright 2014 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "src/compiler/select-lowering.h"
6#include "test/unittests/compiler/graph-unittest.h"
7#include "test/unittests/compiler/node-test-utils.h"
8#include "testing/gmock-support.h"
9
10using testing::AllOf;
11using testing::Capture;
12using testing::CaptureEq;
13using testing::Not;
14
15namespace v8 {
16namespace internal {
17namespace compiler {
18
19class SelectLoweringTest : public GraphTest {
20 public:
21 SelectLoweringTest() : GraphTest(5), lowering_(graph(), common()) {}
22
23 protected:
24 Reduction Reduce(Node* node) { return lowering_.Reduce(node); }
25
26 private:
27 SelectLowering lowering_;
28};
29
30
31TEST_F(SelectLoweringTest, SelectWithSameConditions) {
32 Node* const p0 = Parameter(0);
33 Node* const p1 = Parameter(1);
34 Node* const p2 = Parameter(2);
35 Node* const p3 = Parameter(3);
36 Node* const p4 = Parameter(4);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000037 Node* const s0 = graph()->NewNode(
38 common()->Select(MachineRepresentation::kWord32), p0, p1, p2);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040039
40 Capture<Node*> branch;
41 Capture<Node*> merge;
42 {
43 Reduction const r = Reduce(s0);
44 ASSERT_TRUE(r.Changed());
45 EXPECT_THAT(
46 r.replacement(),
47 IsPhi(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000048 MachineRepresentation::kWord32, p1, p2,
Emily Bernierd0a1eb72015-03-24 16:35:39 -040049 AllOf(CaptureEq(&merge),
50 IsMerge(IsIfTrue(CaptureEq(&branch)),
51 IsIfFalse(AllOf(CaptureEq(&branch),
52 IsBranch(p0, graph()->start())))))));
53 }
54 {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000055 Reduction const r = Reduce(graph()->NewNode(
56 common()->Select(MachineRepresentation::kWord32), p0, p3, p4));
Emily Bernierd0a1eb72015-03-24 16:35:39 -040057 ASSERT_TRUE(r.Changed());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000058 EXPECT_THAT(r.replacement(), IsPhi(MachineRepresentation::kWord32, p3, p4,
59 CaptureEq(&merge)));
Emily Bernierd0a1eb72015-03-24 16:35:39 -040060 }
61 {
62 // We must not reuse the diamond if it is reachable from either else/then
63 // values of the Select, because the resulting graph can not be scheduled.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000064 Reduction const r = Reduce(graph()->NewNode(
65 common()->Select(MachineRepresentation::kWord32), p0, s0, p0));
Emily Bernierd0a1eb72015-03-24 16:35:39 -040066 ASSERT_TRUE(r.Changed());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000067 EXPECT_THAT(r.replacement(), IsPhi(MachineRepresentation::kWord32, s0, p0,
68 Not(CaptureEq(&merge))));
Emily Bernierd0a1eb72015-03-24 16:35:39 -040069 }
70}
71
72} // namespace compiler
73} // namespace internal
74} // namespace v8