1 // 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 "test/unittests/compiler/graph-unittest.h"
6
7 #include "src/compiler/node-properties.h"
8 #include "src/factory.h"
9 #include "src/objects-inl.h" // TODO(everyone): Make typer.h IWYU compliant.
10 #include "test/unittests/compiler/node-test-utils.h"
11
12 namespace v8 {
13 namespace internal {
14 namespace compiler {
15
GraphTest(int num_parameters)16 GraphTest::GraphTest(int num_parameters) : common_(zone()), graph_(zone()) {
17 graph()->SetStart(graph()->NewNode(common()->Start(num_parameters)));
18 graph()->SetEnd(graph()->NewNode(common()->End(1), graph()->start()));
19 }
20
21
~GraphTest()22 GraphTest::~GraphTest() {}
23
24
Parameter(int32_t index)25 Node* GraphTest::Parameter(int32_t index) {
26 return graph()->NewNode(common()->Parameter(index), graph()->start());
27 }
28
29
Float32Constant(volatile float value)30 Node* GraphTest::Float32Constant(volatile float value) {
31 return graph()->NewNode(common()->Float32Constant(value));
32 }
33
34
Float64Constant(volatile double value)35 Node* GraphTest::Float64Constant(volatile double value) {
36 return graph()->NewNode(common()->Float64Constant(value));
37 }
38
39
Int32Constant(int32_t value)40 Node* GraphTest::Int32Constant(int32_t value) {
41 return graph()->NewNode(common()->Int32Constant(value));
42 }
43
44
Int64Constant(int64_t value)45 Node* GraphTest::Int64Constant(int64_t value) {
46 return graph()->NewNode(common()->Int64Constant(value));
47 }
48
49
NumberConstant(volatile double value)50 Node* GraphTest::NumberConstant(volatile double value) {
51 return graph()->NewNode(common()->NumberConstant(value));
52 }
53
54
HeapConstant(const Handle<HeapObject> & value)55 Node* GraphTest::HeapConstant(const Handle<HeapObject>& value) {
56 Node* node = graph()->NewNode(common()->HeapConstant(value));
57 Type* type = Type::Constant(value, zone());
58 NodeProperties::SetType(node, type);
59 return node;
60 }
61
62
FalseConstant()63 Node* GraphTest::FalseConstant() {
64 return HeapConstant(factory()->false_value());
65 }
66
67
TrueConstant()68 Node* GraphTest::TrueConstant() {
69 return HeapConstant(factory()->true_value());
70 }
71
72
UndefinedConstant()73 Node* GraphTest::UndefinedConstant() {
74 return HeapConstant(factory()->undefined_value());
75 }
76
77
EmptyFrameState()78 Node* GraphTest::EmptyFrameState() {
79 Node* state_values = graph()->NewNode(common()->StateValues(0));
80 return graph()->NewNode(
81 common()->FrameState(BailoutId::None(), OutputFrameStateCombine::Ignore(),
82 nullptr),
83 state_values, state_values, state_values, NumberConstant(0),
84 UndefinedConstant(), graph()->start());
85 }
86
87
IsFalseConstant()88 Matcher<Node*> GraphTest::IsFalseConstant() {
89 return IsHeapConstant(factory()->false_value());
90 }
91
92
IsTrueConstant()93 Matcher<Node*> GraphTest::IsTrueConstant() {
94 return IsHeapConstant(factory()->true_value());
95 }
96
97
IsUndefinedConstant()98 Matcher<Node*> GraphTest::IsUndefinedConstant() {
99 return IsHeapConstant(factory()->undefined_value());
100 }
101
102
TypedGraphTest(int num_parameters)103 TypedGraphTest::TypedGraphTest(int num_parameters)
104 : GraphTest(num_parameters), typer_(isolate(), graph()) {}
105
106
~TypedGraphTest()107 TypedGraphTest::~TypedGraphTest() {}
108
109
Parameter(Type * type,int32_t index)110 Node* TypedGraphTest::Parameter(Type* type, int32_t index) {
111 Node* node = GraphTest::Parameter(index);
112 NodeProperties::SetType(node, type);
113 return node;
114 }
115
116
117 namespace {
118
119 const Operator kDummyOperator(0, Operator::kNoProperties, "Dummy", 0, 0, 0, 1,
120 0, 0);
121
122 } // namespace
123
124
TEST_F(GraphTest,NewNode)125 TEST_F(GraphTest, NewNode) {
126 Node* n0 = graph()->NewNode(&kDummyOperator);
127 Node* n1 = graph()->NewNode(&kDummyOperator);
128 EXPECT_NE(n0, n1);
129 EXPECT_LT(0u, n0->id());
130 EXPECT_LT(0u, n1->id());
131 EXPECT_NE(n0->id(), n1->id());
132 EXPECT_EQ(&kDummyOperator, n0->op());
133 EXPECT_EQ(&kDummyOperator, n1->op());
134 }
135
136 } // namespace compiler
137 } // namespace internal
138 } // namespace v8
139