blob: d143382dfd047a71ce3f868e92f308bd57f63d4c [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
Emily Bernierd0a1eb72015-03-24 16:35:39 -04005#include "src/ast.h"
6#include "src/ast-numbering.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#include "src/compiler/access-builder.h"
8#include "src/compiler/ast-graph-builder.h"
9#include "src/compiler/common-operator.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000010#include "src/compiler/graph-inl.h"
11#include "src/compiler/graph-visualizer.h"
12#include "src/compiler/js-inlining.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040013#include "src/compiler/js-intrinsic-builder.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014#include "src/compiler/js-operator.h"
15#include "src/compiler/node-aux-data-inl.h"
16#include "src/compiler/node-matchers.h"
17#include "src/compiler/node-properties-inl.h"
18#include "src/compiler/simplified-operator.h"
19#include "src/compiler/typer.h"
20#include "src/full-codegen.h"
21#include "src/parser.h"
22#include "src/rewriter.h"
23#include "src/scopes.h"
24
25
26namespace v8 {
27namespace internal {
28namespace compiler {
29
30class InlinerVisitor : public NullNodeVisitor {
31 public:
32 explicit InlinerVisitor(JSInliner* inliner) : inliner_(inliner) {}
33
Emily Bernierd0a1eb72015-03-24 16:35:39 -040034 void Post(Node* node) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000035 switch (node->opcode()) {
36 case IrOpcode::kJSCallFunction:
Emily Bernierd0a1eb72015-03-24 16:35:39 -040037 inliner_->TryInlineJSCall(node);
38 break;
39 case IrOpcode::kJSCallRuntime:
40 if (FLAG_turbo_inlining_intrinsics) {
41 inliner_->TryInlineRuntimeCall(node);
42 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000043 break;
44 default:
45 break;
46 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000047 }
48
49 private:
50 JSInliner* inliner_;
51};
52
53
54void JSInliner::Inline() {
55 InlinerVisitor visitor(this);
56 jsgraph_->graph()->VisitNodeInputsFromEnd(&visitor);
57}
58
59
Ben Murdochb8a8cc12014-11-26 15:28:44 +000060// A facade on a JSFunction's graph to facilitate inlining. It assumes the
61// that the function graph has only one return statement, and provides
62// {UnifyReturn} to convert a function graph to that end.
63class Inlinee {
64 public:
65 Inlinee(Node* start, Node* end) : start_(start), end_(end) {}
66
67 // Returns the last regular control node, that is
68 // the last control node before the end node.
69 Node* end_block() { return NodeProperties::GetControlInput(unique_return()); }
70
71 // Return the effect output of the graph,
72 // that is the effect input of the return statement of the inlinee.
73 Node* effect_output() {
74 return NodeProperties::GetEffectInput(unique_return());
75 }
76 // Return the value output of the graph,
77 // that is the value input of the return statement of the inlinee.
78 Node* value_output() {
79 return NodeProperties::GetValueInput(unique_return(), 0);
80 }
81 // Return the unique return statement of the graph.
82 Node* unique_return() {
83 Node* unique_return = NodeProperties::GetControlInput(end_);
84 DCHECK_EQ(IrOpcode::kReturn, unique_return->opcode());
85 return unique_return;
86 }
87
88 // Counts JSFunction, Receiver, arguments, context but not effect, control.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040089 size_t total_parameters() { return start_->op()->ValueOutputCount(); }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000090
91 // Counts only formal parameters.
92 size_t formal_parameters() {
93 DCHECK_GE(total_parameters(), 3);
94 return total_parameters() - 3;
95 }
96
97 // Inline this graph at {call}, use {jsgraph} and its zone to create
98 // any new nodes.
99 void InlineAtCall(JSGraph* jsgraph, Node* call);
100
101 // Ensure that only a single return reaches the end node.
102 static void UnifyReturn(JSGraph* jsgraph);
103
104 private:
105 Node* start_;
106 Node* end_;
107};
108
109
110void Inlinee::UnifyReturn(JSGraph* jsgraph) {
111 Graph* graph = jsgraph->graph();
112
113 Node* final_merge = NodeProperties::GetControlInput(graph->end(), 0);
114 if (final_merge->opcode() == IrOpcode::kReturn) {
115 // nothing to do
116 return;
117 }
118 DCHECK_EQ(IrOpcode::kMerge, final_merge->opcode());
119
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400120 int predecessors = final_merge->op()->ControlInputCount();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000121
122 const Operator* op_phi = jsgraph->common()->Phi(kMachAnyTagged, predecessors);
123 const Operator* op_ephi = jsgraph->common()->EffectPhi(predecessors);
124
125 NodeVector values(jsgraph->zone());
126 NodeVector effects(jsgraph->zone());
127 // Iterate over all control flow predecessors,
128 // which must be return statements.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400129 for (Edge edge : final_merge->input_edges()) {
130 Node* input = edge.to();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000131 switch (input->opcode()) {
132 case IrOpcode::kReturn:
133 values.push_back(NodeProperties::GetValueInput(input, 0));
134 effects.push_back(NodeProperties::GetEffectInput(input));
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400135 edge.UpdateTo(NodeProperties::GetControlInput(input));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000136 input->RemoveAllInputs();
137 break;
138 default:
139 UNREACHABLE();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000140 break;
141 }
142 }
143 values.push_back(final_merge);
144 effects.push_back(final_merge);
145 Node* phi =
146 graph->NewNode(op_phi, static_cast<int>(values.size()), &values.front());
147 Node* ephi = graph->NewNode(op_ephi, static_cast<int>(effects.size()),
148 &effects.front());
149 Node* new_return =
150 graph->NewNode(jsgraph->common()->Return(), phi, ephi, final_merge);
151 graph->end()->ReplaceInput(0, new_return);
152}
153
154
155class CopyVisitor : public NullNodeVisitor {
156 public:
157 CopyVisitor(Graph* source_graph, Graph* target_graph, Zone* temp_zone)
158 : copies_(source_graph->NodeCount(), NULL, temp_zone),
159 sentinels_(source_graph->NodeCount(), NULL, temp_zone),
160 source_graph_(source_graph),
161 target_graph_(target_graph),
162 temp_zone_(temp_zone),
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400163 sentinel_op_(IrOpcode::kDead, Operator::kNoProperties, "sentinel", 0, 0,
164 0, 0, 0, 0) {}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000165
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400166 void Post(Node* original) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000167 NodeVector inputs(temp_zone_);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400168 for (Node* const node : original->inputs()) {
169 inputs.push_back(GetCopy(node));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000170 }
171
172 // Reuse the operator in the copy. This assumes that op lives in a zone
173 // that lives longer than graph()'s zone.
174 Node* copy =
175 target_graph_->NewNode(original->op(), static_cast<int>(inputs.size()),
176 (inputs.empty() ? NULL : &inputs.front()));
177 copies_[original->id()] = copy;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000178 }
179
180 Node* GetCopy(Node* original) {
181 Node* copy = copies_[original->id()];
182 if (copy == NULL) {
183 copy = GetSentinel(original);
184 }
185 DCHECK_NE(NULL, copy);
186 return copy;
187 }
188
189 void CopyGraph() {
190 source_graph_->VisitNodeInputsFromEnd(this);
191 ReplaceSentinels();
192 }
193
194 const NodeVector& copies() { return copies_; }
195
196 private:
197 void ReplaceSentinels() {
198 for (NodeId id = 0; id < source_graph_->NodeCount(); ++id) {
199 Node* sentinel = sentinels_[id];
200 if (sentinel == NULL) continue;
201 Node* copy = copies_[id];
202 DCHECK_NE(NULL, copy);
203 sentinel->ReplaceUses(copy);
204 }
205 }
206
207 Node* GetSentinel(Node* original) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400208 if (sentinels_[original->id()] == NULL) {
209 sentinels_[original->id()] = target_graph_->NewNode(&sentinel_op_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000210 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400211 return sentinels_[original->id()];
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000212 }
213
214 NodeVector copies_;
215 NodeVector sentinels_;
216 Graph* source_graph_;
217 Graph* target_graph_;
218 Zone* temp_zone_;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400219 Operator sentinel_op_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000220};
221
222
223void Inlinee::InlineAtCall(JSGraph* jsgraph, Node* call) {
224 // The scheduler is smart enough to place our code; we just ensure {control}
225 // becomes the control input of the start of the inlinee.
226 Node* control = NodeProperties::GetControlInput(call);
227
228 // The inlinee uses the context from the JSFunction object. This will
229 // also be the effect dependency for the inlinee as it produces an effect.
230 SimplifiedOperatorBuilder simplified(jsgraph->zone());
231 Node* context = jsgraph->graph()->NewNode(
232 simplified.LoadField(AccessBuilder::ForJSFunctionContext()),
233 NodeProperties::GetValueInput(call, 0),
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400234 NodeProperties::GetEffectInput(call), control);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000235
236 // Context is last argument.
237 int inlinee_context_index = static_cast<int>(total_parameters()) - 1;
238 // {inliner_inputs} counts JSFunction, Receiver, arguments, but not
239 // context, effect, control.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400240 int inliner_inputs = call->op()->ValueInputCount();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000241 // Iterate over all uses of the start node.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400242 for (Edge edge : start_->use_edges()) {
243 Node* use = edge.from();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000244 switch (use->opcode()) {
245 case IrOpcode::kParameter: {
246 int index = 1 + OpParameter<int>(use->op());
247 if (index < inliner_inputs && index < inlinee_context_index) {
248 // There is an input from the call, and the index is a value
249 // projection but not the context, so rewire the input.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400250 NodeProperties::ReplaceWithValue(use, call->InputAt(index));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000251 } else if (index == inlinee_context_index) {
252 // This is the context projection, rewire it to the context from the
253 // JSFunction object.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400254 NodeProperties::ReplaceWithValue(use, context);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000255 } else if (index < inlinee_context_index) {
256 // Call has fewer arguments than required, fill with undefined.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400257 NodeProperties::ReplaceWithValue(use, jsgraph->UndefinedConstant());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000258 } else {
259 // We got too many arguments, discard for now.
260 // TODO(sigurds): Fix to treat arguments array correctly.
261 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000262 break;
263 }
264 default:
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400265 if (NodeProperties::IsEffectEdge(edge)) {
266 edge.UpdateTo(context);
267 } else if (NodeProperties::IsControlEdge(edge)) {
268 edge.UpdateTo(control);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000269 } else {
270 UNREACHABLE();
271 }
272 break;
273 }
274 }
275
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400276 NodeProperties::ReplaceWithValue(call, value_output(), effect_output());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000277 call->RemoveAllInputs();
278 DCHECK_EQ(0, call->UseCount());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000279}
280
281
282// TODO(turbofan) Provide such accessors for every node, possibly even
283// generate them.
284class JSCallFunctionAccessor {
285 public:
286 explicit JSCallFunctionAccessor(Node* call) : call_(call) {
287 DCHECK_EQ(IrOpcode::kJSCallFunction, call->opcode());
288 }
289
290 Node* jsfunction() { return call_->InputAt(0); }
291
292 Node* receiver() { return call_->InputAt(1); }
293
294 Node* formal_argument(size_t index) {
295 DCHECK(index < formal_arguments());
296 return call_->InputAt(static_cast<int>(2 + index));
297 }
298
299 size_t formal_arguments() {
300 // {value_inputs} includes jsfunction and receiver.
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400301 size_t value_inputs = call_->op()->ValueInputCount();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000302 DCHECK_GE(call_->InputCount(), 2);
303 return value_inputs - 2;
304 }
305
306 Node* frame_state() { return NodeProperties::GetFrameStateInput(call_); }
307
308 private:
309 Node* call_;
310};
311
312
313void JSInliner::AddClosureToFrameState(Node* frame_state,
314 Handle<JSFunction> jsfunction) {
315 FrameStateCallInfo call_info = OpParameter<FrameStateCallInfo>(frame_state);
316 const Operator* op = jsgraph_->common()->FrameState(
317 FrameStateType::JS_FRAME, call_info.bailout_id(),
318 call_info.state_combine(), jsfunction);
319 frame_state->set_op(op);
320}
321
322
323Node* JSInliner::CreateArgumentsAdaptorFrameState(JSCallFunctionAccessor* call,
324 Handle<JSFunction> jsfunction,
325 Zone* temp_zone) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400326 const Operator* op = jsgraph_->common()->FrameState(
327 FrameStateType::ARGUMENTS_ADAPTOR, BailoutId(-1),
328 OutputFrameStateCombine::Ignore(), jsfunction);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000329 const Operator* op0 = jsgraph_->common()->StateValues(0);
330 Node* node0 = jsgraph_->graph()->NewNode(op0);
331 NodeVector params(temp_zone);
332 params.push_back(call->receiver());
333 for (size_t argument = 0; argument != call->formal_arguments(); ++argument) {
334 params.push_back(call->formal_argument(argument));
335 }
336 const Operator* op_param =
337 jsgraph_->common()->StateValues(static_cast<int>(params.size()));
338 Node* params_node = jsgraph_->graph()->NewNode(
339 op_param, static_cast<int>(params.size()), &params.front());
340 return jsgraph_->graph()->NewNode(op, params_node, node0, node0,
341 jsgraph_->UndefinedConstant(),
342 call->frame_state());
343}
344
345
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400346void JSInliner::TryInlineJSCall(Node* call_node) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000347 JSCallFunctionAccessor call(call_node);
348
349 HeapObjectMatcher<JSFunction> match(call.jsfunction());
350 if (!match.HasValue()) {
351 return;
352 }
353
354 Handle<JSFunction> function = match.Value().handle();
355
356 if (function->shared()->native()) {
357 if (FLAG_trace_turbo_inlining) {
358 SmartArrayPointer<char> name =
359 function->shared()->DebugName()->ToCString();
360 PrintF("Not Inlining %s into %s because inlinee is native\n", name.get(),
361 info_->shared_info()->DebugName()->ToCString().get());
362 }
363 return;
364 }
365
366 CompilationInfoWithZone info(function);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400367 // TODO(wingo): ParseAndAnalyze can fail due to stack overflow.
368 CHECK(Compiler::ParseAndAnalyze(&info));
369 CHECK(Compiler::EnsureDeoptimizationSupport(&info));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000370
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400371 if (info.scope()->arguments() != NULL && info.strict_mode() != STRICT) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000372 // For now do not inline functions that use their arguments array.
373 SmartArrayPointer<char> name = function->shared()->DebugName()->ToCString();
374 if (FLAG_trace_turbo_inlining) {
375 PrintF(
376 "Not Inlining %s into %s because inlinee uses arguments "
377 "array\n",
378 name.get(), info_->shared_info()->DebugName()->ToCString().get());
379 }
380 return;
381 }
382
383 if (FLAG_trace_turbo_inlining) {
384 SmartArrayPointer<char> name = function->shared()->DebugName()->ToCString();
385 PrintF("Inlining %s into %s\n", name.get(),
386 info_->shared_info()->DebugName()->ToCString().get());
387 }
388
389 Graph graph(info.zone());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400390 JSGraph jsgraph(&graph, jsgraph_->common(), jsgraph_->javascript(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000391 jsgraph_->machine());
392
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400393 AstGraphBuilder graph_builder(local_zone_, &info, &jsgraph);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000394 graph_builder.CreateGraph();
395 Inlinee::UnifyReturn(&jsgraph);
396
397 CopyVisitor visitor(&graph, jsgraph_->graph(), info.zone());
398 visitor.CopyGraph();
399
400 Inlinee inlinee(visitor.GetCopy(graph.start()), visitor.GetCopy(graph.end()));
401
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400402 if (FLAG_turbo_deoptimization) {
403 Node* outer_frame_state = call.frame_state();
404 // Insert argument adaptor frame if required.
405 if (call.formal_arguments() != inlinee.formal_parameters()) {
406 outer_frame_state =
407 CreateArgumentsAdaptorFrameState(&call, function, info.zone());
408 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000409
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400410 for (NodeVectorConstIter it = visitor.copies().begin();
411 it != visitor.copies().end(); ++it) {
412 Node* node = *it;
413 if (node != NULL && node->opcode() == IrOpcode::kFrameState) {
414 AddClosureToFrameState(node, function);
415 NodeProperties::ReplaceFrameStateInput(node, outer_frame_state);
416 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000417 }
418 }
419
420 inlinee.InlineAtCall(jsgraph_, call_node);
421}
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400422
423
424class JSCallRuntimeAccessor {
425 public:
426 explicit JSCallRuntimeAccessor(Node* call) : call_(call) {
427 DCHECK_EQ(IrOpcode::kJSCallRuntime, call->opcode());
428 }
429
430 Node* formal_argument(size_t index) {
431 DCHECK(index < formal_arguments());
432 return call_->InputAt(static_cast<int>(index));
433 }
434
435 size_t formal_arguments() {
436 size_t value_inputs = call_->op()->ValueInputCount();
437 return value_inputs;
438 }
439
440 Node* frame_state() const {
441 return NodeProperties::GetFrameStateInput(call_);
442 }
443 Node* context() const { return NodeProperties::GetContextInput(call_); }
444 Node* control() const { return NodeProperties::GetControlInput(call_); }
445 Node* effect() const { return NodeProperties::GetEffectInput(call_); }
446
447 const Runtime::Function* function() const {
448 return Runtime::FunctionForId(CallRuntimeParametersOf(call_->op()).id());
449 }
450
451 NodeVector inputs(Zone* zone) const {
452 NodeVector inputs(zone);
453 for (Node* const node : call_->inputs()) {
454 inputs.push_back(node);
455 }
456 return inputs;
457 }
458
459 private:
460 Node* call_;
461};
462
463
464void JSInliner::TryInlineRuntimeCall(Node* call_node) {
465 JSCallRuntimeAccessor call(call_node);
466 const Runtime::Function* f = call.function();
467
468 if (f->intrinsic_type != Runtime::IntrinsicType::INLINE) {
469 return;
470 }
471
472 JSIntrinsicBuilder intrinsic_builder(jsgraph_);
473
474 ResultAndEffect r = intrinsic_builder.BuildGraphFor(
475 f->function_id, call.inputs(jsgraph_->zone()));
476
477 if (r.first != NULL) {
478 if (FLAG_trace_turbo_inlining) {
479 PrintF("Inlining %s into %s\n", f->name,
480 info_->shared_info()->DebugName()->ToCString().get());
481 }
482 NodeProperties::ReplaceWithValue(call_node, r.first, r.second);
483 call_node->RemoveAllInputs();
484 DCHECK_EQ(0, call_node->UseCount());
485 }
486}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000487}
488}
489} // namespace v8::internal::compiler