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 65540 : 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 3543 : return call_->InputAt(0);
52 : }
53 :
54 : Node* receiver() {
55 : DCHECK_EQ(IrOpcode::kJSCall, call_->opcode());
56 114789 : return call_->InputAt(1);
57 : }
58 :
59 5390 : Node* new_target() {
60 : DCHECK_EQ(IrOpcode::kJSConstruct, call_->opcode());
61 10780 : return call_->InputAt(formal_arguments() + 1);
62 : }
63 :
64 : Node* frame_state() {
65 : // Both, {JSCall} and {JSConstruct}, have frame state.
66 131068 : 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 159704 : return call_->op()->ValueInputCount() - 2;
74 : }
75 :
76 65529 : CallFrequency frequency() const {
77 65529 : return (call_->opcode() == IrOpcode::kJSCall)
78 60139 : ? CallParametersOf(call_->op()).frequency()
79 125668 : : ConstructParametersOf(call_->op()).frequency();
80 : }
81 :
82 : private:
83 : Node* call_;
84 : };
85 :
86 65529 : 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 65529 : Node* control = NodeProperties::GetControlInput(call);
94 65529 : Node* effect = NodeProperties::GetEffectInput(call);
95 :
96 : int const inlinee_new_target_index =
97 65529 : static_cast<int>(start->op()->ValueOutputCount()) - 3;
98 : int const inlinee_arity_index =
99 65529 : static_cast<int>(start->op()->ValueOutputCount()) - 2;
100 : int const inlinee_context_index =
101 65529 : 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 2325939 : for (Edge edge : start->use_edges()) {
108 : Node* use = edge.from();
109 1130205 : switch (use->opcode()) {
110 : case IrOpcode::kParameter: {
111 333417 : int index = 1 + ParameterIndexOf(use->op());
112 : DCHECK_LE(index, inlinee_context_index);
113 333417 : 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 89554 : } else if (index == inlinee_new_target_index) {
118 : // The projection is requesting the new target value.
119 : Replace(use, new_target);
120 87112 : } 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 87112 : } 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 21583 : Replace(use, jsgraph()->UndefinedConstant());
129 : }
130 : break;
131 : }
132 : default:
133 796788 : if (NodeProperties::IsEffectEdge(edge)) {
134 66714 : edge.UpdateTo(effect);
135 730074 : } else if (NodeProperties::IsControlEdge(edge)) {
136 216371 : edge.UpdateTo(control);
137 513703 : } else if (NodeProperties::IsFrameStateEdge(edge)) {
138 513703 : edge.UpdateTo(frame_state);
139 : } else {
140 0 : UNREACHABLE();
141 : }
142 : break;
143 : }
144 : }
145 :
146 65529 : if (exception_target != nullptr) {
147 : // Link uncaught calls in the inlinee to {exception_target}
148 3260 : int subcall_count = static_cast<int>(uncaught_subcalls.size());
149 3260 : if (subcall_count > 0) {
150 3129 : TRACE(
151 : "Inlinee contains %d calls without local exception handler; "
152 : "linking to surrounding exception handler\n",
153 : subcall_count);
154 : }
155 3260 : NodeVector on_exception_nodes(local_zone_);
156 20219 : for (Node* subcall : uncaught_subcalls) {
157 16959 : Node* on_success = graph()->NewNode(common()->IfSuccess(), subcall);
158 16959 : NodeProperties::ReplaceUses(subcall, subcall, subcall, on_success);
159 16959 : NodeProperties::ReplaceControlInput(on_success, subcall);
160 : Node* on_exception =
161 33918 : graph()->NewNode(common()->IfException(), subcall, subcall);
162 16959 : on_exception_nodes.push_back(on_exception);
163 : }
164 :
165 : DCHECK_EQ(subcall_count, static_cast<int>(on_exception_nodes.size()));
166 3260 : if (subcall_count > 0) {
167 : Node* control_output =
168 3129 : graph()->NewNode(common()->Merge(subcall_count), subcall_count,
169 3129 : &on_exception_nodes.front());
170 3129 : NodeVector values_effects(local_zone_);
171 : values_effects = on_exception_nodes;
172 3129 : values_effects.push_back(control_output);
173 3129 : Node* value_output = graph()->NewNode(
174 : common()->Phi(MachineRepresentation::kTagged, subcall_count),
175 3129 : subcall_count + 1, &values_effects.front());
176 : Node* effect_output =
177 3129 : graph()->NewNode(common()->EffectPhi(subcall_count),
178 3129 : subcall_count + 1, &values_effects.front());
179 : ReplaceWithValue(exception_target, value_output, effect_output,
180 3129 : control_output);
181 : } else {
182 131 : ReplaceWithValue(exception_target, exception_target, exception_target,
183 : jsgraph()->Dead());
184 : }
185 : }
186 :
187 65529 : NodeVector values(local_zone_);
188 : NodeVector effects(local_zone_);
189 : NodeVector controls(local_zone_);
190 233289 : for (Node* const input : end->inputs()) {
191 167760 : switch (input->opcode()) {
192 : case IrOpcode::kReturn:
193 204968 : values.push_back(NodeProperties::GetValueInput(input, 1));
194 204968 : effects.push_back(NodeProperties::GetEffectInput(input));
195 204968 : controls.push_back(NodeProperties::GetControlInput(input));
196 102484 : break;
197 : case IrOpcode::kDeoptimize:
198 : case IrOpcode::kTerminate:
199 : case IrOpcode::kThrow:
200 65276 : 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 65529 : if (values.size() > 0) {
214 64815 : int const input_count = static_cast<int>(controls.size());
215 64815 : Node* control_output = graph()->NewNode(common()->Merge(input_count),
216 64815 : input_count, &controls.front());
217 64815 : values.push_back(control_output);
218 64815 : effects.push_back(control_output);
219 64815 : Node* value_output = graph()->NewNode(
220 : common()->Phi(MachineRepresentation::kTagged, input_count),
221 64815 : static_cast<int>(values.size()), &values.front());
222 : Node* effect_output =
223 64815 : graph()->NewNode(common()->EffectPhi(input_count),
224 64815 : static_cast<int>(effects.size()), &effects.front());
225 64815 : ReplaceWithValue(call, value_output, effect_output, control_output);
226 : return Changed(value_output);
227 : } else {
228 714 : ReplaceWithValue(call, jsgraph()->Dead(), jsgraph()->Dead(),
229 : jsgraph()->Dead());
230 : return Changed(call);
231 : }
232 : }
233 :
234 32332 : 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 32332 : common()->CreateFrameStateFunctionInfo(frame_state_type,
242 32332 : parameter_count + 1, 0, shared);
243 :
244 : const Operator* op = common()->FrameState(
245 32332 : bailout_id, OutputFrameStateCombine::Ignore(), state_info);
246 32332 : const Operator* op0 = common()->StateValues(0, SparseInputMask::Dense());
247 : Node* node0 = graph()->NewNode(op0);
248 32332 : NodeVector params(local_zone_);
249 219368 : for (int parameter = 0; parameter < parameter_count + 1; ++parameter) {
250 280554 : params.push_back(node->InputAt(1 + parameter));
251 : }
252 32332 : const Operator* op_param = common()->StateValues(
253 32332 : static_cast<int>(params.size()), SparseInputMask::Dense());
254 32332 : Node* params_node = graph()->NewNode(
255 32332 : op_param, static_cast<int>(params.size()), ¶ms.front());
256 32332 : if (!context) {
257 23399 : context = jsgraph()->UndefinedConstant();
258 : }
259 : return graph()->NewNode(op, params_node, node0, node0, context,
260 32332 : node->InputAt(0), outer_frame_state);
261 : }
262 :
263 : namespace {
264 :
265 : // TODO(mstarzinger,verwaest): Move this predicate onto SharedFunctionInfo?
266 5390 : bool NeedsImplicitReceiver(Handle<SharedFunctionInfo> shared_info) {
267 : DisallowHeapAllocation no_gc;
268 5390 : if (!shared_info->construct_as_builtin()) {
269 5390 : 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 65540 : 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 129342 : 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 63802 : 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 127604 : if (function->native_context() != info_->native_context()) {
304 : return false;
305 : }
306 :
307 63802 : shared_info_out = handle(function->shared(), isolate());
308 63802 : 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 1738 : if (match.IsJSCreateClosure()) {
316 1738 : 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 1738 : if (!cell->value()->IsFeedbackVector()) return false;
325 :
326 1737 : shared_info_out = p.shared_info();
327 1737 : 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 65529 : 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 129325 : if (match.HasValue() && match.Value()->IsJSFunction()) {
345 : Handle<JSFunction> function = Handle<JSFunction>::cast(match.Value());
346 63796 : CHECK(function->has_feedback_vector());
347 :
348 : // The inlinee specializes to the context from the JSFunction object.
349 63796 : context_out = jsgraph()->Constant(handle(function->context(), isolate()));
350 63796 : feedback_vector_out = handle(function->feedback_vector(), isolate());
351 : return;
352 : }
353 :
354 1733 : if (match.IsJSCreateClosure()) {
355 1733 : 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 1733 : context_out = NodeProperties::GetContextInput(match.node());
364 : feedback_vector_out =
365 1733 : handle(FeedbackVector::cast(cell->value()), isolate());
366 : return;
367 : }
368 :
369 : // Must succeed.
370 0 : UNREACHABLE();
371 : }
372 :
373 65529 : Handle<Context> JSInliner::native_context() const {
374 131058 : return handle(info_->native_context(), isolate());
375 : }
376 :
377 65540 : Reduction JSInliner::ReduceJSCall(Node* node) {
378 : DCHECK(IrOpcode::IsInlineeOpcode(node->opcode()));
379 : Handle<SharedFunctionInfo> shared_info;
380 : JSCallAccessor call(node);
381 :
382 : // TODO(mslekova): Remove those when inlining is brokerized.
383 : AllowHandleDereference allow_handle_deref;
384 : AllowHandleAllocation allow_handle_alloc;
385 : AllowHeapAllocation allow_heap_alloc;
386 : AllowCodeDependencyChange allow_code_dep_change;
387 :
388 : // Determine the call target.
389 65540 : if (!DetermineCallTarget(node, shared_info)) return NoChange();
390 :
391 : DCHECK(shared_info->IsInlineable());
392 :
393 : // Constructor must be constructable.
394 70931 : if (node->opcode() == IrOpcode::kJSConstruct &&
395 : !IsConstructable(shared_info->kind())) {
396 0 : TRACE("Not inlining %s into %s because constructor is not constructable.\n",
397 : shared_info->DebugName()->ToCString().get(),
398 : info_->shared_info()->DebugName()->ToCString().get());
399 : return NoChange();
400 : }
401 :
402 : // Class constructors are callable, but [[Call]] will raise an exception.
403 : // See ES6 section 9.2.1 [[Call]] ( thisArgument, argumentsList ).
404 125686 : if (node->opcode() == IrOpcode::kJSCall &&
405 : IsClassConstructor(shared_info->kind())) {
406 0 : TRACE("Not inlining %s into %s because callee is a class constructor.\n",
407 : shared_info->DebugName()->ToCString().get(),
408 : info_->shared_info()->DebugName()->ToCString().get());
409 : return NoChange();
410 : }
411 :
412 : // To ensure inlining always terminates, we have an upper limit on inlining
413 : // the nested calls.
414 : int nesting_level = 0;
415 353253 : for (Node* frame_state = call.frame_state();
416 : frame_state->opcode() == IrOpcode::kFrameState;
417 : frame_state = frame_state->InputAt(kFrameStateOuterStateInput)) {
418 143867 : nesting_level++;
419 143867 : if (nesting_level > kMaxDepthForInlining) {
420 10 : TRACE(
421 : "Not inlining %s into %s because call has exceeded the maximum depth "
422 : "for function inlining\n",
423 : shared_info->DebugName()->ToCString().get(),
424 : info_->shared_info()->DebugName()->ToCString().get());
425 : return NoChange();
426 : }
427 : }
428 :
429 : // Calls surrounded by a local try-block are only inlined if the appropriate
430 : // flag is active. We also discover the {IfException} projection this way.
431 65529 : Node* exception_target = nullptr;
432 68789 : if (NodeProperties::IsExceptionalCall(node, &exception_target) &&
433 3260 : !FLAG_inline_into_try) {
434 0 : TRACE(
435 : "Try block surrounds #%d:%s and --no-inline-into-try active, so not "
436 : "inlining %s into %s.\n",
437 : exception_target->id(), exception_target->op()->mnemonic(),
438 : shared_info->DebugName()->ToCString().get(),
439 : info_->shared_info()->DebugName()->ToCString().get());
440 : return NoChange();
441 : }
442 :
443 65529 : IsCompiledScope is_compiled_scope(shared_info->is_compiled_scope());
444 : // JSInliningHeuristic should have already filtered candidates without
445 : // a BytecodeArray by calling SharedFunctionInfo::IsInlineable. For the ones
446 : // passing the check, a reference to the bytecode was retained to make sure
447 : // it never gets flushed, so the following check should always hold true.
448 65529 : CHECK(is_compiled_scope.is_compiled());
449 :
450 131058 : if (info_->is_source_positions_enabled()) {
451 601 : SharedFunctionInfo::EnsureSourcePositionsAvailable(isolate(), shared_info);
452 : }
453 :
454 65529 : TRACE("Inlining %s into %s%s\n", shared_info->DebugName()->ToCString().get(),
455 : info_->shared_info()->DebugName()->ToCString().get(),
456 : (exception_target != nullptr) ? " (inside try-block)" : "");
457 :
458 : // Determine the targets feedback vector and its context.
459 : Node* context;
460 : Handle<FeedbackVector> feedback_vector;
461 65529 : DetermineCallContext(node, context, feedback_vector);
462 :
463 65529 : if (FLAG_concurrent_inlining) {
464 : SharedFunctionInfoRef sfi(broker(), shared_info);
465 : FeedbackVectorRef feedback(broker(), feedback_vector);
466 70 : if (!sfi.IsSerializedForCompilation(feedback)) {
467 0 : TRACE_BROKER(broker(), "Missed opportunity to inline a function ("
468 : << Brief(*sfi.object()) << " with "
469 : << Brief(*feedback.object()) << ")");
470 0 : return NoChange();
471 : }
472 : }
473 :
474 : // ----------------------------------------------------------------
475 : // After this point, we've made a decision to inline this function.
476 : // We shall not bailout from inlining if we got here.
477 :
478 : Handle<BytecodeArray> bytecode_array =
479 131058 : handle(shared_info->GetBytecodeArray(), isolate());
480 :
481 : // Remember that we inlined this function.
482 65529 : int inlining_id = info_->AddInlinedFunction(
483 131058 : shared_info, bytecode_array, source_positions_->GetSourcePosition(node));
484 :
485 : // Create the subgraph for the inlinee.
486 : Node* start;
487 : Node* end;
488 : {
489 : // Run the BytecodeGraphBuilder to create the subgraph.
490 : Graph::SubgraphScope scope(graph());
491 : JSTypeHintLowering::Flags flags = JSTypeHintLowering::kNoFlags;
492 131058 : if (info_->is_bailout_on_uninitialized()) {
493 : flags |= JSTypeHintLowering::kBailoutOnUninitialized;
494 : }
495 65529 : CallFrequency frequency = call.frequency();
496 : BytecodeGraphBuilder graph_builder(
497 : zone(), bytecode_array, shared_info, feedback_vector, BailoutId::None(),
498 65529 : jsgraph(), frequency, source_positions_, native_context(), inlining_id,
499 262116 : flags, false, info_->is_analyze_environment_liveness());
500 65529 : graph_builder.CreateGraph();
501 :
502 : // Extract the inlinee start/end nodes.
503 : start = graph()->start();
504 : end = graph()->end();
505 : }
506 :
507 : // If we are inlining into a surrounding exception handler, we collect all
508 : // potentially throwing nodes within the inlinee that are not handled locally
509 : // by the inlinee itself. They are later wired into the surrounding handler.
510 65529 : NodeVector uncaught_subcalls(local_zone_);
511 65529 : if (exception_target != nullptr) {
512 : // Find all uncaught 'calls' in the inlinee.
513 3260 : AllNodes inlined_nodes(local_zone_, end, graph());
514 191261 : for (Node* subnode : inlined_nodes.reachable) {
515 : // Every possibly throwing node should get {IfSuccess} and {IfException}
516 : // projections, unless there already is local exception handling.
517 188001 : if (subnode->op()->HasProperty(Operator::kNoThrow)) continue;
518 18155 : if (!NodeProperties::IsExceptionalCall(subnode)) {
519 : DCHECK_EQ(2, subnode->op()->ControlOutputCount());
520 16530 : uncaught_subcalls.push_back(subnode);
521 : }
522 : }
523 : }
524 :
525 : Node* frame_state = call.frame_state();
526 65529 : Node* new_target = jsgraph()->UndefinedConstant();
527 :
528 : // Inline {JSConstruct} requires some additional magic.
529 65529 : if (node->opcode() == IrOpcode::kJSConstruct) {
530 : // Swizzle the inputs of the {JSConstruct} node to look like inputs to a
531 : // normal {JSCall} node so that the rest of the inlining machinery
532 : // behaves as if we were dealing with a regular function invocation.
533 5390 : new_target = call.new_target(); // Retrieve new target value input.
534 5390 : node->RemoveInput(call.formal_arguments() + 1); // Drop new target.
535 5390 : node->InsertInput(graph()->zone(), 1, new_target);
536 :
537 : // Insert nodes around the call that model the behavior required for a
538 : // constructor dispatch (allocate implicit receiver and check return value).
539 : // This models the behavior usually accomplished by our {JSConstructStub}.
540 : // Note that the context has to be the callers context (input to call node).
541 : // Also note that by splitting off the {JSCreate} piece of the constructor
542 : // call, we create an observable deoptimization point after the receiver
543 : // instantiation but before the invocation (i.e. inside {JSConstructStub}
544 : // where execution continues at {construct_stub_create_deopt_pc_offset}).
545 5390 : Node* receiver = jsgraph()->TheHoleConstant(); // Implicit receiver.
546 5390 : Node* context = NodeProperties::GetContextInput(node);
547 5390 : if (NeedsImplicitReceiver(shared_info)) {
548 3543 : Node* effect = NodeProperties::GetEffectInput(node);
549 3543 : Node* control = NodeProperties::GetControlInput(node);
550 : Node* frame_state_inside = CreateArtificialFrameState(
551 : node, frame_state, call.formal_arguments(),
552 : BailoutId::ConstructStubCreate(), FrameStateType::kConstructStub,
553 3543 : shared_info, context);
554 : Node* create =
555 3543 : graph()->NewNode(javascript()->Create(), call.target(), new_target,
556 3543 : context, frame_state_inside, effect, control);
557 3543 : uncaught_subcalls.push_back(create); // Adds {IfSuccess} & {IfException}.
558 3543 : NodeProperties::ReplaceControlInput(node, create);
559 3543 : NodeProperties::ReplaceEffectInput(node, create);
560 : // Placeholder to hold {node}'s value dependencies while {node} is
561 : // replaced.
562 3543 : Node* dummy = graph()->NewNode(common()->Dead());
563 3543 : NodeProperties::ReplaceUses(node, dummy, node, node, node);
564 : Node* result;
565 : // Insert a check of the return value to determine whether the return
566 : // value or the implicit receiver should be selected as a result of the
567 : // call.
568 3543 : Node* check = graph()->NewNode(simplified()->ObjectIsReceiver(), node);
569 : result =
570 3543 : graph()->NewNode(common()->Select(MachineRepresentation::kTagged),
571 : check, node, create);
572 3543 : receiver = create; // The implicit receiver.
573 : ReplaceWithValue(dummy, result);
574 1847 : } else if (IsDerivedConstructor(shared_info->kind())) {
575 : Node* node_success =
576 1847 : NodeProperties::FindSuccessfulControlProjection(node);
577 : Node* is_receiver =
578 1847 : graph()->NewNode(simplified()->ObjectIsReceiver(), node);
579 : Node* branch_is_receiver =
580 1847 : graph()->NewNode(common()->Branch(), is_receiver, node_success);
581 : Node* branch_is_receiver_true =
582 1847 : graph()->NewNode(common()->IfTrue(), branch_is_receiver);
583 : Node* branch_is_receiver_false =
584 3694 : graph()->NewNode(common()->IfFalse(), branch_is_receiver);
585 : branch_is_receiver_false =
586 1847 : graph()->NewNode(javascript()->CallRuntime(
587 : Runtime::kThrowConstructorReturnedNonObject),
588 : context, NodeProperties::GetFrameStateInput(node),
589 1847 : node, branch_is_receiver_false);
590 1847 : uncaught_subcalls.push_back(branch_is_receiver_false);
591 : branch_is_receiver_false =
592 1847 : graph()->NewNode(common()->Throw(), branch_is_receiver_false,
593 1847 : branch_is_receiver_false);
594 : NodeProperties::MergeControlToEnd(graph(), common(),
595 1847 : branch_is_receiver_false);
596 :
597 : ReplaceWithValue(node_success, node_success, node_success,
598 : branch_is_receiver_true);
599 : // Fix input destroyed by the above {ReplaceWithValue} call.
600 1847 : NodeProperties::ReplaceControlInput(branch_is_receiver, node_success, 0);
601 : }
602 5390 : node->ReplaceInput(1, receiver);
603 : // Insert a construct stub frame into the chain of frame states. This will
604 : // reconstruct the proper frame when deoptimizing within the constructor.
605 : frame_state = CreateArtificialFrameState(
606 : node, frame_state, call.formal_arguments(),
607 : BailoutId::ConstructStubInvoke(), FrameStateType::kConstructStub,
608 5390 : shared_info, context);
609 : }
610 :
611 : // Insert a JSConvertReceiver node for sloppy callees. Note that the context
612 : // passed into this node has to be the callees context (loaded above).
613 125668 : if (node->opcode() == IrOpcode::kJSCall &&
614 124165 : is_sloppy(shared_info->language_mode()) && !shared_info->native()) {
615 58636 : Node* effect = NodeProperties::GetEffectInput(node);
616 58636 : if (NodeProperties::CanBePrimitive(broker(), call.receiver(), effect)) {
617 56153 : CallParameters const& p = CallParametersOf(node->op());
618 112306 : Node* global_proxy = jsgraph()->HeapConstant(
619 168459 : handle(info_->native_context()->global_proxy(), isolate()));
620 : Node* receiver = effect =
621 56153 : graph()->NewNode(simplified()->ConvertReceiver(p.convert_mode()),
622 : call.receiver(), global_proxy, effect, start);
623 56153 : NodeProperties::ReplaceValueInput(node, receiver, 1);
624 56153 : NodeProperties::ReplaceEffectInput(node, effect);
625 : }
626 : }
627 :
628 : // Insert argument adaptor frame if required. The callees formal parameter
629 : // count (i.e. value outputs of start node minus target, receiver, new target,
630 : // arguments count and context) have to match the number of arguments passed
631 : // to the call.
632 65529 : int parameter_count = shared_info->internal_formal_parameter_count();
633 : DCHECK_EQ(parameter_count, start->op()->ValueOutputCount() - 5);
634 65529 : if (call.formal_arguments() != parameter_count) {
635 : frame_state = CreateArtificialFrameState(
636 : node, frame_state, call.formal_arguments(), BailoutId::None(),
637 23399 : FrameStateType::kArgumentsAdaptor, shared_info);
638 : }
639 :
640 : return InlineCall(node, new_target, context, frame_state, start, end,
641 65529 : exception_target, uncaught_subcalls);
642 : }
643 :
644 0 : Graph* JSInliner::graph() const { return jsgraph()->graph(); }
645 :
646 0 : JSOperatorBuilder* JSInliner::javascript() const {
647 0 : return jsgraph()->javascript();
648 : }
649 :
650 0 : CommonOperatorBuilder* JSInliner::common() const { return jsgraph()->common(); }
651 :
652 0 : SimplifiedOperatorBuilder* JSInliner::simplified() const {
653 0 : return jsgraph()->simplified();
654 : }
655 :
656 : #undef TRACE
657 :
658 : } // namespace compiler
659 : } // namespace internal
660 122036 : } // namespace v8
|