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