1 // Copyright 2013 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 #ifndef V8_COMPILER_GRAPH_BUILDER_H_
6 #define V8_COMPILER_GRAPH_BUILDER_H_
10 #include "src/allocation.h"
11 #include "src/compiler/common-operator.h"
12 #include "src/compiler/graph.h"
13 #include "src/unique.h"
21 // A common base class for anything that creates nodes in a graph.
24 explicit GraphBuilder(Graph* graph) : graph_(graph) {}
25 virtual ~GraphBuilder() {}
27 Node* NewNode(const Operator* op) {
28 return MakeNode(op, 0, static_cast<Node**>(NULL));
31 Node* NewNode(const Operator* op, Node* n1) { return MakeNode(op, 1, &n1); }
33 Node* NewNode(const Operator* op, Node* n1, Node* n2) {
34 Node* buffer[] = {n1, n2};
35 return MakeNode(op, arraysize(buffer), buffer);
38 Node* NewNode(const Operator* op, Node* n1, Node* n2, Node* n3) {
39 Node* buffer[] = {n1, n2, n3};
40 return MakeNode(op, arraysize(buffer), buffer);
43 Node* NewNode(const Operator* op, Node* n1, Node* n2, Node* n3, Node* n4) {
44 Node* buffer[] = {n1, n2, n3, n4};
45 return MakeNode(op, arraysize(buffer), buffer);
48 Node* NewNode(const Operator* op, Node* n1, Node* n2, Node* n3, Node* n4,
50 Node* buffer[] = {n1, n2, n3, n4, n5};
51 return MakeNode(op, arraysize(buffer), buffer);
54 Node* NewNode(const Operator* op, Node* n1, Node* n2, Node* n3, Node* n4,
56 Node* nodes[] = {n1, n2, n3, n4, n5, n6};
57 return MakeNode(op, arraysize(nodes), nodes);
60 Node* NewNode(const Operator* op, int value_input_count,
61 Node** value_inputs) {
62 return MakeNode(op, value_input_count, value_inputs);
65 Graph* graph() const { return graph_; }
68 // Base implementation used by all factory methods.
69 virtual Node* MakeNode(const Operator* op, int value_input_count,
70 Node** value_inputs) = 0;
77 // The StructuredGraphBuilder produces a high-level IR graph. It is used as the
78 // base class for concrete implementations (e.g the AstGraphBuilder or the
80 class StructuredGraphBuilder : public GraphBuilder {
82 StructuredGraphBuilder(Graph* graph, CommonOperatorBuilder* common);
83 virtual ~StructuredGraphBuilder() {}
85 // Creates a new Phi node having {count} input values.
86 Node* NewPhi(int count, Node* input, Node* control);
87 Node* NewEffectPhi(int count, Node* input, Node* control);
89 // Helpers for merging control, effect or value dependencies.
90 Node* MergeControl(Node* control, Node* other);
91 Node* MergeEffect(Node* value, Node* other, Node* control);
92 Node* MergeValue(Node* value, Node* other, Node* control);
94 // Helpers to create new control nodes.
95 Node* NewIfTrue() { return NewNode(common()->IfTrue()); }
96 Node* NewIfFalse() { return NewNode(common()->IfFalse()); }
97 Node* NewMerge() { return NewNode(common()->Merge(1)); }
98 Node* NewLoop() { return NewNode(common()->Loop(1)); }
99 Node* NewBranch(Node* condition) {
100 return NewNode(common()->Branch(), condition);
105 friend class Environment;
106 friend class ControlBuilder;
108 // The following method creates a new node having the specified operator and
109 // ensures effect and control dependencies are wired up. The dependencies
110 // tracked by the environment might be mutated.
111 virtual Node* MakeNode(const Operator* op, int value_input_count,
112 Node** value_inputs) FINAL;
114 Environment* environment() const { return environment_; }
115 void set_environment(Environment* env) { environment_ = env; }
117 Node* current_context() const { return current_context_; }
118 void set_current_context(Node* context) { current_context_ = context; }
120 Node* exit_control() const { return exit_control_; }
121 void set_exit_control(Node* node) { exit_control_ = node; }
123 Node* dead_control();
125 // TODO(mstarzinger): Use phase-local zone instead!
126 Zone* zone() const { return graph()->zone(); }
127 Isolate* isolate() const { return zone()->isolate(); }
128 CommonOperatorBuilder* common() const { return common_; }
130 // Helper to wrap a Handle<T> into a Unique<T>.
132 Unique<T> MakeUnique(Handle<T> object) {
133 return Unique<T>::CreateUninitialized(object);
136 // Support for control flow builders. The concrete type of the environment
137 // depends on the graph builder, but environments themselves are not virtual.
138 virtual Environment* CopyEnvironment(Environment* env);
140 // Helper to indicate a node exits the function body.
141 void UpdateControlDependencyToLeaveFunction(Node* exit);
144 CommonOperatorBuilder* common_;
145 Environment* environment_;
147 // Node representing the control dependency for dead code.
148 SetOncePointer<Node> dead_control_;
150 // Node representing the current context within the function body.
151 Node* current_context_;
153 // Merge of all control nodes that exit the function body.
156 DISALLOW_COPY_AND_ASSIGN(StructuredGraphBuilder);
160 // The abstract execution environment contains static knowledge about
161 // execution state at arbitrary control-flow points. It allows for
162 // simulation of the control-flow at compile time.
163 class StructuredGraphBuilder::Environment : public ZoneObject {
165 Environment(StructuredGraphBuilder* builder, Node* control_dependency);
166 Environment(const Environment& copy);
168 // Control dependency tracked by this environment.
169 Node* GetControlDependency() { return control_dependency_; }
170 void UpdateControlDependency(Node* dependency) {
171 control_dependency_ = dependency;
174 // Effect dependency tracked by this environment.
175 Node* GetEffectDependency() { return effect_dependency_; }
176 void UpdateEffectDependency(Node* dependency) {
177 effect_dependency_ = dependency;
180 // Mark this environment as being unreachable.
181 void MarkAsUnreachable() {
182 UpdateControlDependency(builder()->dead_control());
184 bool IsMarkedAsUnreachable() {
185 return GetControlDependency()->opcode() == IrOpcode::kDead;
188 // Merge another environment into this one.
189 void Merge(Environment* other);
191 // Copies this environment at a control-flow split point.
192 Environment* CopyForConditional() { return builder()->CopyEnvironment(this); }
194 // Copies this environment to a potentially unreachable control-flow point.
195 Environment* CopyAsUnreachable() {
196 Environment* env = builder()->CopyEnvironment(this);
197 env->MarkAsUnreachable();
201 // Copies this environment at a loop header control-flow point.
202 Environment* CopyForLoop() {
204 return builder()->CopyEnvironment(this);
207 Node* GetContext() { return builder_->current_context(); }
210 // TODO(mstarzinger): Use phase-local zone instead!
211 Zone* zone() const { return graph()->zone(); }
212 Graph* graph() const { return builder_->graph(); }
213 StructuredGraphBuilder* builder() const { return builder_; }
214 CommonOperatorBuilder* common() { return builder_->common(); }
215 NodeVector* values() { return &values_; }
217 // Prepare environment to be used as loop header.
218 void PrepareForLoop();
221 StructuredGraphBuilder* builder_;
222 Node* control_dependency_;
223 Node* effect_dependency_;
228 } // namespace v8::internal::compiler
230 #endif // V8_COMPILER_GRAPH_BUILDER_H__