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" |
11 #include "src/compiler/js-inlining.h" | 11 #include "src/compiler/js-inlining.h" |
12 #include "src/compiler/js-operator.h" | 12 #include "src/compiler/js-operator.h" |
13 #include "src/compiler/node-aux-data-inl.h" | 13 #include "src/compiler/node-aux-data-inl.h" |
14 #include "src/compiler/node-matchers.h" | 14 #include "src/compiler/node-matchers.h" |
15 #include "src/compiler/node-properties-inl.h" | 15 #include "src/compiler/node-properties-inl.h" |
16 #include "src/compiler/simplified-operator.h" | 16 #include "src/compiler/simplified-operator.h" |
17 #include "src/compiler/typer.h" | 17 #include "src/compiler/typer.h" |
18 #include "src/full-codegen.h" | |
18 #include "src/parser.h" | 19 #include "src/parser.h" |
19 #include "src/rewriter.h" | 20 #include "src/rewriter.h" |
20 #include "src/scopes.h" | 21 #include "src/scopes.h" |
21 | 22 |
22 | 23 |
23 namespace v8 { | 24 namespace v8 { |
24 namespace internal { | 25 namespace internal { |
25 namespace compiler { | 26 namespace compiler { |
26 | 27 |
27 class InlinerVisitor : public NullNodeVisitor { | 28 class InlinerVisitor : public NullNodeVisitor { |
(...skipping 19 matching lines...) Expand all Loading... | |
47 void JSInliner::Inline() { | 48 void JSInliner::Inline() { |
48 InlinerVisitor visitor(this); | 49 InlinerVisitor visitor(this); |
49 jsgraph_->graph()->VisitNodeInputsFromEnd(&visitor); | 50 jsgraph_->graph()->VisitNodeInputsFromEnd(&visitor); |
50 } | 51 } |
51 | 52 |
52 | 53 |
53 // TODO(sigurds) Find a home for this function and reuse it everywhere (esp. in | 54 // TODO(sigurds) Find a home for this function and reuse it everywhere (esp. in |
54 // test cases, where similar code is currently duplicated). | 55 // test cases, where similar code is currently duplicated). |
55 static void Parse(Handle<JSFunction> function, CompilationInfoWithZone* info) { | 56 static void Parse(Handle<JSFunction> function, CompilationInfoWithZone* info) { |
56 CHECK(Parser::Parse(info)); | 57 CHECK(Parser::Parse(info)); |
57 info->SetOptimizing(BailoutId::None(), Handle<Code>(function->code())); | |
58 CHECK(Rewriter::Rewrite(info)); | 58 CHECK(Rewriter::Rewrite(info)); |
59 CHECK(Scope::Analyze(info)); | 59 CHECK(Scope::Analyze(info)); |
60 CHECK_NE(NULL, info->scope()); | 60 CHECK_NE(NULL, info->scope()); |
61 Handle<ScopeInfo> scope_info = ScopeInfo::Create(info->scope(), info->zone()); | 61 Handle<ScopeInfo> scope_info = ScopeInfo::Create(info->scope(), info->zone()); |
62 info->shared_info()->set_scope_info(*scope_info); | 62 info->shared_info()->set_scope_info(*scope_info); |
63 } | 63 } |
64 | 64 |
65 | 65 |
66 // A facade on a JSFunction's graph to facilitate inlining. It assumes the | 66 // A facade on a JSFunction's graph to facilitate inlining. It assumes the |
67 // that the function graph has only one return statement, and provides | 67 // that the function graph has only one return statement, and provides |
(...skipping 15 matching lines...) Expand all Loading... | |
83 // that is the value input of the return statement of the inlinee. | 83 // that is the value input of the return statement of the inlinee. |
84 Node* value_output() { | 84 Node* value_output() { |
85 return NodeProperties::GetValueInput(unique_return(), 0); | 85 return NodeProperties::GetValueInput(unique_return(), 0); |
86 } | 86 } |
87 // Return the unique return statement of the graph. | 87 // Return the unique return statement of the graph. |
88 Node* unique_return() { | 88 Node* unique_return() { |
89 Node* unique_return = NodeProperties::GetControlInput(end_); | 89 Node* unique_return = NodeProperties::GetControlInput(end_); |
90 DCHECK_EQ(IrOpcode::kReturn, unique_return->opcode()); | 90 DCHECK_EQ(IrOpcode::kReturn, unique_return->opcode()); |
91 return unique_return; | 91 return unique_return; |
92 } | 92 } |
93 | |
94 // Counts JSFunction, Receiver, arguments, context but not effect, control. | |
95 size_t total_parameters() { return start_->op()->OutputCount(); } | |
96 | |
97 // Counts only formal parameters. | |
98 size_t formal_parameters() { | |
99 DCHECK_GE(total_parameters(), 3); | |
100 return total_parameters() - 3; | |
101 } | |
102 | |
93 // Inline this graph at {call}, use {jsgraph} and its zone to create | 103 // Inline this graph at {call}, use {jsgraph} and its zone to create |
94 // any new nodes. | 104 // any new nodes. |
95 void InlineAtCall(JSGraph* jsgraph, Node* call); | 105 void InlineAtCall(JSGraph* jsgraph, Node* call); |
96 | 106 |
97 // Ensure that only a single return reaches the end node. | 107 // Ensure that only a single return reaches the end node. |
98 static void UnifyReturn(JSGraph* jsgraph); | 108 static void UnifyReturn(JSGraph* jsgraph); |
99 | 109 |
100 private: | 110 private: |
101 Node* start_; | 111 Node* start_; |
102 Node* end_; | 112 Node* end_; |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
189 | 199 |
190 void CopyGraph() { | 200 void CopyGraph() { |
191 source_graph_->VisitNodeInputsFromEnd(this); | 201 source_graph_->VisitNodeInputsFromEnd(this); |
192 ReplaceSentinels(); | 202 ReplaceSentinels(); |
193 } | 203 } |
194 | 204 |
195 const NodeVector& copies() { return copies_; } | 205 const NodeVector& copies() { return copies_; } |
196 | 206 |
197 private: | 207 private: |
198 void ReplaceSentinels() { | 208 void ReplaceSentinels() { |
199 for (int id = 0; id < source_graph_->NodeCount(); ++id) { | 209 for (NodeId id = 0; id < source_graph_->NodeCount(); ++id) { |
200 Node* sentinel = sentinels_[id]; | 210 Node* sentinel = sentinels_[id]; |
201 if (sentinel == NULL) continue; | 211 if (sentinel == NULL) continue; |
202 Node* copy = copies_[id]; | 212 Node* copy = copies_[id]; |
203 DCHECK_NE(NULL, copy); | 213 DCHECK_NE(NULL, copy); |
204 sentinel->ReplaceUses(copy); | 214 sentinel->ReplaceUses(copy); |
205 } | 215 } |
206 } | 216 } |
207 | 217 |
208 Node* GetSentinel(Node* original) { | 218 Node* GetSentinel(Node* original) { |
209 Node* sentinel = sentinels_[original->id()]; | 219 Node* sentinel = sentinels_[original->id()]; |
(...skipping 18 matching lines...) Expand all Loading... | |
228 Node* control = NodeProperties::GetControlInput(call); | 238 Node* control = NodeProperties::GetControlInput(call); |
229 | 239 |
230 // The inlinee uses the context from the JSFunction object. This will | 240 // The inlinee uses the context from the JSFunction object. This will |
231 // also be the effect dependency for the inlinee as it produces an effect. | 241 // also be the effect dependency for the inlinee as it produces an effect. |
232 SimplifiedOperatorBuilder simplified(jsgraph->zone()); | 242 SimplifiedOperatorBuilder simplified(jsgraph->zone()); |
233 Node* context = jsgraph->graph()->NewNode( | 243 Node* context = jsgraph->graph()->NewNode( |
234 simplified.LoadField(AccessBuilder::ForJSFunctionContext()), | 244 simplified.LoadField(AccessBuilder::ForJSFunctionContext()), |
235 NodeProperties::GetValueInput(call, 0), | 245 NodeProperties::GetValueInput(call, 0), |
236 NodeProperties::GetEffectInput(call)); | 246 NodeProperties::GetEffectInput(call)); |
237 | 247 |
238 // {inlinee_inputs} counts JSFunction, Receiver, arguments, context, | |
239 // but not effect, control. | |
240 int inlinee_inputs = start_->op()->OutputCount(); | |
241 // Context is last argument. | 248 // Context is last argument. |
242 int inlinee_context_index = inlinee_inputs - 1; | 249 int inlinee_context_index = static_cast<int>(total_parameters()) - 1; |
243 // {inliner_inputs} counts JSFunction, Receiver, arguments, but not | 250 // {inliner_inputs} counts JSFunction, Receiver, arguments, but not |
244 // context, effect, control. | 251 // context, effect, control. |
245 int inliner_inputs = OperatorProperties::GetValueInputCount(call->op()); | 252 int inliner_inputs = OperatorProperties::GetValueInputCount(call->op()); |
246 // Iterate over all uses of the start node. | 253 // Iterate over all uses of the start node. |
247 UseIter iter = start_->uses().begin(); | 254 UseIter iter = start_->uses().begin(); |
248 while (iter != start_->uses().end()) { | 255 while (iter != start_->uses().end()) { |
249 Node* use = *iter; | 256 Node* use = *iter; |
250 switch (use->opcode()) { | 257 switch (use->opcode()) { |
251 case IrOpcode::kParameter: { | 258 case IrOpcode::kParameter: { |
252 int index = 1 + OpParameter<int>(use->op()); | 259 int index = 1 + OpParameter<int>(use->op()); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
292 iter.UpdateToAndIncrement(value_output()); | 299 iter.UpdateToAndIncrement(value_output()); |
293 } | 300 } |
294 } | 301 } |
295 call->RemoveAllInputs(); | 302 call->RemoveAllInputs(); |
296 DCHECK_EQ(0, call->UseCount()); | 303 DCHECK_EQ(0, call->UseCount()); |
297 // TODO(sigurds) Remove this once we copy. | 304 // TODO(sigurds) Remove this once we copy. |
298 unique_return()->RemoveAllInputs(); | 305 unique_return()->RemoveAllInputs(); |
299 } | 306 } |
300 | 307 |
301 | 308 |
302 void JSInliner::TryInlineCall(Node* call) { | 309 // TODO(turbofan) Provide such accessors for every node, possibly even |
303 DCHECK_EQ(IrOpcode::kJSCallFunction, call->opcode()); | 310 // generate them. |
311 class JSCallFunctionAccessor { | |
312 public: | |
313 explicit JSCallFunctionAccessor(Node* call) : call_(call) { | |
314 DCHECK_EQ(IrOpcode::kJSCallFunction, call->opcode()); | |
315 } | |
304 | 316 |
305 HeapObjectMatcher<JSFunction> match(call->InputAt(0)); | 317 Node* jsfunction() { return call_->InputAt(0); } |
318 | |
319 Node* receiver() { return call_->InputAt(1); } | |
320 | |
321 Node* formal_argument(size_t index) { | |
322 DCHECK(index < formal_arguments()); | |
323 return call_->InputAt(static_cast<int>(2 + index)); | |
324 } | |
325 | |
326 size_t formal_arguments() { | |
327 // {value_inputs} includes jsfunction and receiver. | |
328 size_t value_inputs = OperatorProperties::GetValueInputCount(call_->op()); | |
329 DCHECK_GE(call_->InputCount(), 2); | |
330 return value_inputs - 2; | |
331 } | |
332 | |
333 Node* frame_state() { return NodeProperties::GetFrameStateInput(call_); } | |
334 | |
335 private: | |
336 Node* call_; | |
337 }; | |
338 | |
339 | |
340 static void AddClosureToFrameState(JSGraph* jsgraph, Node* frame_state, | |
Michael Starzinger
2014/09/17 13:31:43
nit: This could be turned into a member method in
sigurds
2014/09/17 14:05:12
Done.
| |
341 Handle<JSFunction> jsfunction) { | |
342 FrameStateCallInfo call_info = OpParameter<FrameStateCallInfo>(frame_state); | |
343 const Operator* op = jsgraph->common()->FrameState( | |
344 FrameStateType::JS_FRAME, call_info.bailout_id(), | |
345 call_info.state_combine(), jsfunction); | |
346 frame_state->set_op(op); | |
347 } | |
348 | |
349 | |
350 static Node* CreateArgumentsAdaptorFrameState(JSGraph* jsgraph, | |
Michael Starzinger
2014/09/17 13:31:43
nit: This could be turned into a member method in
sigurds
2014/09/17 14:05:12
Done.
| |
351 JSCallFunctionAccessor* call, | |
352 Handle<JSFunction> jsfunction, | |
353 Zone* temp_zone) { | |
354 const Operator* op = | |
355 jsgraph->common()->FrameState(FrameStateType::ARGUMENTS_ADAPTOR, | |
356 BailoutId(-1), kIgnoreOutput, jsfunction); | |
357 const Operator* op0 = jsgraph->common()->StateValues(0); | |
358 Node* node0 = jsgraph->graph()->NewNode(op0); | |
359 NodeVector params(temp_zone); | |
360 params.push_back(call->receiver()); | |
361 for (size_t argument = 0; argument != call->formal_arguments(); ++argument) { | |
362 params.push_back(call->formal_argument(argument)); | |
363 } | |
364 const Operator* op_param = | |
365 jsgraph->common()->StateValues(static_cast<int>(params.size())); | |
366 Node* params_node = jsgraph->graph()->NewNode( | |
367 op_param, static_cast<int>(params.size()), ¶ms.front()); | |
368 return jsgraph->graph()->NewNode(op, params_node, node0, node0, | |
369 jsgraph->UndefinedConstant(), | |
370 call->frame_state()); | |
371 } | |
372 | |
373 | |
374 void JSInliner::TryInlineCall(Node* call_node) { | |
375 JSCallFunctionAccessor call(call_node); | |
376 | |
377 HeapObjectMatcher<JSFunction> match(call.jsfunction()); | |
306 if (!match.HasValue()) { | 378 if (!match.HasValue()) { |
307 return; | 379 return; |
308 } | 380 } |
309 | 381 |
310 Handle<JSFunction> function = match.Value().handle(); | 382 Handle<JSFunction> function = match.Value().handle(); |
311 | 383 |
312 if (function->shared()->native()) { | 384 if (function->shared()->native()) { |
313 if (FLAG_trace_turbo_inlining) { | 385 if (FLAG_trace_turbo_inlining) { |
314 SmartArrayPointer<char> name = | 386 SmartArrayPointer<char> name = |
315 function->shared()->DebugName()->ToCString(); | 387 function->shared()->DebugName()->ToCString(); |
316 PrintF("Not Inlining %s into %s because inlinee is native\n", name.get(), | 388 PrintF("Not Inlining %s into %s because inlinee is native\n", name.get(), |
317 info_->shared_info()->DebugName()->ToCString().get()); | 389 info_->shared_info()->DebugName()->ToCString().get()); |
318 } | 390 } |
319 return; | 391 return; |
320 } | 392 } |
321 | 393 |
322 CompilationInfoWithZone info(function); | 394 CompilationInfoWithZone info(function); |
323 Parse(function, &info); | 395 Parse(function, &info); |
324 | 396 |
397 if (!function->shared()->has_deoptimization_support()) { | |
398 // TODO(turbofan) In the future, unoptimized code with deopt support could | |
399 // be generated lazily once deopt is triggered. | |
400 info.EnableDeoptimizationSupport(); | |
401 if (!FullCodeGenerator::MakeCode(&info)) { | |
402 DCHECK(false); | |
403 return; | |
404 } | |
405 function->shared()->EnableDeoptimizationSupport(*info.code()); | |
406 function->shared()->set_feedback_vector(*info.feedback_vector()); | |
407 } | |
408 | |
325 if (info.scope()->arguments() != NULL) { | 409 if (info.scope()->arguments() != NULL) { |
326 // For now do not inline functions that use their arguments array. | 410 // For now do not inline functions that use their arguments array. |
327 SmartArrayPointer<char> name = function->shared()->DebugName()->ToCString(); | 411 SmartArrayPointer<char> name = function->shared()->DebugName()->ToCString(); |
328 if (FLAG_trace_turbo_inlining) { | 412 if (FLAG_trace_turbo_inlining) { |
329 PrintF( | 413 PrintF( |
330 "Not Inlining %s into %s because inlinee uses arguments " | 414 "Not Inlining %s into %s because inlinee uses arguments " |
331 "array\n", | 415 "array\n", |
332 name.get(), info_->shared_info()->DebugName()->ToCString().get()); | 416 name.get(), info_->shared_info()->DebugName()->ToCString().get()); |
333 } | 417 } |
334 return; | 418 return; |
(...skipping 11 matching lines...) Expand all Loading... | |
346 jsgraph_->machine()); | 430 jsgraph_->machine()); |
347 | 431 |
348 AstGraphBuilder graph_builder(&info, &jsgraph); | 432 AstGraphBuilder graph_builder(&info, &jsgraph); |
349 graph_builder.CreateGraph(); | 433 graph_builder.CreateGraph(); |
350 Inlinee::UnifyReturn(&jsgraph); | 434 Inlinee::UnifyReturn(&jsgraph); |
351 | 435 |
352 CopyVisitor visitor(&graph, jsgraph_->graph(), info.zone()); | 436 CopyVisitor visitor(&graph, jsgraph_->graph(), info.zone()); |
353 visitor.CopyGraph(); | 437 visitor.CopyGraph(); |
354 | 438 |
355 Inlinee inlinee(visitor.GetCopy(graph.start()), visitor.GetCopy(graph.end())); | 439 Inlinee inlinee(visitor.GetCopy(graph.start()), visitor.GetCopy(graph.end())); |
356 inlinee.InlineAtCall(jsgraph_, call); | 440 |
441 for (NodeVectorConstIter it = visitor.copies().begin(); | |
442 it != visitor.copies().end(); ++it) { | |
443 Node* node = *it; | |
444 if (node != NULL && node->opcode() == IrOpcode::kFrameState) { | |
445 AddClosureToFrameState(jsgraph_, node, function); | |
446 Node* outer_frame_state = call.frame_state(); | |
447 // Insert argument adaptor frame if required. | |
448 if (call.formal_arguments() != inlinee.formal_parameters()) { | |
449 outer_frame_state = CreateArgumentsAdaptorFrameState( | |
Michael Starzinger
2014/09/17 13:31:43
This create a new arguments-adaptor FrameState for
sigurds
2014/09/17 14:05:13
Good catch, thanks!
| |
450 jsgraph_, &call, function, info.zone()); | |
451 } | |
452 NodeProperties::ReplaceFrameStateInput(node, outer_frame_state); | |
453 } | |
454 } | |
455 | |
456 inlinee.InlineAtCall(jsgraph_, call_node); | |
357 } | 457 } |
358 } | 458 } |
359 } | 459 } |
360 } // namespace v8::internal::compiler | 460 } // namespace v8::internal::compiler |
OLD | NEW |