5 #include "src/compiler/js-inlining.h" 7 #include "src/ast/ast.h" 8 #include "src/compiler.h" 9 #include "src/compiler/all-nodes.h" 10 #include "src/compiler/bytecode-graph-builder.h" 11 #include "src/compiler/common-operator.h" 12 #include "src/compiler/compiler-source-position-table.h" 13 #include "src/compiler/graph-reducer.h" 14 #include "src/compiler/js-operator.h" 15 #include "src/compiler/node-matchers.h" 16 #include "src/compiler/node-properties.h" 17 #include "src/compiler/operator-properties.h" 18 #include "src/compiler/simplified-operator.h" 19 #include "src/isolate-inl.h" 20 #include "src/optimized-compilation-info.h" 21 #include "src/parsing/parse-info.h" 30 static const int kMaxDepthForInlining = 50;
35 if (FLAG_trace_turbo_inlining) PrintF(__VA_ARGS__); \ 44 DCHECK(call->opcode() == IrOpcode::kJSCall ||
45 call->opcode() == IrOpcode::kJSConstruct);
50 return call_->InputAt(0);
54 DCHECK_EQ(IrOpcode::kJSCall, call_->opcode());
55 return call_->InputAt(1);
59 DCHECK_EQ(IrOpcode::kJSConstruct, call_->opcode());
60 return call_->InputAt(formal_arguments() + 1);
65 return NodeProperties::GetFrameStateInput(call_);
68 int formal_arguments() {
72 return call_->op()->ValueInputCount() - 2;
76 return (call_->opcode() == IrOpcode::kJSCall)
77 ? CallParametersOf(call_->op()).frequency()
78 : ConstructParametersOf(call_->op()).frequency();
87 Node* exception_target,
92 Node* control = NodeProperties::GetControlInput(call);
93 Node* effect = NodeProperties::GetEffectInput(call);
95 int const inlinee_new_target_index =
96 static_cast<int>(start->op()->ValueOutputCount()) - 3;
97 int const inlinee_arity_index =
98 static_cast<int>(start->op()->ValueOutputCount()) - 2;
99 int const inlinee_context_index =
100 static_cast<int>(start->op()->ValueOutputCount()) - 1;
104 int inliner_inputs = call->op()->ValueInputCount();
106 for (
Edge edge : start->use_edges()) {
107 Node* use = edge.from();
108 switch (use->opcode()) {
109 case IrOpcode::kParameter: {
110 int index = 1 + ParameterIndexOf(use->op());
111 DCHECK_LE(index, inlinee_context_index);
112 if (index < inliner_inputs && index < inlinee_new_target_index) {
115 Replace(use, call->InputAt(index));
116 }
else if (index == inlinee_new_target_index) {
118 Replace(use, new_target);
119 }
else if (index == inlinee_arity_index) {
121 Replace(use, jsgraph()->Constant(inliner_inputs - 2));
122 }
else if (index == inlinee_context_index) {
124 Replace(use, context);
127 Replace(use, jsgraph()->UndefinedConstant());
132 if (NodeProperties::IsEffectEdge(edge)) {
133 edge.UpdateTo(effect);
134 }
else if (NodeProperties::IsControlEdge(edge)) {
135 edge.UpdateTo(control);
136 }
else if (NodeProperties::IsFrameStateEdge(edge)) {
137 edge.UpdateTo(frame_state);
145 if (exception_target !=
nullptr) {
147 int subcall_count =
static_cast<int>(uncaught_subcalls.size());
148 if (subcall_count > 0) {
150 "Inlinee contains %d calls without local exception handler; " 151 "linking to surrounding exception handler\n",
154 NodeVector on_exception_nodes(local_zone_);
155 for (Node* subcall : uncaught_subcalls) {
156 Node* on_success = graph()->NewNode(common()->IfSuccess(), subcall);
157 NodeProperties::ReplaceUses(subcall, subcall, subcall, on_success);
158 NodeProperties::ReplaceControlInput(on_success, subcall);
160 graph()->NewNode(common()->IfException(), subcall, subcall);
161 on_exception_nodes.push_back(on_exception);
164 DCHECK_EQ(subcall_count, static_cast<int>(on_exception_nodes.size()));
165 if (subcall_count > 0) {
166 Node* control_output =
167 graph()->NewNode(common()->Merge(subcall_count), subcall_count,
168 &on_exception_nodes.front());
169 NodeVector values_effects(local_zone_);
170 values_effects = on_exception_nodes;
171 values_effects.push_back(control_output);
172 Node* value_output = graph()->NewNode(
173 common()->Phi(MachineRepresentation::kTagged, subcall_count),
174 subcall_count + 1, &values_effects.front());
175 Node* effect_output =
176 graph()->NewNode(common()->EffectPhi(subcall_count),
177 subcall_count + 1, &values_effects.front());
178 ReplaceWithValue(exception_target, value_output, effect_output,
181 ReplaceWithValue(exception_target, exception_target, exception_target,
186 NodeVector values(local_zone_);
187 NodeVector effects(local_zone_);
188 NodeVector controls(local_zone_);
189 for (Node*
const input : end->inputs()) {
190 switch (input->opcode()) {
191 case IrOpcode::kReturn:
192 values.push_back(NodeProperties::GetValueInput(input, 1));
193 effects.push_back(NodeProperties::GetEffectInput(input));
194 controls.push_back(NodeProperties::GetControlInput(input));
196 case IrOpcode::kDeoptimize:
197 case IrOpcode::kTerminate:
198 case IrOpcode::kThrow:
199 NodeProperties::MergeControlToEnd(graph(), common(), input);
200 Revisit(graph()->end());
207 DCHECK_EQ(values.size(), effects.size());
208 DCHECK_EQ(values.size(), controls.size());
212 if (values.size() > 0) {
213 int const input_count =
static_cast<int>(controls.size());
214 Node* control_output = graph()->NewNode(common()->Merge(input_count),
215 input_count, &controls.front());
216 values.push_back(control_output);
217 effects.push_back(control_output);
218 Node* value_output = graph()->NewNode(
219 common()->Phi(MachineRepresentation::kTagged, input_count),
220 static_cast<int>(values.size()), &values.front());
221 Node* effect_output =
222 graph()->NewNode(common()->EffectPhi(input_count),
223 static_cast<int>(effects.size()), &effects.front());
224 ReplaceWithValue(call, value_output, effect_output, control_output);
225 return Changed(value_output);
227 ReplaceWithValue(call, jsgraph()->Dead(), jsgraph()->Dead(),
229 return Changed(call);
233 Node* JSInliner::CreateArtificialFrameState(Node* node, Node* outer_frame_state,
235 BailoutId bailout_id,
236 FrameStateType frame_state_type,
237 Handle<SharedFunctionInfo> shared,
239 const FrameStateFunctionInfo* state_info =
240 common()->CreateFrameStateFunctionInfo(frame_state_type,
241 parameter_count + 1, 0, shared);
243 const Operator* op = common()->FrameState(
244 bailout_id, OutputFrameStateCombine::Ignore(), state_info);
245 const Operator* op0 = common()->StateValues(0, SparseInputMask::Dense());
246 Node* node0 = graph()->NewNode(op0);
247 NodeVector params(local_zone_);
248 for (
int parameter = 0; parameter < parameter_count + 1; ++parameter) {
249 params.push_back(node->InputAt(1 + parameter));
251 const Operator* op_param = common()->StateValues(
252 static_cast<int>(params.size()), SparseInputMask::Dense());
253 Node* params_node = graph()->NewNode(
254 op_param, static_cast<int>(params.size()), ¶ms.front());
256 context = jsgraph()->UndefinedConstant();
258 return graph()->NewNode(op, params_node, node0, node0, context,
259 node->InputAt(0), outer_frame_state);
265 bool NeedsImplicitReceiver(Handle<SharedFunctionInfo> shared_info) {
266 DisallowHeapAllocation no_gc;
267 if (!shared_info->construct_as_builtin()) {
268 return !IsDerivedConstructor(shared_info->kind());
279 bool JSInliner::DetermineCallTarget(
280 Node* node, Handle<SharedFunctionInfo>& shared_info_out) {
281 DCHECK(IrOpcode::IsInlineeOpcode(node->opcode()));
282 HeapObjectMatcher match(node->InputAt(0));
288 if (match.HasValue() && match.Value()->IsJSFunction()) {
289 Handle<JSFunction>
function = Handle<JSFunction>::cast(match.Value());
299 if (function->native_context() != info_->native_context()) {
303 shared_info_out = handle(function->shared(), isolate());
311 if (match.IsJSCreateClosure()) {
312 CreateClosureParameters
const& p = CreateClosureParametersOf(match.op());
319 Handle<FeedbackCell> cell = p.feedback_cell();
320 if (!cell->value()->IsFeedbackVector())
return false;
322 shared_info_out = p.shared_info();
334 void JSInliner::DetermineCallContext(
335 Node* node, Node*& context_out,
336 Handle<FeedbackVector>& feedback_vector_out) {
337 DCHECK(IrOpcode::IsInlineeOpcode(node->opcode()));
338 HeapObjectMatcher match(node->InputAt(0));
340 if (match.HasValue() && match.Value()->IsJSFunction()) {
341 Handle<JSFunction>
function = Handle<JSFunction>::cast(match.Value());
345 JSFunction::EnsureFeedbackVector(
function);
348 context_out = jsgraph()->Constant(handle(function->context(), isolate()));
349 feedback_vector_out = handle(function->feedback_vector(), isolate());
353 if (match.IsJSCreateClosure()) {
354 CreateClosureParameters
const& p = CreateClosureParametersOf(match.op());
358 Handle<FeedbackCell> cell = p.feedback_cell();
359 DCHECK(cell->value()->IsFeedbackVector());
362 context_out = NodeProperties::GetContextInput(match.node());
363 feedback_vector_out =
364 handle(FeedbackVector::cast(cell->value()), isolate());
372 Reduction JSInliner::Reduce(Node* node) {
373 if (!IrOpcode::IsInlineeOpcode(node->opcode()))
return NoChange();
374 return ReduceJSCall(node);
377 Handle<Context> JSInliner::native_context()
const {
378 return handle(info_->native_context(), isolate());
381 Reduction JSInliner::ReduceJSCall(Node* node) {
382 DCHECK(IrOpcode::IsInlineeOpcode(node->opcode()));
383 Handle<SharedFunctionInfo> shared_info;
384 JSCallAccessor call(node);
387 if (!DetermineCallTarget(node, shared_info))
return NoChange();
390 if (!shared_info->IsInlineable()) {
391 TRACE(
"Not inlining %s into %s because callee is not inlineable\n",
392 shared_info->DebugName()->ToCString().get(),
393 info_->shared_info()->DebugName()->ToCString().get());
398 if (node->opcode() == IrOpcode::kJSConstruct &&
399 !IsConstructable(shared_info->kind())) {
400 TRACE(
"Not inlining %s into %s because constructor is not constructable.\n",
401 shared_info->DebugName()->ToCString().get(),
402 info_->shared_info()->DebugName()->ToCString().get());
408 if (node->opcode() == IrOpcode::kJSCall &&
409 IsClassConstructor(shared_info->kind())) {
410 TRACE(
"Not inlining %s into %s because callee is a class constructor.\n",
411 shared_info->DebugName()->ToCString().get(),
412 info_->shared_info()->DebugName()->ToCString().get());
417 if (shared_info->HasBreakInfo()) {
418 TRACE(
"Not inlining %s into %s because callee may contain break points\n",
419 shared_info->DebugName()->ToCString().get(),
420 info_->shared_info()->DebugName()->ToCString().get());
426 int nesting_level = 0;
427 for (Node* frame_state = call.frame_state();
428 frame_state->opcode() == IrOpcode::kFrameState;
429 frame_state = frame_state->InputAt(kFrameStateOuterStateInput)) {
431 if (nesting_level > kMaxDepthForInlining) {
433 "Not inlining %s into %s because call has exceeded the maximum depth " 434 "for function inlining\n",
435 shared_info->DebugName()->ToCString().get(),
436 info_->shared_info()->DebugName()->ToCString().get());
443 Node* exception_target =
nullptr;
444 if (NodeProperties::IsExceptionalCall(node, &exception_target) &&
445 !FLAG_inline_into_try) {
447 "Try block surrounds #%d:%s and --no-inline-into-try active, so not " 448 "inlining %s into %s.\n",
449 exception_target->id(), exception_target->op()->mnemonic(),
450 shared_info->DebugName()->ToCString().get(),
451 info_->shared_info()->DebugName()->ToCString().get());
455 if (!shared_info->is_compiled() &&
456 !Compiler::Compile(shared_info, Compiler::CLEAR_EXCEPTION)) {
457 TRACE(
"Not inlining %s into %s because bytecode generation failed\n",
458 shared_info->DebugName()->ToCString().get(),
459 info_->shared_info()->DebugName()->ToCString().get());
467 TRACE(
"Inlining %s into %s%s\n", shared_info->DebugName()->ToCString().get(),
468 info_->shared_info()->DebugName()->ToCString().get(),
469 (exception_target !=
nullptr) ?
" (inside try-block)" :
"");
472 Handle<BytecodeArray> bytecode_array =
473 handle(shared_info->GetBytecodeArray(), isolate());
477 Handle<FeedbackVector> feedback_vector;
478 DetermineCallContext(node, context, feedback_vector);
481 int inlining_id = info_->AddInlinedFunction(
482 shared_info, bytecode_array, source_positions_->GetSourcePosition(node));
489 Graph::SubgraphScope scope(graph());
490 JSTypeHintLowering::Flags flags = JSTypeHintLowering::kNoFlags;
491 if (info_->is_bailout_on_uninitialized()) {
492 flags |= JSTypeHintLowering::kBailoutOnUninitialized;
494 CallFrequency frequency = call.frequency();
495 BytecodeGraphBuilder graph_builder(
496 zone(), bytecode_array, shared_info, feedback_vector, BailoutId::None(),
497 jsgraph(), frequency, source_positions_, native_context(), inlining_id,
498 flags,
false, info_->is_analyze_environment_liveness());
499 graph_builder.CreateGraph();
502 start = graph()->start();
503 end = graph()->end();
509 NodeVector uncaught_subcalls(local_zone_);
510 if (exception_target !=
nullptr) {
512 AllNodes inlined_nodes(local_zone_, end, graph());
513 for (Node* subnode : inlined_nodes.reachable) {
516 if (subnode->op()->HasProperty(Operator::kNoThrow))
continue;
517 if (!NodeProperties::IsExceptionalCall(subnode)) {
518 DCHECK_EQ(2, subnode->op()->ControlOutputCount());
519 uncaught_subcalls.push_back(subnode);
524 Node* frame_state = call.frame_state();
525 Node* new_target = jsgraph()->UndefinedConstant();
528 if (node->opcode() == IrOpcode::kJSConstruct) {
532 new_target = call.new_target();
533 node->RemoveInput(call.formal_arguments() + 1);
534 node->InsertInput(graph()->zone(), 1, new_target);
544 Node* receiver = jsgraph()->TheHoleConstant();
545 Node* context = NodeProperties::GetContextInput(node);
546 if (NeedsImplicitReceiver(shared_info)) {
547 Node* effect = NodeProperties::GetEffectInput(node);
548 Node* control = NodeProperties::GetControlInput(node);
549 Node* frame_state_inside = CreateArtificialFrameState(
550 node, frame_state, call.formal_arguments(),
551 BailoutId::ConstructStubCreate(), FrameStateType::kConstructStub,
552 shared_info, context);
554 graph()->NewNode(javascript()->Create(), call.target(), new_target,
555 context, frame_state_inside, effect, control);
556 uncaught_subcalls.push_back(create);
557 NodeProperties::ReplaceControlInput(node, create);
558 NodeProperties::ReplaceEffectInput(node, create);
561 Node* dummy = graph()->NewNode(common()->Dead());
562 NodeProperties::ReplaceUses(node, dummy, node, node, node);
567 Node* check = graph()->NewNode(simplified()->ObjectIsReceiver(), node);
569 graph()->NewNode(common()->Select(MachineRepresentation::kTagged),
570 check, node, create);
572 ReplaceWithValue(dummy, result);
573 }
else if (IsDerivedConstructor(shared_info->kind())) {
575 NodeProperties::FindSuccessfulControlProjection(node);
577 graph()->NewNode(simplified()->ObjectIsReceiver(), node);
578 Node* branch_is_receiver =
579 graph()->NewNode(common()->Branch(), is_receiver, node_success);
580 Node* branch_is_receiver_true =
581 graph()->NewNode(common()->IfTrue(), branch_is_receiver);
582 Node* branch_is_receiver_false =
583 graph()->NewNode(common()->IfFalse(), branch_is_receiver);
584 branch_is_receiver_false =
585 graph()->NewNode(javascript()->CallRuntime(
586 Runtime::kThrowConstructorReturnedNonObject),
587 context, NodeProperties::GetFrameStateInput(node),
588 node, branch_is_receiver_false);
589 uncaught_subcalls.push_back(branch_is_receiver_false);
590 branch_is_receiver_false =
591 graph()->NewNode(common()->Throw(), branch_is_receiver_false,
592 branch_is_receiver_false);
593 NodeProperties::MergeControlToEnd(graph(), common(),
594 branch_is_receiver_false);
596 ReplaceWithValue(node_success, node_success, node_success,
597 branch_is_receiver_true);
599 NodeProperties::ReplaceControlInput(branch_is_receiver, node_success, 0);
601 node->ReplaceInput(1, receiver);
604 frame_state = CreateArtificialFrameState(
605 node, frame_state, call.formal_arguments(),
606 BailoutId::ConstructStubInvoke(), FrameStateType::kConstructStub,
607 shared_info, context);
612 if (node->opcode() == IrOpcode::kJSCall &&
613 is_sloppy(shared_info->language_mode()) && !shared_info->native()) {
614 Node* effect = NodeProperties::GetEffectInput(node);
615 if (NodeProperties::CanBePrimitive(broker(), call.receiver(), effect)) {
616 CallParameters
const& p = CallParametersOf(node->op());
617 Node* global_proxy = jsgraph()->HeapConstant(
618 handle(info_->native_context()->global_proxy(), isolate()));
619 Node* receiver = effect =
620 graph()->NewNode(simplified()->ConvertReceiver(p.convert_mode()),
621 call.receiver(), global_proxy, effect, start);
622 NodeProperties::ReplaceValueInput(node, receiver, 1);
623 NodeProperties::ReplaceEffectInput(node, effect);
631 int parameter_count = shared_info->internal_formal_parameter_count();
632 DCHECK_EQ(parameter_count, start->op()->ValueOutputCount() - 5);
633 if (call.formal_arguments() != parameter_count) {
634 frame_state = CreateArtificialFrameState(
635 node, frame_state, call.formal_arguments(), BailoutId::None(),
636 FrameStateType::kArgumentsAdaptor, shared_info);
639 return InlineCall(node, new_target, context, frame_state, start, end,
640 exception_target, uncaught_subcalls);
643 Graph* JSInliner::graph()
const {
return jsgraph()->graph(); }
645 JSOperatorBuilder* JSInliner::javascript()
const {
646 return jsgraph()->javascript();
649 CommonOperatorBuilder* JSInliner::common()
const {
return jsgraph()->common(); }
651 SimplifiedOperatorBuilder* JSInliner::simplified()
const {
652 return jsgraph()->simplified();