OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/compiler/access-builder.h" | 5 #include "src/compiler/access-builder.h" |
6 #include "src/compiler/ast-graph-builder.h" | 6 #include "src/compiler/ast-graph-builder.h" |
7 #include "src/compiler/common-operator.h" | 7 #include "src/compiler/common-operator.h" |
8 #include "src/compiler/generic-node-inl.h" | 8 #include "src/compiler/generic-node-inl.h" |
9 #include "src/compiler/graph-inl.h" | 9 #include "src/compiler/graph-inl.h" |
10 #include "src/compiler/graph-visualizer.h" | 10 #include "src/compiler/graph-visualizer.h" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 CHECK(Scope::Analyze(info)); | 61 CHECK(Scope::Analyze(info)); |
62 CHECK_NE(NULL, info->scope()); | 62 CHECK_NE(NULL, info->scope()); |
63 Handle<ScopeInfo> scope_info = ScopeInfo::Create(info->scope(), info->zone()); | 63 Handle<ScopeInfo> scope_info = ScopeInfo::Create(info->scope(), info->zone()); |
64 info->shared_info()->set_scope_info(*scope_info); | 64 info->shared_info()->set_scope_info(*scope_info); |
65 } | 65 } |
66 | 66 |
67 | 67 |
68 // A facade on a JSFunction's graph to facilitate inlining. It assumes the | 68 // A facade on a JSFunction's graph to facilitate inlining. It assumes the |
69 // that the function graph has only one return statement, and provides | 69 // that the function graph has only one return statement, and provides |
70 // {UnifyReturn} to convert a function graph to that end. | 70 // {UnifyReturn} to convert a function graph to that end. |
71 // InlineAtCall will create some new nodes using {graph}'s builders (and hence | |
72 // those nodes will live in {graph}'s zone. | |
73 class Inlinee { | 71 class Inlinee { |
74 public: | 72 public: |
75 explicit Inlinee(JSGraph* graph) : jsgraph_(graph) {} | 73 Inlinee(Node* start, Node* end) : start_(start), end_(end) {} |
76 | |
77 Graph* graph() { return jsgraph_->graph(); } | |
78 JSGraph* jsgraph() { return jsgraph_; } | |
79 | 74 |
80 // Returns the last regular control node, that is | 75 // Returns the last regular control node, that is |
81 // the last control node before the end node. | 76 // the last control node before the end node. |
82 Node* end_block() { return NodeProperties::GetControlInput(unique_return()); } | 77 Node* end_block() { return NodeProperties::GetControlInput(unique_return()); } |
83 | 78 |
84 // Return the effect output of the graph, | 79 // Return the effect output of the graph, |
85 // that is the effect input of the return statement of the inlinee. | 80 // that is the effect input of the return statement of the inlinee. |
86 Node* effect_output() { | 81 Node* effect_output() { |
87 return NodeProperties::GetEffectInput(unique_return()); | 82 return NodeProperties::GetEffectInput(unique_return()); |
88 } | 83 } |
89 // Return the value output of the graph, | 84 // Return the value output of the graph, |
90 // that is the value input of the return statement of the inlinee. | 85 // that is the value input of the return statement of the inlinee. |
91 Node* value_output() { | 86 Node* value_output() { |
92 return NodeProperties::GetValueInput(unique_return(), 0); | 87 return NodeProperties::GetValueInput(unique_return(), 0); |
93 } | 88 } |
94 // Return the unique return statement of the graph. | 89 // Return the unique return statement of the graph. |
95 Node* unique_return() { | 90 Node* unique_return() { |
96 Node* unique_return = | 91 Node* unique_return = NodeProperties::GetControlInput(end_); |
97 NodeProperties::GetControlInput(jsgraph_->graph()->end()); | |
98 DCHECK_EQ(IrOpcode::kReturn, unique_return->opcode()); | 92 DCHECK_EQ(IrOpcode::kReturn, unique_return->opcode()); |
99 return unique_return; | 93 return unique_return; |
100 } | 94 } |
101 // Inline this graph at {call}, use {jsgraph} and its zone to create | 95 // Inline this graph at {call}, use {jsgraph} and its zone to create |
102 // any new nodes. | 96 // any new nodes. |
103 void InlineAtCall(JSGraph* jsgraph, Node* call); | 97 void InlineAtCall(JSGraph* jsgraph, Node* call); |
| 98 |
104 // Ensure that only a single return reaches the end node. | 99 // Ensure that only a single return reaches the end node. |
105 void UnifyReturn(); | 100 static void UnifyReturn(JSGraph* jsgraph); |
106 | 101 |
107 private: | 102 private: |
108 JSGraph* jsgraph_; | 103 Node* start_; |
| 104 Node* end_; |
109 }; | 105 }; |
110 | 106 |
111 | 107 |
112 void Inlinee::UnifyReturn() { | 108 void Inlinee::UnifyReturn(JSGraph* jsgraph) { |
113 Graph* graph = jsgraph_->graph(); | 109 Graph* graph = jsgraph->graph(); |
114 | 110 |
115 Node* final_merge = NodeProperties::GetControlInput(graph->end(), 0); | 111 Node* final_merge = NodeProperties::GetControlInput(graph->end(), 0); |
116 if (final_merge->opcode() == IrOpcode::kReturn) { | 112 if (final_merge->opcode() == IrOpcode::kReturn) { |
117 // nothing to do | 113 // nothing to do |
118 return; | 114 return; |
119 } | 115 } |
120 DCHECK_EQ(IrOpcode::kMerge, final_merge->opcode()); | 116 DCHECK_EQ(IrOpcode::kMerge, final_merge->opcode()); |
121 | 117 |
122 int predecessors = | 118 int predecessors = |
123 OperatorProperties::GetControlInputCount(final_merge->op()); | 119 OperatorProperties::GetControlInputCount(final_merge->op()); |
124 const Operator* op_phi = | |
125 jsgraph_->common()->Phi(kMachAnyTagged, predecessors); | |
126 const Operator* op_ephi = jsgraph_->common()->EffectPhi(predecessors); | |
127 | 120 |
128 NodeVector values(jsgraph_->zone()); | 121 const Operator* op_phi = jsgraph->common()->Phi(kMachAnyTagged, predecessors); |
129 NodeVector effects(jsgraph_->zone()); | 122 const Operator* op_ephi = jsgraph->common()->EffectPhi(predecessors); |
| 123 |
| 124 NodeVector values(jsgraph->zone()); |
| 125 NodeVector effects(jsgraph->zone()); |
130 // Iterate over all control flow predecessors, | 126 // Iterate over all control flow predecessors, |
131 // which must be return statements. | 127 // which must be return statements. |
132 InputIter iter = final_merge->inputs().begin(); | 128 InputIter iter = final_merge->inputs().begin(); |
133 while (iter != final_merge->inputs().end()) { | 129 while (iter != final_merge->inputs().end()) { |
134 Node* input = *iter; | 130 Node* input = *iter; |
135 switch (input->opcode()) { | 131 switch (input->opcode()) { |
136 case IrOpcode::kReturn: | 132 case IrOpcode::kReturn: |
137 values.push_back(NodeProperties::GetValueInput(input, 0)); | 133 values.push_back(NodeProperties::GetValueInput(input, 0)); |
138 effects.push_back(NodeProperties::GetEffectInput(input)); | 134 effects.push_back(NodeProperties::GetEffectInput(input)); |
139 iter.UpdateToAndIncrement(NodeProperties::GetControlInput(input)); | 135 iter.UpdateToAndIncrement(NodeProperties::GetControlInput(input)); |
140 input->RemoveAllInputs(); | 136 input->RemoveAllInputs(); |
141 break; | 137 break; |
142 default: | 138 default: |
143 UNREACHABLE(); | 139 UNREACHABLE(); |
144 ++iter; | 140 ++iter; |
145 break; | 141 break; |
146 } | 142 } |
147 } | 143 } |
148 values.push_back(final_merge); | 144 values.push_back(final_merge); |
149 effects.push_back(final_merge); | 145 effects.push_back(final_merge); |
150 Node* phi = | 146 Node* phi = |
151 graph->NewNode(op_phi, static_cast<int>(values.size()), &values.front()); | 147 graph->NewNode(op_phi, static_cast<int>(values.size()), &values.front()); |
152 Node* ephi = graph->NewNode(op_ephi, static_cast<int>(effects.size()), | 148 Node* ephi = graph->NewNode(op_ephi, static_cast<int>(effects.size()), |
153 &effects.front()); | 149 &effects.front()); |
154 Node* new_return = | 150 Node* new_return = |
155 graph->NewNode(jsgraph_->common()->Return(), phi, ephi, final_merge); | 151 graph->NewNode(jsgraph->common()->Return(), phi, ephi, final_merge); |
156 graph->end()->ReplaceInput(0, new_return); | 152 graph->end()->ReplaceInput(0, new_return); |
157 } | 153 } |
158 | 154 |
159 | 155 |
| 156 class CopyVisitor : public NullNodeVisitor { |
| 157 public: |
| 158 CopyVisitor(Graph* source_graph, Graph* target_graph, Zone* temp_zone) |
| 159 : copies_(source_graph->NodeCount(), NULL, temp_zone), |
| 160 sentinels_(source_graph->NodeCount(), NULL, temp_zone), |
| 161 source_graph_(source_graph), |
| 162 target_graph_(target_graph), |
| 163 temp_zone_(temp_zone), |
| 164 sentinel_op_(IrOpcode::kDead, Operator::kNoProperties, 0, 0, |
| 165 "sentinel") {} |
| 166 |
| 167 GenericGraphVisit::Control Post(Node* original) { |
| 168 NodeVector inputs(temp_zone_); |
| 169 for (InputIter it = original->inputs().begin(); |
| 170 it != original->inputs().end(); ++it) { |
| 171 inputs.push_back(GetCopy(*it)); |
| 172 } |
| 173 |
| 174 // Reuse the operator in the copy. This assumes that op lives in a zone |
| 175 // that lives longer than graph()'s zone. |
| 176 Node* copy = |
| 177 target_graph_->NewNode(original->op(), static_cast<int>(inputs.size()), |
| 178 (inputs.empty() ? NULL : &inputs.front())); |
| 179 copies_[original->id()] = copy; |
| 180 return GenericGraphVisit::CONTINUE; |
| 181 } |
| 182 |
| 183 Node* GetCopy(Node* original) { |
| 184 Node* copy = copies_[original->id()]; |
| 185 if (copy == NULL) { |
| 186 copy = GetSentinel(original); |
| 187 } |
| 188 DCHECK_NE(NULL, copy); |
| 189 return copy; |
| 190 } |
| 191 |
| 192 void CopyGraph() { |
| 193 source_graph_->VisitNodeInputsFromEnd(this); |
| 194 ReplaceSentinels(); |
| 195 } |
| 196 |
| 197 const NodeVector& copies() { return copies_; } |
| 198 |
| 199 private: |
| 200 void ReplaceSentinels() { |
| 201 for (int id = 0; id < source_graph_->NodeCount(); ++id) { |
| 202 Node* sentinel = sentinels_[id]; |
| 203 if (sentinel == NULL) continue; |
| 204 Node* copy = copies_[id]; |
| 205 DCHECK_NE(NULL, copy); |
| 206 sentinel->ReplaceUses(copy); |
| 207 } |
| 208 } |
| 209 |
| 210 Node* GetSentinel(Node* original) { |
| 211 Node* sentinel = sentinels_[original->id()]; |
| 212 if (sentinel == NULL) { |
| 213 sentinel = target_graph_->NewNode(&sentinel_op_); |
| 214 } |
| 215 return sentinel; |
| 216 } |
| 217 |
| 218 NodeVector copies_; |
| 219 NodeVector sentinels_; |
| 220 Graph* source_graph_; |
| 221 Graph* target_graph_; |
| 222 Zone* temp_zone_; |
| 223 SimpleOperator sentinel_op_; |
| 224 }; |
| 225 |
| 226 |
160 void Inlinee::InlineAtCall(JSGraph* jsgraph, Node* call) { | 227 void Inlinee::InlineAtCall(JSGraph* jsgraph, Node* call) { |
161 // The scheduler is smart enough to place our code; we just ensure {control} | 228 // The scheduler is smart enough to place our code; we just ensure {control} |
162 // becomes the control input of the start of the inlinee. | 229 // becomes the control input of the start of the inlinee. |
163 Node* control = NodeProperties::GetControlInput(call); | 230 Node* control = NodeProperties::GetControlInput(call); |
164 | 231 |
165 // The inlinee uses the context from the JSFunction object. This will | 232 // The inlinee uses the context from the JSFunction object. This will |
166 // also be the effect dependency for the inlinee as it produces an effect. | 233 // also be the effect dependency for the inlinee as it produces an effect. |
167 SimplifiedOperatorBuilder simplified(jsgraph->zone()); | 234 SimplifiedOperatorBuilder simplified(jsgraph->zone()); |
168 Node* context = jsgraph->graph()->NewNode( | 235 Node* context = jsgraph->graph()->NewNode( |
169 simplified.LoadField(AccessBuilder::ForJSFunctionContext()), | 236 simplified.LoadField(AccessBuilder::ForJSFunctionContext()), |
170 NodeProperties::GetValueInput(call, 0), | 237 NodeProperties::GetValueInput(call, 0), |
171 NodeProperties::GetEffectInput(call)); | 238 NodeProperties::GetEffectInput(call)); |
172 | 239 |
173 // {inlinee_inputs} counts JSFunction, Receiver, arguments, context, | 240 // {inlinee_inputs} counts JSFunction, Receiver, arguments, context, |
174 // but not effect, control. | 241 // but not effect, control. |
175 int inlinee_inputs = graph()->start()->op()->OutputCount(); | 242 int inlinee_inputs = start_->op()->OutputCount(); |
176 // Context is last argument. | 243 // Context is last argument. |
177 int inlinee_context_index = inlinee_inputs - 1; | 244 int inlinee_context_index = inlinee_inputs - 1; |
178 // {inliner_inputs} counts JSFunction, Receiver, arguments, but not | 245 // {inliner_inputs} counts JSFunction, Receiver, arguments, but not |
179 // context, effect, control. | 246 // context, effect, control. |
180 int inliner_inputs = OperatorProperties::GetValueInputCount(call->op()); | 247 int inliner_inputs = OperatorProperties::GetValueInputCount(call->op()); |
181 // Iterate over all uses of the start node. | 248 // Iterate over all uses of the start node. |
182 UseIter iter = graph()->start()->uses().begin(); | 249 UseIter iter = start_->uses().begin(); |
183 while (iter != graph()->start()->uses().end()) { | 250 while (iter != start_->uses().end()) { |
184 Node* use = *iter; | 251 Node* use = *iter; |
185 switch (use->opcode()) { | 252 switch (use->opcode()) { |
186 case IrOpcode::kParameter: { | 253 case IrOpcode::kParameter: { |
187 int index = 1 + OpParameter<int>(use->op()); | 254 int index = 1 + OpParameter<int>(use->op()); |
188 if (index < inliner_inputs && index < inlinee_context_index) { | 255 if (index < inliner_inputs && index < inlinee_context_index) { |
189 // There is an input from the call, and the index is a value | 256 // There is an input from the call, and the index is a value |
190 // projection but not the context, so rewire the input. | 257 // projection but not the context, so rewire the input. |
191 NodeProperties::ReplaceWithValue(*iter, call->InputAt(index)); | 258 NodeProperties::ReplaceWithValue(*iter, call->InputAt(index)); |
192 } else if (index == inlinee_context_index) { | 259 } else if (index == inlinee_context_index) { |
193 // This is the context projection, rewire it to the context from the | 260 // This is the context projection, rewire it to the context from the |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
227 iter.UpdateToAndIncrement(value_output()); | 294 iter.UpdateToAndIncrement(value_output()); |
228 } | 295 } |
229 } | 296 } |
230 call->RemoveAllInputs(); | 297 call->RemoveAllInputs(); |
231 DCHECK_EQ(0, call->UseCount()); | 298 DCHECK_EQ(0, call->UseCount()); |
232 // TODO(sigurds) Remove this once we copy. | 299 // TODO(sigurds) Remove this once we copy. |
233 unique_return()->RemoveAllInputs(); | 300 unique_return()->RemoveAllInputs(); |
234 } | 301 } |
235 | 302 |
236 | 303 |
237 void JSInliner::TryInlineCall(Node* node) { | 304 void JSInliner::TryInlineCall(Node* call) { |
238 DCHECK_EQ(IrOpcode::kJSCallFunction, node->opcode()); | 305 DCHECK_EQ(IrOpcode::kJSCallFunction, call->opcode()); |
239 | 306 |
240 HeapObjectMatcher<JSFunction> match(node->InputAt(0)); | 307 HeapObjectMatcher<JSFunction> match(call->InputAt(0)); |
241 if (!match.HasValue()) { | 308 if (!match.HasValue()) { |
242 return; | 309 return; |
243 } | 310 } |
244 | 311 |
245 Handle<JSFunction> function = match.Value().handle(); | 312 Handle<JSFunction> function = match.Value().handle(); |
246 | 313 |
247 if (function->shared()->native()) { | 314 if (function->shared()->native()) { |
248 if (FLAG_trace_turbo_inlining) { | 315 if (FLAG_trace_turbo_inlining) { |
249 SmartArrayPointer<char> name = | 316 SmartArrayPointer<char> name = |
250 function->shared()->DebugName()->ToCString(); | 317 function->shared()->DebugName()->ToCString(); |
(...skipping 17 matching lines...) Expand all Loading... |
268 } | 335 } |
269 return; | 336 return; |
270 } | 337 } |
271 | 338 |
272 if (FLAG_trace_turbo_inlining) { | 339 if (FLAG_trace_turbo_inlining) { |
273 SmartArrayPointer<char> name = function->shared()->DebugName()->ToCString(); | 340 SmartArrayPointer<char> name = function->shared()->DebugName()->ToCString(); |
274 PrintF("Inlining %s into %s\n", name.get(), | 341 PrintF("Inlining %s into %s\n", name.get(), |
275 info_->shared_info()->DebugName()->ToCString().get()); | 342 info_->shared_info()->DebugName()->ToCString().get()); |
276 } | 343 } |
277 | 344 |
278 Graph graph(info_->zone()); | 345 Graph graph(info.zone()); |
279 graph.SetNextNodeId(jsgraph_->graph()->NextNodeID()); | 346 Typer typer(info.zone()); |
280 | 347 JSGraph jsgraph(&graph, jsgraph_->common(), jsgraph_->javascript(), &typer, |
281 Typer typer(info_->zone()); | 348 jsgraph_->machine()); |
282 CommonOperatorBuilder common(info_->zone()); | |
283 JSGraph jsgraph(&graph, &common, &typer); | |
284 | 349 |
285 AstGraphBuilder graph_builder(&info, &jsgraph); | 350 AstGraphBuilder graph_builder(&info, &jsgraph); |
286 graph_builder.CreateGraph(); | 351 graph_builder.CreateGraph(); |
| 352 Inlinee::UnifyReturn(&jsgraph); |
287 | 353 |
288 Inlinee inlinee(&jsgraph); | 354 CopyVisitor visitor(&graph, jsgraph_->graph(), info.zone()); |
289 inlinee.UnifyReturn(); | 355 visitor.CopyGraph(); |
290 inlinee.InlineAtCall(jsgraph_, node); | |
291 | 356 |
292 jsgraph_->graph()->SetNextNodeId(inlinee.graph()->NextNodeID()); | 357 Inlinee inlinee(visitor.GetCopy(graph.start()), visitor.GetCopy(graph.end())); |
| 358 inlinee.InlineAtCall(jsgraph_, call); |
293 } | 359 } |
294 } | 360 } |
295 } | 361 } |
296 } // namespace v8::internal::compiler | 362 } // namespace v8::internal::compiler |
OLD | NEW |