blob: 2244f9bbfe9294651d70edc6870839f3979d1780 [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 Murdoch4a90d5f2016-03-22 12:00:34 +00005#include "src/compiler/js-inlining.h"
6
7#include "src/ast/ast.h"
8#include "src/ast/ast-numbering.h"
9#include "src/ast/scopes.h"
10#include "src/compiler.h"
11#include "src/compiler/all-nodes.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012#include "src/compiler/ast-graph-builder.h"
13#include "src/compiler/common-operator.h"
Ben Murdoch4a90d5f2016-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 Murdoch4a90d5f2016-03-22 12:00:34 +000017#include "src/compiler/node-properties.h"
18#include "src/compiler/operator-properties.h"
19#include "src/isolate-inl.h"
20#include "src/parsing/parser.h"
21#include "src/parsing/rewriter.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000022
23namespace v8 {
24namespace internal {
25namespace compiler {
26
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000027#define TRACE(...) \
28 do { \
29 if (FLAG_trace_turbo_inlining) PrintF(__VA_ARGS__); \
30 } while (false)
Ben Murdochb8a8cc12014-11-26 15:28:44 +000031
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000032
33// Provides convenience accessors for the common layout of nodes having either
34// the {JSCallFunction} or the {JSCallConstruct} operator.
35class JSCallAccessor {
36 public:
37 explicit JSCallAccessor(Node* call) : call_(call) {
38 DCHECK(call->opcode() == IrOpcode::kJSCallFunction ||
39 call->opcode() == IrOpcode::kJSCallConstruct);
40 }
41
42 Node* target() {
43 // Both, {JSCallFunction} and {JSCallConstruct}, have same layout here.
44 return call_->InputAt(0);
45 }
46
47 Node* receiver() {
48 DCHECK_EQ(IrOpcode::kJSCallFunction, call_->opcode());
49 return call_->InputAt(1);
50 }
51
52 Node* new_target() {
53 DCHECK_EQ(IrOpcode::kJSCallConstruct, call_->opcode());
54 return call_->InputAt(formal_arguments() + 1);
55 }
56
57 Node* frame_state_before() {
58 return NodeProperties::GetFrameStateInput(call_, 1);
59 }
60
61 Node* frame_state_after() {
62 // Both, {JSCallFunction} and {JSCallConstruct}, have frame state after.
63 return NodeProperties::GetFrameStateInput(call_, 0);
64 }
65
66 int formal_arguments() {
67 // Both, {JSCallFunction} and {JSCallConstruct}, have two extra inputs:
68 // - JSCallConstruct: Includes target function and new target.
69 // - JSCallFunction: Includes target function and receiver.
70 return call_->op()->ValueInputCount() - 2;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000071 }
72
73 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000074 Node* call_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000075};
76
77
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000078class CopyVisitor {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000079 public:
80 CopyVisitor(Graph* source_graph, Graph* target_graph, Zone* temp_zone)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000081 : sentinel_op_(IrOpcode::kDead, Operator::kNoProperties, "Sentinel", 0, 0,
82 0, 0, 0, 0),
83 sentinel_(target_graph->NewNode(&sentinel_op_)),
84 copies_(source_graph->NodeCount(), sentinel_, temp_zone),
Ben Murdochb8a8cc12014-11-26 15:28:44 +000085 source_graph_(source_graph),
86 target_graph_(target_graph),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000087 temp_zone_(temp_zone) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +000088
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000089 Node* GetCopy(Node* orig) { return copies_[orig->id()]; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000090
91 void CopyGraph() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000092 NodeVector inputs(temp_zone_);
93 // TODO(bmeurer): AllNodes should be turned into something like
94 // Graph::CollectNodesReachableFromEnd() and the gray set stuff should be
95 // removed since it's only needed by the visualizer.
96 AllNodes all(temp_zone_, source_graph_);
97 // Copy all nodes reachable from end.
98 for (Node* orig : all.live) {
99 Node* copy = GetCopy(orig);
100 if (copy != sentinel_) {
101 // Mapping already exists.
102 continue;
103 }
104 // Copy the node.
105 inputs.clear();
106 for (Node* input : orig->inputs()) inputs.push_back(copies_[input->id()]);
107 copy = target_graph_->NewNode(orig->op(), orig->InputCount(),
108 inputs.empty() ? nullptr : &inputs[0]);
109 copies_[orig->id()] = copy;
110 }
111 // For missing inputs.
112 for (Node* orig : all.live) {
113 Node* copy = copies_[orig->id()];
114 for (int i = 0; i < copy->InputCount(); ++i) {
115 Node* input = copy->InputAt(i);
116 if (input == sentinel_) {
117 copy->ReplaceInput(i, GetCopy(orig->InputAt(i)));
118 }
119 }
120 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000121 }
122
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000123 const NodeVector& copies() const { return copies_; }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000124
125 private:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000126 Operator const sentinel_op_;
127 Node* const sentinel_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000128 NodeVector copies_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000129 Graph* const source_graph_;
130 Graph* const target_graph_;
131 Zone* const temp_zone_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000132};
133
134
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000135Reduction JSInliner::InlineCall(Node* call, Node* new_target, Node* context,
136 Node* frame_state, Node* start, Node* end) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000137 // The scheduler is smart enough to place our code; we just ensure {control}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000138 // becomes the control input of the start of the inlinee, and {effect} becomes
139 // the effect input of the start of the inlinee.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000140 Node* control = NodeProperties::GetControlInput(call);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000141 Node* effect = NodeProperties::GetEffectInput(call);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000142
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000143 int const inlinee_new_target_index =
144 static_cast<int>(start->op()->ValueOutputCount()) - 3;
145 int const inlinee_arity_index =
146 static_cast<int>(start->op()->ValueOutputCount()) - 2;
147 int const inlinee_context_index =
148 static_cast<int>(start->op()->ValueOutputCount()) - 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000149
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000150 // {inliner_inputs} counts JSFunction, receiver, arguments, but not
151 // new target value, argument count, context, effect or control.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400152 int inliner_inputs = call->op()->ValueInputCount();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000153 // Iterate over all uses of the start node.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000154 for (Edge edge : start->use_edges()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400155 Node* use = edge.from();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000156 switch (use->opcode()) {
157 case IrOpcode::kParameter: {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000158 int index = 1 + ParameterIndexOf(use->op());
159 DCHECK_LE(index, inlinee_context_index);
160 if (index < inliner_inputs && index < inlinee_new_target_index) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000161 // There is an input from the call, and the index is a value
162 // projection but not the context, so rewire the input.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000163 Replace(use, call->InputAt(index));
164 } else if (index == inlinee_new_target_index) {
165 // The projection is requesting the new target value.
166 Replace(use, new_target);
167 } else if (index == inlinee_arity_index) {
168 // The projection is requesting the number of arguments.
169 Replace(use, jsgraph_->Int32Constant(inliner_inputs - 2));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000170 } else if (index == inlinee_context_index) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000171 // The projection is requesting the inlinee function context.
172 Replace(use, context);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000173 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000174 // Call has fewer arguments than required, fill with undefined.
175 Replace(use, jsgraph_->UndefinedConstant());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000176 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000177 break;
178 }
179 default:
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400180 if (NodeProperties::IsEffectEdge(edge)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000181 edge.UpdateTo(effect);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400182 } else if (NodeProperties::IsControlEdge(edge)) {
183 edge.UpdateTo(control);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000184 } else if (NodeProperties::IsFrameStateEdge(edge)) {
185 edge.UpdateTo(frame_state);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000186 } else {
187 UNREACHABLE();
188 }
189 break;
190 }
191 }
192
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000193 NodeVector values(local_zone_);
194 NodeVector effects(local_zone_);
195 NodeVector controls(local_zone_);
196 for (Node* const input : end->inputs()) {
197 switch (input->opcode()) {
198 case IrOpcode::kReturn:
199 values.push_back(NodeProperties::GetValueInput(input, 0));
200 effects.push_back(NodeProperties::GetEffectInput(input));
201 controls.push_back(NodeProperties::GetControlInput(input));
202 break;
203 case IrOpcode::kDeoptimize:
204 case IrOpcode::kTerminate:
205 case IrOpcode::kThrow:
206 NodeProperties::MergeControlToEnd(jsgraph_->graph(), jsgraph_->common(),
207 input);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100208 Revisit(jsgraph_->graph()->end());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000209 break;
210 default:
211 UNREACHABLE();
212 break;
213 }
214 }
215 DCHECK_EQ(values.size(), effects.size());
216 DCHECK_EQ(values.size(), controls.size());
217
218 // Depending on whether the inlinee produces a value, we either replace value
219 // uses with said value or kill value uses if no value can be returned.
220 if (values.size() > 0) {
221 int const input_count = static_cast<int>(controls.size());
222 Node* control_output = jsgraph_->graph()->NewNode(
223 jsgraph_->common()->Merge(input_count), input_count, &controls.front());
224 values.push_back(control_output);
225 effects.push_back(control_output);
226 Node* value_output = jsgraph_->graph()->NewNode(
227 jsgraph_->common()->Phi(MachineRepresentation::kTagged, input_count),
228 static_cast<int>(values.size()), &values.front());
229 Node* effect_output = jsgraph_->graph()->NewNode(
230 jsgraph_->common()->EffectPhi(input_count),
231 static_cast<int>(effects.size()), &effects.front());
232 ReplaceWithValue(call, value_output, effect_output, control_output);
233 return Changed(value_output);
234 } else {
235 ReplaceWithValue(call, call, call, jsgraph_->Dead());
236 return Changed(call);
237 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000238}
239
240
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000241Node* JSInliner::CreateArtificialFrameState(Node* node, Node* outer_frame_state,
242 int parameter_count,
243 FrameStateType frame_state_type,
244 Handle<SharedFunctionInfo> shared) {
245 const FrameStateFunctionInfo* state_info =
246 jsgraph_->common()->CreateFrameStateFunctionInfo(
Ben Murdoch097c5b22016-05-18 11:27:45 +0100247 frame_state_type, parameter_count + 1, 0, shared);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000248
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000249 const Operator* op = jsgraph_->common()->FrameState(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000250 BailoutId(-1), OutputFrameStateCombine::Ignore(), state_info);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000251 const Operator* op0 = jsgraph_->common()->StateValues(0);
252 Node* node0 = jsgraph_->graph()->NewNode(op0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000253 NodeVector params(local_zone_);
254 for (int parameter = 0; parameter < parameter_count + 1; ++parameter) {
255 params.push_back(node->InputAt(1 + parameter));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000256 }
257 const Operator* op_param =
258 jsgraph_->common()->StateValues(static_cast<int>(params.size()));
259 Node* params_node = jsgraph_->graph()->NewNode(
260 op_param, static_cast<int>(params.size()), &params.front());
261 return jsgraph_->graph()->NewNode(op, params_node, node0, node0,
262 jsgraph_->UndefinedConstant(),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000263 node->InputAt(0), outer_frame_state);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000264}
265
266
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000267namespace {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000268
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000269// TODO(mstarzinger,verwaest): Move this predicate onto SharedFunctionInfo?
Ben Murdoch097c5b22016-05-18 11:27:45 +0100270bool NeedsImplicitReceiver(Handle<SharedFunctionInfo> shared_info) {
271 DisallowHeapAllocation no_gc;
272 Isolate* const isolate = shared_info->GetIsolate();
273 Code* const construct_stub = shared_info->construct_stub();
274 return construct_stub != *isolate->builtins()->JSBuiltinsConstructStub();
275}
276
277bool IsNonConstructible(Handle<SharedFunctionInfo> shared_info) {
278 DisallowHeapAllocation no_gc;
279 Isolate* const isolate = shared_info->GetIsolate();
280 Code* const construct_stub = shared_info->construct_stub();
281 return construct_stub == *isolate->builtins()->ConstructedNonConstructable();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000282}
283
284} // namespace
285
286
287Reduction JSInliner::Reduce(Node* node) {
288 if (!IrOpcode::IsInlineeOpcode(node->opcode())) return NoChange();
289
290 // This reducer can handle both normal function calls as well a constructor
291 // calls whenever the target is a constant function object, as follows:
292 // - JSCallFunction(target:constant, receiver, args...)
293 // - JSCallConstruct(target:constant, args..., new.target)
294 HeapObjectMatcher match(node->InputAt(0));
295 if (!match.HasValue() || !match.Value()->IsJSFunction()) return NoChange();
296 Handle<JSFunction> function = Handle<JSFunction>::cast(match.Value());
297
298 return ReduceJSCall(node, function);
299}
300
301
302Reduction JSInliner::ReduceJSCall(Node* node, Handle<JSFunction> function) {
303 DCHECK(IrOpcode::IsInlineeOpcode(node->opcode()));
304 JSCallAccessor call(node);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100305 Handle<SharedFunctionInfo> shared_info(function->shared());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000306
307 // Function must be inlineable.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100308 if (!shared_info->IsInlineable()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000309 TRACE("Not inlining %s into %s because callee is not inlineable\n",
Ben Murdoch097c5b22016-05-18 11:27:45 +0100310 shared_info->DebugName()->ToCString().get(),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000311 info_->shared_info()->DebugName()->ToCString().get());
312 return NoChange();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000313 }
314
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000315 // Constructor must be constructable.
316 if (node->opcode() == IrOpcode::kJSCallConstruct &&
Ben Murdoch097c5b22016-05-18 11:27:45 +0100317 IsNonConstructible(shared_info)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000318 TRACE("Not inlining %s into %s because constructor is not constructable.\n",
Ben Murdoch097c5b22016-05-18 11:27:45 +0100319 shared_info->DebugName()->ToCString().get(),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000320 info_->shared_info()->DebugName()->ToCString().get());
321 return NoChange();
322 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000323
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000324 // Class constructors are callable, but [[Call]] will raise an exception.
325 // See ES6 section 9.2.1 [[Call]] ( thisArgument, argumentsList ).
326 if (node->opcode() == IrOpcode::kJSCallFunction &&
Ben Murdoch097c5b22016-05-18 11:27:45 +0100327 IsClassConstructor(shared_info->kind())) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000328 TRACE("Not inlining %s into %s because callee is a class constructor.\n",
Ben Murdoch097c5b22016-05-18 11:27:45 +0100329 shared_info->DebugName()->ToCString().get(),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000330 info_->shared_info()->DebugName()->ToCString().get());
331 return NoChange();
332 }
333
334 // Function contains break points.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100335 if (shared_info->HasDebugInfo()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000336 TRACE("Not inlining %s into %s because callee may contain break points\n",
Ben Murdoch097c5b22016-05-18 11:27:45 +0100337 shared_info->DebugName()->ToCString().get(),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000338 info_->shared_info()->DebugName()->ToCString().get());
339 return NoChange();
340 }
341
342 // Disallow cross native-context inlining for now. This means that all parts
343 // of the resulting code will operate on the same global object.
344 // This also prevents cross context leaks for asm.js code, where we could
345 // inline functions from a different context and hold on to that context (and
346 // closure) from the code object.
347 // TODO(turbofan): We might want to revisit this restriction later when we
348 // have a need for this, and we know how to model different native contexts
349 // in the same graph in a compositional way.
350 if (function->context()->native_context() !=
351 info_->context()->native_context()) {
352 TRACE("Not inlining %s into %s because of different native contexts\n",
Ben Murdoch097c5b22016-05-18 11:27:45 +0100353 shared_info->DebugName()->ToCString().get(),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000354 info_->shared_info()->DebugName()->ToCString().get());
355 return NoChange();
356 }
357
358 // TODO(turbofan): TranslatedState::GetAdaptedArguments() currently relies on
359 // not inlining recursive functions. We might want to relax that at some
360 // point.
361 for (Node* frame_state = call.frame_state_after();
362 frame_state->opcode() == IrOpcode::kFrameState;
363 frame_state = frame_state->InputAt(kFrameStateOuterStateInput)) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100364 FrameStateInfo const& frame_info = OpParameter<FrameStateInfo>(frame_state);
365 Handle<SharedFunctionInfo> frame_shared_info;
366 if (frame_info.shared_info().ToHandle(&frame_shared_info) &&
367 *frame_shared_info == *shared_info) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000368 TRACE("Not inlining %s into %s because call is recursive\n",
Ben Murdoch097c5b22016-05-18 11:27:45 +0100369 shared_info->DebugName()->ToCString().get(),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000370 info_->shared_info()->DebugName()->ToCString().get());
371 return NoChange();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000372 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000373 }
374
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000375 // TODO(turbofan): Inlining into a try-block is not yet supported.
376 if (NodeProperties::IsExceptionalCall(node)) {
377 TRACE("Not inlining %s into %s because of surrounding try-block\n",
Ben Murdoch097c5b22016-05-18 11:27:45 +0100378 shared_info->DebugName()->ToCString().get(),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000379 info_->shared_info()->DebugName()->ToCString().get());
380 return NoChange();
381 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000382
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000383 Zone zone;
384 ParseInfo parse_info(&zone, function);
385 CompilationInfo info(&parse_info);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100386 if (info_->is_deoptimization_enabled()) info.MarkAsDeoptimizationEnabled();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000387
388 if (!Compiler::ParseAndAnalyze(info.parse_info())) {
389 TRACE("Not inlining %s into %s because parsing failed\n",
Ben Murdoch097c5b22016-05-18 11:27:45 +0100390 shared_info->DebugName()->ToCString().get(),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000391 info_->shared_info()->DebugName()->ToCString().get());
392 if (info_->isolate()->has_pending_exception()) {
393 info_->isolate()->clear_pending_exception();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000394 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000395 return NoChange();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000396 }
397
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000398 // In strong mode, in case of too few arguments we need to throw a TypeError
399 // so we must not inline this call.
400 int parameter_count = info.literal()->parameter_count();
401 if (is_strong(info.language_mode()) &&
402 call.formal_arguments() < parameter_count) {
403 TRACE("Not inlining %s into %s because too few arguments for strong mode\n",
Ben Murdoch097c5b22016-05-18 11:27:45 +0100404 shared_info->DebugName()->ToCString().get(),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000405 info_->shared_info()->DebugName()->ToCString().get());
406 return NoChange();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000407 }
408
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000409 if (!Compiler::EnsureDeoptimizationSupport(&info)) {
410 TRACE("Not inlining %s into %s because deoptimization support failed\n",
Ben Murdoch097c5b22016-05-18 11:27:45 +0100411 shared_info->DebugName()->ToCString().get(),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000412 info_->shared_info()->DebugName()->ToCString().get());
413 return NoChange();
414 }
415 // Remember that we inlined this function. This needs to be called right
416 // after we ensure deoptimization support so that the code flusher
417 // does not remove the code with the deoptimization support.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100418 info_->AddInlinedFunction(shared_info);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000419
420 // ----------------------------------------------------------------
421 // After this point, we've made a decision to inline this function.
422 // We shall not bailout from inlining if we got here.
423
424 TRACE("Inlining %s into %s\n",
Ben Murdoch097c5b22016-05-18 11:27:45 +0100425 shared_info->DebugName()->ToCString().get(),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000426 info_->shared_info()->DebugName()->ToCString().get());
427
428 // TODO(mstarzinger): We could use the temporary zone for the graph because
429 // nodes are copied. This however leads to Zone-Types being allocated in the
430 // wrong zone and makes the engine explode at high speeds. Explosion bad!
431 Graph graph(jsgraph_->zone());
432 JSGraph jsgraph(info.isolate(), &graph, jsgraph_->common(),
433 jsgraph_->javascript(), jsgraph_->simplified(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000434 jsgraph_->machine());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400435 AstGraphBuilder graph_builder(local_zone_, &info, &jsgraph);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000436 graph_builder.CreateGraph(false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000437
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000438 CopyVisitor visitor(&graph, jsgraph_->graph(), &zone);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000439 visitor.CopyGraph();
440
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000441 Node* start = visitor.GetCopy(graph.start());
442 Node* end = visitor.GetCopy(graph.end());
443 Node* frame_state = call.frame_state_after();
444 Node* new_target = jsgraph_->UndefinedConstant();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000445
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000446 // Insert nodes around the call that model the behavior required for a
447 // constructor dispatch (allocate implicit receiver and check return value).
448 // This models the behavior usually accomplished by our {JSConstructStub}.
449 // Note that the context has to be the callers context (input to call node).
450 Node* receiver = jsgraph_->UndefinedConstant(); // Implicit receiver.
451 if (node->opcode() == IrOpcode::kJSCallConstruct &&
Ben Murdoch097c5b22016-05-18 11:27:45 +0100452 NeedsImplicitReceiver(shared_info)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000453 Node* effect = NodeProperties::GetEffectInput(node);
454 Node* context = NodeProperties::GetContextInput(node);
455 Node* create = jsgraph_->graph()->NewNode(
456 jsgraph_->javascript()->Create(), call.target(), call.new_target(),
457 context, call.frame_state_before(), effect);
458 NodeProperties::ReplaceEffectInput(node, create);
459 // Insert a check of the return value to determine whether the return value
460 // or the implicit receiver should be selected as a result of the call.
461 Node* check = jsgraph_->graph()->NewNode(
462 jsgraph_->javascript()->CallRuntime(Runtime::kInlineIsJSReceiver, 1),
463 node, context, node, start);
464 Node* select = jsgraph_->graph()->NewNode(
465 jsgraph_->common()->Select(MachineRepresentation::kTagged), check, node,
466 create);
467 NodeProperties::ReplaceUses(node, select, check, node, node);
468 NodeProperties::ReplaceValueInput(select, node, 1);
469 NodeProperties::ReplaceValueInput(check, node, 0);
470 NodeProperties::ReplaceEffectInput(check, node);
471 receiver = create; // The implicit receiver.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000472 }
473
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000474 // Swizzle the inputs of the {JSCallConstruct} node to look like inputs to a
475 // normal {JSCallFunction} node so that the rest of the inlining machinery
476 // behaves as if we were dealing with a regular function invocation.
477 if (node->opcode() == IrOpcode::kJSCallConstruct) {
478 new_target = call.new_target(); // Retrieve new target value input.
479 node->RemoveInput(call.formal_arguments() + 1); // Drop new target.
480 node->InsertInput(jsgraph_->graph()->zone(), 1, receiver);
481 // Insert a construct stub frame into the chain of frame states. This will
482 // reconstruct the proper frame when deoptimizing within the constructor.
483 frame_state = CreateArtificialFrameState(
484 node, frame_state, call.formal_arguments(),
485 FrameStateType::kConstructStub, info.shared_info());
486 }
487
488 // The inlinee specializes to the context from the JSFunction object.
489 // TODO(turbofan): We might want to load the context from the JSFunction at
490 // runtime in case we only know the SharedFunctionInfo once we have dynamic
491 // type feedback in the compiler.
492 Node* context = jsgraph_->Constant(handle(function->context()));
493
494 // Insert a JSConvertReceiver node for sloppy callees. Note that the context
495 // passed into this node has to be the callees context (loaded above). Note
496 // that the frame state passed to the JSConvertReceiver must be the frame
497 // state _before_ the call; it is not necessary to fiddle with the receiver
498 // in that frame state tho, as the conversion of the receiver can be repeated
499 // any number of times, it's not observable.
500 if (node->opcode() == IrOpcode::kJSCallFunction &&
Ben Murdoch097c5b22016-05-18 11:27:45 +0100501 is_sloppy(info.language_mode()) && !shared_info->native()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000502 const CallFunctionParameters& p = CallFunctionParametersOf(node->op());
503 Node* effect = NodeProperties::GetEffectInput(node);
504 Node* convert = jsgraph_->graph()->NewNode(
505 jsgraph_->javascript()->ConvertReceiver(p.convert_mode()),
506 call.receiver(), context, call.frame_state_before(), effect, start);
507 NodeProperties::ReplaceValueInput(node, convert, 1);
508 NodeProperties::ReplaceEffectInput(node, convert);
509 }
510
511 // Insert argument adaptor frame if required. The callees formal parameter
512 // count (i.e. value outputs of start node minus target, receiver, new target,
513 // arguments count and context) have to match the number of arguments passed
514 // to the call.
515 DCHECK_EQ(parameter_count, start->op()->ValueOutputCount() - 5);
516 if (call.formal_arguments() != parameter_count) {
517 frame_state = CreateArtificialFrameState(
518 node, frame_state, call.formal_arguments(),
Ben Murdoch097c5b22016-05-18 11:27:45 +0100519 FrameStateType::kArgumentsAdaptor, shared_info);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000520 }
521
522 return InlineCall(node, new_target, context, frame_state, start, end);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000523}
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400524
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000525} // namespace compiler
526} // namespace internal
527} // namespace v8