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
9using testing::ElementsAre;
10using testing::UnorderedElementsAre;
11
12namespace v8 {
13namespace internal {
14namespace compiler {
15
16class GraphTrimmerTest : public GraphTest {
17 public:
18  GraphTrimmerTest() : GraphTest(1) {}
19
20 protected:
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  }
26  void TrimGraph() {
27    GraphTrimmer trimmer(zone(), graph());
28    trimmer.TrimGraph();
29  }
30};
31
32
33namespace {
34
35const Operator kDead0(IrOpcode::kDead, Operator::kNoProperties, "Dead0", 0, 0,
36                      1, 0, 0, 0);
37const Operator kLive0(IrOpcode::kDead, Operator::kNoProperties, "Live0", 0, 0,
38                      1, 0, 0, 1);
39
40}  // namespace
41
42
43TEST_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
55TEST_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
64TEST_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
75TEST_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