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/cctest/compiler/simplified-graph-builder.h"
6
7 #include "src/compiler/operator-properties.h"
8 #include "src/compiler/operator-properties-inl.h"
9
10 namespace v8 {
11 namespace internal {
12 namespace compiler {
13
SimplifiedGraphBuilder(Graph * graph,CommonOperatorBuilder * common,MachineOperatorBuilder * machine,SimplifiedOperatorBuilder * simplified)14 SimplifiedGraphBuilder::SimplifiedGraphBuilder(
15 Graph* graph, CommonOperatorBuilder* common,
16 MachineOperatorBuilder* machine, SimplifiedOperatorBuilder* simplified)
17 : GraphBuilder(graph),
18 effect_(NULL),
19 return_(NULL),
20 common_(common),
21 machine_(machine),
22 simplified_(simplified) {}
23
24
Begin(int num_parameters)25 void SimplifiedGraphBuilder::Begin(int num_parameters) {
26 DCHECK(graph()->start() == NULL);
27 Node* start = graph()->NewNode(common()->Start(num_parameters));
28 graph()->SetStart(start);
29 effect_ = start;
30 }
31
32
Return(Node * value)33 void SimplifiedGraphBuilder::Return(Node* value) {
34 return_ =
35 graph()->NewNode(common()->Return(), value, effect_, graph()->start());
36 effect_ = NULL;
37 }
38
39
End()40 void SimplifiedGraphBuilder::End() {
41 Node* end = graph()->NewNode(common()->End(), return_);
42 graph()->SetEnd(end);
43 }
44
45
MakeNode(const Operator * op,int value_input_count,Node ** value_inputs)46 Node* SimplifiedGraphBuilder::MakeNode(const Operator* op,
47 int value_input_count,
48 Node** value_inputs) {
49 DCHECK(op->InputCount() == value_input_count);
50
51 DCHECK(!OperatorProperties::HasContextInput(op));
52 DCHECK(!OperatorProperties::HasFrameStateInput(op));
53 bool has_control = OperatorProperties::GetControlInputCount(op) == 1;
54 bool has_effect = OperatorProperties::GetEffectInputCount(op) == 1;
55
56 DCHECK(OperatorProperties::GetControlInputCount(op) < 2);
57 DCHECK(OperatorProperties::GetEffectInputCount(op) < 2);
58
59 Node* result = NULL;
60 if (!has_control && !has_effect) {
61 result = graph()->NewNode(op, value_input_count, value_inputs);
62 } else {
63 int input_count_with_deps = value_input_count;
64 if (has_control) ++input_count_with_deps;
65 if (has_effect) ++input_count_with_deps;
66 Node** buffer = zone()->NewArray<Node*>(input_count_with_deps);
67 memcpy(buffer, value_inputs, kPointerSize * value_input_count);
68 Node** current_input = buffer + value_input_count;
69 if (has_effect) {
70 *current_input++ = effect_;
71 }
72 if (has_control) {
73 *current_input++ = graph()->start();
74 }
75 result = graph()->NewNode(op, input_count_with_deps, buffer);
76 if (has_effect) {
77 effect_ = result;
78 }
79 if (OperatorProperties::HasControlOutput(result->op())) {
80 // This graph builder does not support control flow.
81 UNREACHABLE();
82 }
83 }
84
85 return result;
86 }
87
88 } // namespace compiler
89 } // namespace internal
90 } // namespace v8
91