2014-08-20 13:05:03 +00:00
|
|
|
// 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.
|
|
|
|
|
2015-02-20 10:11:45 +00:00
|
|
|
#include "src/compiler/js-inlining.h"
|
|
|
|
|
2015-11-26 16:22:34 +00:00
|
|
|
#include "src/ast/ast-numbering.h"
|
2016-05-24 12:28:55 +00:00
|
|
|
#include "src/ast/ast.h"
|
2015-11-26 16:22:34 +00:00
|
|
|
#include "src/ast/scopes.h"
|
2015-09-01 10:30:40 +00:00
|
|
|
#include "src/compiler.h"
|
2014-08-20 13:05:03 +00:00
|
|
|
#include "src/compiler/ast-graph-builder.h"
|
2016-05-24 12:28:55 +00:00
|
|
|
#include "src/compiler/ast-loop-assignment-analyzer.h"
|
2014-08-20 13:05:03 +00:00
|
|
|
#include "src/compiler/common-operator.h"
|
2015-10-14 12:20:39 +00:00
|
|
|
#include "src/compiler/graph-reducer.h"
|
2014-08-20 13:05:03 +00:00
|
|
|
#include "src/compiler/js-operator.h"
|
|
|
|
#include "src/compiler/node-matchers.h"
|
2015-01-29 09:17:45 +00:00
|
|
|
#include "src/compiler/node-properties.h"
|
2015-03-09 08:37:01 +00:00
|
|
|
#include "src/compiler/operator-properties.h"
|
2016-05-24 12:28:55 +00:00
|
|
|
#include "src/compiler/type-hint-analyzer.h"
|
2015-09-09 10:24:17 +00:00
|
|
|
#include "src/isolate-inl.h"
|
2015-11-26 16:22:34 +00:00
|
|
|
#include "src/parsing/parser.h"
|
|
|
|
#include "src/parsing/rewriter.h"
|
2014-08-20 13:05:03 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace compiler {
|
|
|
|
|
2015-03-17 18:51:08 +00:00
|
|
|
#define TRACE(...) \
|
|
|
|
do { \
|
|
|
|
if (FLAG_trace_turbo_inlining) PrintF(__VA_ARGS__); \
|
|
|
|
} while (false)
|
|
|
|
|
2015-02-17 10:30:54 +00:00
|
|
|
|
2015-11-12 08:50:51 +00:00
|
|
|
// Provides convenience accessors for the common layout of nodes having either
|
|
|
|
// the {JSCallFunction} or the {JSCallConstruct} operator.
|
|
|
|
class JSCallAccessor {
|
2014-08-20 13:05:03 +00:00
|
|
|
public:
|
2015-11-12 08:50:51 +00:00
|
|
|
explicit JSCallAccessor(Node* call) : call_(call) {
|
|
|
|
DCHECK(call->opcode() == IrOpcode::kJSCallFunction ||
|
|
|
|
call->opcode() == IrOpcode::kJSCallConstruct);
|
2015-02-17 10:30:54 +00:00
|
|
|
}
|
2014-08-20 13:05:03 +00:00
|
|
|
|
2015-11-12 08:50:51 +00:00
|
|
|
Node* target() {
|
|
|
|
// Both, {JSCallFunction} and {JSCallConstruct}, have same layout here.
|
|
|
|
return call_->InputAt(0);
|
|
|
|
}
|
2015-02-17 10:30:54 +00:00
|
|
|
|
2015-11-12 08:50:51 +00:00
|
|
|
Node* receiver() {
|
|
|
|
DCHECK_EQ(IrOpcode::kJSCallFunction, call_->opcode());
|
|
|
|
return call_->InputAt(1);
|
2014-08-20 13:05:03 +00:00
|
|
|
}
|
|
|
|
|
2015-11-13 11:53:16 +00:00
|
|
|
Node* new_target() {
|
2015-11-12 08:50:51 +00:00
|
|
|
DCHECK_EQ(IrOpcode::kJSCallConstruct, call_->opcode());
|
|
|
|
return call_->InputAt(formal_arguments() + 1);
|
2015-02-17 10:30:54 +00:00
|
|
|
}
|
|
|
|
|
2016-06-02 08:02:28 +00:00
|
|
|
Node* frame_state() {
|
|
|
|
// Both, {JSCallFunction} and {JSCallConstruct}, have frame state.
|
2016-08-03 10:38:25 +00:00
|
|
|
return NodeProperties::GetFrameStateInput(call_);
|
2015-10-30 06:58:56 +00:00
|
|
|
}
|
2015-02-17 10:30:54 +00:00
|
|
|
|
2015-11-12 08:50:51 +00:00
|
|
|
int formal_arguments() {
|
|
|
|
// Both, {JSCallFunction} and {JSCallConstruct}, have two extra inputs:
|
2015-11-13 11:53:16 +00:00
|
|
|
// - JSCallConstruct: Includes target function and new target.
|
2015-11-12 08:50:51 +00:00
|
|
|
// - JSCallFunction: Includes target function and receiver.
|
|
|
|
return call_->op()->ValueInputCount() - 2;
|
|
|
|
}
|
|
|
|
|
2014-08-20 13:05:03 +00:00
|
|
|
private:
|
2015-02-17 10:30:54 +00:00
|
|
|
Node* call_;
|
2014-08-20 13:05:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-11-19 12:47:29 +00:00
|
|
|
Reduction JSInliner::InlineCall(Node* call, Node* new_target, Node* context,
|
|
|
|
Node* frame_state, Node* start, Node* end) {
|
2014-08-28 12:18:25 +00:00
|
|
|
// The scheduler is smart enough to place our code; we just ensure {control}
|
2015-03-09 13:01:30 +00:00
|
|
|
// becomes the control input of the start of the inlinee, and {effect} becomes
|
|
|
|
// the effect input of the start of the inlinee.
|
2014-08-20 13:05:03 +00:00
|
|
|
Node* control = NodeProperties::GetControlInput(call);
|
2015-03-09 13:01:30 +00:00
|
|
|
Node* effect = NodeProperties::GetEffectInput(call);
|
2014-08-20 13:05:03 +00:00
|
|
|
|
2015-11-19 12:47:29 +00:00
|
|
|
int const inlinee_new_target_index =
|
|
|
|
static_cast<int>(start->op()->ValueOutputCount()) - 3;
|
2015-11-04 14:08:46 +00:00
|
|
|
int const inlinee_arity_index =
|
|
|
|
static_cast<int>(start->op()->ValueOutputCount()) - 2;
|
2015-05-21 13:02:18 +00:00
|
|
|
int const inlinee_context_index =
|
|
|
|
static_cast<int>(start->op()->ValueOutputCount()) - 1;
|
|
|
|
|
2015-11-19 12:47:29 +00:00
|
|
|
// {inliner_inputs} counts JSFunction, receiver, arguments, but not
|
|
|
|
// new target value, argument count, context, effect or control.
|
2014-10-29 18:46:44 +00:00
|
|
|
int inliner_inputs = call->op()->ValueInputCount();
|
2014-08-20 13:05:03 +00:00
|
|
|
// Iterate over all uses of the start node.
|
2015-05-21 13:02:18 +00:00
|
|
|
for (Edge edge : start->use_edges()) {
|
2014-12-02 14:38:55 +00:00
|
|
|
Node* use = edge.from();
|
2014-08-20 13:05:03 +00:00
|
|
|
switch (use->opcode()) {
|
|
|
|
case IrOpcode::kParameter: {
|
2015-04-22 11:34:43 +00:00
|
|
|
int index = 1 + ParameterIndexOf(use->op());
|
2015-09-16 13:04:25 +00:00
|
|
|
DCHECK_LE(index, inlinee_context_index);
|
2015-11-19 12:47:29 +00:00
|
|
|
if (index < inliner_inputs && index < inlinee_new_target_index) {
|
2014-08-20 13:05:03 +00:00
|
|
|
// There is an input from the call, and the index is a value
|
|
|
|
// projection but not the context, so rewire the input.
|
2015-07-06 12:56:05 +00:00
|
|
|
Replace(use, call->InputAt(index));
|
2015-11-19 12:47:29 +00:00
|
|
|
} else if (index == inlinee_new_target_index) {
|
|
|
|
// The projection is requesting the new target value.
|
|
|
|
Replace(use, new_target);
|
2015-11-04 14:08:46 +00:00
|
|
|
} else if (index == inlinee_arity_index) {
|
|
|
|
// The projection is requesting the number of arguments.
|
2016-08-05 09:53:06 +00:00
|
|
|
Replace(use, jsgraph()->Int32Constant(inliner_inputs - 2));
|
2014-08-20 13:05:03 +00:00
|
|
|
} else if (index == inlinee_context_index) {
|
2015-09-16 13:04:25 +00:00
|
|
|
// The projection is requesting the inlinee function context.
|
2015-07-06 12:56:05 +00:00
|
|
|
Replace(use, context);
|
2015-09-16 13:04:25 +00:00
|
|
|
} else {
|
2014-08-20 13:05:03 +00:00
|
|
|
// Call has fewer arguments than required, fill with undefined.
|
2016-08-05 09:53:06 +00:00
|
|
|
Replace(use, jsgraph()->UndefinedConstant());
|
2014-08-20 13:05:03 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2014-12-02 14:38:55 +00:00
|
|
|
if (NodeProperties::IsEffectEdge(edge)) {
|
2015-03-09 13:01:30 +00:00
|
|
|
edge.UpdateTo(effect);
|
2014-12-02 14:38:55 +00:00
|
|
|
} else if (NodeProperties::IsControlEdge(edge)) {
|
|
|
|
edge.UpdateTo(control);
|
2015-05-27 11:01:51 +00:00
|
|
|
} else if (NodeProperties::IsFrameStateEdge(edge)) {
|
|
|
|
edge.UpdateTo(frame_state);
|
2014-08-20 13:05:03 +00:00
|
|
|
} else {
|
|
|
|
UNREACHABLE();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-26 10:31:55 +00:00
|
|
|
NodeVector values(local_zone_);
|
|
|
|
NodeVector effects(local_zone_);
|
|
|
|
NodeVector controls(local_zone_);
|
|
|
|
for (Node* const input : end->inputs()) {
|
|
|
|
switch (input->opcode()) {
|
|
|
|
case IrOpcode::kReturn:
|
|
|
|
values.push_back(NodeProperties::GetValueInput(input, 0));
|
|
|
|
effects.push_back(NodeProperties::GetEffectInput(input));
|
|
|
|
controls.push_back(NodeProperties::GetControlInput(input));
|
|
|
|
break;
|
2015-05-26 12:17:57 +00:00
|
|
|
case IrOpcode::kDeoptimize:
|
|
|
|
case IrOpcode::kTerminate:
|
|
|
|
case IrOpcode::kThrow:
|
2016-08-05 09:53:06 +00:00
|
|
|
NodeProperties::MergeControlToEnd(graph(), common(), input);
|
|
|
|
Revisit(graph()->end());
|
2015-05-26 12:17:57 +00:00
|
|
|
break;
|
2015-05-26 10:31:55 +00:00
|
|
|
default:
|
|
|
|
UNREACHABLE();
|
|
|
|
break;
|
2015-05-21 13:02:18 +00:00
|
|
|
}
|
|
|
|
}
|
2015-05-26 10:31:55 +00:00
|
|
|
DCHECK_EQ(values.size(), effects.size());
|
|
|
|
DCHECK_EQ(values.size(), controls.size());
|
2015-09-15 11:19:02 +00:00
|
|
|
|
|
|
|
// 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());
|
2016-08-05 09:53:06 +00:00
|
|
|
Node* control_output = graph()->NewNode(common()->Merge(input_count),
|
|
|
|
input_count, &controls.front());
|
2015-09-15 11:19:02 +00:00
|
|
|
values.push_back(control_output);
|
|
|
|
effects.push_back(control_output);
|
2016-08-05 09:53:06 +00:00
|
|
|
Node* value_output = graph()->NewNode(
|
|
|
|
common()->Phi(MachineRepresentation::kTagged, input_count),
|
2015-09-15 11:19:02 +00:00
|
|
|
static_cast<int>(values.size()), &values.front());
|
2016-08-05 09:53:06 +00:00
|
|
|
Node* effect_output =
|
|
|
|
graph()->NewNode(common()->EffectPhi(input_count),
|
|
|
|
static_cast<int>(effects.size()), &effects.front());
|
2015-09-15 11:19:02 +00:00
|
|
|
ReplaceWithValue(call, value_output, effect_output, control_output);
|
|
|
|
return Changed(value_output);
|
|
|
|
} else {
|
2016-08-05 09:53:06 +00:00
|
|
|
ReplaceWithValue(call, call, call, jsgraph()->Dead());
|
2015-09-15 11:19:02 +00:00
|
|
|
return Changed(call);
|
|
|
|
}
|
2014-08-20 13:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-12 08:50:51 +00:00
|
|
|
Node* JSInliner::CreateArtificialFrameState(Node* node, Node* outer_frame_state,
|
|
|
|
int parameter_count,
|
|
|
|
FrameStateType frame_state_type,
|
|
|
|
Handle<SharedFunctionInfo> shared) {
|
2015-06-23 07:17:07 +00:00
|
|
|
const FrameStateFunctionInfo* state_info =
|
2016-08-05 09:53:06 +00:00
|
|
|
common()->CreateFrameStateFunctionInfo(frame_state_type,
|
|
|
|
parameter_count + 1, 0, shared);
|
2015-06-23 07:17:07 +00:00
|
|
|
|
2016-08-05 09:53:06 +00:00
|
|
|
const Operator* op = common()->FrameState(
|
2015-06-23 07:17:07 +00:00
|
|
|
BailoutId(-1), OutputFrameStateCombine::Ignore(), state_info);
|
2016-08-05 09:53:06 +00:00
|
|
|
const Operator* op0 = common()->StateValues(0);
|
|
|
|
Node* node0 = graph()->NewNode(op0);
|
2015-10-22 12:21:39 +00:00
|
|
|
NodeVector params(local_zone_);
|
2015-11-12 08:50:51 +00:00
|
|
|
for (int parameter = 0; parameter < parameter_count + 1; ++parameter) {
|
|
|
|
params.push_back(node->InputAt(1 + parameter));
|
2014-09-18 08:56:52 +00:00
|
|
|
}
|
|
|
|
const Operator* op_param =
|
2016-08-05 09:53:06 +00:00
|
|
|
common()->StateValues(static_cast<int>(params.size()));
|
|
|
|
Node* params_node = graph()->NewNode(
|
2014-09-18 08:56:52 +00:00
|
|
|
op_param, static_cast<int>(params.size()), ¶ms.front());
|
2016-08-05 09:53:06 +00:00
|
|
|
return graph()->NewNode(op, params_node, node0, node0,
|
|
|
|
jsgraph()->UndefinedConstant(), node->InputAt(0),
|
|
|
|
outer_frame_state);
|
2014-09-18 08:56:52 +00:00
|
|
|
}
|
|
|
|
|
2016-03-09 11:33:10 +00:00
|
|
|
Node* JSInliner::CreateTailCallerFrameState(Node* node, Node* frame_state) {
|
|
|
|
FrameStateInfo const& frame_info = OpParameter<FrameStateInfo>(frame_state);
|
2016-03-11 11:40:18 +00:00
|
|
|
Handle<SharedFunctionInfo> shared;
|
|
|
|
frame_info.shared_info().ToHandle(&shared);
|
2016-03-09 11:33:10 +00:00
|
|
|
|
|
|
|
Node* function = frame_state->InputAt(kFrameStateFunctionInput);
|
|
|
|
|
|
|
|
// If we are inlining a tail call drop caller's frame state and an
|
|
|
|
// arguments adaptor if it exists.
|
2016-08-03 10:38:25 +00:00
|
|
|
frame_state = NodeProperties::GetFrameStateInput(frame_state);
|
2016-03-09 11:33:10 +00:00
|
|
|
if (frame_state->opcode() == IrOpcode::kFrameState) {
|
2016-03-11 11:40:18 +00:00
|
|
|
FrameStateInfo const& frame_info = OpParameter<FrameStateInfo>(frame_state);
|
|
|
|
if (frame_info.type() == FrameStateType::kArgumentsAdaptor) {
|
2016-08-03 10:38:25 +00:00
|
|
|
frame_state = NodeProperties::GetFrameStateInput(frame_state);
|
2016-03-09 11:33:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const FrameStateFunctionInfo* state_info =
|
2016-08-05 09:53:06 +00:00
|
|
|
common()->CreateFrameStateFunctionInfo(
|
2016-03-09 11:33:10 +00:00
|
|
|
FrameStateType::kTailCallerFunction, 0, 0, shared);
|
|
|
|
|
2016-08-05 09:53:06 +00:00
|
|
|
const Operator* op = common()->FrameState(
|
2016-03-09 11:33:10 +00:00
|
|
|
BailoutId(-1), OutputFrameStateCombine::Ignore(), state_info);
|
2016-08-05 09:53:06 +00:00
|
|
|
const Operator* op0 = common()->StateValues(0);
|
|
|
|
Node* node0 = graph()->NewNode(op0);
|
|
|
|
return graph()->NewNode(op, node0, node0, node0,
|
|
|
|
jsgraph()->UndefinedConstant(), function,
|
|
|
|
frame_state);
|
2016-03-09 11:33:10 +00:00
|
|
|
}
|
2014-08-20 13:05:03 +00:00
|
|
|
|
2015-11-26 13:58:18 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// TODO(mstarzinger,verwaest): Move this predicate onto SharedFunctionInfo?
|
2016-02-19 07:01:51 +00:00
|
|
|
bool NeedsImplicitReceiver(Handle<SharedFunctionInfo> shared_info) {
|
|
|
|
DisallowHeapAllocation no_gc;
|
|
|
|
Isolate* const isolate = shared_info->GetIsolate();
|
|
|
|
Code* const construct_stub = shared_info->construct_stub();
|
2016-03-15 10:59:20 +00:00
|
|
|
return construct_stub != *isolate->builtins()->JSBuiltinsConstructStub() &&
|
|
|
|
construct_stub !=
|
|
|
|
*isolate->builtins()->JSBuiltinsConstructStubForDerived() &&
|
|
|
|
construct_stub != *isolate->builtins()->JSConstructStubApi();
|
2016-02-19 07:01:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IsNonConstructible(Handle<SharedFunctionInfo> shared_info) {
|
|
|
|
DisallowHeapAllocation no_gc;
|
|
|
|
Isolate* const isolate = shared_info->GetIsolate();
|
|
|
|
Code* const construct_stub = shared_info->construct_stub();
|
|
|
|
return construct_stub == *isolate->builtins()->ConstructedNonConstructable();
|
2015-11-26 13:58:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
2015-02-20 10:11:45 +00:00
|
|
|
Reduction JSInliner::Reduce(Node* node) {
|
2015-11-12 08:50:51 +00:00
|
|
|
if (!IrOpcode::IsInlineeOpcode(node->opcode())) return NoChange();
|
2015-02-20 10:11:45 +00:00
|
|
|
|
2015-11-12 08:50:51 +00:00
|
|
|
// This reducer can handle both normal function calls as well a constructor
|
|
|
|
// calls whenever the target is a constant function object, as follows:
|
|
|
|
// - JSCallFunction(target:constant, receiver, args...)
|
|
|
|
// - JSCallConstruct(target:constant, args..., new.target)
|
|
|
|
HeapObjectMatcher match(node->InputAt(0));
|
2015-10-07 12:18:21 +00:00
|
|
|
if (!match.HasValue() || !match.Value()->IsJSFunction()) return NoChange();
|
2015-08-31 08:24:52 +00:00
|
|
|
Handle<JSFunction> function = Handle<JSFunction>::cast(match.Value());
|
2015-10-07 12:18:21 +00:00
|
|
|
|
2015-11-12 08:50:51 +00:00
|
|
|
return ReduceJSCall(node, function);
|
2015-10-07 12:18:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-12 08:50:51 +00:00
|
|
|
Reduction JSInliner::ReduceJSCall(Node* node, Handle<JSFunction> function) {
|
|
|
|
DCHECK(IrOpcode::IsInlineeOpcode(node->opcode()));
|
|
|
|
JSCallAccessor call(node);
|
2016-02-19 07:01:51 +00:00
|
|
|
Handle<SharedFunctionInfo> shared_info(function->shared());
|
2015-02-20 10:11:45 +00:00
|
|
|
|
2015-11-26 13:58:18 +00:00
|
|
|
// Function must be inlineable.
|
2016-02-19 07:01:51 +00:00
|
|
|
if (!shared_info->IsInlineable()) {
|
2015-10-07 11:43:20 +00:00
|
|
|
TRACE("Not inlining %s into %s because callee is not inlineable\n",
|
2016-02-19 07:01:51 +00:00
|
|
|
shared_info->DebugName()->ToCString().get(),
|
2015-10-07 11:43:20 +00:00
|
|
|
info_->shared_info()->DebugName()->ToCString().get());
|
|
|
|
return NoChange();
|
|
|
|
}
|
|
|
|
|
2015-11-26 13:58:18 +00:00
|
|
|
// Constructor must be constructable.
|
2015-11-24 17:16:40 +00:00
|
|
|
if (node->opcode() == IrOpcode::kJSCallConstruct &&
|
2016-02-19 07:01:51 +00:00
|
|
|
IsNonConstructible(shared_info)) {
|
2015-11-26 13:58:18 +00:00
|
|
|
TRACE("Not inlining %s into %s because constructor is not constructable.\n",
|
2016-02-19 07:01:51 +00:00
|
|
|
shared_info->DebugName()->ToCString().get(),
|
2015-11-24 17:16:40 +00:00
|
|
|
info_->shared_info()->DebugName()->ToCString().get());
|
|
|
|
return NoChange();
|
|
|
|
}
|
|
|
|
|
2015-11-04 14:08:46 +00:00
|
|
|
// Class constructors are callable, but [[Call]] will raise an exception.
|
|
|
|
// See ES6 section 9.2.1 [[Call]] ( thisArgument, argumentsList ).
|
2015-11-26 13:58:18 +00:00
|
|
|
if (node->opcode() == IrOpcode::kJSCallFunction &&
|
2016-02-19 07:01:51 +00:00
|
|
|
IsClassConstructor(shared_info->kind())) {
|
2015-11-26 13:58:18 +00:00
|
|
|
TRACE("Not inlining %s into %s because callee is a class constructor.\n",
|
2016-02-19 07:01:51 +00:00
|
|
|
shared_info->DebugName()->ToCString().get(),
|
2015-11-04 14:08:46 +00:00
|
|
|
info_->shared_info()->DebugName()->ToCString().get());
|
|
|
|
return NoChange();
|
|
|
|
}
|
|
|
|
|
2015-11-26 13:58:18 +00:00
|
|
|
// Function contains break points.
|
2016-02-19 07:01:51 +00:00
|
|
|
if (shared_info->HasDebugInfo()) {
|
2015-07-20 14:53:28 +00:00
|
|
|
TRACE("Not inlining %s into %s because callee may contain break points\n",
|
2016-02-19 07:01:51 +00:00
|
|
|
shared_info->DebugName()->ToCString().get(),
|
2015-07-20 14:53:28 +00:00
|
|
|
info_->shared_info()->DebugName()->ToCString().get());
|
|
|
|
return NoChange();
|
|
|
|
}
|
|
|
|
|
2015-06-30 11:38:19 +00:00
|
|
|
// 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 for asm.js code, 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.
|
|
|
|
if (function->context()->native_context() !=
|
|
|
|
info_->context()->native_context()) {
|
|
|
|
TRACE("Not inlining %s into %s because of different native contexts\n",
|
2016-02-19 07:01:51 +00:00
|
|
|
shared_info->DebugName()->ToCString().get(),
|
2015-06-30 11:38:19 +00:00
|
|
|
info_->shared_info()->DebugName()->ToCString().get());
|
|
|
|
return NoChange();
|
|
|
|
}
|
|
|
|
|
2015-06-30 11:05:03 +00:00
|
|
|
// TODO(turbofan): TranslatedState::GetAdaptedArguments() currently relies on
|
|
|
|
// not inlining recursive functions. We might want to relax that at some
|
|
|
|
// point.
|
2016-06-02 08:02:28 +00:00
|
|
|
for (Node* frame_state = call.frame_state();
|
2015-06-30 11:05:03 +00:00
|
|
|
frame_state->opcode() == IrOpcode::kFrameState;
|
|
|
|
frame_state = frame_state->InputAt(kFrameStateOuterStateInput)) {
|
2016-02-19 07:01:51 +00:00
|
|
|
FrameStateInfo const& frame_info = OpParameter<FrameStateInfo>(frame_state);
|
|
|
|
Handle<SharedFunctionInfo> frame_shared_info;
|
|
|
|
if (frame_info.shared_info().ToHandle(&frame_shared_info) &&
|
|
|
|
*frame_shared_info == *shared_info) {
|
2015-06-30 11:05:03 +00:00
|
|
|
TRACE("Not inlining %s into %s because call is recursive\n",
|
2016-02-19 07:01:51 +00:00
|
|
|
shared_info->DebugName()->ToCString().get(),
|
2015-06-30 11:05:03 +00:00
|
|
|
info_->shared_info()->DebugName()->ToCString().get());
|
|
|
|
return NoChange();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-15 12:01:52 +00:00
|
|
|
// TODO(turbofan): Inlining into a try-block is not yet supported.
|
|
|
|
if (NodeProperties::IsExceptionalCall(node)) {
|
|
|
|
TRACE("Not inlining %s into %s because of surrounding try-block\n",
|
2016-02-19 07:01:51 +00:00
|
|
|
shared_info->DebugName()->ToCString().get(),
|
2015-10-15 12:01:52 +00:00
|
|
|
info_->shared_info()->DebugName()->ToCString().get());
|
|
|
|
return NoChange();
|
|
|
|
}
|
|
|
|
|
2016-04-01 10:00:30 +00:00
|
|
|
Zone zone(info_->isolate()->allocator());
|
2015-10-22 12:21:39 +00:00
|
|
|
ParseInfo parse_info(&zone, function);
|
2016-04-08 12:31:38 +00:00
|
|
|
CompilationInfo info(&parse_info, function);
|
2016-02-19 07:01:51 +00:00
|
|
|
if (info_->is_deoptimization_enabled()) info.MarkAsDeoptimizationEnabled();
|
2016-06-02 09:20:50 +00:00
|
|
|
if (info_->is_type_feedback_enabled()) info.MarkAsTypeFeedbackEnabled();
|
2015-02-17 10:30:54 +00:00
|
|
|
|
2015-09-09 10:24:17 +00:00
|
|
|
if (!Compiler::ParseAndAnalyze(info.parse_info())) {
|
|
|
|
TRACE("Not inlining %s into %s because parsing failed\n",
|
2016-02-19 07:01:51 +00:00
|
|
|
shared_info->DebugName()->ToCString().get(),
|
2015-09-09 10:24:17 +00:00
|
|
|
info_->shared_info()->DebugName()->ToCString().get());
|
|
|
|
if (info_->isolate()->has_pending_exception()) {
|
|
|
|
info_->isolate()->clear_pending_exception();
|
|
|
|
}
|
|
|
|
return NoChange();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!Compiler::EnsureDeoptimizationSupport(&info)) {
|
|
|
|
TRACE("Not inlining %s into %s because deoptimization support failed\n",
|
2016-02-19 07:01:51 +00:00
|
|
|
shared_info->DebugName()->ToCString().get(),
|
2015-09-09 10:24:17 +00:00
|
|
|
info_->shared_info()->DebugName()->ToCString().get());
|
|
|
|
return NoChange();
|
|
|
|
}
|
2016-05-27 08:09:12 +00:00
|
|
|
|
2015-10-21 15:05:13 +00:00
|
|
|
// Remember that we inlined this function. This needs to be called right
|
|
|
|
// after we ensure deoptimization support so that the code flusher
|
|
|
|
// does not remove the code with the deoptimization support.
|
2016-02-19 07:01:51 +00:00
|
|
|
info_->AddInlinedFunction(shared_info);
|
2015-10-21 15:05:13 +00:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------
|
|
|
|
// After this point, we've made a decision to inline this function.
|
|
|
|
// We shall not bailout from inlining if we got here.
|
2014-08-20 13:05:03 +00:00
|
|
|
|
2015-03-17 18:51:08 +00:00
|
|
|
TRACE("Inlining %s into %s\n",
|
2016-02-19 07:01:51 +00:00
|
|
|
shared_info->DebugName()->ToCString().get(),
|
2015-03-17 18:51:08 +00:00
|
|
|
info_->shared_info()->DebugName()->ToCString().get());
|
2014-08-20 13:05:03 +00:00
|
|
|
|
2016-05-27 08:09:12 +00:00
|
|
|
// If function was lazily compiled, it's literals array may not yet be set up.
|
|
|
|
JSFunction::EnsureLiterals(function);
|
|
|
|
|
2016-05-25 10:04:35 +00:00
|
|
|
// Create the subgraph for the inlinee.
|
|
|
|
Node* start;
|
|
|
|
Node* end;
|
|
|
|
{
|
|
|
|
// Run the loop assignment analyzer on the inlinee.
|
|
|
|
AstLoopAssignmentAnalyzer loop_assignment_analyzer(&zone, &info);
|
|
|
|
LoopAssignmentAnalysis* loop_assignment =
|
|
|
|
loop_assignment_analyzer.Analyze();
|
|
|
|
|
|
|
|
// Run the type hint analyzer on the inlinee.
|
|
|
|
TypeHintAnalyzer type_hint_analyzer(&zone);
|
|
|
|
TypeHintAnalysis* type_hint_analysis =
|
|
|
|
type_hint_analyzer.Analyze(handle(shared_info->code(), info.isolate()));
|
|
|
|
|
|
|
|
// Run the AstGraphBuilder to create the subgraph.
|
|
|
|
Graph::SubgraphScope scope(graph());
|
|
|
|
AstGraphBuilder graph_builder(&zone, &info, jsgraph(), loop_assignment,
|
|
|
|
type_hint_analysis);
|
|
|
|
graph_builder.CreateGraph(false);
|
|
|
|
|
|
|
|
// Extract the inlinee start/end nodes.
|
|
|
|
start = graph()->start();
|
|
|
|
end = graph()->end();
|
|
|
|
}
|
|
|
|
|
2016-06-02 08:02:28 +00:00
|
|
|
Node* frame_state = call.frame_state();
|
2016-08-05 09:53:06 +00:00
|
|
|
Node* new_target = jsgraph()->UndefinedConstant();
|
2014-09-18 08:56:52 +00:00
|
|
|
|
2016-05-25 10:04:35 +00:00
|
|
|
// Inline {JSCallConstruct} requires some additional magic.
|
2015-11-26 13:58:18 +00:00
|
|
|
if (node->opcode() == IrOpcode::kJSCallConstruct) {
|
2016-05-25 10:04:35 +00:00
|
|
|
// 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).
|
2016-08-05 09:53:06 +00:00
|
|
|
Node* receiver = jsgraph()->UndefinedConstant(); // Implicit receiver.
|
2016-05-25 10:04:35 +00:00
|
|
|
if (NeedsImplicitReceiver(shared_info)) {
|
2016-06-02 08:02:28 +00:00
|
|
|
Node* frame_state_before = NodeProperties::FindFrameStateBefore(node);
|
2016-05-25 10:04:35 +00:00
|
|
|
Node* effect = NodeProperties::GetEffectInput(node);
|
|
|
|
Node* context = NodeProperties::GetContextInput(node);
|
2016-08-05 09:53:06 +00:00
|
|
|
Node* create = graph()->NewNode(javascript()->Create(), call.target(),
|
|
|
|
call.new_target(), context,
|
|
|
|
frame_state_before, effect);
|
2016-05-25 10:04:35 +00:00
|
|
|
NodeProperties::ReplaceEffectInput(node, create);
|
|
|
|
// 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.
|
2016-08-05 09:53:06 +00:00
|
|
|
Node* check = graph()->NewNode(
|
|
|
|
javascript()->CallRuntime(Runtime::kInlineIsJSReceiver, 1), node,
|
|
|
|
context, node, start);
|
|
|
|
Node* select =
|
|
|
|
graph()->NewNode(common()->Select(MachineRepresentation::kTagged),
|
|
|
|
check, node, create);
|
2016-05-25 10:04:35 +00:00
|
|
|
NodeProperties::ReplaceUses(node, select, check, node, node);
|
|
|
|
NodeProperties::ReplaceValueInput(select, node, 1);
|
|
|
|
NodeProperties::ReplaceValueInput(check, node, 0);
|
|
|
|
NodeProperties::ReplaceEffectInput(check, node);
|
|
|
|
receiver = create; // The implicit receiver.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swizzle the inputs of the {JSCallConstruct} node to look like inputs to a
|
|
|
|
// normal {JSCallFunction} node so that the rest of the inlining machinery
|
|
|
|
// behaves as if we were dealing with a regular function invocation.
|
2015-11-26 13:58:18 +00:00
|
|
|
new_target = call.new_target(); // Retrieve new target value input.
|
|
|
|
node->RemoveInput(call.formal_arguments() + 1); // Drop new target.
|
2016-08-05 09:53:06 +00:00
|
|
|
node->InsertInput(graph()->zone(), 1, receiver);
|
2016-05-25 10:04:35 +00:00
|
|
|
|
2015-11-12 08:50:51 +00:00
|
|
|
// Insert a construct stub frame into the chain of frame states. This will
|
|
|
|
// reconstruct the proper frame when deoptimizing within the constructor.
|
|
|
|
frame_state = CreateArtificialFrameState(
|
|
|
|
node, frame_state, call.formal_arguments(),
|
|
|
|
FrameStateType::kConstructStub, info.shared_info());
|
|
|
|
}
|
|
|
|
|
|
|
|
// The inlinee specializes to the context from the JSFunction object.
|
|
|
|
// TODO(turbofan): We might want to load the context from the JSFunction at
|
|
|
|
// runtime in case we only know the SharedFunctionInfo once we have dynamic
|
|
|
|
// type feedback in the compiler.
|
2016-08-05 09:53:06 +00:00
|
|
|
Node* context = jsgraph()->Constant(handle(function->context()));
|
2015-11-12 08:50:51 +00:00
|
|
|
|
2015-10-27 12:13:32 +00:00
|
|
|
// Insert a JSConvertReceiver node for sloppy callees. Note that the context
|
2015-10-30 06:58:56 +00:00
|
|
|
// passed into this node has to be the callees context (loaded above). Note
|
|
|
|
// that the frame state passed to the JSConvertReceiver must be the frame
|
|
|
|
// state _before_ the call; it is not necessary to fiddle with the receiver
|
|
|
|
// in that frame state tho, as the conversion of the receiver can be repeated
|
|
|
|
// any number of times, it's not observable.
|
2015-11-12 08:50:51 +00:00
|
|
|
if (node->opcode() == IrOpcode::kJSCallFunction &&
|
2016-05-04 09:27:26 +00:00
|
|
|
is_sloppy(parse_info.language_mode()) && !shared_info->native()) {
|
2015-10-27 12:13:32 +00:00
|
|
|
const CallFunctionParameters& p = CallFunctionParametersOf(node->op());
|
2016-06-02 08:02:28 +00:00
|
|
|
Node* frame_state_before = NodeProperties::FindFrameStateBefore(node);
|
2015-10-27 12:13:32 +00:00
|
|
|
Node* effect = NodeProperties::GetEffectInput(node);
|
2016-08-05 09:53:06 +00:00
|
|
|
Node* convert = graph()->NewNode(
|
|
|
|
javascript()->ConvertReceiver(p.convert_mode()), call.receiver(),
|
|
|
|
context, frame_state_before, effect, start);
|
2015-10-27 12:13:32 +00:00
|
|
|
NodeProperties::ReplaceValueInput(node, convert, 1);
|
|
|
|
NodeProperties::ReplaceEffectInput(node, convert);
|
|
|
|
}
|
|
|
|
|
2016-03-09 11:33:10 +00:00
|
|
|
// If we are inlining a JS call at tail position then we have to pop current
|
|
|
|
// frame state and its potential arguments adaptor frame state in order to
|
|
|
|
// make the call stack be consistent with non-inlining case.
|
|
|
|
// After that we add a tail caller frame state which lets deoptimizer handle
|
|
|
|
// the case when the outermost function inlines a tail call (it should remove
|
|
|
|
// potential arguments adaptor frame that belongs to outermost function when
|
|
|
|
// deopt happens).
|
|
|
|
if (node->opcode() == IrOpcode::kJSCallFunction) {
|
|
|
|
const CallFunctionParameters& p = CallFunctionParametersOf(node->op());
|
|
|
|
if (p.tail_call_mode() == TailCallMode::kAllow) {
|
|
|
|
frame_state = CreateTailCallerFrameState(node, frame_state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-30 06:58:56 +00:00
|
|
|
// Insert argument adaptor frame if required. The callees formal parameter
|
2015-11-19 12:47:29 +00:00
|
|
|
// 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.
|
2016-03-10 12:43:51 +00:00
|
|
|
int parameter_count = info.literal()->parameter_count();
|
2015-11-19 12:47:29 +00:00
|
|
|
DCHECK_EQ(parameter_count, start->op()->ValueOutputCount() - 5);
|
2015-10-30 06:58:56 +00:00
|
|
|
if (call.formal_arguments() != parameter_count) {
|
2015-11-12 08:50:51 +00:00
|
|
|
frame_state = CreateArtificialFrameState(
|
|
|
|
node, frame_state, call.formal_arguments(),
|
2016-02-19 07:01:51 +00:00
|
|
|
FrameStateType::kArgumentsAdaptor, shared_info);
|
2015-10-30 06:58:56 +00:00
|
|
|
}
|
|
|
|
|
2015-11-19 12:47:29 +00:00
|
|
|
return InlineCall(node, new_target, context, frame_state, start, end);
|
2014-08-20 13:05:03 +00:00
|
|
|
}
|
2014-10-20 07:56:50 +00:00
|
|
|
|
2016-05-25 10:04:35 +00:00
|
|
|
Graph* JSInliner::graph() const { return jsgraph()->graph(); }
|
|
|
|
|
2016-08-05 09:53:06 +00:00
|
|
|
JSOperatorBuilder* JSInliner::javascript() const {
|
|
|
|
return jsgraph()->javascript();
|
|
|
|
}
|
|
|
|
|
|
|
|
CommonOperatorBuilder* JSInliner::common() const { return jsgraph()->common(); }
|
|
|
|
|
2015-01-26 09:05:47 +00:00
|
|
|
} // namespace compiler
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|