js-inlining.cc 26.6 KB
Newer Older
1 2 3 4
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
#include "src/compiler/js-inlining.h"

7
#include "src/ast/ast.h"
8 9
#include "src/codegen/compiler.h"
#include "src/codegen/optimized-compilation-info.h"
10
#include "src/codegen/tick-counter.h"
11
#include "src/compiler/access-builder.h"
12
#include "src/compiler/all-nodes.h"
13
#include "src/compiler/bytecode-graph-builder.h"
14
#include "src/compiler/common-operator.h"
15
#include "src/compiler/compiler-source-position-table.h"
16
#include "src/compiler/graph-reducer.h"
17
#include "src/compiler/js-heap-broker.h"
18 19
#include "src/compiler/js-operator.h"
#include "src/compiler/node-matchers.h"
20
#include "src/compiler/node-properties.h"
21
#include "src/compiler/operator-properties.h"
22
#include "src/compiler/simplified-operator.h"
23
#include "src/execution/isolate-inl.h"
24
#include "src/objects/feedback-cell-inl.h"
25
#include "src/parsing/parse-info.h"
26 27 28 29 30

namespace v8 {
namespace internal {
namespace compiler {

31 32 33 34 35 36
namespace {
// This is just to avoid some corner cases, especially since we allow recursive
// inlining.
static const int kMaxDepthForInlining = 50;
}  // namespace

37 38 39 40 41
#define TRACE(x)                     \
  do {                               \
    if (FLAG_trace_turbo_inlining) { \
      StdoutStream() << x << "\n";   \
    }                                \
42 43
  } while (false)

44
// Provides convenience accessors for the common layout of nodes having either
45
// the {JSCall} or the {JSConstruct} operator.
46
class JSCallAccessor {
47
 public:
48
  explicit JSCallAccessor(Node* call) : call_(call) {
49
    DCHECK(call->opcode() == IrOpcode::kJSCall ||
50
           call->opcode() == IrOpcode::kJSConstruct);
51
  }
52

53
  Node* target() const {
54
    return call_->InputAt(JSCallOrConstructNode::TargetIndex());
55
  }
56

57 58
  Node* receiver() const {
    return JSCallNode{call_}.receiver();
59 60
  }

61
  Node* new_target() const { return JSConstructNode{call_}.new_target(); }
62 63

