blob: e3254bd077220fd18a5d6796cf64ab988ed88919 [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
Ben Murdochda12d292016-06-02 14:46:10 +0100266Node* JSInliner::CreateTailCallerFrameState(Node* node, Node* frame_state) {
267 FrameStateInfo const& frame_info = OpParameter<FrameStateInfo>(frame_state);
268 Handle<SharedFunctionInfo> shared;
269 frame_info.shared_info().ToHandle(&shared);
270
271 Node* function = frame_state->InputAt(kFrameStateFunctionInput);
272
273 // If we are inlining a tail call drop caller's frame state and an
274 // arguments adaptor if it exists.
275 frame_state = NodeProperties::GetFrameStateInput(frame_state, 0);
276 if (frame_state->opcode() == IrOpcode::kFrameState) {
277 FrameStateInfo const& frame_info = OpParameter<FrameStateInfo>(frame_state);
278 if (frame_info.type() == FrameStateType::kArgumentsAdaptor) {
279 frame_state = NodeProperties::GetFrameStateInput(frame_state, 0);
280 }
281 }
282
283 const FrameStateFunctionInfo* state_info =
284 jsgraph_->common()->CreateFrameStateFunctionInfo(
285 FrameStateType::kTailCallerFunction, 0, 0, shared);
286
287 const Operator* op = jsgraph_->common()->FrameState(
288 BailoutId(-1), OutputFrameStateCombine::Ignore(), state_info);
289 const Operator* op0 = jsgraph_->common()->StateValues(0);
290 Node* node0 = jsgraph_->graph()->NewNode(op0);
291 return jsgraph_->graph()->NewNode(op, node0, node0, node0,
292 jsgraph_->UndefinedConstant(), function,
293 frame_state);
294}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000295
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000296namespace {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000297
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000298// TODO(mstarzinger,verwaest): Move this predicate onto SharedFunctionInfo?
Ben Murdoch097c5b22016-05-18 11:27:45 +0100299bool NeedsImplicitReceiver(Handle<SharedFunctionInfo> shared_info) {
300 DisallowHeapAllocation no_gc;
301 Isolate* const isolate = shared_info->GetIsolate();
302 Code* const construct_stub = shared_info->construct_stub();
Ben Murdochda12d292016-06-02 14:46:10 +0100303 return construct_stub != *isolate->builtins()->JSBuiltinsConstructStub() &&
304 construct_stub !=
305 *isolate->builtins()->JSBuiltinsConstructStubForDerived() &&
306 construct_stub != *isolate->builtins()->JSConstructStubApi();
Ben Murdoch097c5b22016-05-18 11:27:45 +0100307}
308
309bool IsNonConstructible(Handle<SharedFunctionInfo> shared_info) {
310 DisallowHeapAllocation no_gc;
311 Isolate* const isolate = shared_info->GetIsolate();
312 Code* const construct_stub = shared_info->construct_stub();
313 return construct_stub == *isolate->builtins()->ConstructedNonConstructable();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000314}
315
316} // namespace
317
318
319Reduction JSInliner::Reduce(Node* node) {
320 if (!IrOpcode::IsInlineeOpcode(node->opcode())) return NoChange();
321
322 // This reducer can handle both normal function calls as well a constructor
323 // calls whenever the target is a constant function object, as follows:
324 // - JSCallFunction(target:constant, receiver, args...)
325 // - JSCallConstruct(target:constant, args..., new.target)
326 HeapObjectMatcher match(node->InputAt(0));
327 if (!match.HasValue() || !match.Value()->IsJSFunction()) return NoChange();
328 Handle<JSFunction> function = Handle<JSFunction>::cast(match.Value());
329
330 return ReduceJSCall(node, function);
331}
332
333
334Reduction JSInliner::ReduceJSCall(Node* node, Handle<JSFunction> function) {
335 DCHECK(IrOpcode::IsInlineeOpcode(node->opcode()));
336 JSCallAccessor call(node);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100337 Handle<SharedFunctionInfo> shared_info(function->shared());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000338
339 // Function must be inlineable.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100340 if (!shared_info->IsInlineable()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000341 TRACE("Not inlining %s into %s because callee is not inlineable\n",
Ben Murdoch097c5b22016-05-18 11:27:45 +0100342 shared_info->DebugName()->ToCString().get(),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000343 info_->shared_info()->DebugName()->ToCString().get());
344 return NoChange();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000345 }
346
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000347 // Constructor must be constructable.
348 if (node->opcode() == IrOpcode::kJSCallConstruct &&
Ben Murdoch097c5b22016-05-18 11:27:45 +0100349 IsNonConstructible(shared_info)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000350 TRACE("Not inlining %s into %s because constructor is not constructable.\n",
Ben Murdoch097c5b22016-05-18 11:27:45 +0100351 shared_info->DebugName()->ToCString().get(),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000352 info_->shared_info()->DebugName()->ToCString().get());
353 return NoChange();
354 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000355
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000356 // Class constructors are callable, but [[Call]] will raise an exception.
357 // See ES6 section 9.2.1 [[Call]] ( thisArgument, argumentsList ).
358 if (node->opcode() == IrOpcode::kJSCallFunction &&
Ben Murdoch097c5b22016-05-18 11:27:45 +0100359 IsClassConstructor(shared_info->kind())) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000360 TRACE("Not inlining %s into %s because callee is a class constructor.\n",
Ben Murdoch097c5b22016-05-18 11:27:45 +0100361 shared_info->DebugName()->ToCString().get(),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000362 info_->shared_info()->DebugName()->ToCString().get());
363 return NoChange();
364 }
365
366 // Function contains break points.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100367 if (shared_info->HasDebugInfo()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000368 TRACE("Not inlining %s into %s because callee may contain break points\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();
372 }
373
374 // Disallow cross native-context inlining for now. This means that all parts
375 // of the resulting code will operate on the same global object.
376 // This also prevents cross context leaks for asm.js code, where we could
377 // inline functions from a different context and hold on to that context (and
378 // closure) from the code object.
379 // TODO(turbofan): We might want to revisit this restriction later when we
380 // have a need for this, and we know how to model different native contexts
381 // in the same graph in a compositional way.
382 if (function->context()->native_context() !=
383 info_->context()->native_context()) {
384 TRACE("Not inlining %s into %s because of different native contexts\n",
Ben Murdoch097c5b22016-05-18 11:27:45 +0100385 shared_info->DebugName()->ToCString().get(),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000386 info_->shared_info()->DebugName()->ToCString().get());
387 return NoChange();
388 }
389
390 // TODO(turbofan): TranslatedState::GetAdaptedArguments() currently relies on
391 // not inlining recursive functions. We might want to relax that at some
392 // point.
393 for (Node* frame_state = call.frame_state_after();
394 frame_state->opcode() == IrOpcode::kFrameState;
395 frame_state = frame_state->InputAt(kFrameStateOuterStateInput)) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100396 FrameStateInfo const& frame_info = OpParameter<FrameStateInfo>(frame_state);
397 Handle<SharedFunctionInfo> frame_shared_info;
398 if (frame_info.shared_info().ToHandle(&frame_shared_info) &&
399 *frame_shared_info == *shared_info) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000400 TRACE("Not inlining %s into %s because call is recursive\n",
Ben Murdoch097c5b22016-05-18 11:27:45 +0100401 shared_info->DebugName()->ToCString().get(),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000402 info_->shared_info()->DebugName()->ToCString().get());
403 return NoChange();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000404 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000405 }
406
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000407 // TODO(turbofan): Inlining into a try-block is not yet supported.
408 if (NodeProperties::IsExceptionalCall(node)) {
409 TRACE("Not inlining %s into %s because of surrounding try-block\n",
Ben Murdoch097c5b22016-05-18 11:27:45 +0100410 shared_info->DebugName()->ToCString().get(),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000411 info_->shared_info()->DebugName()->ToCString().get());
412 return NoChange();
413 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000414
Ben Murdochda12d292016-06-02 14:46:10 +0100415 Zone zone(info_->isolate()->allocator());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000416 ParseInfo parse_info(&zone, function);
417 CompilationInfo info(&parse_info);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100418 if (info_->is_deoptimization_enabled()) info.MarkAsDeoptimizationEnabled();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000419
420 if (!Compiler::ParseAndAnalyze(info.parse_info())) {
421 TRACE("Not inlining %s into %s because parsing failed\n",
Ben Murdoch097c5b22016-05-18 11:27:45 +0100422 shared_info->DebugName()->ToCString().get(),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000423 info_->shared_info()->DebugName()->ToCString().get());
424 if (info_->isolate()->has_pending_exception()) {
425 info_->isolate()->clear_pending_exception();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000426 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000427 return NoChange();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000428 }
429
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000430 if (!Compiler::EnsureDeoptimizationSupport(&info)) {
431 TRACE("Not inlining %s into %s because deoptimization support failed\n",
Ben Murdoch097c5b22016-05-18 11:27:45 +0100432 shared_info->DebugName()->ToCString().get(),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000433 info_->shared_info()->DebugName()->ToCString().get());
434 return NoChange();
435 }
436 // Remember that we inlined this function. This needs to be called right
437 // after we ensure deoptimization support so that the code flusher
438 // does not remove the code with the deoptimization support.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100439 info_->AddInlinedFunction(shared_info);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000440
441 // ----------------------------------------------------------------
442 // After this point, we've made a decision to inline this function.
443 // We shall not bailout from inlining if we got here.
444
445 TRACE("Inlining %s into %s\n",
Ben Murdoch097c5b22016-05-18 11:27:45 +0100446 shared_info->DebugName()->ToCString().get(),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000447 info_->shared_info()->DebugName()->ToCString().get());
448
449 // TODO(mstarzinger): We could use the temporary zone for the graph because
450 // nodes are copied. This however leads to Zone-Types being allocated in the
451 // wrong zone and makes the engine explode at high speeds. Explosion bad!
452 Graph graph(jsgraph_->zone());
453 JSGraph jsgraph(info.isolate(), &graph, jsgraph_->common(),
454 jsgraph_->javascript(), jsgraph_->simplified(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000455 jsgraph_->machine());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400456 AstGraphBuilder graph_builder(local_zone_, &info, &jsgraph);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000457 graph_builder.CreateGraph(false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000458
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000459 CopyVisitor visitor(&graph, jsgraph_->graph(), &zone);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000460 visitor.CopyGraph();
461
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000462 Node* start = visitor.GetCopy(graph.start());
463 Node* end = visitor.GetCopy(graph.end());
464 Node* frame_state = call.frame_state_after();
465 Node* new_target = jsgraph_->UndefinedConstant();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000466
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000467 // Insert nodes around the call that model the behavior required for a
468 // constructor dispatch (allocate implicit receiver and check return value).
469 // This models the behavior usually accomplished by our {JSConstructStub}.
470 // Note that the context has to be the callers context (input to call node).
471 Node* receiver = jsgraph_->UndefinedConstant(); // Implicit receiver.
472 if (node->opcode() == IrOpcode::kJSCallConstruct &&
Ben Murdoch097c5b22016-05-18 11:27:45 +0100473 NeedsImplicitReceiver(shared_info)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000474 Node* effect = NodeProperties::GetEffectInput(node);
475 Node* context = NodeProperties::GetContextInput(node);
476 Node* create = jsgraph_->graph()->NewNode(
477 jsgraph_->javascript()->Create(), call.target(), call.new_target(),
478 context, call.frame_state_before(), effect);
479 NodeProperties::ReplaceEffectInput(node, create);
480 // Insert a check of the return value to determine whether the return value
481 // or the implicit receiver should be selected as a result of the call.
482 Node* check = jsgraph_->graph()->NewNode(
483 jsgraph_->javascript()->CallRuntime(Runtime::kInlineIsJSReceiver, 1),
484 node, context, node, start);
485 Node* select = jsgraph_->graph()->NewNode(
486 jsgraph_->common()->Select(MachineRepresentation::kTagged), check, node,
487 create);
488 NodeProperties::ReplaceUses(node, select, check, node, node);
489 NodeProperties::ReplaceValueInput(select, node, 1);
490 NodeProperties::ReplaceValueInput(check, node, 0);
491 NodeProperties::ReplaceEffectInput(check, node);
492 receiver = create; // The implicit receiver.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000493 }
494
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000495 // Swizzle the inputs of the {JSCallConstruct} node to look like inputs to a
496 // normal {JSCallFunction} node so that the rest of the inlining machinery
497 // behaves as if we were dealing with a regular function invocation.
498 if (node->opcode() == IrOpcode::kJSCallConstruct) {
499 new_target = call.new_target(); // Retrieve new target value input.
500 node->RemoveInput(call.formal_arguments() + 1); // Drop new target.
501 node->InsertInput(jsgraph_->graph()->zone(), 1, receiver);
502 // Insert a construct stub frame into the chain of frame states. This will
503 // reconstruct the proper frame when deoptimizing within the constructor.
504 frame_state = CreateArtificialFrameState(
505 node, frame_state, call.formal_arguments(),
506 FrameStateType::kConstructStub, info.shared_info());
507 }
508
509 // The inlinee specializes to the context from the JSFunction object.
510 // TODO(turbofan): We might want to load the context from the JSFunction at
511 // runtime in case we only know the SharedFunctionInfo once we have dynamic
512 // type feedback in the compiler.
513 Node* context = jsgraph_->Constant(handle(function->context()));
514
515 // Insert a JSConvertReceiver node for sloppy callees. Note that the context
516 // passed into this node has to be the callees context (loaded above). Note
517 // that the frame state passed to the JSConvertReceiver must be the frame
518 // state _before_ the call; it is not necessary to fiddle with the receiver
519 // in that frame state tho, as the conversion of the receiver can be repeated
520 // any number of times, it's not observable.
521 if (node->opcode() == IrOpcode::kJSCallFunction &&
Ben Murdoch097c5b22016-05-18 11:27:45 +0100522 is_sloppy(info.language_mode()) && !shared_info->native()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000523 const CallFunctionParameters& p = CallFunctionParametersOf(node->op());
524 Node* effect = NodeProperties::GetEffectInput(node);
525 Node* convert = jsgraph_->graph()->NewNode(
526 jsgraph_->javascript()->ConvertReceiver(p.convert_mode()),
527 call.receiver(), context, call.frame_state_before(), effect, start);
528 NodeProperties::ReplaceValueInput(node, convert, 1);
529 NodeProperties::ReplaceEffectInput(node, convert);
530 }
531
Ben Murdochda12d292016-06-02 14:46:10 +0100532 // If we are inlining a JS call at tail position then we have to pop current
533 // frame state and its potential arguments adaptor frame state in order to
534 // make the call stack be consistent with non-inlining case.
535 // After that we add a tail caller frame state which lets deoptimizer handle
536 // the case when the outermost function inlines a tail call (it should remove
537 // potential arguments adaptor frame that belongs to outermost function when
538 // deopt happens).
539 if (node->opcode() == IrOpcode::kJSCallFunction) {
540 const CallFunctionParameters& p = CallFunctionParametersOf(node->op());
541 if (p.tail_call_mode() == TailCallMode::kAllow) {
542 frame_state = CreateTailCallerFrameState(node, frame_state);
543 }
544 }
545
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000546 // Insert argument adaptor frame if required. The callees formal parameter
547 // count (i.e. value outputs of start node minus target, receiver, new target,
548 // arguments count and context) have to match the number of arguments passed
549 // to the call.
Ben Murdochda12d292016-06-02 14:46:10 +0100550 int parameter_count = info.literal()->parameter_count();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000551 DCHECK_EQ(parameter_count, start->op()->ValueOutputCount() - 5);
552 if (call.formal_arguments() != parameter_count) {
553 frame_state = CreateArtificialFrameState(
554 node, frame_state, call.formal_arguments(),
Ben Murdoch097c5b22016-05-18 11:27:45 +0100555 FrameStateType::kArgumentsAdaptor, shared_info);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000556 }
557
558 return InlineCall(node, new_target, context, frame_state, start, end);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000559}
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400560
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000561} // namespace compiler
562} // namespace internal
563} // namespace v8