Chromium Code Reviews| 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 21 matching lines...) Expand all Loading... | |
| 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 StrictMode strict_mode = info->function()->strict_mode(); | 58 StrictMode strict_mode = info->function()->strict_mode(); |
| 58 info->SetStrictMode(strict_mode); | 59 info->SetStrictMode(strict_mode); |
| 59 info->SetOptimizing(BailoutId::None(), Handle<Code>(function->code())); | |
| 60 CHECK(Rewriter::Rewrite(info)); | 60 CHECK(Rewriter::Rewrite(info)); |
| 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 |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 85 // 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. |
| 86 Node* value_output() { | 86 Node* value_output() { |
| 87 return NodeProperties::GetValueInput(unique_return(), 0); | 87 return NodeProperties::GetValueInput(unique_return(), 0); |
| 88 } | 88 } |
| 89 // Return the unique return statement of the graph. | 89 // Return the unique return statement of the graph. |
| 90 Node* unique_return() { | 90 Node* unique_return() { |
| 91 Node* unique_return = NodeProperties::GetControlInput(end_); | 91 Node* unique_return = NodeProperties::GetControlInput(end_); |
| 92 DCHECK_EQ(IrOpcode::kReturn, unique_return->opcode()); | 92 DCHECK_EQ(IrOpcode::kReturn, unique_return->opcode()); |
| 93 return unique_return; | 93 return unique_return; |
| 94 } | 94 } |
| 95 | |
| 96 // Counts JSFunction, Receiver, arguments, context but not effect, control. | |
| 97 size_t total_parameters() { return start_->op()->OutputCount(); } | |
| 98 | |
| 99 // Counts only formal parameters. | |
| 100 size_t formal_parameters() { | |
| 101 DCHECK_GE(total_parameters(), 3); | |
| 102 return total_parameters() - 3; | |
| 103 } | |
| 104 | |
| 95 // Inline this graph at {call}, use {jsgraph} and its zone to create | 105 // Inline this graph at {call}, use {jsgraph} and its zone to create |
| 96 // any new nodes. | 106 // any new nodes. |
| 97 void InlineAtCall(JSGraph* jsgraph, Node* call); | 107 void InlineAtCall(JSGraph* jsgraph, Node* call); |
| 98 | 108 |
| 99 // Ensure that only a single return reaches the end node. | 109 // Ensure that only a single return reaches the end node. |
| 100 static void UnifyReturn(JSGraph* jsgraph); | 110 static void UnifyReturn(JSGraph* jsgraph); |
| 101 | 111 |
| 102 private: | 112 private: |
| 103 Node* start_; | 113 Node* start_; |
| 104 Node* end_; | 114 Node* end_; |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 191 | 201 |
| 192 void CopyGraph() { | 202 void CopyGraph() { |
| 193 source_graph_->VisitNodeInputsFromEnd(this); | 203 source_graph_->VisitNodeInputsFromEnd(this); |
| 194 ReplaceSentinels(); | 204 ReplaceSentinels(); |
| 195 } | 205 } |
| 196 | 206 |
| 197 const NodeVector& copies() { return copies_; } | 207 const NodeVector& copies() { return copies_; } |
| 198 | 208 |
| 199 private: | 209 private: |
| 200 void ReplaceSentinels() { | 210 void ReplaceSentinels() { |
| 201 for (int id = 0; id < source_graph_->NodeCount(); ++id) { | 211 for (NodeId id = 0; id < source_graph_->NodeCount(); ++id) { |
| 202 Node* sentinel = sentinels_[id]; | 212 Node* sentinel = sentinels_[id]; |
| 203 if (sentinel == NULL) continue; | 213 if (sentinel == NULL) continue; |
| 204 Node* copy = copies_[id]; | 214 Node* copy = copies_[id]; |
| 205 DCHECK_NE(NULL, copy); | 215 DCHECK_NE(NULL, copy); |
| 206 sentinel->ReplaceUses(copy); | 216 sentinel->ReplaceUses(copy); |
| 207 } | 217 } |
| 208 } | 218 } |
| 209 | 219 |
| 210 Node* GetSentinel(Node* original) { | 220 Node* GetSentinel(Node* original) { |
| 211 Node* sentinel = sentinels_[original->id()]; | 221 Node* sentinel = sentinels_[original->id()]; |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 230 Node* control = NodeProperties::GetControlInput(call); | 240 Node* control = NodeProperties::GetControlInput(call); |
| 231 | 241 |
| 232 // The inlinee uses the context from the JSFunction object. This will | 242 // The inlinee uses the context from the JSFunction object. This will |
| 233 // also be the effect dependency for the inlinee as it produces an effect. | 243 // also be the effect dependency for the inlinee as it produces an effect. |
| 234 SimplifiedOperatorBuilder simplified(jsgraph->zone()); | 244 SimplifiedOperatorBuilder simplified(jsgraph->zone()); |
| 235 Node* context = jsgraph->graph()->NewNode( | 245 Node* context = jsgraph->graph()->NewNode( |
| 236 simplified.LoadField(AccessBuilder::ForJSFunctionContext()), | 246 simplified.LoadField(AccessBuilder::ForJSFunctionContext()), |
| 237 NodeProperties::GetValueInput(call, 0), | 247 NodeProperties::GetValueInput(call, 0), |
| 238 NodeProperties::GetEffectInput(call)); | 248 NodeProperties::GetEffectInput(call)); |
| 239 | 249 |
| 240 // {inlinee_inputs} counts JSFunction, Receiver, arguments, context, | |
| 241 // but not effect, control. | |
| 242 int inlinee_inputs = start_->op()->OutputCount(); | |
| 243 // Context is last argument. | 250 // Context is last argument. |
| 244 int inlinee_context_index = inlinee_inputs - 1; | 251 int inlinee_context_index = total_parameters() - 1; |
| 245 // {inliner_inputs} counts JSFunction, Receiver, arguments, but not | 252 // {inliner_inputs} counts JSFunction, Receiver, arguments, but not |
| 246 // context, effect, control. | 253 // context, effect, control. |
| 247 int inliner_inputs = OperatorProperties::GetValueInputCount(call->op()); | 254 int inliner_inputs = OperatorProperties::GetValueInputCount(call->op()); |
| 248 // Iterate over all uses of the start node. | 255 // Iterate over all uses of the start node. |
| 249 UseIter iter = start_->uses().begin(); | 256 UseIter iter = start_->uses().begin(); |
| 250 while (iter != start_->uses().end()) { | 257 while (iter != start_->uses().end()) { |
| 251 Node* use = *iter; | 258 Node* use = *iter; |
| 252 switch (use->opcode()) { | 259 switch (use->opcode()) { |
| 253 case IrOpcode::kParameter: { | 260 case IrOpcode::kParameter: { |
| 254 int index = 1 + OpParameter<int>(use->op()); | 261 int index = 1 + OpParameter<int>(use->op()); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 294 iter.UpdateToAndIncrement(value_output()); | 301 iter.UpdateToAndIncrement(value_output()); |
| 295 } | 302 } |
| 296 } | 303 } |
| 297 call->RemoveAllInputs(); | 304 call->RemoveAllInputs(); |
| 298 DCHECK_EQ(0, call->UseCount()); | 305 DCHECK_EQ(0, call->UseCount()); |
| 299 // TODO(sigurds) Remove this once we copy. | 306 // TODO(sigurds) Remove this once we copy. |
| 300 unique_return()->RemoveAllInputs(); | 307 unique_return()->RemoveAllInputs(); |
| 301 } | 308 } |
| 302 | 309 |
| 303 | 310 |
| 304 void JSInliner::TryInlineCall(Node* call) { | 311 class JSCallFunctionAccessor { |
| 305 DCHECK_EQ(IrOpcode::kJSCallFunction, call->opcode()); | 312 public: |
| 313 explicit JSCallFunctionAccessor(Node* call) : call_(call) { | |
| 314 DCHECK_EQ(IrOpcode::kJSCallFunction, call->opcode()); | |
| 315 } | |
| 306 | 316 |
| 307 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(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, | |
| 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(), | |
| 346 Unique<JSFunction>::CreateImmovable(jsfunction)); | |
| 347 frame_state->set_op(op); | |
| 348 } | |
| 349 | |
| 350 | |
| 351 static Node* CreateArgumentsAdaptorFrameState(JSGraph* jsgraph, | |
| 352 JSCallFunctionAccessor* call, | |
| 353 Handle<JSFunction> jsfunction, | |
| 354 Zone* temp_zone) { | |
| 355 const Operator* op = jsgraph->common()->FrameState( | |
| 356 FrameStateType::ARGUMENTS_ADAPTOR, BailoutId(-1), kIgnoreOutput, | |
| 357 Unique<JSFunction>::CreateImmovable(jsfunction)); | |
| 358 const Operator* op0 = jsgraph->common()->StateValues(0); | |
| 359 Node* node0 = jsgraph->graph()->NewNode(op0); | |
| 360 NodeVector params(temp_zone); | |
| 361 params.push_back(call->receiver()); | |
| 362 for (size_t argument = 0; argument != call->formal_arguments(); ++argument) { | |
| 363 params.push_back(call->formal_argument(argument)); | |
| 364 } | |
| 365 const Operator* op_param = jsgraph->common()->StateValues(params.size()); | |
| 366 Node* params_node = | |
| 367 jsgraph->graph()->NewNode(op_param, 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()); | |
| 308 if (!match.HasValue()) { | 378 if (!match.HasValue()) { |
| 309 return; | 379 return; |
| 310 } | 380 } |
| 311 | 381 |
| 312 Handle<JSFunction> function = match.Value().handle(); | 382 Handle<JSFunction> function = match.Value().handle(); |
| 313 | 383 |
| 314 if (function->shared()->native()) { | 384 if (function->shared()->native()) { |
| 315 if (FLAG_trace_turbo_inlining) { | 385 if (FLAG_trace_turbo_inlining) { |
| 316 SmartArrayPointer<char> name = | 386 SmartArrayPointer<char> name = |
| 317 function->shared()->DebugName()->ToCString(); | 387 function->shared()->DebugName()->ToCString(); |
| 318 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(), |
| 319 info_->shared_info()->DebugName()->ToCString().get()); | 389 info_->shared_info()->DebugName()->ToCString().get()); |
| 320 } | 390 } |
| 321 return; | 391 return; |
| 322 } | 392 } |
| 323 | 393 |
| 324 CompilationInfoWithZone info(function); | 394 CompilationInfoWithZone info(function); |
| 325 Parse(function, &info); | 395 Parse(function, &info); |
| 326 | 396 |
| 397 if (!function->shared()->has_deoptimization_support()) { | |
| 398 info.EnableDeoptimizationSupport(); | |
| 399 if (!FullCodeGenerator::MakeCode(&info)) { | |
| 400 DCHECK(false); | |
| 401 return; | |
| 402 } | |
| 403 function->shared()->EnableDeoptimizationSupport(*info.code()); | |
| 404 function->shared()->set_feedback_vector(*info.feedback_vector()); | |
| 405 } | |
| 406 | |
| 327 if (info.scope()->arguments() != NULL) { | 407 if (info.scope()->arguments() != NULL) { |
| 328 // For now do not inline functions that use their arguments array. | 408 // For now do not inline functions that use their arguments array. |
| 329 SmartArrayPointer<char> name = function->shared()->DebugName()->ToCString(); | 409 SmartArrayPointer<char> name = function->shared()->DebugName()->ToCString(); |
| 330 if (FLAG_trace_turbo_inlining) { | 410 if (FLAG_trace_turbo_inlining) { |
| 331 PrintF( | 411 PrintF( |
| 332 "Not Inlining %s into %s because inlinee uses arguments " | 412 "Not Inlining %s into %s because inlinee uses arguments " |
| 333 "array\n", | 413 "array\n", |
| 334 name.get(), info_->shared_info()->DebugName()->ToCString().get()); | 414 name.get(), info_->shared_info()->DebugName()->ToCString().get()); |
| 335 } | 415 } |
| 336 return; | 416 return; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 348 jsgraph_->machine()); | 428 jsgraph_->machine()); |
| 349 | 429 |
| 350 AstGraphBuilder graph_builder(&info, &jsgraph); | 430 AstGraphBuilder graph_builder(&info, &jsgraph); |
| 351 graph_builder.CreateGraph(); | 431 graph_builder.CreateGraph(); |
| 352 Inlinee::UnifyReturn(&jsgraph); | 432 Inlinee::UnifyReturn(&jsgraph); |
| 353 | 433 |
| 354 CopyVisitor visitor(&graph, jsgraph_->graph(), info.zone()); | 434 CopyVisitor visitor(&graph, jsgraph_->graph(), info.zone()); |
| 355 visitor.CopyGraph(); | 435 visitor.CopyGraph(); |
| 356 | 436 |
| 357 Inlinee inlinee(visitor.GetCopy(graph.start()), visitor.GetCopy(graph.end())); | 437 Inlinee inlinee(visitor.GetCopy(graph.start()), visitor.GetCopy(graph.end())); |
| 358 inlinee.InlineAtCall(jsgraph_, call); | 438 |
| 439 for (Node* node : visitor.copies()) { | |
|
Jarin
2014/09/15 13:37:51
Are we allowed to use C++11? I think not yet.
| |
| 440 if (node != NULL && node->opcode() == IrOpcode::kFrameState) { | |
| 441 AddClosureToFrameState(jsgraph_, node, function); | |
| 442 Node* outer_frame_state = call.frame_state(); | |
| 443 // Insert argument adaptor frame if required. | |
| 444 if (call.formal_arguments() != inlinee.formal_parameters()) { | |
| 445 outer_frame_state = CreateArgumentsAdaptorFrameState( | |
| 446 jsgraph_, &call, function, info.zone()); | |
| 447 } | |
| 448 NodeProperties::ReplaceFrameStateInput(node, outer_frame_state); | |
| 449 } | |
| 450 } | |
| 451 | |
| 452 | |
| 453 inlinee.InlineAtCall(jsgraph_, call_node); | |
| 359 } | 454 } |
| 360 } | 455 } |
| 361 } | 456 } |
| 362 } // namespace v8::internal::compiler | 457 } // namespace v8::internal::compiler |
| OLD | NEW |