  Node* frame_state() const {
64
    return NodeProperties::GetFrameStateInput(call_);
65
  }
66

67
  int argument_count() const {
68 69
    return (call_->opcode() == IrOpcode::kJSCall)
               ? JSCallNode{call_}.ArgumentCount()
70
               : JSConstructNode{call_}.ArgumentCount();
71 72
  }

73
  CallFrequency const& frequency() const {
74
    return (call_->opcode() == IrOpcode::kJSCall)
75
               ? JSCallNode{call_}.Parameters().frequency()
76
               : JSConstructNode{call_}.Parameters().frequency();
77 78
  }

79
 private:
80
  Node* call_;
81 82
};

83
Reduction JSInliner::InlineCall(Node* call, Node* new_target, Node* context,
84 85 86
                                Node* frame_state, Node* start, Node* end,
                                Node* exception_target,
                                const NodeVector& uncaught_subcalls) {
87 88
  JSCallAccessor c(call);

89
  // The scheduler is smart enough to place our code; we just ensure {control}
90 91
  // becomes the control input of the start of the inlinee, and {effect} becomes
  // the effect input of the start of the inlinee.
92
  Node* control = NodeProperties::GetControlInput(call);
93
  Node* effect = NodeProperties::GetEffectInput(call);
94

95 96
  int const inlinee_new_target_index =
      static_cast<int>(start->op()->ValueOutputCount()) - 3;
97 98
  int const inlinee_arity_index =
      static_cast<int>(start->op()->ValueOutputCount()) - 2;
99 100 101
  int const inlinee_context_index =
      static_cast<int>(start->op()->ValueOutputCount()) - 1;

102 103 104 105 106
  // {inliner_inputs} counts the target, receiver/new_target, and arguments; but
  // not feedback vector, context, effect or control.
  const int inliner_inputs = c.argument_count() +
                             JSCallOrConstructNode::kExtraInputCount -
                             JSCallOrConstructNode::kFeedbackVectorInputCount;
107
  // Iterate over all uses of the start node.
108
  for (Edge edge : start->use_edges()) {
danno's avatar
danno committed
109
    Node* use = edge.from();
110 111
    switch (use->opcode()) {
      case IrOpcode::kParameter: {
112
        int index = 1 + ParameterIndexOf(use->op());
113
        DCHECK_LE(index, inlinee_context_index);
114
        if (index < inliner_inputs && index < inlinee_new_target_index) {
115 116
          // There is an input from the call, and the index is a value
          // projection but not the context, so rewire the input.
117
          Replace(use, call->InputAt(index));
118 119 120
        } else if (index == inlinee_new_target_index) {
          // The projection is requesting the new target value.
          Replace(use, new_target);
121 122
        } else if (index == inlinee_arity_index) {
          // The projection is requesting the number of arguments.
123
          Replace(use, jsgraph()->Constant(c.argument_count()));
124
        } else if (index == inlinee_context_index) {
125
          // The projection is requesting the inlinee function context.
126
          Replace(use, context);
127
        } else {
128
          // Call has fewer arguments than required, fill with undefined.
129
          Replace(use, jsgraph()->UndefinedConstant());
130 131 132 133
        }
        break;
      }
      default:
danno's avatar
danno committed
134
        if (NodeProperties::IsEffectEdge(edge)) {
135
          edge.UpdateTo(effect);
danno's avatar
danno committed
136 137
        } else if (NodeProperties::IsControlEdge(edge)) {
          edge.UpdateTo(control);
138 139
        } else if (NodeProperties::IsFrameStateEdge(edge)) {
          edge.UpdateTo(frame_state);
140 141 142 143 144 145 146
        } else {
          UNREACHABLE();
        }
        break;
    }
  }

147 148 149 150
  if (exception_target != nullptr) {
    // Link uncaught calls in the inlinee to {exception_target}
    int subcall_count = static_cast<int>(uncaught_subcalls.size());
    if (subcall_count > 0) {
151 152 153
      TRACE("Inlinee contains " << subcall_count
                                << " calls without local exception handler; "
                                << "linking to surrounding exception handler.");
154 155 156
    }
    NodeVector on_exception_nodes(local_zone_);
    for (Node* subcall : uncaught_subcalls) {
157 158 159
      Node* on_success = graph()->NewNode(common()->IfSuccess(), subcall);
      NodeProperties::ReplaceUses(subcall, subcall, subcall, on_success);
      NodeProperties::ReplaceControlInput(on_success, subcall);
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
      Node* on_exception =
          graph()->NewNode(common()->IfException(), subcall, subcall);
      on_exception_nodes.push_back(on_exception);
    }

    DCHECK_EQ(subcall_count, static_cast<int>(on_exception_nodes.size()));
    if (subcall_count > 0) {
      Node* control_output =
          graph()->NewNode(common()->Merge(subcall_count), subcall_count,
                           &on_exception_nodes.front());
      NodeVector values_effects(local_zone_);
      values_effects = on_exception_nodes;
      values_effects.push_back(control_output);
      Node* value_output = graph()->NewNode(
          common()->Phi(MachineRepresentation::kTagged, subcall_count),
          subcall_count + 1, &values_effects.front());
      Node* effect_output =
          graph()->NewNode(common()->EffectPhi(subcall_count),
                           subcall_count + 1, &values_effects.front());
      ReplaceWithValue(exception_target, value_output, effect_output,
                       control_output);
    } else {
      ReplaceWithValue(exception_target, exception_target, exception_target,
183
                       jsgraph()->Dead());
184 185 186
    }
  }

187 188 189 190 191 192
  NodeVector values(local_zone_);
  NodeVector effects(local_zone_);
  NodeVector controls(local_zone_);
  for (Node* const input : end->inputs()) {
    switch (input->opcode()) {
      case IrOpcode::kReturn:
193
        values.push_back(NodeProperties::GetValueInput(input, 1));
194 195 196
        effects.push_back(NodeProperties::GetEffectInput(input));
        controls.push_back(NodeProperties::GetControlInput(input));
        break;
197 198 199
      case IrOpcode::kDeoptimize:
      case IrOpcode::kTerminate:
      case IrOpcode::kThrow:
200 201
        NodeProperties::MergeControlToEnd(graph(), common(), input);
        Revisit(graph()->end());
202
        break;
203 204 205
      default:
        UNREACHABLE();
        break;
206 207
    }
  }
208 209
  DCHECK_EQ(values.size(), effects.size());
  DCHECK_EQ(values.size(), controls.size());
210 211 212 213 214

  // Depending on whether the inlinee produces a value, we either replace value
  // uses with said value or kill value uses if no value can be returned.
  if (values.size() > 0) {
    int const input_count = static_cast<int>(controls.size());
215 216
    Node* control_output = graph()->NewNode(common()->Merge(input_count),
                                            input_count, &controls.front());
217 218
    values.push_back(control_output);
    effects.push_back(control_output);
219 220
    Node* value_output = graph()->NewNode(
        common()->Phi(MachineRepresentation::kTagged, input_count),
221
        static_cast<int>(values.size()), &values.front());
222 223 224
    Node* effect_output =
        graph()->NewNode(common()->EffectPhi(input_count),
                         static_cast<int>(effects.size()), &effects.front());
225 226 227
    ReplaceWithValue(call, value_output, effect_output, control_output);
    return Changed(value_output);
  } else {
228 229
    ReplaceWithValue(call, jsgraph()->Dead(), jsgraph()->Dead(),
                     jsgraph()->Dead());
230 231
    return Changed(call);
  }
232 233
}

234 235
Node* JSInliner::CreateArtificialFrameState(Node* node, Node* outer_frame_state,
                                            int parameter_count,
236
                                            BailoutId bailout_id,
237
                                            FrameStateType frame_state_type,
238
                                            SharedFunctionInfoRef shared,
239
                                            Node* context) {
240
  const int parameter_count_with_receiver =
241
      parameter_count + JSCallOrConstructNode::kReceiverOrNewTargetInputCount;
242
  const FrameStateFunctionInfo* state_info =
243
      common()->CreateFrameStateFunctionInfo(
244
          frame_state_type, parameter_count_with_receiver, 0, shared.object());
245

246
  const Operator* op = common()->FrameState(
247
      bailout_id, OutputFrameStateCombine::Ignore(), state_info);
248
  const Operator* op0 = common()->StateValues(0, SparseInputMask::Dense());
249
  Node* node0 = graph()->NewNode(op0);
250

251
  NodeVector params(local_zone_);
252 253
  params.push_back(
      node->InputAt(JSCallOrConstructNode::ReceiverOrNewTargetIndex()));
254
  for (int i = 0; i < parameter_count; i++) {
255
    params.push_back(node->InputAt(JSCallOrConstructNode::ArgumentIndex(i)));
256
  }
257 258
  const Operator* op_param = common()->StateValues(
      static_cast<int>(params.size()), SparseInputMask::Dense());
259
  Node* params_node = graph()->NewNode(
260
      op_param, static_cast<int>(params.size()), &params.front());
261
  if (context == nullptr) context = jsgraph()->UndefinedConstant();
262
  return graph()->NewNode(op, params_node, node0, node0, context,
263
                          node->InputAt(JSCallOrConstructNode::TargetIndex()),
264
                          outer_frame_state);
265 266
}

267 268
namespace {

269
bool NeedsImplicitReceiver(SharedFunctionInfoRef shared_info) {
270
  DisallowHeapAllocation no_gc;
271 272
  return !shared_info.construct_as_builtin() &&
         !IsDerivedConstructor(shared_info.kind());
273 274
}

275 276
}  // namespace

277 278 279
// Determines whether the call target of the given call {node} is statically
// known and can be used as an inlining candidate. The {SharedFunctionInfo} of
// the call target is provided (the exact closure might be unknown).
280 281
base::Optional<SharedFunctionInfoRef> JSInliner::DetermineCallTarget(
    Node* node) {
282
  DCHECK(IrOpcode::IsInlineeOpcode(node->opcode()));
283 284
  Node* target = node->InputAt(JSCallOrConstructNode::TargetIndex());
  HeapObjectMatcher match(target);
285

286 287
  // This reducer can handle both normal function calls as well a constructor
  // calls whenever the target is a constant function object, as follows:
288 289
  //  - JSCall(target:constant, receiver, args..., vector)
  //  - JSConstruct(target:constant, new.target, args..., vector)
290 291
  if (match.HasValue() && match.Ref(broker()).IsJSFunction()) {
    JSFunctionRef function = match.Ref(broker()).AsJSFunction();
292 293 294 295 296

    // The function might have not been called yet.
    if (!function.has_feedback_vector()) {
      return base::nullopt;
    }
297

298 299 300 301 302 303 304 305
    // Disallow cross native-context inlining for now. This means that all parts
    // of the resulting code will operate on the same global object. This also
    // prevents cross context leaks, where we could inline functions from a
    // different context and hold on to that context (and closure) from the code
    // object.
    // TODO(turbofan): We might want to revisit this restriction later when we
    // have a need for this, and we know how to model different native contexts
    // in the same graph in a compositional way.
306
    if (!function.native_context().equals(broker()->target_native_context())) {
307
      return base::nullopt;
308 309
    }

310
    return function.shared();
311 312 313 314
  }

  // This reducer can also handle calls where the target is statically known to
  // be the result of a closure instantiation operation, as follows:
315 316 317
  //  - JSCall(JSCreateClosure[shared](context), receiver, args..., vector)
  //  - JSConstruct(JSCreateClosure[shared](context),
  //                new.target, args..., vector)
318
  if (match.IsJSCreateClosure()) {
319 320
    JSCreateClosureNode n(target);
    FeedbackCellRef cell = n.GetFeedbackCellRefChecked(broker());
321 322 323 324
    return cell.shared_function_info();
  } else if (match.IsCheckClosure()) {
    FeedbackCellRef cell(broker(), FeedbackCellOf(match.op()));
    return cell.shared_function_info();
325 326
  }

327
  return base::nullopt;
328 329 330 331 332 333 334
}

// Determines statically known information about the call target (assuming that
// the call target is known according to {DetermineCallTarget} above). The
// following static information is provided:
//  - context         : The context (as SSA value) bound by the call target.
//  - feedback_vector : The target is guaranteed to use this feedback vector.
335
FeedbackVectorRef JSInliner::DetermineCallContext(Node* node,
336
                                                  Node** context_out) {
337
  DCHECK(IrOpcode::IsInlineeOpcode(node->opcode()));
338 339
  Node* target = node->InputAt(JSCallOrConstructNode::TargetIndex());
  HeapObjectMatcher match(target);
340

341 342
  if (match.HasValue() && match.Ref(broker()).IsJSFunction()) {
    JSFunctionRef function = match.Ref(broker()).AsJSFunction();
343
    // This was already ensured by DetermineCallTarget
344
    CHECK(function.has_feedback_vector());
345 346

    // The inlinee specializes to the context from the JSFunction object.
347
    *context_out = jsgraph()->Constant(function.context());
348
    return function.feedback_vector();
349 350 351 352 353
  }

  if (match.IsJSCreateClosure()) {
    // Load the feedback vector of the target by looking up its vector cell at
    // the instantiation site (we only decide to inline if it's populated).
354 355
    JSCreateClosureNode n(target);
    FeedbackCellRef cell = n.GetFeedbackCellRefChecked(broker());
356 357

    // The inlinee uses the locally provided context at instantiation.
358
    *context_out = NodeProperties::GetContextInput(match.node());
359 360 361 362 363 364 365 366 367 368 369
    return cell.value().AsFeedbackVector();
  } else if (match.IsCheckClosure()) {
    FeedbackCellRef cell(broker(), FeedbackCellOf(match.op()));

    Node* effect = NodeProperties::GetEffectInput(node);
    Node* control = NodeProperties::GetControlInput(node);
    *context_out = effect = graph()->NewNode(
        simplified()->LoadField(AccessBuilder::ForJSFunctionContext()),
        match.node(), effect, control);
    NodeProperties::ReplaceEffectInput(node, effect);

370
    return cell.value().AsFeedbackVector();
371 372 373 374
  }

  // Must succeed.
  UNREACHABLE();
375 376
}

377
Reduction JSInliner::ReduceJSCall(Node* node) {
378 379
  DCHECK(IrOpcode::IsInlineeOpcode(node->opcode()));
  JSCallAccessor call(node);
380 381

  // Determine the call target.
382 383
  base::Optional<SharedFunctionInfoRef> shared_info(DetermineCallTarget(node));
  if (!shared_info.has_value()) return NoChange();
384
  DCHECK(shared_info->IsInlineable());
385

386 387
  SharedFunctionInfoRef outer_shared_info(broker(), info_->shared_info());

388
  // Constructor must be constructable.
389
  if (node->opcode() == IrOpcode::kJSConstruct &&
390
      !IsConstructable(shared_info->kind())) {
391
    TRACE("Not inlining " << *shared_info << " into " << outer_shared_info
392
                          << " because constructor is not constructable.");
393
    return NoChange();
394 395
  }

396 397
  // Class constructors are callable, but [[Call]] will raise an exception.
  // See ES6 section 9.2.1 [[Call]] ( thisArgument, argumentsList ).
398
  if (node->opcode() == IrOpcode::kJSCall &&
399
      IsClassConstructor(shared_info->kind())) {
400
    TRACE("Not inlining " << *shared_info << " into " << outer_shared_info
401
                          << " because callee is a class constructor.");
402 403 404
    return NoChange();
  }

405 406 407
  // To ensure inlining always terminates, we have an upper limit on inlining
  // the nested calls.
  int nesting_level = 0;
408
  for (Node* frame_state = call.frame_state();
409 410
       frame_state->opcode() == IrOpcode::kFrameState;
       frame_state = frame_state->InputAt(kFrameStateOuterStateInput)) {
411 412
    nesting_level++;
    if (nesting_level > kMaxDepthForInlining) {
413
      TRACE("Not inlining "
414
            << *shared_info << " into " << outer_shared_info
415 416
            << " because call has exceeded the maximum depth for function "
               "inlining.");
417 418 419 420
      return NoChange();
    }
  }

421
  Node* exception_target = nullptr;
422
  NodeProperties::IsExceptionalCall(node, &exception_target);
423

424
  // JSInliningHeuristic has already filtered candidates without a BytecodeArray
425 426 427 428
  // based on SharedFunctionInfoRef::GetInlineability. For the inlineable ones
  // (kIsInlineable), the broker holds a reference to the bytecode array, which
  // prevents it from getting flushed.  Therefore, the following check should
  // always hold true.
429
  CHECK(shared_info->is_compiled());
430

431
  if (!broker()->is_concurrent_inlining() && info_->source_positions()) {
432 433
    SharedFunctionInfo::EnsureSourcePositionsAvailable(isolate(),
                                                       shared_info->object());
434 435
  }

436
  TRACE("Inlining " << *shared_info << " into " << outer_shared_info
437 438
                    << ((exception_target != nullptr) ? " (inside try-block)"
                                                      : ""));
439
  // Determine the target's feedback vector and its context.
440
  Node* context;
441
  FeedbackVectorRef feedback_vector = DetermineCallContext(node, &context);
442
  CHECK(broker()->IsSerializedForCompilation(*shared_info, feedback_vector));
443

444 445 446 447
  // ----------------------------------------------------------------
  // After this point, we've made a decision to inline this function.
  // We shall not bailout from inlining if we got here.

448
  BytecodeArrayRef bytecode_array = shared_info->GetBytecodeArray();
449

450
  // Remember that we inlined this function.
451 452 453
  int inlining_id =
      info_->AddInlinedFunction(shared_info->object(), bytecode_array.object(),
                                source_positions_->GetSourcePosition(node));
454

455 456 457
  // Create the subgraph for the inlinee.
  Node* start;
  Node* end;
458
  {
459 460
    // Run the BytecodeGraphBuilder to create the subgraph.
    Graph::SubgraphScope scope(graph());
461 462
    BytecodeGraphBuilderFlags flags(
        BytecodeGraphBuilderFlag::kSkipFirstStackCheck);
463
    if (info_->analyze_environment_liveness()) {
464 465
      flags |= BytecodeGraphBuilderFlag::kAnalyzeEnvironmentLiveness;
    }
466
    if (info_->bailout_on_uninitialized()) {
467
      flags |= BytecodeGraphBuilderFlag::kBailoutOnUninitialized;
468
    }
469
    {
470
      CallFrequency frequency = call.frequency();
471 472
      BuildGraphFromBytecode(broker(), zone(), *shared_info, feedback_vector,
                             BailoutId::None(), jsgraph(), frequency,
473 474
                             source_positions_, inlining_id, info_->code_kind(),
                             flags, &info_->tick_counter());
475
    }
476

477 478 479 480 481
    // Extract the inlinee start/end nodes.
    start = graph()->start();
    end = graph()->end();
  }

482 483 484
  // If we are inlining into a surrounding exception handler, we collect all
  // potentially throwing nodes within the inlinee that are not handled locally
  // by the inlinee itself. They are later wired into the surrounding handler.
485
  NodeVector uncaught_subcalls(local_zone_);
486 487 488 489
  if (exception_target != nullptr) {
    // Find all uncaught 'calls' in the inlinee.
    AllNodes inlined_nodes(local_zone_, end, graph());
    for (Node* subnode : inlined_nodes.reachable) {
490 491 492 493
      // Every possibly throwing node should get {IfSuccess} and {IfException}
      // projections, unless there already is local exception handling.
      if (subnode->op()->HasProperty(Operator::kNoThrow)) continue;
      if (!NodeProperties::IsExceptionalCall(subnode)) {
494 495 496 497 498 499
        DCHECK_EQ(2, subnode->op()->ControlOutputCount());
        uncaught_subcalls.push_back(subnode);
      }
    }
  }

500
  Node* frame_state = call.frame_state();
501
  Node* new_target = jsgraph()->UndefinedConstant();
502

503 504
  // Inline {JSConstruct} requires some additional magic.
  if (node->opcode() == IrOpcode::kJSConstruct) {
505
    STATIC_ASSERT(JSCallOrConstructNode::kHaveIdenticalLayouts);
506
    JSConstructNode n(node);
507 508

    new_target = n.new_target();
509

510 511 512 513
    // Insert nodes around the call that model the behavior required for a
    // constructor dispatch (allocate implicit receiver and check return value).
    // This models the behavior usually accomplished by our {JSConstructStub}.
    // Note that the context has to be the callers context (input to call node).
514 515 516 517
    // Also note that by splitting off the {JSCreate} piece of the constructor
    // call, we create an observable deoptimization point after the receiver
    // instantiation but before the invocation (i.e. inside {JSConstructStub}
    // where execution continues at {construct_stub_create_deopt_pc_offset}).
518
    Node* receiver = jsgraph()->TheHoleConstant();  // Implicit receiver.
519
    Node* context = NodeProperties::GetContextInput(node);
520
    if (NeedsImplicitReceiver(*shared_info)) {
521 522
      Effect effect = n.effect();
      Control control = n.control();
523
      Node* frame_state_inside = CreateArtificialFrameState(
524
          node, frame_state, n.ArgumentCount(),
525
          BailoutId::ConstructStubCreate(), FrameStateType::kConstructStub,
526
          *shared_info, context);
527 528 529
      Node* create =
          graph()->NewNode(javascript()->Create(), call.target(), new_target,
                           context, frame_state_inside, effect, control);
530 531
      uncaught_subcalls.push_back(create);  // Adds {IfSuccess} & {IfException}.
      NodeProperties::ReplaceControlInput(node, create);
532
      NodeProperties::ReplaceEffectInput(node, create);
533 534 535 536 537
      // Placeholder to hold {node}'s value dependencies while {node} is
      // replaced.
      Node* dummy = graph()->NewNode(common()->Dead());
      NodeProperties::ReplaceUses(node, dummy, node, node, node);
      Node* result;
538 539 540 541 542 543 544
      // Insert a check of the return value to determine whether the return
      // value or the implicit receiver should be selected as a result of the
      // call.
      Node* check = graph()->NewNode(simplified()->ObjectIsReceiver(), node);
      result =
          graph()->NewNode(common()->Select(MachineRepresentation::kTagged),
                           check, node, create);
545
      receiver = create;  // The implicit receiver.
546
      ReplaceWithValue(dummy, result);
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
    } else if (IsDerivedConstructor(shared_info->kind())) {
      Node* node_success =
          NodeProperties::FindSuccessfulControlProjection(node);
      Node* is_receiver =
          graph()->NewNode(simplified()->ObjectIsReceiver(), node);
      Node* branch_is_receiver =
          graph()->NewNode(common()->Branch(), is_receiver, node_success);
      Node* branch_is_receiver_true =
          graph()->NewNode(common()->IfTrue(), branch_is_receiver);
      Node* branch_is_receiver_false =
          graph()->NewNode(common()->IfFalse(), branch_is_receiver);
      branch_is_receiver_false =
          graph()->NewNode(javascript()->CallRuntime(
                               Runtime::kThrowConstructorReturnedNonObject),
                           context, NodeProperties::GetFrameStateInput(node),
                           node, branch_is_receiver_false);
      uncaught_subcalls.push_back(branch_is_receiver_false);
      branch_is_receiver_false =
          graph()->NewNode(common()->Throw(), branch_is_receiver_false,
                           branch_is_receiver_false);
      NodeProperties::MergeControlToEnd(graph(), common(),
                                        branch_is_receiver_false);

570 571 572
      ReplaceWithValue(node_success, node_success, node_success,
                       branch_is_receiver_true);
      // Fix input destroyed by the above {ReplaceWithValue} call.
573
      NodeProperties::ReplaceControlInput(branch_is_receiver, node_success, 0);
574
    }
575
    node->ReplaceInput(JSCallNode::ReceiverIndex(), receiver);
576 577
    // Insert a construct stub frame into the chain of frame states. This will
    // reconstruct the proper frame when deoptimizing within the constructor.
578
    frame_state = CreateArtificialFrameState(
579 580
        node, frame_state, n.ArgumentCount(), BailoutId::ConstructStubInvoke(),
        FrameStateType::kConstructStub, *shared_info, context);
581 582
  }

583
  // Insert a JSConvertReceiver node for sloppy callees. Note that the context
584
  // passed into this node has to be the callees context (loaded above).
585
  if (node->opcode() == IrOpcode::kJSCall &&
586
      is_sloppy(shared_info->language_mode()) && !shared_info->native()) {
587
    Node* effect = NodeProperties::GetEffectInput(node);
588
    if (NodeProperties::CanBePrimitive(broker(), call.receiver(), effect)) {
589
      CallParameters const& p = CallParametersOf(node->op());
590 591
      Node* global_proxy = jsgraph()->Constant(
          broker()->target_native_context().global_proxy_object());
592 593 594
      Node* receiver = effect =
          graph()->NewNode(simplified()->ConvertReceiver(p.convert_mode()),
                           call.receiver(), global_proxy, effect, start);
595 596
      NodeProperties::ReplaceValueInput(node, receiver,
                                        JSCallNode::ReceiverIndex());
597 598
      NodeProperties::ReplaceEffectInput(node, effect);
    }
599 600
  }

601
  // Insert argument adaptor frame if required. The callees formal parameter
602 603 604
  // count (i.e. value outputs of start node minus target, receiver, new target,
  // arguments count and context) have to match the number of arguments passed
  // to the call.
605
  int parameter_count = shared_info->internal_formal_parameter_count();
606
  DCHECK_EQ(parameter_count, start->op()->ValueOutputCount() - 5);
607
  if (call.argument_count() != parameter_count) {
608
    frame_state = CreateArtificialFrameState(
609
        node, frame_state, call.argument_count(), BailoutId::None(),
610
        FrameStateType::kArgumentsAdaptor, *shared_info);
611 612
  }

613 614
  return InlineCall(node, new_target, context, frame_state, start, end,
                    exception_target, uncaught_subcalls);
615
}
616

617 618
Graph* JSInliner::graph() const { return jsgraph()->graph(); }

619 620 621 622 623 624
JSOperatorBuilder* JSInliner::javascript() const {
  return jsgraph()->javascript();
}

CommonOperatorBuilder* JSInliner::common() const { return jsgraph()->common(); }

625 626 627 628
SimplifiedOperatorBuilder* JSInliner::simplified() const {
  return jsgraph()->simplified();
}

629 630
#undef TRACE

631 632 633
}  // namespace compiler
}  // namespace internal
}  // namespace v8