1 // Copyright 2015 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/graph-trimmer.h"
6 #include "test/unittests/compiler/graph-unittest.h"
7 #include "testing/gmock-support.h"
8
9 using testing::ElementsAre;
10 using testing::UnorderedElementsAre;
11
12 namespace v8 {
13 namespace internal {
14 namespace compiler {
15
16 class GraphTrimmerTest : public GraphTest {
17 public:
GraphTrimmerTest()18 GraphTrimmerTest() : GraphTest(1) {}
19
20 protected:
TrimGraph(Node * root)21 void TrimGraph(Node* root) {
22 Node* const roots[1] = {root};
23 GraphTrimmer trimmer(zone(), graph());
24 trimmer.TrimGraph(&roots[0], &roots[arraysize(roots)]);
25 }
TrimGraph()26 void TrimGraph() {
27 GraphTrimmer trimmer(zone(), graph());
28 trimmer.TrimGraph();
29 }
30 };
31
32
33 namespace {
34
35 const Operator kDead0(IrOpcode::kDead, Operator::kNoProperties, "Dead0", 0, 0,
36 1, 0, 0, 0);
37 const Operator kLive0(IrOpcode::kDead, Operator::kNoProperties, "Live0", 0, 0,
38 1, 0, 0, 1);
39
40 } // namespace
41
42
TEST_F(GraphTrimmerTest,Empty)43 TEST_F(GraphTrimmerTest, Empty) {
44 Node* const start = graph()->NewNode(common()->Start(0));
45 Node* const end = graph()->NewNode(common()->End(1), start);
46 graph()->SetStart(start);
47 graph()->SetEnd(end);
48 TrimGraph();
49 EXPECT_EQ(end, graph()->end());
50 EXPECT_EQ(start, graph()->start());
51 EXPECT_EQ(start, end->InputAt(0));
52 }
53
54
TEST_F(GraphTrimmerTest,DeadUseOfStart)55 TEST_F(GraphTrimmerTest, DeadUseOfStart) {
56 Node* const dead0 = graph()->NewNode(&kDead0, graph()->start());
57 graph()->SetEnd(graph()->NewNode(common()->End(1), graph()->start()));
58 TrimGraph();
59 EXPECT_THAT(dead0->inputs(), ElementsAre(nullptr));
60 EXPECT_THAT(graph()->start()->uses(), ElementsAre(graph()->end()));
61 }
62
63
TEST_F(GraphTrimmerTest,DeadAndLiveUsesOfStart)64 TEST_F(GraphTrimmerTest, DeadAndLiveUsesOfStart) {
65 Node* const dead0 = graph()->NewNode(&kDead0, graph()->start());
66 Node* const live0 = graph()->NewNode(&kLive0, graph()->start());
67 graph()->SetEnd(graph()->NewNode(common()->End(1), live0));
68 TrimGraph();
69 EXPECT_THAT(dead0->inputs(), ElementsAre(nullptr));
70 EXPECT_THAT(graph()->start()->uses(), ElementsAre(live0));
71 EXPECT_THAT(live0->uses(), ElementsAre(graph()->end()));
72 }
73
74
TEST_F(GraphTrimmerTest,Roots)75 TEST_F(GraphTrimmerTest, Roots) {
76 Node* const live0 = graph()->NewNode(&kLive0, graph()->start());
77 Node* const live1 = graph()->NewNode(&kLive0, graph()->start());
78 graph()->SetEnd(graph()->NewNode(common()->End(1), live0));
79 TrimGraph(live1);
80 EXPECT_THAT(graph()->start()->uses(), UnorderedElementsAre(live0, live1));
81 }
82
83 } // namespace compiler
84 } // namespace internal
85 } // namespace v8
86