Line data Source code
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
3 : // found in the LICENSE file.
4 :
5 : #include "src/compiler/js-inlining.h"
6 :
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/objects/feedback-cell-inl.h"
21 : #include "src/optimized-compilation-info.h"
22 : #include "src/parsing/parse-info.h"
23 :
24 : namespace v8 {
25 : namespace internal {
26 : namespace compiler {
27 :
28 : namespace {
29 : // This is just to avoid some corner cases, especially since we allow recursive
30 : // inlining.
31 : static const int kMaxDepthForInlining = 50;
32 : } // namespace
33 :
34 : #define TRACE(...) \
35 : do { \
36 : if (FLAG_trace_turbo_inlining) PrintF(__VA_ARGS__); \
37 : } while (false)
38 :
39 :
40 : // Provides convenience accessors for the common layout of nodes having either
41 : // the {JSCall} or the {JSConstruct} operator.
42 : class JSCallAccessor {
43 : public:
44 65118 : explicit JSCallAccessor(Node* call) : call_(call) {
45 : DCHECK(call->opcode() == IrOpcode::kJSCall ||
46 : call->opcode() == IrOpcode::kJSConstruct);
47 : }
48 :
49 : Node* target() {
50 : // Both, {JSCall} and {JSConstruct}, have same layout here.
51 3567 : return call_->InputAt(0);
52 : }
53 :
54 : Node* receiver() {
55 : DCHECK_EQ(IrOpcode::kJSCall, call_->opcode());
56 114096 : return call_->InputAt(1);
57 : }
58 :
59 4952 : Node* new_target() {
60 : DCHECK_EQ(IrOpcode::kJSConstruct, call_->opcode());
61 9904 : return call_->InputAt(formal_arguments() + 1);
62 : }
63 :
64 : Node* frame_state() {
65 : // Both, {JSCall} and {JSConstruct}, have frame state.
66 130225 : return NodeProperties::GetFrameStateInput(call_);
67 : }
68 :
69 : int formal_arguments() {
70 : // Both, {JSCall} and {JSConstruct}, have two extra inputs:
71 : // - JSConstruct: Includes target function and new target.
72 : // - JSCall: Includes target function and receiver.
73 157158 : return call_->op()->ValueInputCount() - 2;
74 : }
75 :
76 65108 : CallFrequency frequency() const {
77 65108 : return (call_->opcode() == IrOpcode::kJSCall)
78 60156 : ? CallParametersOf(call_->op()).frequency()
79 125264 : : ConstructParametersOf(call_->op()).frequency();
80 : }
81 :
82 : private:
83 : Node* call_;
84 : };
85 :
86 65108 : Reduction JSInliner::InlineCall(Node* call, Node* new_target, Node* context,
87 : Node* frame_state, Node* start, Node* end,
88 : Node* exception_target,
89 : const NodeVector& uncaught_subcalls) {
90 : // The scheduler is smart enough to place our code; we just ensure {control}
91 : // becomes the control input of the start of the inlinee, and {effect} becomes
92 : // the effect input of the start of the inlinee.
93 65108 : Node* control = NodeProperties::GetControlInput(call);
94 65108 : Node* effect = NodeProperties::GetEffectInput(call);
95 :
96 : int const inlinee_new_target_index =
97 65108 : static_cast<int>(start->op()->ValueOutputCount()) - 3;
98 : int const inlinee_arity_index =
99 65108 : static_cast<int>(start->op()->ValueOutputCount()) - 2;
100 : int const inlinee_context_index =
101 65108 : static_cast<int>(start->op()->ValueOutputCount()) - 1;
102 :
103 : // {inliner_inputs} counts JSFunction, receiver, arguments, but not
104 : // new target value, argument count, context, effect or control.
105 : int inliner_inputs = call->op()->ValueInputCount();
106 : // Iterate over all uses of the start node.
107 2327548 : for (Edge edge : start->use_edges()) {
108 : Node* use = edge.from();
109 1131220 : switch (use->opcode()) {
110 : case IrOpcode::kParameter: {
111 331446 : int index = 1 + ParameterIndexOf(use->op());
112 : DCHECK_LE(index, inlinee_context_index);
113 331446 : if (index < inliner_inputs && index < inlinee_new_target_index) {
114 : // There is an input from the call, and the index is a value
115 : // projection but not the context, so rewire the input.
116 : Replace(use, call->InputAt(index));
117 88611 : } else if (index == inlinee_new_target_index) {
118 : // The projection is requesting the new target value.
119 : Replace(use, new_target);
120 86599 : } else if (index == inlinee_arity_index) {
121 : // The projection is requesting the number of arguments.
122 0 : Replace(use, jsgraph()->Constant(inliner_inputs - 2));
123 86599 : } else if (index == inlinee_context_index) {
124 : // The projection is requesting the inlinee function context.
125 : Replace(use, context);
126 : } else {
127 : // Call has fewer arguments than required, fill with undefined.
128 21491 : Replace(use, jsgraph()->UndefinedConstant());
129 : }
130 : break;
131 : }
132 : default:
133 799774 : if (NodeProperties::IsEffectEdge(edge)) {
134 66299 : edge.UpdateTo(effect);
135 733475 : } else if (NodeProperties::IsControlEdge(edge)) {
136 216363 : edge.UpdateTo(control);
137 517112 : } else if (NodeProperties::IsFrameStateEdge(edge)) {
138 517112 : edge.UpdateTo(frame_state);
139 : } else {
140 0 : UNREACHABLE();
141 : }
142 : break;
143 : }
144 : }
145 :
146 65108 : if (exception_target != nullptr) {
147 : // Link uncaught calls in the inlinee to {exception_target}
148 3447 : int subcall_count = static_cast<int>(uncaught_subcalls.size());
149 3447 : if (subcall_count > 0) {
150 3324 : TRACE(
151 : "Inlinee contains %d calls without local exception handler; "
152 : "linking to surrounding exception handler\n",
153 : subcall_count);
154 : }
155 3447 : NodeVector on_exception_nodes(local_zone_);
156 21825 : for (Node* subcall : uncaught_subcalls) {
157 18378 : Node* on_success = graph()->NewNode(common()->IfSuccess(), subcall);
158 18378 : NodeProperties::ReplaceUses(subcall, subcall, subcall, on_success);
159 18378 : NodeProperties::ReplaceControlInput(on_success, subcall);
160 : Node* on_exception =
161 36756 : graph()->NewNode(common()->IfException(), subcall, subcall);
162 18378 : on_exception_nodes.push_back(on_exception);
163 : }
164 :
165 : DCHECK_EQ(subcall_count, static_cast<int>(on_exception_nodes.size()));
166 3447 : if (subcall_count > 0) {
167 : Node* control_output =
168 3324 : graph()->NewNode(common()->Merge(subcall_count), subcall_count,
169 3324 : &on_exception_nodes.front());
170 3324 : NodeVector values_effects(local_zone_);
171 : values_effects = on_exception_nodes;
172 3324 : values_effects.push_back(control_output);
173 3324 : Node* value_output = graph()->NewNode(
174 : common()->Phi(MachineRepresentation::kTagged, subcall_count),
175 3324 : subcall_count + 1, &values_effects.front());
176 : Node* effect_output =
177 3324 : graph()->NewNode(common()->EffectPhi(subcall_count),
178 3324 : subcall_count + 1, &values_effects.front());
179 : ReplaceWithValue(exception_target, value_output, effect_output,
180 3324 : control_output);
181 : } else {
182 123 : ReplaceWithValue(exception_target, exception_target, exception_target,
183 : jsgraph()->Dead());
184 : }
185 : }
186 :
187 65108 : NodeVector values(local_zone_);
188 : NodeVector effects(local_zone_);
189 : NodeVector controls(local_zone_);
190 231618 : for (Node* const input : end->inputs()) {
191 166510 : switch (input->opcode()) {
192 : case IrOpcode::kReturn:
193 204060 : values.push_back(NodeProperties::GetValueInput(input, 1));
194 204060 : effects.push_back(NodeProperties::GetEffectInput(input));
195 204060 : controls.push_back(NodeProperties::GetControlInput(input));
196 102030 : break;
197 : case IrOpcode::kDeoptimize:
198 : case IrOpcode::kTerminate:
199 : case IrOpcode::kThrow:
200 64480 : NodeProperties::MergeControlToEnd(graph(), common(), input);
201 : Revisit(graph()->end());
202 : break;
203 : default:
204 0 : UNREACHABLE();
205 : break;
206 : }
207 : }
208 : DCHECK_EQ(values.size(), effects.size());
209 : DCHECK_EQ(values.size(), controls.size());
210 :
211 : // Depending on whether the inlinee produces a value, we either replace value
212 : // uses with said value or kill value uses if no value can be returned.
213 65108 : if (values.size() > 0) {
214 64381 : int const input_count = static_cast<int>(controls.size());
215 64381 : Node* control_output = graph()->NewNode(common()->Merge(input_count),
216 64381 : input_count, &controls.front());
217 64381 : values.push_back(control_output);
218 64381 : effects.push_back(control_output);
219 64381 : Node* value_output = graph()->NewNode(
220 : common()->Phi(MachineRepresentation::kTagged, input_count),
221 64381 : static_cast<int>(values.size()), &values.front());
222 : Node* effect_output =
223 64381 : graph()->NewNode(common()->EffectPhi(input_count),
224 64381 : static_cast<int>(effects.size()), &effects.front());
225 64381 : ReplaceWithValue(call, value_output, effect_output, control_output);
226 : return Changed(value_output);
227 : } else {
228 727 : ReplaceWithValue(call, jsgraph()->Dead(), jsgraph()->Dead(),
229 : jsgraph()->Dead());
230 : return Changed(call);
231 : }
232 : }
233 :
234 31623 : Node* JSInliner::CreateArtificialFrameState(Node* node, Node* outer_frame_state,
235 : int parameter_count,
236 : BailoutId bailout_id,
237 : FrameStateType frame_state_type,
238 : Handle<SharedFunctionInfo> shared,
239 : Node* context) {
240 : const FrameStateFunctionInfo* state_info =
241 31623 : common()->CreateFrameStateFunctionInfo(frame_state_type,
242 31623 : parameter_count + 1, 0, shared);
243 :
244 : const Operator* op = common()->FrameState(
245 31623 : bailout_id, OutputFrameStateCombine::Ignore(), state_info);
246 31623 : const Operator* op0 = common()->StateValues(0, SparseInputMask::Dense());
247 : Node* node0 = graph()->NewNode(op0);
248 31623 : NodeVector params(local_zone_);
249 214761 : for (int parameter = 0; parameter < parameter_count + 1; ++parameter) {
250 274707 : params.push_back(node->InputAt(1 + parameter));
251 : }
252 31623 : const Operator* op_param = common()->StateValues(
253 31623 : static_cast<int>(params.size()), SparseInputMask::Dense());
254 31623 : Node* params_node = graph()->NewNode(
255 31623 : op_param, static_cast<int>(params.size()), ¶ms.front());
256 31623 : if (!context) {
257 23104 : context = jsgraph()->UndefinedConstant();
258 : }
259 : return graph()->NewNode(op, params_node, node0, node0, context,
260 31623 : node->InputAt(0), outer_frame_state);
261 : }
262 :
263 : namespace {
264 :
265 : // TODO(mstarzinger,verwaest): Move this predicate onto SharedFunctionInfo?
266 4952 : bool NeedsImplicitReceiver(Handle<SharedFunctionInfo> shared_info) {
267 : DisallowHeapAllocation no_gc;
268 4952 : if (!shared_info->construct_as_builtin()) {
269 4952 : return !IsDerivedConstructor(shared_info->kind());
270 : } else {
271 : return false;
272 : }
273 : }
274 :
275 : } // namespace
276 :
277 : // Determines whether the call target of the given call {node} is statically
278 : // known and can be used as an inlining candidate. The {SharedFunctionInfo} of
279 : // the call target is provided (the exact closure might be unknown).
280 65118 : bool JSInliner::DetermineCallTarget(
281 : Node* node, Handle<SharedFunctionInfo>& shared_info_out) {
282 : DCHECK(IrOpcode::IsInlineeOpcode(node->opcode()));
283 : HeapObjectMatcher match(node->InputAt(0));
284 :
285 : // This reducer can handle both normal function calls as well a constructor
286 : // calls whenever the target is a constant function object, as follows:
287 : // - JSCall(target:constant, receiver, args...)
288 : // - JSConstruct(target:constant, args..., new.target)
289 128514 : if (match.HasValue() && match.Value()->IsJSFunction()) {
290 : Handle<JSFunction> function = Handle<JSFunction>::cast(match.Value());
291 :
292 : // Don't inline if the function has never run.
293 63396 : if (!function->has_feedback_vector()) return false;
294 :
295 : // Disallow cross native-context inlining for now. This means that all parts
296 : // of the resulting code will operate on the same global object. This also
297 : // prevents cross context leaks, where we could inline functions from a
298 : // different context and hold on to that context (and closure) from the code
299 : // object.
300 : // TODO(turbofan): We might want to revisit this restriction later when we
301 : // have a need for this, and we know how to model different native contexts
302 : // in the same graph in a compositional way.
303 190188 : if (function->native_context() != info_->native_context()) {
304 : return false;
305 : }
306 :
307 63396 : shared_info_out = handle(function->shared(), isolate());
308 63396 : return true;
309 : }
310 :
311 : // This reducer can also handle calls where the target is statically known to
312 : // be the result of a closure instantiation operation, as follows:
313 : // - JSCall(JSCreateClosure[shared](context), receiver, args...)
314 : // - JSConstruct(JSCreateClosure[shared](context), args..., new.target)
315 1722 : if (match.IsJSCreateClosure()) {
316 1722 : CreateClosureParameters const& p = CreateClosureParametersOf(match.op());
317 :
318 : // Disallow inlining in case the instantiation site was never run and hence
319 : // the vector cell does not contain a valid feedback vector for the call
320 : // target.
321 : // TODO(turbofan): We might consider to eagerly create the feedback vector
322 : // in such a case (in {DetermineCallContext} below) eventually.
323 : Handle<FeedbackCell> cell = p.feedback_cell();
324 1722 : if (!cell->value()->IsFeedbackVector()) return false;
325 :
326 1721 : shared_info_out = p.shared_info();
327 1721 : return true;
328 : }
329 :
330 : return false;
331 : }
332 :
333 : // Determines statically known information about the call target (assuming that
334 : // the call target is known according to {DetermineCallTarget} above). The
335 : // following static information is provided:
336 : // - context : The context (as SSA value) bound by the call target.
337 : // - feedback_vector : The target is guaranteed to use this feedback vector.
338 65108 : void JSInliner::DetermineCallContext(
339 : Node* node, Node*& context_out,
340 : Handle<FeedbackVector>& feedback_vector_out) {
341 : DCHECK(IrOpcode::IsInlineeOpcode(node->opcode()));
342 : HeapObjectMatcher match(node->InputAt(0));
343 :
344 128499 : if (match.HasValue() && match.Value()->IsJSFunction()) {
345 : Handle<JSFunction> function = Handle<JSFunction>::cast(match.Value());
346 63391 : CHECK(function->has_feedback_vector());
347 :
348 : // The inlinee specializes to the context from the JSFunction object.
349 63391 : context_out = jsgraph()->Constant(handle(function->context(), isolate()));
350 126782 : feedback_vector_out = handle(function->feedback_vector(), isolate());
351 : return;
352 : }
353 :
354 1717 : if (match.IsJSCreateClosure()) {
355 1717 : CreateClosureParameters const& p = CreateClosureParametersOf(match.op());
356 :
357 : // Load the feedback vector of the target by looking up its vector cell at
358 : // the instantiation site (we only decide to inline if it's populated).
359 : Handle<FeedbackCell> cell = p.feedback_cell();
360 : DCHECK(cell->value()->IsFeedbackVector());
361 :
362 : // The inlinee uses the locally provided context at instantiation.
363 1717 : context_out = NodeProperties::GetContextInput(match.node());
364 : feedback_vector_out =
365 1717 : handle(FeedbackVector::cast(cell->value()), isolate());
366 : return;
367 : }
368 :
369 : // Must succeed.
370 0 : UNREACHABLE();
371 : }
372 :
373 65108 : Handle<Context> JSInliner::native_context() const {
374 130216 : return handle(info_->native_context(), isolate());
375 : }
376 :
377 65118 : Reduction JSInliner::ReduceJSCall(Node* node) {
378 : DCHECK(IrOpcode::IsInlineeOpcode(node->opcode()));
379 : Handle<SharedFunctionInfo> shared_info;
380 : JSCallAccessor call(node);
381 :
382 : // Determine the call target.
383 65118 : if (!DetermineCallTarget(node, shared_info)) return NoChange();
384 :
385 : DCHECK(shared_info->IsInlineable());
386 :
387 : // Constructor must be constructable.
388 70070 : if (node->opcode() == IrOpcode::kJSConstruct &&
389 : !IsConstructable(shared_info->kind())) {
390 0 : TRACE("Not inlining %s into %s because constructor is not constructable.\n",
391 : shared_info->DebugName()->ToCString().get(),
392 : info_->shared_info()->DebugName()->ToCString().get());
393 : return NoChange();
394 : }
395 :
396 : // Class constructors are callable, but [[Call]] will raise an exception.
397 : // See ES6 section 9.2.1 [[Call]] ( thisArgument, argumentsList ).
398 125281 : if (node->opcode() == IrOpcode::kJSCall &&
399 : IsClassConstructor(shared_info->kind())) {
400 0 : TRACE("Not inlining %s into %s because callee is a class constructor.\n",
401 : shared_info->DebugName()->ToCString().get(),
402 : info_->shared_info()->DebugName()->ToCString().get());
403 : return NoChange();
404 : }
405 :
406 : // To ensure inlining always terminates, we have an upper limit on inlining
407 : // the nested calls.
408 : int nesting_level = 0;
409 352883 : for (Node* frame_state = call.frame_state();
410 : frame_state->opcode() == IrOpcode::kFrameState;
411 : frame_state = frame_state->InputAt(kFrameStateOuterStateInput)) {
412 143892 : nesting_level++;
413 143892 : if (nesting_level > kMaxDepthForInlining) {
414 9 : TRACE(
415 : "Not inlining %s into %s because call has exceeded the maximum depth "
416 : "for function inlining\n",
417 : shared_info->DebugName()->ToCString().get(),
418 : info_->shared_info()->DebugName()->ToCString().get());
419 : return NoChange();
420 : }
421 : }
422 :
423 : // Calls surrounded by a local try-block are only inlined if the appropriate
424 : // flag is active. We also discover the {IfException} projection this way.
425 65108 : Node* exception_target = nullptr;
426 68555 : if (NodeProperties::IsExceptionalCall(node, &exception_target) &&
427 3447 : !FLAG_inline_into_try) {
428 0 : TRACE(
429 : "Try block surrounds #%d:%s and --no-inline-into-try active, so not "
430 : "inlining %s into %s.\n",
431 : exception_target->id(), exception_target->op()->mnemonic(),
432 : shared_info->DebugName()->ToCString().get(),
433 : info_->shared_info()->DebugName()->ToCString().get());
434 : return NoChange();
435 : }
436 :
437 65108 : IsCompiledScope is_compiled_scope(shared_info->is_compiled_scope());
438 : // JSInliningHeuristic should have already filtered candidates without
439 : // a BytecodeArray by calling SharedFunctionInfo::IsInlineable. For the ones
440 : // passing the check, a reference to the bytecode was retained to make sure
441 : // it never gets flushed, so the following check should always hold true.
442 65108 : CHECK(is_compiled_scope.is_compiled());
443 :
444 130216 : if (info_->is_source_positions_enabled()) {
445 592 : SharedFunctionInfo::EnsureSourcePositionsAvailable(isolate(), shared_info);
446 : }
447 :
448 65108 : TRACE("Inlining %s into %s%s\n", shared_info->DebugName()->ToCString().get(),
449 : info_->shared_info()->DebugName()->ToCString().get(),
450 : (exception_target != nullptr) ? " (inside try-block)" : "");
451 :
452 : // Determine the targets feedback vector and its context.
453 : Node* context;
454 : Handle<FeedbackVector> feedback_vector;
455 65108 : DetermineCallContext(node, context, feedback_vector);
456 :
457 65108 : if (FLAG_concurrent_inlining) {
458 : SharedFunctionInfoRef sfi(broker(), shared_info);
459 : FeedbackVectorRef feedback(broker(), feedback_vector);
460 60 : if (!sfi.IsSerializedForCompilation(feedback)) {
461 0 : TRACE_BROKER(broker(), "Missed opportunity to inline a function ("
462 : << Brief(*sfi.object()) << " with "
463 : << Brief(*feedback.object()) << ")");
464 0 : return NoChange();
465 : }
466 : }
467 :
468 : // ----------------------------------------------------------------
469 : // After this point, we've made a decision to inline this function.
470 : // We shall not bailout from inlining if we got here.
471 :
472 : Handle<BytecodeArray> bytecode_array =
473 130216 : handle(shared_info->GetBytecodeArray(), isolate());
474 :
475 : // Remember that we inlined this function.
476 65108 : int inlining_id = info_->AddInlinedFunction(
477 130216 : shared_info, bytecode_array, source_positions_->GetSourcePosition(node));
478 :
479 : // Create the subgraph for the inlinee.
480 : Node* start;
481 : Node* end;
482 : {
483 : // Run the BytecodeGraphBuilder to create the subgraph.
484 : Graph::SubgraphScope scope(graph());
485 : JSTypeHintLowering::Flags flags = JSTypeHintLowering::kNoFlags;
486 130216 : if (info_->is_bailout_on_uninitialized()) {
487 : flags |= JSTypeHintLowering::kBailoutOnUninitialized;
488 : }
489 65108 : CallFrequency frequency = call.frequency();
490 : BytecodeGraphBuilder graph_builder(
491 : zone(), bytecode_array, shared_info, feedback_vector, BailoutId::None(),
492 65108 : jsgraph(), frequency, source_positions_, native_context(), inlining_id,
493 260432 : flags, false, info_->is_analyze_environment_liveness());
494 65108 : graph_builder.CreateGraph();
495 :
496 : // Extract the inlinee start/end nodes.
497 : start = graph()->start();
498 : end = graph()->end();
499 : }
500 :
501 : // If we are inlining into a surrounding exception handler, we collect all
502 : // potentially throwing nodes within the inlinee that are not handled locally
503 : // by the inlinee itself. They are later wired into the surrounding handler.
504 65108 : NodeVector uncaught_subcalls(local_zone_);
505 65108 : if (exception_target != nullptr) {
506 : // Find all uncaught 'calls' in the inlinee.
507 3447 : AllNodes inlined_nodes(local_zone_, end, graph());
508 201783 : for (Node* subnode : inlined_nodes.reachable) {
509 : // Every possibly throwing node should get {IfSuccess} and {IfException}
510 : // projections, unless there already is local exception handling.
511 198336 : if (subnode->op()->HasProperty(Operator::kNoThrow)) continue;
512 19546 : if (!NodeProperties::IsExceptionalCall(subnode)) {
513 : DCHECK_EQ(2, subnode->op()->ControlOutputCount());
514 17948 : uncaught_subcalls.push_back(subnode);
515 : }
516 : }
517 : }
518 :
519 : Node* frame_state = call.frame_state();
520 65108 : Node* new_target = jsgraph()->UndefinedConstant();
521 :
522 : // Inline {JSConstruct} requires some additional magic.
523 65108 : if (node->opcode() == IrOpcode::kJSConstruct) {
524 : // Swizzle the inputs of the {JSConstruct} node to look like inputs to a
525 : // normal {JSCall} node so that the rest of the inlining machinery
526 : // behaves as if we were dealing with a regular function invocation.
527 4952 : new_target = call.new_target(); // Retrieve new target value input.
528 4952 : node->RemoveInput(call.formal_arguments() + 1); // Drop new target.
529 4952 : node->InsertInput(graph()->zone(), 1, new_target);
530 :
531 : // Insert nodes around the call that model the behavior required for a
532 : // constructor dispatch (allocate implicit receiver and check return value).
533 : // This models the behavior usually accomplished by our {JSConstructStub}.
534 : // Note that the context has to be the callers context (input to call node).
535 : // Also note that by splitting off the {JSCreate} piece of the constructor
536 : // call, we create an observable deoptimization point after the receiver
537 : // instantiation but before the invocation (i.e. inside {JSConstructStub}
538 : // where execution continues at {construct_stub_create_deopt_pc_offset}).
539 4952 : Node* receiver = jsgraph()->TheHoleConstant(); // Implicit receiver.
540 4952 : Node* context = NodeProperties::GetContextInput(node);
541 4952 : if (NeedsImplicitReceiver(shared_info)) {
542 3567 : Node* effect = NodeProperties::GetEffectInput(node);
543 3567 : Node* control = NodeProperties::GetControlInput(node);
544 : Node* frame_state_inside = CreateArtificialFrameState(
545 : node, frame_state, call.formal_arguments(),
546 : BailoutId::ConstructStubCreate(), FrameStateType::kConstructStub,
547 3567 : shared_info, context);
548 : Node* create =
549 3567 : graph()->NewNode(javascript()->Create(), call.target(), new_target,
550 3567 : context, frame_state_inside, effect, control);
551 3567 : uncaught_subcalls.push_back(create); // Adds {IfSuccess} & {IfException}.
552 3567 : NodeProperties::ReplaceControlInput(node, create);
553 3567 : NodeProperties::ReplaceEffectInput(node, create);
554 : // Placeholder to hold {node}'s value dependencies while {node} is
555 : // replaced.
556 3567 : Node* dummy = graph()->NewNode(common()->Dead());
557 3567 : NodeProperties::ReplaceUses(node, dummy, node, node, node);
558 : Node* result;
559 : // Insert a check of the return value to determine whether the return
560 : // value or the implicit receiver should be selected as a result of the
561 : // call.
562 3567 : Node* check = graph()->NewNode(simplified()->ObjectIsReceiver(), node);
563 : result =
564 3567 : graph()->NewNode(common()->Select(MachineRepresentation::kTagged),
565 : check, node, create);
566 3567 : receiver = create; // The implicit receiver.
567 : ReplaceWithValue(dummy, result);
568 1385 : } else if (IsDerivedConstructor(shared_info->kind())) {
569 : Node* node_success =
570 1385 : NodeProperties::FindSuccessfulControlProjection(node);
571 : Node* is_receiver =
572 1385 : graph()->NewNode(simplified()->ObjectIsReceiver(), node);
573 : Node* branch_is_receiver =
574 1385 : graph()->NewNode(common()->Branch(), is_receiver, node_success);
575 : Node* branch_is_receiver_true =
576 1385 : graph()->NewNode(common()->IfTrue(), branch_is_receiver);
577 : Node* branch_is_receiver_false =
578 2770 : graph()->NewNode(common()->IfFalse(), branch_is_receiver);
579 : branch_is_receiver_false =
580 1385 : graph()->NewNode(javascript()->CallRuntime(
581 : Runtime::kThrowConstructorReturnedNonObject),
582 : context, NodeProperties::GetFrameStateInput(node),
583 1385 : node, branch_is_receiver_false);
584 1385 : uncaught_subcalls.push_back(branch_is_receiver_false);
585 : branch_is_receiver_false =
586 1385 : graph()->NewNode(common()->Throw(), branch_is_receiver_false,
587 1385 : branch_is_receiver_false);
588 : NodeProperties::MergeControlToEnd(graph(), common(),
589 1385 : branch_is_receiver_false);
590 :
591 : ReplaceWithValue(node_success, node_success, node_success,
592 : branch_is_receiver_true);
593 : // Fix input destroyed by the above {ReplaceWithValue} call.
594 1385 : NodeProperties::ReplaceControlInput(branch_is_receiver, node_success, 0);
595 : }
596 4952 : node->ReplaceInput(1, receiver);
597 : // Insert a construct stub frame into the chain of frame states. This will
598 : // reconstruct the proper frame when deoptimizing within the constructor.
599 : frame_state = CreateArtificialFrameState(
600 : node, frame_state, call.formal_arguments(),
601 : BailoutId::ConstructStubInvoke(), FrameStateType::kConstructStub,
602 4952 : shared_info, context);
603 : }
604 :
605 : // Insert a JSConvertReceiver node for sloppy callees. Note that the context
606 : // passed into this node has to be the callees context (loaded above).
607 125264 : if (node->opcode() == IrOpcode::kJSCall &&
608 123411 : is_sloppy(shared_info->language_mode()) && !shared_info->native()) {
609 58303 : Node* effect = NodeProperties::GetEffectInput(node);
610 58303 : if (NodeProperties::CanBePrimitive(broker(), call.receiver(), effect)) {
611 55793 : CallParameters const& p = CallParametersOf(node->op());
612 111586 : Node* global_proxy = jsgraph()->HeapConstant(
613 167379 : handle(info_->native_context()->global_proxy(), isolate()));
614 : Node* receiver = effect =
615 55793 : graph()->NewNode(simplified()->ConvertReceiver(p.convert_mode()),
616 : call.receiver(), global_proxy, effect, start);
617 55793 : NodeProperties::ReplaceValueInput(node, receiver, 1);
618 55793 : NodeProperties::ReplaceEffectInput(node, effect);
619 : }
620 : }
621 :
622 : // Insert argument adaptor frame if required. The callees formal parameter
623 : // count (i.e. value outputs of start node minus target, receiver, new target,
624 : // arguments count and context) have to match the number of arguments passed
625 : // to the call.
626 65108 : int parameter_count = shared_info->internal_formal_parameter_count();
627 : DCHECK_EQ(parameter_count, start->op()->ValueOutputCount() - 5);
628 65108 : if (call.formal_arguments() != parameter_count) {
629 : frame_state = CreateArtificialFrameState(
630 : node, frame_state, call.formal_arguments(), BailoutId::None(),
631 23104 : FrameStateType::kArgumentsAdaptor, shared_info);
632 : }
633 :
634 : return InlineCall(node, new_target, context, frame_state, start, end,
635 65108 : exception_target, uncaught_subcalls);
636 : }
637 :
638 0 : Graph* JSInliner::graph() const { return jsgraph()->graph(); }
639 :
640 0 : JSOperatorBuilder* JSInliner::javascript() const {
641 0 : return jsgraph()->javascript();
642 : }
643 :
644 0 : CommonOperatorBuilder* JSInliner::common() const { return jsgraph()->common(); }
645 :
646 0 : SimplifiedOperatorBuilder* JSInliner::simplified() const {
647 0 : return jsgraph()->simplified();
648 : }
649 :
650 : #undef TRACE
651 :
652 : } // namespace compiler
653 : } // namespace internal
654 120216 : } // namespace v8
|