blob: 635daa4d761421e87dd10f7ff060e7598ce91803 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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
Ben Murdoch014dc512016-03-22 12:00:34 +00005#include "src/compiler/js-inlining.h"
6
Ben Murdoch014dc512016-03-22 12:00:34 +00007#include "src/ast/ast-numbering.h"
Ben Murdoch13e2dad2016-09-16 13:49:30 +01008#include "src/ast/ast.h"
Ben Murdoch014dc512016-03-22 12:00:34 +00009#include "src/ast/scopes.h"
10#include "src/compiler.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011#include "src/compiler/ast-graph-builder.h"
Ben Murdoch13e2dad2016-09-16 13:49:30 +010012#include "src/compiler/ast-loop-assignment-analyzer.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000013#include "src/compiler/common-operator.h"
Ben Murdoch014dc512016-03-22 12:00:34 +000014#include "src/compiler/graph-reducer.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015#include "src/compiler/js-operator.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000016#include "src/compiler/node-matchers.h"
Ben Murdoch014dc512016-03-22 12:00:34 +000017#include "src/compiler/node-properties.h"
18#include "src/compiler/operator-properties.h"
Ben Murdochf91f0612016-11-29 16:50:11 +000019#include "src/compiler/simplified-operator.h"
Ben Murdoch13e2dad2016-09-16 13:49:30 +010020#include "src/compiler/type-hint-analyzer.h"
Ben Murdoch014dc512016-03-22 12:00:34 +000021#include "src/isolate-inl.h"
Ben Murdochf91f0612016-11-29 16:50:11 +000022#include "src/parsing/parse-info.h"
Ben Murdoch014dc512016-03-22 12:00:34 +000023#include "src/parsing/rewriter.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000024
25namespace v8 {
26namespace internal {
27namespace compiler {
28
Ben Murdoch014dc512016-03-22 12:00:34 +000029#define TRACE(...) \
30 do { \
31 if (FLAG_trace_turbo_inlining) PrintF(__VA_ARGS__); \
32 } while (false)
Ben Murdochb8a8cc12014-11-26 15:28:44 +000033
Ben Murdoch014dc512016-03-22 12:00:34 +000034
35// Provides convenience accessors for the common layout of nodes having either
36// the {JSCallFunction} or the {JSCallConstruct} operator.
37class JSCallAccessor {
38 public:
39 explicit JSCallAccessor(Node* call) : call_(call) {
40 DCHECK(call->opcode() == IrOpcode::kJSCallFunction ||
41 call->opcode() == IrOpcode::kJSCallConstruct);
42 }
43
44 Node* target() {
45 // Both, {JSCallFunction} and {JSCallConstruct}, have same layout here.
46 return call_->InputAt(0);
47 }
48
49 Node* receiver() {
50 DCHECK_EQ(IrOpcode::kJSCallFunction, call_->opcode());
51 return call_->InputAt(1);
52 }
53
54 Node* new_target() {
55 DCHECK_EQ(IrOpcode::kJSCallConstruct, call_->opcode());
56 return call_->InputAt(formal_arguments() + 1);
57 }
58
Ben Murdoch13e2dad2016-09-16 13:49:30 +010059 Node* frame_state() {
60 // Both, {JSCallFunction} and {JSCallConstruct}, have frame state.
Ben Murdochf91f0612016-11-29 16:50:11 +000061 return NodeProperties::GetFrameStateInput(call_);
Ben Murdoch014dc512016-03-22 12:00:34 +000062 }
63
64 int formal_arguments() {
65 // Both, {JSCallFunction} and {JSCallConstruct}, have two extra inputs:
66 // - JSCallConstruct: Includes target function and new target.
67 // - JSCallFunction: Includes target function and receiver.
68 return call_->op()->ValueInputCount() - 2;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000069 }
70
71 private:
Ben Murdoch014dc512016-03-22 12:00:34 +000072 Node* call_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000073};
74
75
Ben Murdoch014dc512016-03-22 12:00:34 +000076Reduction JSInliner::InlineCall(Node* call, Node* new_target, Node* context,
77 Node* frame_state, Node* start, Node* end) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000078 // The scheduler is smart enough to place our code; we just ensure {control}
Ben Murdoch014dc512016-03-22 12:00:34 +000079 // becomes the control input of the start of the inlinee, and {effect} becomes
80 // the effect input of the start of the inlinee.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000081 Node* control = NodeProperties::GetControlInput(call);
Ben Murdoch014dc512016-03-22 12:00:34 +000082 Node* effect = NodeProperties::GetEffectInput(call);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000083
Ben Murdoch014dc512016-03-22 12:00:34 +000084 int const inlinee_new_target_index =
85 static_cast<int>(start->op()->ValueOutputCount()) - 3;
86 int const inlinee_arity_index =
87 static_cast<int>(start->op()->ValueOutputCount()) - 2;
88 int const inlinee_context_index =
89 static_cast<int>(start->op()->ValueOutputCount()) - 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000090
Ben Murdoch014dc512016-03-22 12:00:34 +000091 // {inliner_inputs} counts JSFunction, receiver, arguments, but not
92 // new target value, argument count, context, effect or control.
Emily Bernier958fae72015-03-24 16:35:39 -040093 int inliner_inputs = call->op()->ValueInputCount();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000094 // Iterate over all uses of the start node.
Ben Murdoch014dc512016-03-22 12:00:34 +000095 for (Edge edge : start->use_edges()) {
Emily Bernier958fae72015-03-24 16:35:39 -040096 Node* use = edge.from();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000097 switch (use->opcode()) {
98 case IrOpcode::kParameter: {
Ben Murdoch014dc512016-03-22 12:00:34 +000099 int index = 1 + ParameterIndexOf(use->op());
100 DCHECK_LE(index, inlinee_context_index);
101 if (index < inliner_inputs && index < inlinee_new_target_index) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000102 // There is an input from the call, and the index is a value
103 // projection but not the context, so rewire the input.
Ben Murdoch014dc512016-03-22 12:00:34 +0000104 Replace(use, call->InputAt(index));
105 } else if (index == inlinee_new_target_index) {
106 // The projection is requesting the new target value.
107 Replace(use, new_target);
108 } else if (index == inlinee_arity_index) {
109 // The projection is requesting the number of arguments.
Ben Murdochf91f0612016-11-29 16:50:11 +0000110 Replace(use, jsgraph()->Int32Constant(inliner_inputs - 2));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000111 } else if (index == inlinee_context_index) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000112 // The projection is requesting the inlinee function context.
113 Replace(use, context);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000114 } else {
Ben Murdoch014dc512016-03-22 12:00:34 +0000115 // Call has fewer arguments than required, fill with undefined.
Ben Murdochf91f0612016-11-29 16:50:11 +0000116 Replace(use, jsgraph()->UndefinedConstant());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000117 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000118 break;
119 }
120 default:
Emily Bernier958fae72015-03-24 16:35:39 -0400121 if (NodeProperties::IsEffectEdge(edge)) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000122 edge.UpdateTo(effect);
Emily Bernier958fae72015-03-24 16:35:39 -0400123 } else if (NodeProperties::IsControlEdge(edge)) {
124 edge.UpdateTo(control);
Ben Murdoch014dc512016-03-22 12:00:34 +0000125 } else if (NodeProperties::IsFrameStateEdge(edge)) {
126 edge.UpdateTo(frame_state);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000127 } else {
128 UNREACHABLE();
129 }
130 break;
131 }
132 }
133
Ben Murdoch014dc512016-03-22 12:00:34 +0000134 NodeVector values(local_zone_);
135 NodeVector effects(local_zone_);
136 NodeVector controls(local_zone_);
137 for (Node* const input : end->inputs()) {
138 switch (input->opcode()) {
139 case IrOpcode::kReturn:
140 values.push_back(NodeProperties::GetValueInput(input, 0));
141 effects.push_back(NodeProperties::GetEffectInput(input));
142 controls.push_back(NodeProperties::GetControlInput(input));
143 break;
144 case IrOpcode::kDeoptimize:
145 case IrOpcode::kTerminate:
146 case IrOpcode::kThrow:
Ben Murdochf91f0612016-11-29 16:50:11 +0000147 NodeProperties::MergeControlToEnd(graph(), common(), input);
148 Revisit(graph()->end());
Ben Murdoch014dc512016-03-22 12:00:34 +0000149 break;
150 default:
151 UNREACHABLE();
152 break;
153 }
154 }
155 DCHECK_EQ(values.size(), effects.size());
156 DCHECK_EQ(values.size(), controls.size());
157
158 // Depending on whether the inlinee produces a value, we either replace value
159 // uses with said value or kill value uses if no value can be returned.
160 if (values.size() > 0) {
161 int const input_count = static_cast<int>(controls.size());
Ben Murdochf91f0612016-11-29 16:50:11 +0000162 Node* control_output = graph()->NewNode(common()->Merge(input_count),
163 input_count, &controls.front());
Ben Murdoch014dc512016-03-22 12:00:34 +0000164 values.push_back(control_output);
165 effects.push_back(control_output);
Ben Murdochf91f0612016-11-29 16:50:11 +0000166 Node* value_output = graph()->NewNode(
167 common()->Phi(MachineRepresentation::kTagged, input_count),
Ben Murdoch014dc512016-03-22 12:00:34 +0000168 static_cast<int>(values.size()), &values.front());
Ben Murdochf91f0612016-11-29 16:50:11 +0000169 Node* effect_output =
170 graph()->NewNode(common()->EffectPhi(input_count),
171 static_cast<int>(effects.size()), &effects.front());
Ben Murdoch014dc512016-03-22 12:00:34 +0000172 ReplaceWithValue(call, value_output, effect_output, control_output);
173 return Changed(value_output);
174 } else {
Ben Murdochf91f0612016-11-29 16:50:11 +0000175 ReplaceWithValue(call, call, call, jsgraph()->Dead());
Ben Murdoch014dc512016-03-22 12:00:34 +0000176 return Changed(call);
177 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000178}
179
180
Ben Murdoch014dc512016-03-22 12:00:34 +0000181Node* JSInliner::CreateArtificialFrameState(Node* node, Node* outer_frame_state,
182 int parameter_count,
183 FrameStateType frame_state_type,
184 Handle<SharedFunctionInfo> shared) {
185 const FrameStateFunctionInfo* state_info =
Ben Murdochf91f0612016-11-29 16:50:11 +0000186 common()->CreateFrameStateFunctionInfo(frame_state_type,
187 parameter_count + 1, 0, shared);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000188
Ben Murdochf91f0612016-11-29 16:50:11 +0000189 const Operator* op = common()->FrameState(
Ben Murdoch014dc512016-03-22 12:00:34 +0000190 BailoutId(-1), OutputFrameStateCombine::Ignore(), state_info);
Ben Murdochf91f0612016-11-29 16:50:11 +0000191 const Operator* op0 = common()->StateValues(0);
192 Node* node0 = graph()->NewNode(op0);
Ben Murdoch014dc512016-03-22 12:00:34 +0000193 NodeVector params(local_zone_);
194 for (int parameter = 0; parameter < parameter_count + 1; ++parameter) {
195 params.push_back(node->InputAt(1 + parameter));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000196 }
197 const Operator* op_param =
Ben Murdochf91f0612016-11-29 16:50:11 +0000198 common()->StateValues(static_cast<int>(params.size()));
199 Node* params_node = graph()->NewNode(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000200 op_param, static_cast<int>(params.size()), &params.front());
Ben Murdochf91f0612016-11-29 16:50:11 +0000201 return graph()->NewNode(op, params_node, node0, node0,
202 jsgraph()->UndefinedConstant(), node->InputAt(0),
203 outer_frame_state);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000204}
205
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100206Node* JSInliner::CreateTailCallerFrameState(Node* node, Node* frame_state) {
207 FrameStateInfo const& frame_info = OpParameter<FrameStateInfo>(frame_state);
208 Handle<SharedFunctionInfo> shared;
209 frame_info.shared_info().ToHandle(&shared);
210
211 Node* function = frame_state->InputAt(kFrameStateFunctionInput);
212
213 // If we are inlining a tail call drop caller's frame state and an
214 // arguments adaptor if it exists.
Ben Murdochf91f0612016-11-29 16:50:11 +0000215 frame_state = NodeProperties::GetFrameStateInput(frame_state);
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100216 if (frame_state->opcode() == IrOpcode::kFrameState) {
217 FrameStateInfo const& frame_info = OpParameter<FrameStateInfo>(frame_state);
218 if (frame_info.type() == FrameStateType::kArgumentsAdaptor) {
Ben Murdochf91f0612016-11-29 16:50:11 +0000219 frame_state = NodeProperties::GetFrameStateInput(frame_state);
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100220 }
221 }
222
223 const FrameStateFunctionInfo* state_info =
Ben Murdochf91f0612016-11-29 16:50:11 +0000224 common()->CreateFrameStateFunctionInfo(
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100225 FrameStateType::kTailCallerFunction, 0, 0, shared);
226
Ben Murdochf91f0612016-11-29 16:50:11 +0000227 const Operator* op = common()->FrameState(
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100228 BailoutId(-1), OutputFrameStateCombine::Ignore(), state_info);
Ben Murdochf91f0612016-11-29 16:50:11 +0000229 const Operator* op0 = common()->StateValues(0);
230 Node* node0 = graph()->NewNode(op0);
231 return graph()->NewNode(op, node0, node0, node0,
232 jsgraph()->UndefinedConstant(), function,
233 frame_state);
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100234}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000235
Ben Murdoch014dc512016-03-22 12:00:34 +0000236namespace {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000237
Ben Murdoch014dc512016-03-22 12:00:34 +0000238// TODO(mstarzinger,verwaest): Move this predicate onto SharedFunctionInfo?
Ben Murdoch109988c2016-05-18 11:27:45 +0100239bool NeedsImplicitReceiver(Handle<SharedFunctionInfo> shared_info) {
240 DisallowHeapAllocation no_gc;
241 Isolate* const isolate = shared_info->GetIsolate();
242 Code* const construct_stub = shared_info->construct_stub();
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100243 return construct_stub != *isolate->builtins()->JSBuiltinsConstructStub() &&
244 construct_stub !=
245 *isolate->builtins()->JSBuiltinsConstructStubForDerived() &&
246 construct_stub != *isolate->builtins()->JSConstructStubApi();
Ben Murdoch109988c2016-05-18 11:27:45 +0100247}
248
249bool IsNonConstructible(Handle<SharedFunctionInfo> shared_info) {
250 DisallowHeapAllocation no_gc;
251 Isolate* const isolate = shared_info->GetIsolate();
252 Code* const construct_stub = shared_info->construct_stub();
253 return construct_stub == *isolate->builtins()->ConstructedNonConstructable();
Ben Murdoch014dc512016-03-22 12:00:34 +0000254}
255
256} // namespace
257
258
259Reduction JSInliner::Reduce(Node* node) {
260 if (!IrOpcode::IsInlineeOpcode(node->opcode())) return NoChange();
261
262 // This reducer can handle both normal function calls as well a constructor
263 // calls whenever the target is a constant function object, as follows:
264 // - JSCallFunction(target:constant, receiver, args...)
265 // - JSCallConstruct(target:constant, args..., new.target)
266 HeapObjectMatcher match(node->InputAt(0));
267 if (!match.HasValue() || !match.Value()->IsJSFunction()) return NoChange();
268 Handle<JSFunction> function = Handle<JSFunction>::cast(match.Value());
269
270 return ReduceJSCall(node, function);
271}
272
273
274Reduction JSInliner::ReduceJSCall(Node* node, Handle<JSFunction> function) {
275 DCHECK(IrOpcode::IsInlineeOpcode(node->opcode()));
276 JSCallAccessor call(node);
Ben Murdoch109988c2016-05-18 11:27:45 +0100277 Handle<SharedFunctionInfo> shared_info(function->shared());
Ben Murdoch014dc512016-03-22 12:00:34 +0000278
279 // Function must be inlineable.
Ben Murdoch109988c2016-05-18 11:27:45 +0100280 if (!shared_info->IsInlineable()) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000281 TRACE("Not inlining %s into %s because callee is not inlineable\n",
Ben Murdoch109988c2016-05-18 11:27:45 +0100282 shared_info->DebugName()->ToCString().get(),
Ben Murdoch014dc512016-03-22 12:00:34 +0000283 info_->shared_info()->DebugName()->ToCString().get());
284 return NoChange();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000285 }
286
Ben Murdoch014dc512016-03-22 12:00:34 +0000287 // Constructor must be constructable.
288 if (node->opcode() == IrOpcode::kJSCallConstruct &&
Ben Murdoch109988c2016-05-18 11:27:45 +0100289 IsNonConstructible(shared_info)) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000290 TRACE("Not inlining %s into %s because constructor is not constructable.\n",
Ben Murdoch109988c2016-05-18 11:27:45 +0100291 shared_info->DebugName()->ToCString().get(),
Ben Murdoch014dc512016-03-22 12:00:34 +0000292 info_->shared_info()->DebugName()->ToCString().get());
293 return NoChange();
294 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000295
Ben Murdoch014dc512016-03-22 12:00:34 +0000296 // Class constructors are callable, but [[Call]] will raise an exception.
297 // See ES6 section 9.2.1 [[Call]] ( thisArgument, argumentsList ).
298 if (node->opcode() == IrOpcode::kJSCallFunction &&
Ben Murdoch109988c2016-05-18 11:27:45 +0100299 IsClassConstructor(shared_info->kind())) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000300 TRACE("Not inlining %s into %s because callee is a class constructor.\n",
Ben Murdoch109988c2016-05-18 11:27:45 +0100301 shared_info->DebugName()->ToCString().get(),
Ben Murdoch014dc512016-03-22 12:00:34 +0000302 info_->shared_info()->DebugName()->ToCString().get());
303 return NoChange();
304 }
305
306 // Function contains break points.
Ben Murdoch109988c2016-05-18 11:27:45 +0100307 if (shared_info->HasDebugInfo()) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000308 TRACE("Not inlining %s into %s because callee may contain break points\n",
Ben Murdoch109988c2016-05-18 11:27:45 +0100309 shared_info->DebugName()->ToCString().get(),
Ben Murdoch014dc512016-03-22 12:00:34 +0000310 info_->shared_info()->DebugName()->ToCString().get());
311 return NoChange();
312 }
313
314 // Disallow cross native-context inlining for now. This means that all parts
315 // of the resulting code will operate on the same global object.
316 // This also prevents cross context leaks for asm.js code, where we could
317 // inline functions from a different context and hold on to that context (and
318 // closure) from the code object.
319 // TODO(turbofan): We might want to revisit this restriction later when we
320 // have a need for this, and we know how to model different native contexts
321 // in the same graph in a compositional way.
322 if (function->context()->native_context() !=
323 info_->context()->native_context()) {
324 TRACE("Not inlining %s into %s because of different native contexts\n",
Ben Murdoch109988c2016-05-18 11:27:45 +0100325 shared_info->DebugName()->ToCString().get(),
Ben Murdoch014dc512016-03-22 12:00:34 +0000326 info_->shared_info()->DebugName()->ToCString().get());
327 return NoChange();
328 }
329
330 // TODO(turbofan): TranslatedState::GetAdaptedArguments() currently relies on
331 // not inlining recursive functions. We might want to relax that at some
332 // point.
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100333 for (Node* frame_state = call.frame_state();
Ben Murdoch014dc512016-03-22 12:00:34 +0000334 frame_state->opcode() == IrOpcode::kFrameState;
335 frame_state = frame_state->InputAt(kFrameStateOuterStateInput)) {
Ben Murdoch109988c2016-05-18 11:27:45 +0100336 FrameStateInfo const& frame_info = OpParameter<FrameStateInfo>(frame_state);
337 Handle<SharedFunctionInfo> frame_shared_info;
338 if (frame_info.shared_info().ToHandle(&frame_shared_info) &&
339 *frame_shared_info == *shared_info) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000340 TRACE("Not inlining %s into %s because call is recursive\n",
Ben Murdoch109988c2016-05-18 11:27:45 +0100341 shared_info->DebugName()->ToCString().get(),
Ben Murdoch014dc512016-03-22 12:00:34 +0000342 info_->shared_info()->DebugName()->ToCString().get());
343 return NoChange();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000344 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000345 }
346
Ben Murdoch014dc512016-03-22 12:00:34 +0000347 // TODO(turbofan): Inlining into a try-block is not yet supported.
348 if (NodeProperties::IsExceptionalCall(node)) {
349 TRACE("Not inlining %s into %s because of surrounding try-block\n",
Ben Murdoch109988c2016-05-18 11:27:45 +0100350 shared_info->DebugName()->ToCString().get(),
Ben Murdoch014dc512016-03-22 12:00:34 +0000351 info_->shared_info()->DebugName()->ToCString().get());
352 return NoChange();
353 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000354
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100355 Zone zone(info_->isolate()->allocator());
Ben Murdoch014dc512016-03-22 12:00:34 +0000356 ParseInfo parse_info(&zone, function);
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100357 CompilationInfo info(&parse_info, function);
Ben Murdoch109988c2016-05-18 11:27:45 +0100358 if (info_->is_deoptimization_enabled()) info.MarkAsDeoptimizationEnabled();
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100359 if (info_->is_type_feedback_enabled()) info.MarkAsTypeFeedbackEnabled();
Ben Murdoch014dc512016-03-22 12:00:34 +0000360
361 if (!Compiler::ParseAndAnalyze(info.parse_info())) {
362 TRACE("Not inlining %s into %s because parsing failed\n",
Ben Murdoch109988c2016-05-18 11:27:45 +0100363 shared_info->DebugName()->ToCString().get(),
Ben Murdoch014dc512016-03-22 12:00:34 +0000364 info_->shared_info()->DebugName()->ToCString().get());
365 if (info_->isolate()->has_pending_exception()) {
366 info_->isolate()->clear_pending_exception();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000367 }
Ben Murdoch014dc512016-03-22 12:00:34 +0000368 return NoChange();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000369 }
370
Ben Murdoch014dc512016-03-22 12:00:34 +0000371 if (!Compiler::EnsureDeoptimizationSupport(&info)) {
372 TRACE("Not inlining %s into %s because deoptimization support failed\n",
Ben Murdoch109988c2016-05-18 11:27:45 +0100373 shared_info->DebugName()->ToCString().get(),
Ben Murdoch014dc512016-03-22 12:00:34 +0000374 info_->shared_info()->DebugName()->ToCString().get());
375 return NoChange();
376 }
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100377
Ben Murdoch014dc512016-03-22 12:00:34 +0000378 // Remember that we inlined this function. This needs to be called right
379 // after we ensure deoptimization support so that the code flusher
380 // does not remove the code with the deoptimization support.
Ben Murdoch109988c2016-05-18 11:27:45 +0100381 info_->AddInlinedFunction(shared_info);
Ben Murdoch014dc512016-03-22 12:00:34 +0000382
383 // ----------------------------------------------------------------
384 // After this point, we've made a decision to inline this function.
385 // We shall not bailout from inlining if we got here.
386
387 TRACE("Inlining %s into %s\n",
Ben Murdoch109988c2016-05-18 11:27:45 +0100388 shared_info->DebugName()->ToCString().get(),
Ben Murdoch014dc512016-03-22 12:00:34 +0000389 info_->shared_info()->DebugName()->ToCString().get());
390
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100391 // If function was lazily compiled, it's literals array may not yet be set up.
392 JSFunction::EnsureLiterals(function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000393
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100394 // Create the subgraph for the inlinee.
395 Node* start;
396 Node* end;
397 {
398 // Run the loop assignment analyzer on the inlinee.
399 AstLoopAssignmentAnalyzer loop_assignment_analyzer(&zone, &info);
400 LoopAssignmentAnalysis* loop_assignment =
401 loop_assignment_analyzer.Analyze();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000402
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100403 // Run the type hint analyzer on the inlinee.
404 TypeHintAnalyzer type_hint_analyzer(&zone);
405 TypeHintAnalysis* type_hint_analysis =
406 type_hint_analyzer.Analyze(handle(shared_info->code(), info.isolate()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000407
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100408 // Run the AstGraphBuilder to create the subgraph.
409 Graph::SubgraphScope scope(graph());
410 AstGraphBuilder graph_builder(&zone, &info, jsgraph(), loop_assignment,
411 type_hint_analysis);
412 graph_builder.CreateGraph(false);
413
414 // Extract the inlinee start/end nodes.
415 start = graph()->start();
416 end = graph()->end();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000417 }
418
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100419 Node* frame_state = call.frame_state();
Ben Murdochf91f0612016-11-29 16:50:11 +0000420 Node* new_target = jsgraph()->UndefinedConstant();
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100421
422 // Inline {JSCallConstruct} requires some additional magic.
Ben Murdoch014dc512016-03-22 12:00:34 +0000423 if (node->opcode() == IrOpcode::kJSCallConstruct) {
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100424 // Insert nodes around the call that model the behavior required for a
425 // constructor dispatch (allocate implicit receiver and check return value).
426 // This models the behavior usually accomplished by our {JSConstructStub}.
427 // Note that the context has to be the callers context (input to call node).
Ben Murdochf91f0612016-11-29 16:50:11 +0000428 Node* receiver = jsgraph()->UndefinedConstant(); // Implicit receiver.
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100429 if (NeedsImplicitReceiver(shared_info)) {
430 Node* frame_state_before = NodeProperties::FindFrameStateBefore(node);
431 Node* effect = NodeProperties::GetEffectInput(node);
432 Node* context = NodeProperties::GetContextInput(node);
Ben Murdochf91f0612016-11-29 16:50:11 +0000433 Node* create = graph()->NewNode(javascript()->Create(), call.target(),
434 call.new_target(), context,
435 frame_state_before, effect);
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100436 NodeProperties::ReplaceEffectInput(node, create);
437 // Insert a check of the return value to determine whether the return
Ben Murdochf91f0612016-11-29 16:50:11 +0000438 // value or the implicit receiver should be selected as a result of the
439 // call.
440 Node* check = graph()->NewNode(simplified()->ObjectIsReceiver(), node);
441 Node* select =
442 graph()->NewNode(common()->Select(MachineRepresentation::kTagged),
443 check, node, create);
444 NodeProperties::ReplaceUses(node, select, node, node, node);
445 // Fix-up inputs that have been mangled by the {ReplaceUses} call above.
446 NodeProperties::ReplaceValueInput(select, node, 1); // Fix-up input.
447 NodeProperties::ReplaceValueInput(check, node, 0); // Fix-up input.
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100448 receiver = create; // The implicit receiver.
449 }
450
451 // Swizzle the inputs of the {JSCallConstruct} node to look like inputs to a
452 // normal {JSCallFunction} node so that the rest of the inlining machinery
453 // behaves as if we were dealing with a regular function invocation.
Ben Murdoch014dc512016-03-22 12:00:34 +0000454 new_target = call.new_target(); // Retrieve new target value input.
455 node->RemoveInput(call.formal_arguments() + 1); // Drop new target.
Ben Murdochf91f0612016-11-29 16:50:11 +0000456 node->InsertInput(graph()->zone(), 1, receiver);
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100457
Ben Murdoch014dc512016-03-22 12:00:34 +0000458 // Insert a construct stub frame into the chain of frame states. This will
459 // reconstruct the proper frame when deoptimizing within the constructor.
460 frame_state = CreateArtificialFrameState(
461 node, frame_state, call.formal_arguments(),
462 FrameStateType::kConstructStub, info.shared_info());
463 }
464
465 // The inlinee specializes to the context from the JSFunction object.
466 // TODO(turbofan): We might want to load the context from the JSFunction at
467 // runtime in case we only know the SharedFunctionInfo once we have dynamic
468 // type feedback in the compiler.
Ben Murdochf91f0612016-11-29 16:50:11 +0000469 Node* context = jsgraph()->Constant(handle(function->context()));
Ben Murdoch014dc512016-03-22 12:00:34 +0000470
471 // Insert a JSConvertReceiver node for sloppy callees. Note that the context
472 // passed into this node has to be the callees context (loaded above). Note
473 // that the frame state passed to the JSConvertReceiver must be the frame
474 // state _before_ the call; it is not necessary to fiddle with the receiver
475 // in that frame state tho, as the conversion of the receiver can be repeated
476 // any number of times, it's not observable.
477 if (node->opcode() == IrOpcode::kJSCallFunction &&
Ben Murdochbcf72ee2016-08-08 18:44:38 +0100478 is_sloppy(parse_info.language_mode()) && !shared_info->native()) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000479 const CallFunctionParameters& p = CallFunctionParametersOf(node->op());
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100480 Node* frame_state_before = NodeProperties::FindFrameStateBefore(node);
Ben Murdoch014dc512016-03-22 12:00:34 +0000481 Node* effect = NodeProperties::GetEffectInput(node);
Ben Murdochf91f0612016-11-29 16:50:11 +0000482 Node* convert = graph()->NewNode(
483 javascript()->ConvertReceiver(p.convert_mode()), call.receiver(),
484 context, frame_state_before, effect, start);
Ben Murdoch014dc512016-03-22 12:00:34 +0000485 NodeProperties::ReplaceValueInput(node, convert, 1);
486 NodeProperties::ReplaceEffectInput(node, convert);
487 }
488
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100489 // If we are inlining a JS call at tail position then we have to pop current
490 // frame state and its potential arguments adaptor frame state in order to
491 // make the call stack be consistent with non-inlining case.
492 // After that we add a tail caller frame state which lets deoptimizer handle
493 // the case when the outermost function inlines a tail call (it should remove
494 // potential arguments adaptor frame that belongs to outermost function when
495 // deopt happens).
496 if (node->opcode() == IrOpcode::kJSCallFunction) {
497 const CallFunctionParameters& p = CallFunctionParametersOf(node->op());
498 if (p.tail_call_mode() == TailCallMode::kAllow) {
499 frame_state = CreateTailCallerFrameState(node, frame_state);
500 }
501 }
502
Ben Murdoch014dc512016-03-22 12:00:34 +0000503 // Insert argument adaptor frame if required. The callees formal parameter
504 // count (i.e. value outputs of start node minus target, receiver, new target,
505 // arguments count and context) have to match the number of arguments passed
506 // to the call.
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100507 int parameter_count = info.literal()->parameter_count();
Ben Murdoch014dc512016-03-22 12:00:34 +0000508 DCHECK_EQ(parameter_count, start->op()->ValueOutputCount() - 5);
509 if (call.formal_arguments() != parameter_count) {
510 frame_state = CreateArtificialFrameState(
511 node, frame_state, call.formal_arguments(),
Ben Murdoch109988c2016-05-18 11:27:45 +0100512 FrameStateType::kArgumentsAdaptor, shared_info);
Ben Murdoch014dc512016-03-22 12:00:34 +0000513 }
514
515 return InlineCall(node, new_target, context, frame_state, start, end);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000516}
Emily Bernier958fae72015-03-24 16:35:39 -0400517
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100518Graph* JSInliner::graph() const { return jsgraph()->graph(); }
519
Ben Murdochf91f0612016-11-29 16:50:11 +0000520JSOperatorBuilder* JSInliner::javascript() const {
521 return jsgraph()->javascript();
522}
523
524CommonOperatorBuilder* JSInliner::common() const { return jsgraph()->common(); }
525
526SimplifiedOperatorBuilder* JSInliner::simplified() const {
527 return jsgraph()->simplified();
528}
529
Ben Murdoch014dc512016-03-22 12:00:34 +0000530} // namespace compiler
531} // namespace internal
532} // namespace v8