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.
5 #include "src/compiler/common-operator.h"
6 #include "src/compiler/node-properties.h"
7 #include "test/unittests/test-utils.h"
8 #include "testing/gmock/include/gmock/gmock.h"
11 using testing::ElementsAre;
12 using testing::IsNull;
13 using testing::UnorderedElementsAre;
19 typedef TestWithZone NodePropertiesTest;
24 const Operator kMockOperator(IrOpcode::kDead, Operator::kNoProperties,
25 "MockOperator", 0, 0, 0, 1, 0, 0);
26 const Operator kMockOpEffect(IrOpcode::kDead, Operator::kNoProperties,
27 "MockOpEffect", 0, 1, 0, 1, 1, 0);
28 const Operator kMockOpControl(IrOpcode::kDead, Operator::kNoProperties,
29 "MockOpControl", 0, 0, 1, 1, 0, 1);
30 const Operator kMockCallOperator(IrOpcode::kCall, Operator::kNoProperties,
31 "MockCallOperator", 0, 0, 0, 0, 0, 2);
36 TEST_F(NodePropertiesTest, ReplaceWithValue_ValueUse) {
37 CommonOperatorBuilder common(zone());
38 Node* node = Node::New(zone(), 0, &kMockOperator, 0, nullptr, false);
39 Node* use_value = Node::New(zone(), 0, common.Return(), 1, &node, false);
40 Node* replacement = Node::New(zone(), 0, &kMockOperator, 0, nullptr, false);
41 NodeProperties::ReplaceWithValue(node, replacement);
42 EXPECT_EQ(replacement, use_value->InputAt(0));
43 EXPECT_EQ(0, node->UseCount());
44 EXPECT_EQ(1, replacement->UseCount());
45 EXPECT_THAT(replacement->uses(), ElementsAre(use_value));
49 TEST_F(NodePropertiesTest, ReplaceWithValue_EffectUse) {
50 CommonOperatorBuilder common(zone());
51 Node* start = Node::New(zone(), 0, common.Start(1), 0, nullptr, false);
52 Node* node = Node::New(zone(), 0, &kMockOpEffect, 1, &start, false);
53 Node* use_effect = Node::New(zone(), 0, common.EffectPhi(1), 1, &node, false);
54 Node* replacement = Node::New(zone(), 0, &kMockOperator, 0, nullptr, false);
55 NodeProperties::ReplaceWithValue(node, replacement);
56 EXPECT_EQ(start, use_effect->InputAt(0));
57 EXPECT_EQ(0, node->UseCount());
58 EXPECT_EQ(2, start->UseCount());
59 EXPECT_EQ(0, replacement->UseCount());
60 EXPECT_THAT(start->uses(), UnorderedElementsAre(use_effect, node));
64 TEST_F(NodePropertiesTest, ReplaceWithValue_ControlUse) {
65 CommonOperatorBuilder common(zone());
66 Node* start = Node::New(zone(), 0, common.Start(1), 0, nullptr, false);
67 Node* node = Node::New(zone(), 0, &kMockOpControl, 1, &start, false);
68 Node* success = Node::New(zone(), 0, common.IfSuccess(), 1, &node, false);
69 Node* use_control = Node::New(zone(), 0, common.Merge(1), 1, &success, false);
70 Node* replacement = Node::New(zone(), 0, &kMockOperator, 0, nullptr, false);
71 NodeProperties::ReplaceWithValue(node, replacement);
72 EXPECT_EQ(start, use_control->InputAt(0));
73 EXPECT_EQ(0, node->UseCount());
74 EXPECT_EQ(2, start->UseCount());
75 EXPECT_EQ(0, replacement->UseCount());
76 EXPECT_THAT(start->uses(), UnorderedElementsAre(use_control, node));
80 TEST_F(NodePropertiesTest, FindProjection) {
81 CommonOperatorBuilder common(zone());
82 Node* start = Node::New(zone(), 0, common.Start(1), 0, nullptr, false);
83 Node* proj0 = Node::New(zone(), 1, common.Projection(0), 1, &start, false);
84 Node* proj1 = Node::New(zone(), 2, common.Projection(1), 1, &start, false);
85 EXPECT_EQ(proj0, NodeProperties::FindProjection(start, 0));
86 EXPECT_EQ(proj1, NodeProperties::FindProjection(start, 1));
87 EXPECT_THAT(NodeProperties::FindProjection(start, 2), IsNull());
88 EXPECT_THAT(NodeProperties::FindProjection(start, 1234567890), IsNull());
92 TEST_F(NodePropertiesTest, CollectControlProjections_Branch) {
94 CommonOperatorBuilder common(zone());
95 Node* branch = Node::New(zone(), 1, common.Branch(), 0, nullptr, false);
96 Node* if_false = Node::New(zone(), 2, common.IfFalse(), 1, &branch, false);
97 Node* if_true = Node::New(zone(), 3, common.IfTrue(), 1, &branch, false);
98 NodeProperties::CollectControlProjections(branch, result, arraysize(result));
99 EXPECT_EQ(if_true, result[0]);
100 EXPECT_EQ(if_false, result[1]);
104 TEST_F(NodePropertiesTest, CollectControlProjections_Call) {
106 CommonOperatorBuilder common(zone());
107 Node* call = Node::New(zone(), 1, &kMockCallOperator, 0, nullptr, false);
108 Node* if_ex = Node::New(zone(), 2, common.IfException(), 1, &call, false);
109 Node* if_ok = Node::New(zone(), 3, common.IfSuccess(), 1, &call, false);
110 NodeProperties::CollectControlProjections(call, result, arraysize(result));
111 EXPECT_EQ(if_ok, result[0]);
112 EXPECT_EQ(if_ex, result[1]);
116 TEST_F(NodePropertiesTest, CollectControlProjections_Switch) {
118 CommonOperatorBuilder common(zone());
119 Node* sw = Node::New(zone(), 1, common.Switch(3), 0, nullptr, false);
120 Node* if_default = Node::New(zone(), 2, common.IfDefault(), 1, &sw, false);
121 Node* if_value1 = Node::New(zone(), 3, common.IfValue(1), 1, &sw, false);
122 Node* if_value2 = Node::New(zone(), 4, common.IfValue(2), 1, &sw, false);
123 NodeProperties::CollectControlProjections(sw, result, arraysize(result));
124 EXPECT_THAT(result[0], AnyOf(if_value1, if_value2));
125 EXPECT_THAT(result[1], AnyOf(if_value1, if_value2));
126 EXPECT_EQ(if_default, result[2]);
129 } // namespace compiler
130 } // namespace internal