blob: c87be6c236266a710a9d821fce1ed8efd98b9572 [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 Murdoch13e2dad2016-09-16 13:49:30 +01007#include "src/ast/ast.h"
Ben Murdochf3b273f2017-01-17 12:11:28 +00008#include "src/compilation-info.h"
Ben Murdoch014dc512016-03-22 12:00:34 +00009#include "src/compiler.h"
Ben Murdochf3b273f2017-01-17 12:11:28 +000010#include "src/compiler/all-nodes.h"
Ben Murdochf3b273f2017-01-17 12:11:28 +000011#include "src/compiler/bytecode-graph-builder.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012#include "src/compiler/common-operator.h"
Ben Murdoch62ed6312017-06-06 11:06:27 +010013#include "src/compiler/compiler-source-position-table.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 Murdoch014dc512016-03-22 12:00:34 +000020#include "src/isolate-inl.h"
Ben Murdochf91f0612016-11-29 16:50:11 +000021#include "src/parsing/parse-info.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000022
23namespace v8 {
24namespace internal {
25namespace compiler {
26
Ben Murdoch014dc512016-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 Murdoch014dc512016-03-22 12:00:34 +000032
33// Provides convenience accessors for the common layout of nodes having either
Ben Murdoch62ed6312017-06-06 11:06:27 +010034// the {JSCall} or the {JSConstruct} operator.
Ben Murdoch014dc512016-03-22 12:00:34 +000035class JSCallAccessor {
36 public:
37 explicit JSCallAccessor(Node* call) : call_(call) {
Ben Murdoch62ed6312017-06-06 11:06:27 +010038 DCHECK(call->opcode() == IrOpcode::kJSCall ||
39 call->opcode() == IrOpcode::kJSConstruct);
Ben Murdoch014dc512016-03-22 12:00:34 +000040 }
41
42 Node* target() {
Ben Murdoch62ed6312017-06-06 11:06:27 +010043 // Both, {JSCall} and {JSConstruct}, have same layout here.
Ben Murdoch014dc512016-03-22 12:00:34 +000044 return call_->InputAt(0);
45 }
46
47 Node* receiver() {
Ben Murdoch62ed6312017-06-06 11:06:27 +010048 DCHECK_EQ(IrOpcode::kJSCall, call_->opcode());
Ben Murdoch014dc512016-03-22 12:00:34 +000049 return call_->InputAt(1);
50 }
51
52 Node* new_target() {
Ben Murdoch62ed6312017-06-06 11:06:27 +010053 DCHECK_EQ(IrOpcode::kJSConstruct, call_->opcode());
Ben Murdoch014dc512016-03-22 12:00:34 +000054 return call_->InputAt(formal_arguments() + 1);
55 }
56
Ben Murdoch13e2dad2016-09-16 13:49:30 +010057 Node* frame_state() {
Ben Murdoch62ed6312017-06-06 11:06:27 +010058 // Both, {JSCall} and {JSConstruct}, have frame state.
Ben Murdochf91f0612016-11-29 16:50:11 +000059 return NodeProperties::GetFrameStateInput(call_);
Ben Murdoch014dc512016-03-22 12:00:34 +000060 }
61
62 int formal_arguments() {
Ben Murdoch62ed6312017-06-06 11:06:27 +010063 // Both, {JSCall} and {JSConstruct}, have two extra inputs:
64 // - JSConstruct: Includes target function and new target.
65 // - JSCall: Includes target function and receiver.
Ben Murdoch014dc512016-03-22 12:00:34 +000066 return call_->op()->ValueInputCount() - 2;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000067 }
68
Ben Murdochf3b273f2017-01-17 12:11:28 +000069 float frequency() const {
Ben Murdoch62ed6312017-06-06 11:06:27 +010070 return (call_->opcode() == IrOpcode::kJSCall)
71 ? CallParametersOf(call_->op()).frequency()
72 : ConstructParametersOf(call_->op()).frequency();
Ben Murdochf3b273f2017-01-17 12:11:28 +000073 }
74
Ben Murdochb8a8cc12014-11-26 15:28:44 +000075 private:
Ben Murdoch014dc512016-03-22 12:00:34 +000076 Node* call_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000077};
78
Ben Murdoch014dc512016-03-22 12:00:34 +000079Reduction JSInliner::InlineCall(Node* call, Node* new_target, Node* context,
Ben Murdochf3b273f2017-01-17 12:11:28 +000080 Node* frame_state, Node* start, Node* end,
81 Node* exception_target,
82 const NodeVector& uncaught_subcalls) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000083 // The scheduler is smart enough to place our code; we just ensure {control}
Ben Murdoch014dc512016-03-22 12:00:34 +000084 // becomes the control input of the start of the inlinee, and {effect} becomes
85 // the effect input of the start of the inlinee.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000086 Node* control = NodeProperties::GetControlInput(call);
Ben Murdoch014dc512016-03-22 12:00:34 +000087 Node* effect = NodeProperties::GetEffectInput(call);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000088
Ben Murdoch014dc512016-03-22 12:00:34 +000089 int const inlinee_new_target_index =
90 static_cast<int>(start->op()->ValueOutputCount()) - 3;
91 int const inlinee_arity_index =
92 static_cast<int>(start->op()->ValueOutputCount()) - 2;
93 int const inlinee_context_index =
94 static_cast<int>(start->op()->ValueOutputCount()) - 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000095
Ben Murdoch014dc512016-03-22 12:00:34 +000096 // {inliner_inputs} counts JSFunction, receiver, arguments, but not
97 // new target value, argument count, context, effect or control.
Emily Bernier958fae72015-03-24 16:35:39 -040098 int inliner_inputs = call->op()->ValueInputCount();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000099 // Iterate over all uses of the start node.
Ben Murdoch014dc512016-03-22 12:00:34 +0000100 for (Edge edge : start->use_edges()) {
Emily Bernier958fae72015-03-24 16:35:39 -0400101 Node* use = edge.from();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000102 switch (use->opcode()) {
103 case IrOpcode::kParameter: {
Ben Murdoch014dc512016-03-22 12:00:34 +0000104 int index = 1 + ParameterIndexOf(use->op());
105 DCHECK_LE(index, inlinee_context_index);
106 if (index < inliner_inputs && index < inlinee_new_target_index) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000107 // There is an input from the call, and the index is a value
108 // projection but not the context, so rewire the input.
Ben Murdoch014dc512016-03-22 12:00:34 +0000109 Replace(use, call->InputAt(index));
110 } else if (index == inlinee_new_target_index) {
111 // The projection is requesting the new target value.
112 Replace(use, new_target);
113 } else if (index == inlinee_arity_index) {
114 // The projection is requesting the number of arguments.
Ben Murdochc8c1d9e2017-03-08 14:04:23 +0000115 Replace(use, jsgraph()->Constant(inliner_inputs - 2));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000116 } else if (index == inlinee_context_index) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000117 // The projection is requesting the inlinee function context.
118 Replace(use, context);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000119 } else {
Ben Murdoch014dc512016-03-22 12:00:34 +0000120 // Call has fewer arguments than required, fill with undefined.
Ben Murdochf91f0612016-11-29 16:50:11 +0000121 Replace(use, jsgraph()->UndefinedConstant());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000122 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000123 break;
124 }
125 default:
Emily Bernier958fae72015-03-24 16:35:39 -0400126 if (NodeProperties::IsEffectEdge(edge)) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000127 edge.UpdateTo(effect);
Emily Bernier958fae72015-03-24 16:35:39 -0400128 } else if (NodeProperties::IsControlEdge(edge)) {
129 edge.UpdateTo(control);
Ben Murdoch014dc512016-03-22 12:00:34 +0000130 } else if (NodeProperties::IsFrameStateEdge(edge)) {
131 edge.UpdateTo(frame_state);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000132 } else {
133 UNREACHABLE();
134 }
135 break;
136 }
137 }
138
Ben Murdochf3b273f2017-01-17 12:11:28 +0000139 if (exception_target != nullptr) {
140 // Link uncaught calls in the inlinee to {exception_target}
141 int subcall_count = static_cast<int>(uncaught_subcalls.size());
142 if (subcall_count > 0) {
143 TRACE(
144 "Inlinee contains %d calls without IfException; "
145 "linking to existing IfException\n",
146 subcall_count);
147 }
148 NodeVector on_exception_nodes(local_zone_);
149 for (Node* subcall : uncaught_subcalls) {
150 Node* on_exception =
151 graph()->NewNode(common()->IfException(), subcall, subcall);
152 on_exception_nodes.push_back(on_exception);
153 }
154
155 DCHECK_EQ(subcall_count, static_cast<int>(on_exception_nodes.size()));
156 if (subcall_count > 0) {
157 Node* control_output =
158 graph()->NewNode(common()->Merge(subcall_count), subcall_count,
159 &on_exception_nodes.front());
160 NodeVector values_effects(local_zone_);
161 values_effects = on_exception_nodes;
162 values_effects.push_back(control_output);
163 Node* value_output = graph()->NewNode(
164 common()->Phi(MachineRepresentation::kTagged, subcall_count),
165 subcall_count + 1, &values_effects.front());
166 Node* effect_output =
167 graph()->NewNode(common()->EffectPhi(subcall_count),
168 subcall_count + 1, &values_effects.front());
169 ReplaceWithValue(exception_target, value_output, effect_output,
170 control_output);
171 } else {
172 ReplaceWithValue(exception_target, exception_target, exception_target,
173 jsgraph()->Dead());
174 }
175 }
176
Ben Murdoch014dc512016-03-22 12:00:34 +0000177 NodeVector values(local_zone_);
178 NodeVector effects(local_zone_);
179 NodeVector controls(local_zone_);
180 for (Node* const input : end->inputs()) {
181 switch (input->opcode()) {
182 case IrOpcode::kReturn:
Ben Murdochc8c1d9e2017-03-08 14:04:23 +0000183 values.push_back(NodeProperties::GetValueInput(input, 1));
Ben Murdoch014dc512016-03-22 12:00:34 +0000184 effects.push_back(NodeProperties::GetEffectInput(input));
185 controls.push_back(NodeProperties::GetControlInput(input));
186 break;
187 case IrOpcode::kDeoptimize:
188 case IrOpcode::kTerminate:
189 case IrOpcode::kThrow:
Ben Murdochf91f0612016-11-29 16:50:11 +0000190 NodeProperties::MergeControlToEnd(graph(), common(), input);
191 Revisit(graph()->end());
Ben Murdoch014dc512016-03-22 12:00:34 +0000192 break;
193 default:
194 UNREACHABLE();
195 break;
196 }
197 }
198 DCHECK_EQ(values.size(), effects.size());
199 DCHECK_EQ(values.size(), controls.size());
200
201 // Depending on whether the inlinee produces a value, we either replace value
202 // uses with said value or kill value uses if no value can be returned.
203 if (values.size() > 0) {
204 int const input_count = static_cast<int>(controls.size());
Ben Murdochf91f0612016-11-29 16:50:11 +0000205 Node* control_output = graph()->NewNode(common()->Merge(input_count),
206 input_count, &controls.front());
Ben Murdoch014dc512016-03-22 12:00:34 +0000207 values.push_back(control_output);
208 effects.push_back(control_output);
Ben Murdochf91f0612016-11-29 16:50:11 +0000209 Node* value_output = graph()->NewNode(
210 common()->Phi(MachineRepresentation::kTagged, input_count),
Ben Murdoch014dc512016-03-22 12:00:34 +0000211 static_cast<int>(values.size()), &values.front());
Ben Murdochf91f0612016-11-29 16:50:11 +0000212 Node* effect_output =
213 graph()->NewNode(common()->EffectPhi(input_count),
214 static_cast<int>(effects.size()), &effects.front());
Ben Murdoch014dc512016-03-22 12:00:34 +0000215 ReplaceWithValue(call, value_output, effect_output, control_output);
216 return Changed(value_output);
217 } else {
Ben Murdochf91f0612016-11-29 16:50:11 +0000218 ReplaceWithValue(call, call, call, jsgraph()->Dead());
Ben Murdoch014dc512016-03-22 12:00:34 +0000219 return Changed(call);
220 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000221}
222
Ben Murdoch014dc512016-03-22 12:00:34 +0000223Node* JSInliner::CreateArtificialFrameState(Node* node, Node* outer_frame_state,
224 int parameter_count,
Ben Murdoch62ed6312017-06-06 11:06:27 +0100225 BailoutId bailout_id,
Ben Murdoch014dc512016-03-22 12:00:34 +0000226 FrameStateType frame_state_type,
227 Handle<SharedFunctionInfo> shared) {
228 const FrameStateFunctionInfo* state_info =
Ben Murdochf91f0612016-11-29 16:50:11 +0000229 common()->CreateFrameStateFunctionInfo(frame_state_type,
230 parameter_count + 1, 0, shared);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000231
Ben Murdochf91f0612016-11-29 16:50:11 +0000232 const Operator* op = common()->FrameState(
Ben Murdoch62ed6312017-06-06 11:06:27 +0100233 bailout_id, OutputFrameStateCombine::Ignore(), state_info);
234 const Operator* op0 = common()->StateValues(0, SparseInputMask::Dense());
Ben Murdochf91f0612016-11-29 16:50:11 +0000235 Node* node0 = graph()->NewNode(op0);
Ben Murdoch014dc512016-03-22 12:00:34 +0000236 NodeVector params(local_zone_);
237 for (int parameter = 0; parameter < parameter_count + 1; ++parameter) {
238 params.push_back(node->InputAt(1 + parameter));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000239 }
Ben Murdoch62ed6312017-06-06 11:06:27 +0100240 const Operator* op_param = common()->StateValues(
241 static_cast<int>(params.size()), SparseInputMask::Dense());
Ben Murdochf91f0612016-11-29 16:50:11 +0000242 Node* params_node = graph()->NewNode(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000243 op_param, static_cast<int>(params.size()), &params.front());
Ben Murdochf91f0612016-11-29 16:50:11 +0000244 return graph()->NewNode(op, params_node, node0, node0,
245 jsgraph()->UndefinedConstant(), node->InputAt(0),
246 outer_frame_state);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000247}
248
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100249Node* JSInliner::CreateTailCallerFrameState(Node* node, Node* frame_state) {
250 FrameStateInfo const& frame_info = OpParameter<FrameStateInfo>(frame_state);
251 Handle<SharedFunctionInfo> shared;
252 frame_info.shared_info().ToHandle(&shared);
253
254 Node* function = frame_state->InputAt(kFrameStateFunctionInput);
255
256 // If we are inlining a tail call drop caller's frame state and an
257 // arguments adaptor if it exists.
Ben Murdochf91f0612016-11-29 16:50:11 +0000258 frame_state = NodeProperties::GetFrameStateInput(frame_state);
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100259 if (frame_state->opcode() == IrOpcode::kFrameState) {
260 FrameStateInfo const& frame_info = OpParameter<FrameStateInfo>(frame_state);
261 if (frame_info.type() == FrameStateType::kArgumentsAdaptor) {
Ben Murdochf91f0612016-11-29 16:50:11 +0000262 frame_state = NodeProperties::GetFrameStateInput(frame_state);
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100263 }
264 }
265
266 const FrameStateFunctionInfo* state_info =
Ben Murdochf91f0612016-11-29 16:50:11 +0000267 common()->CreateFrameStateFunctionInfo(
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100268 FrameStateType::kTailCallerFunction, 0, 0, shared);
269
Ben Murdochf91f0612016-11-29 16:50:11 +0000270 const Operator* op = common()->FrameState(
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100271 BailoutId(-1), OutputFrameStateCombine::Ignore(), state_info);
Ben Murdoch62ed6312017-06-06 11:06:27 +0100272 const Operator* op0 = common()->StateValues(0, SparseInputMask::Dense());
Ben Murdochf91f0612016-11-29 16:50:11 +0000273 Node* node0 = graph()->NewNode(op0);
274 return graph()->NewNode(op, node0, node0, node0,
275 jsgraph()->UndefinedConstant(), function,
276 frame_state);
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100277}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000278
Ben Murdoch014dc512016-03-22 12:00:34 +0000279namespace {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000280
Ben Murdochf3b273f2017-01-17 12:11:28 +0000281// TODO(bmeurer): Unify this with the witness helper functions in the
282// js-builtin-reducer.cc once we have a better understanding of the
283// map tracking we want to do, and eventually changed the CheckMaps
284// operator to carry map constants on the operator instead of inputs.
285// I.e. if the CheckMaps has some kind of SmallMapSet as operator
286// parameter, then this could be changed to call a generic
287//
288// SmallMapSet NodeProperties::CollectMapWitness(receiver, effect)
289//
290// function, which either returns the map set from the CheckMaps or
291// a singleton set from a StoreField.
292bool NeedsConvertReceiver(Node* receiver, Node* effect) {
Ben Murdoch62ed6312017-06-06 11:06:27 +0100293 // Check if the {receiver} is already a JSReceiver.
294 switch (receiver->opcode()) {
295 case IrOpcode::kJSConstruct:
296 case IrOpcode::kJSConstructWithSpread:
297 case IrOpcode::kJSCreate:
298 case IrOpcode::kJSCreateArguments:
299 case IrOpcode::kJSCreateArray:
300 case IrOpcode::kJSCreateClosure:
301 case IrOpcode::kJSCreateIterResultObject:
302 case IrOpcode::kJSCreateKeyValueArray:
303 case IrOpcode::kJSCreateLiteralArray:
304 case IrOpcode::kJSCreateLiteralObject:
305 case IrOpcode::kJSCreateLiteralRegExp:
306 case IrOpcode::kJSConvertReceiver:
307 case IrOpcode::kJSGetSuperConstructor:
308 case IrOpcode::kJSToObject: {
Ben Murdochf3b273f2017-01-17 12:11:28 +0000309 return false;
310 }
Ben Murdoch62ed6312017-06-06 11:06:27 +0100311 default: {
312 // We don't really care about the exact maps here, just the instance
313 // types, which don't change across potential side-effecting operations.
314 ZoneHandleSet<Map> maps;
315 NodeProperties::InferReceiverMapsResult result =
316 NodeProperties::InferReceiverMaps(receiver, effect, &maps);
317 if (result != NodeProperties::kNoReceiverMaps) {
318 // Check if all {maps} are actually JSReceiver maps.
319 for (size_t i = 0; i < maps.size(); ++i) {
320 if (!maps[i]->IsJSReceiverMap()) return true;
Ben Murdochf3b273f2017-01-17 12:11:28 +0000321 }
Ben Murdoch62ed6312017-06-06 11:06:27 +0100322 return false;
Ben Murdochf3b273f2017-01-17 12:11:28 +0000323 }
Ben Murdoch62ed6312017-06-06 11:06:27 +0100324 return true;
Ben Murdochf3b273f2017-01-17 12:11:28 +0000325 }
Ben Murdochf3b273f2017-01-17 12:11:28 +0000326 }
327}
328
Ben Murdoch014dc512016-03-22 12:00:34 +0000329// TODO(mstarzinger,verwaest): Move this predicate onto SharedFunctionInfo?
Ben Murdoch109988c2016-05-18 11:27:45 +0100330bool NeedsImplicitReceiver(Handle<SharedFunctionInfo> shared_info) {
331 DisallowHeapAllocation no_gc;
332 Isolate* const isolate = shared_info->GetIsolate();
333 Code* const construct_stub = shared_info->construct_stub();
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100334 return construct_stub != *isolate->builtins()->JSBuiltinsConstructStub() &&
335 construct_stub !=
336 *isolate->builtins()->JSBuiltinsConstructStubForDerived() &&
337 construct_stub != *isolate->builtins()->JSConstructStubApi();
Ben Murdoch109988c2016-05-18 11:27:45 +0100338}
339
340bool IsNonConstructible(Handle<SharedFunctionInfo> shared_info) {
341 DisallowHeapAllocation no_gc;
342 Isolate* const isolate = shared_info->GetIsolate();
343 Code* const construct_stub = shared_info->construct_stub();
344 return construct_stub == *isolate->builtins()->ConstructedNonConstructable();
Ben Murdoch014dc512016-03-22 12:00:34 +0000345}
346
347} // namespace
348
Ben Murdoch62ed6312017-06-06 11:06:27 +0100349// Determines whether the call target of the given call {node} is statically
350// known and can be used as an inlining candidate. The {SharedFunctionInfo} of
351// the call target is provided (the exact closure might be unknown).
352bool JSInliner::DetermineCallTarget(
353 Node* node, Handle<SharedFunctionInfo>& shared_info_out) {
354 DCHECK(IrOpcode::IsInlineeOpcode(node->opcode()));
355 HeapObjectMatcher match(node->InputAt(0));
Ben Murdoch014dc512016-03-22 12:00:34 +0000356
357 // This reducer can handle both normal function calls as well a constructor
358 // calls whenever the target is a constant function object, as follows:
Ben Murdoch62ed6312017-06-06 11:06:27 +0100359 // - JSCall(target:constant, receiver, args...)
360 // - JSConstruct(target:constant, args..., new.target)
361 if (match.HasValue() && match.Value()->IsJSFunction()) {
362 Handle<JSFunction> function = Handle<JSFunction>::cast(match.Value());
Ben Murdoch014dc512016-03-22 12:00:34 +0000363
Ben Murdoch62ed6312017-06-06 11:06:27 +0100364 // Disallow cross native-context inlining for now. This means that all parts
365 // of the resulting code will operate on the same global object. This also
366 // prevents cross context leaks, where we could inline functions from a
367 // different context and hold on to that context (and closure) from the code
368 // object.
369 // TODO(turbofan): We might want to revisit this restriction later when we
370 // have a need for this, and we know how to model different native contexts
371 // in the same graph in a compositional way.
372 if (function->context()->native_context() !=
373 info_->context()->native_context()) {
374 return false;
375 }
376
377 shared_info_out = handle(function->shared());
378 return true;
379 }
380
381 // This reducer can also handle calls where the target is statically known to
382 // be the result of a closure instantiation operation, as follows:
383 // - JSCall(JSCreateClosure[shared](context), receiver, args...)
384 // - JSConstruct(JSCreateClosure[shared](context), args..., new.target)
385 if (match.IsJSCreateClosure()) {
386 CreateClosureParameters const& p = CreateClosureParametersOf(match.op());
387
388 // Disallow inlining in case the instantiation site was never run and hence
389 // the vector cell does not contain a valid feedback vector for the call
390 // target.
391 // TODO(turbofan): We might consider to eagerly create the feedback vector
392 // in such a case (in {DetermineCallContext} below) eventually.
393 FeedbackSlot slot = p.feedback().slot();
394 Handle<Cell> cell(Cell::cast(p.feedback().vector()->Get(slot)));
395 if (!cell->value()->IsFeedbackVector()) return false;
396
397 shared_info_out = p.shared_info();
398 return true;
399 }
400
401 return false;
Ben Murdoch014dc512016-03-22 12:00:34 +0000402}
403
Ben Murdoch62ed6312017-06-06 11:06:27 +0100404// Determines statically known information about the call target (assuming that
405// the call target is known according to {DetermineCallTarget} above). The
406// following static information is provided:
407// - context : The context (as SSA value) bound by the call target.
408// - feedback_vector : The target is guaranteed to use this feedback vector.
409void JSInliner::DetermineCallContext(
410 Node* node, Node*& context_out,
411 Handle<FeedbackVector>& feedback_vector_out) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000412 DCHECK(IrOpcode::IsInlineeOpcode(node->opcode()));
Ben Murdoch62ed6312017-06-06 11:06:27 +0100413 HeapObjectMatcher match(node->InputAt(0));
414
415 if (match.HasValue() && match.Value()->IsJSFunction()) {
416 Handle<JSFunction> function = Handle<JSFunction>::cast(match.Value());
417
418 // If the target function was never invoked, its literals array might not
419 // contain a feedback vector. We ensure at this point that it is created.
420 JSFunction::EnsureLiterals(function);
421
422 // The inlinee specializes to the context from the JSFunction object.
423 context_out = jsgraph()->Constant(handle(function->context()));
424 feedback_vector_out = handle(function->feedback_vector());
425 return;
426 }
427
428 if (match.IsJSCreateClosure()) {
429 CreateClosureParameters const& p = CreateClosureParametersOf(match.op());
430
431 // Load the feedback vector of the target by looking up its vector cell at
432 // the instantiation site (we only decide to inline if it's populated).
433 FeedbackSlot slot = p.feedback().slot();
434 Handle<Cell> cell(Cell::cast(p.feedback().vector()->Get(slot)));
435 DCHECK(cell->value()->IsFeedbackVector());
436
437 // The inlinee uses the locally provided context at instantiation.
438 context_out = NodeProperties::GetContextInput(match.node());
439 feedback_vector_out = handle(FeedbackVector::cast(cell->value()));
440 return;
441 }
442
443 // Must succeed.
444 UNREACHABLE();
445}
446
447Reduction JSInliner::Reduce(Node* node) {
448 if (!IrOpcode::IsInlineeOpcode(node->opcode())) return NoChange();
449 return ReduceJSCall(node);
450}
451
452Reduction JSInliner::ReduceJSCall(Node* node) {
453 DCHECK(IrOpcode::IsInlineeOpcode(node->opcode()));
454 Handle<SharedFunctionInfo> shared_info;
Ben Murdoch014dc512016-03-22 12:00:34 +0000455 JSCallAccessor call(node);
Ben Murdoch62ed6312017-06-06 11:06:27 +0100456
457 // Determine the call target.
458 if (!DetermineCallTarget(node, shared_info)) return NoChange();
459
460 // Inlining is only supported in the bytecode pipeline.
461 if (!info_->is_optimizing_from_bytecode()) {
462 TRACE("Not inlining %s into %s due to use of the deprecated pipeline\n",
463 shared_info->DebugName()->ToCString().get(),
464 info_->shared_info()->DebugName()->ToCString().get());
465 return NoChange();
466 }
Ben Murdoch014dc512016-03-22 12:00:34 +0000467
468 // Function must be inlineable.
Ben Murdoch109988c2016-05-18 11:27:45 +0100469 if (!shared_info->IsInlineable()) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000470 TRACE("Not inlining %s into %s because callee is not inlineable\n",
Ben Murdoch109988c2016-05-18 11:27:45 +0100471 shared_info->DebugName()->ToCString().get(),
Ben Murdoch014dc512016-03-22 12:00:34 +0000472 info_->shared_info()->DebugName()->ToCString().get());
473 return NoChange();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000474 }
475
Ben Murdoch014dc512016-03-22 12:00:34 +0000476 // Constructor must be constructable.
Ben Murdoch62ed6312017-06-06 11:06:27 +0100477 if (node->opcode() == IrOpcode::kJSConstruct &&
Ben Murdoch109988c2016-05-18 11:27:45 +0100478 IsNonConstructible(shared_info)) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000479 TRACE("Not inlining %s into %s because constructor is not constructable.\n",
Ben Murdoch109988c2016-05-18 11:27:45 +0100480 shared_info->DebugName()->ToCString().get(),
Ben Murdoch014dc512016-03-22 12:00:34 +0000481 info_->shared_info()->DebugName()->ToCString().get());
482 return NoChange();
483 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000484
Ben Murdoch62ed6312017-06-06 11:06:27 +0100485 // TODO(706642): Don't inline derived class constructors for now, as the
486 // inlining logic doesn't deal properly with derived class constructors
487 // that return a primitive, i.e. it's not in sync with what the Parser
488 // and the JSConstructSub does.
489 if (node->opcode() == IrOpcode::kJSConstruct &&
490 IsDerivedConstructor(shared_info->kind())) {
491 TRACE("Not inlining %s into %s because constructor is derived.\n",
492 shared_info->DebugName()->ToCString().get(),
493 info_->shared_info()->DebugName()->ToCString().get());
494 return NoChange();
495 }
496
Ben Murdoch014dc512016-03-22 12:00:34 +0000497 // Class constructors are callable, but [[Call]] will raise an exception.
498 // See ES6 section 9.2.1 [[Call]] ( thisArgument, argumentsList ).
Ben Murdoch62ed6312017-06-06 11:06:27 +0100499 if (node->opcode() == IrOpcode::kJSCall &&
Ben Murdoch109988c2016-05-18 11:27:45 +0100500 IsClassConstructor(shared_info->kind())) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000501 TRACE("Not inlining %s into %s because callee is a class constructor.\n",
Ben Murdoch109988c2016-05-18 11:27:45 +0100502 shared_info->DebugName()->ToCString().get(),
Ben Murdoch014dc512016-03-22 12:00:34 +0000503 info_->shared_info()->DebugName()->ToCString().get());
504 return NoChange();
505 }
506
507 // Function contains break points.
Ben Murdoch109988c2016-05-18 11:27:45 +0100508 if (shared_info->HasDebugInfo()) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000509 TRACE("Not inlining %s into %s because callee may contain break points\n",
Ben Murdoch109988c2016-05-18 11:27:45 +0100510 shared_info->DebugName()->ToCString().get(),
Ben Murdoch014dc512016-03-22 12:00:34 +0000511 info_->shared_info()->DebugName()->ToCString().get());
512 return NoChange();
513 }
514
Ben Murdoch014dc512016-03-22 12:00:34 +0000515 // TODO(turbofan): TranslatedState::GetAdaptedArguments() currently relies on
516 // not inlining recursive functions. We might want to relax that at some
517 // point.
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100518 for (Node* frame_state = call.frame_state();
Ben Murdoch014dc512016-03-22 12:00:34 +0000519 frame_state->opcode() == IrOpcode::kFrameState;
520 frame_state = frame_state->InputAt(kFrameStateOuterStateInput)) {
Ben Murdoch109988c2016-05-18 11:27:45 +0100521 FrameStateInfo const& frame_info = OpParameter<FrameStateInfo>(frame_state);
522 Handle<SharedFunctionInfo> frame_shared_info;
523 if (frame_info.shared_info().ToHandle(&frame_shared_info) &&
524 *frame_shared_info == *shared_info) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000525 TRACE("Not inlining %s into %s because call is recursive\n",
Ben Murdoch109988c2016-05-18 11:27:45 +0100526 shared_info->DebugName()->ToCString().get(),
Ben Murdoch014dc512016-03-22 12:00:34 +0000527 info_->shared_info()->DebugName()->ToCString().get());
528 return NoChange();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000529 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000530 }
531
Ben Murdochf3b273f2017-01-17 12:11:28 +0000532 // Find the IfException node, if any.
533 Node* exception_target = nullptr;
534 for (Edge edge : node->use_edges()) {
535 if (NodeProperties::IsControlEdge(edge) &&
536 edge.from()->opcode() == IrOpcode::kIfException) {
537 DCHECK_NULL(exception_target);
538 exception_target = edge.from();
539 }
540 }
541
542 NodeVector uncaught_subcalls(local_zone_);
543
544 if (exception_target != nullptr) {
545 if (!FLAG_inline_into_try) {
546 TRACE(
547 "Try block surrounds #%d:%s and --no-inline-into-try active, so not "
548 "inlining %s into %s.\n",
549 exception_target->id(), exception_target->op()->mnemonic(),
Ben Murdoch109988c2016-05-18 11:27:45 +0100550 shared_info->DebugName()->ToCString().get(),
Ben Murdoch014dc512016-03-22 12:00:34 +0000551 info_->shared_info()->DebugName()->ToCString().get());
Ben Murdochf3b273f2017-01-17 12:11:28 +0000552 return NoChange();
553 } else {
554 TRACE(
555 "Inlining %s into %s regardless of surrounding try-block to catcher "
556 "#%d:%s\n",
557 shared_info->DebugName()->ToCString().get(),
558 info_->shared_info()->DebugName()->ToCString().get(),
559 exception_target->id(), exception_target->op()->mnemonic());
560 }
Ben Murdoch014dc512016-03-22 12:00:34 +0000561 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000562
Ben Murdoch62ed6312017-06-06 11:06:27 +0100563 ParseInfo parse_info(shared_info);
564 CompilationInfo info(parse_info.zone(), &parse_info,
565 Handle<JSFunction>::null());
Ben Murdoch109988c2016-05-18 11:27:45 +0100566 if (info_->is_deoptimization_enabled()) info.MarkAsDeoptimizationEnabled();
Ben Murdoch62ed6312017-06-06 11:06:27 +0100567 info.MarkAsOptimizeFromBytecode();
Ben Murdoch014dc512016-03-22 12:00:34 +0000568
Ben Murdoch62ed6312017-06-06 11:06:27 +0100569 if (!Compiler::EnsureBytecode(&info)) {
Ben Murdochf3b273f2017-01-17 12:11:28 +0000570 TRACE("Not inlining %s into %s because bytecode generation failed\n",
571 shared_info->DebugName()->ToCString().get(),
572 info_->shared_info()->DebugName()->ToCString().get());
573 if (info_->isolate()->has_pending_exception()) {
574 info_->isolate()->clear_pending_exception();
575 }
576 return NoChange();
577 }
578
Ben Murdoch014dc512016-03-22 12:00:34 +0000579 // Remember that we inlined this function. This needs to be called right
580 // after we ensure deoptimization support so that the code flusher
581 // does not remove the code with the deoptimization support.
Ben Murdochc8c1d9e2017-03-08 14:04:23 +0000582 int inlining_id = info_->AddInlinedFunction(
583 shared_info, source_positions_->GetSourcePosition(node));
Ben Murdoch014dc512016-03-22 12:00:34 +0000584
585 // ----------------------------------------------------------------
586 // After this point, we've made a decision to inline this function.
587 // We shall not bailout from inlining if we got here.
588
589 TRACE("Inlining %s into %s\n",
Ben Murdoch109988c2016-05-18 11:27:45 +0100590 shared_info->DebugName()->ToCString().get(),
Ben Murdoch014dc512016-03-22 12:00:34 +0000591 info_->shared_info()->DebugName()->ToCString().get());
592
Ben Murdoch62ed6312017-06-06 11:06:27 +0100593 // Determine the targets feedback vector and its context.
594 Node* context;
595 Handle<FeedbackVector> feedback_vector;
596 DetermineCallContext(node, context, feedback_vector);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000597
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100598 // Create the subgraph for the inlinee.
599 Node* start;
600 Node* end;
Ben Murdoch62ed6312017-06-06 11:06:27 +0100601 {
Ben Murdochf3b273f2017-01-17 12:11:28 +0000602 // Run the BytecodeGraphBuilder to create the subgraph.
603 Graph::SubgraphScope scope(graph());
Ben Murdoch62ed6312017-06-06 11:06:27 +0100604 BytecodeGraphBuilder graph_builder(
605 parse_info.zone(), shared_info, feedback_vector, BailoutId::None(),
606 jsgraph(), call.frequency(), source_positions_, inlining_id);
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100607 graph_builder.CreateGraph(false);
608
609 // Extract the inlinee start/end nodes.
610 start = graph()->start();
611 end = graph()->end();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000612 }
613
Ben Murdochf3b273f2017-01-17 12:11:28 +0000614 if (exception_target != nullptr) {
615 // Find all uncaught 'calls' in the inlinee.
616 AllNodes inlined_nodes(local_zone_, end, graph());
617 for (Node* subnode : inlined_nodes.reachable) {
618 // Every possibly throwing node with an IfSuccess should get an
619 // IfException.
620 if (subnode->op()->HasProperty(Operator::kNoThrow)) {
621 continue;
622 }
623 bool hasIfException = false;
624 for (Node* use : subnode->uses()) {
625 if (use->opcode() == IrOpcode::kIfException) {
626 hasIfException = true;
627 break;
628 }
629 }
630 if (!hasIfException) {
631 DCHECK_EQ(2, subnode->op()->ControlOutputCount());
632 uncaught_subcalls.push_back(subnode);
633 }
634 }
635 }
636
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100637 Node* frame_state = call.frame_state();
Ben Murdochf91f0612016-11-29 16:50:11 +0000638 Node* new_target = jsgraph()->UndefinedConstant();
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100639
Ben Murdoch62ed6312017-06-06 11:06:27 +0100640 // Inline {JSConstruct} requires some additional magic.
641 if (node->opcode() == IrOpcode::kJSConstruct) {
642 // Swizzle the inputs of the {JSConstruct} node to look like inputs to a
643 // normal {JSCall} node so that the rest of the inlining machinery
644 // behaves as if we were dealing with a regular function invocation.
645 new_target = call.new_target(); // Retrieve new target value input.
646 node->RemoveInput(call.formal_arguments() + 1); // Drop new target.
647 node->InsertInput(graph()->zone(), 1, new_target);
648
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100649 // Insert nodes around the call that model the behavior required for a
650 // constructor dispatch (allocate implicit receiver and check return value).
651 // This models the behavior usually accomplished by our {JSConstructStub}.
652 // Note that the context has to be the callers context (input to call node).
Ben Murdoch62ed6312017-06-06 11:06:27 +0100653 // Also note that by splitting off the {JSCreate} piece of the constructor
654 // call, we create an observable deoptimization point after the receiver
655 // instantiation but before the invocation (i.e. inside {JSConstructStub}
656 // where execution continues at {construct_stub_create_deopt_pc_offset}).
Ben Murdochc8c1d9e2017-03-08 14:04:23 +0000657 Node* receiver = jsgraph()->TheHoleConstant(); // Implicit receiver.
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100658 if (NeedsImplicitReceiver(shared_info)) {
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100659 Node* effect = NodeProperties::GetEffectInput(node);
Ben Murdoch62ed6312017-06-06 11:06:27 +0100660 Node* control = NodeProperties::GetControlInput(node);
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100661 Node* context = NodeProperties::GetContextInput(node);
Ben Murdoch62ed6312017-06-06 11:06:27 +0100662 Node* frame_state_inside = CreateArtificialFrameState(
663 node, frame_state, call.formal_arguments(),
664 BailoutId::ConstructStubCreate(), FrameStateType::kConstructStub,
665 info.shared_info());
666 Node* create =
667 graph()->NewNode(javascript()->Create(), call.target(), new_target,
668 context, frame_state_inside, effect, control);
669 Node* success = graph()->NewNode(common()->IfSuccess(), create);
670 uncaught_subcalls.push_back(create); // Adds {IfException}.
671 NodeProperties::ReplaceControlInput(node, success);
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100672 NodeProperties::ReplaceEffectInput(node, create);
673 // Insert a check of the return value to determine whether the return
Ben Murdochf91f0612016-11-29 16:50:11 +0000674 // value or the implicit receiver should be selected as a result of the
675 // call.
676 Node* check = graph()->NewNode(simplified()->ObjectIsReceiver(), node);
677 Node* select =
678 graph()->NewNode(common()->Select(MachineRepresentation::kTagged),
679 check, node, create);
680 NodeProperties::ReplaceUses(node, select, node, node, node);
681 // Fix-up inputs that have been mangled by the {ReplaceUses} call above.
682 NodeProperties::ReplaceValueInput(select, node, 1); // Fix-up input.
683 NodeProperties::ReplaceValueInput(check, node, 0); // Fix-up input.
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100684 receiver = create; // The implicit receiver.
685 }
Ben Murdoch62ed6312017-06-06 11:06:27 +0100686 node->ReplaceInput(1, receiver);
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100687
Ben Murdoch014dc512016-03-22 12:00:34 +0000688 // Insert a construct stub frame into the chain of frame states. This will
689 // reconstruct the proper frame when deoptimizing within the constructor.
690 frame_state = CreateArtificialFrameState(
691 node, frame_state, call.formal_arguments(),
Ben Murdoch62ed6312017-06-06 11:06:27 +0100692 BailoutId::ConstructStubInvoke(), FrameStateType::kConstructStub,
693 info.shared_info());
Ben Murdoch014dc512016-03-22 12:00:34 +0000694 }
695
Ben Murdoch014dc512016-03-22 12:00:34 +0000696 // Insert a JSConvertReceiver node for sloppy callees. Note that the context
Ben Murdoch62ed6312017-06-06 11:06:27 +0100697 // passed into this node has to be the callees context (loaded above).
698 if (node->opcode() == IrOpcode::kJSCall &&
Ben Murdochf3b273f2017-01-17 12:11:28 +0000699 is_sloppy(shared_info->language_mode()) && !shared_info->native()) {
Ben Murdoch014dc512016-03-22 12:00:34 +0000700 Node* effect = NodeProperties::GetEffectInput(node);
Ben Murdochf3b273f2017-01-17 12:11:28 +0000701 if (NeedsConvertReceiver(call.receiver(), effect)) {
Ben Murdoch62ed6312017-06-06 11:06:27 +0100702 const CallParameters& p = CallParametersOf(node->op());
703 Node* convert = effect =
704 graph()->NewNode(javascript()->ConvertReceiver(p.convert_mode()),
705 call.receiver(), context, effect, start);
Ben Murdochf3b273f2017-01-17 12:11:28 +0000706 NodeProperties::ReplaceValueInput(node, convert, 1);
707 NodeProperties::ReplaceEffectInput(node, effect);
708 }
Ben Murdoch014dc512016-03-22 12:00:34 +0000709 }
710
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100711 // If we are inlining a JS call at tail position then we have to pop current
712 // frame state and its potential arguments adaptor frame state in order to
713 // make the call stack be consistent with non-inlining case.
714 // After that we add a tail caller frame state which lets deoptimizer handle
715 // the case when the outermost function inlines a tail call (it should remove
716 // potential arguments adaptor frame that belongs to outermost function when
717 // deopt happens).
Ben Murdoch62ed6312017-06-06 11:06:27 +0100718 if (node->opcode() == IrOpcode::kJSCall) {
719 const CallParameters& p = CallParametersOf(node->op());
Ben Murdoch3b9bc312016-06-02 14:46:10 +0100720 if (p.tail_call_mode() == TailCallMode::kAllow) {
721 frame_state = CreateTailCallerFrameState(node, frame_state);
722 }
723 }
724
Ben Murdoch014dc512016-03-22 12:00:34 +0000725 // Insert argument adaptor frame if required. The callees formal parameter
726 // count (i.e. value outputs of start node minus target, receiver, new target,
727 // arguments count and context) have to match the number of arguments passed
728 // to the call.
Ben Murdochf3b273f2017-01-17 12:11:28 +0000729 int parameter_count = shared_info->internal_formal_parameter_count();
Ben Murdoch014dc512016-03-22 12:00:34 +0000730 DCHECK_EQ(parameter_count, start->op()->ValueOutputCount() - 5);
731 if (call.formal_arguments() != parameter_count) {
732 frame_state = CreateArtificialFrameState(
Ben Murdoch62ed6312017-06-06 11:06:27 +0100733 node, frame_state, call.formal_arguments(), BailoutId::None(),
Ben Murdoch109988c2016-05-18 11:27:45 +0100734 FrameStateType::kArgumentsAdaptor, shared_info);
Ben Murdoch014dc512016-03-22 12:00:34 +0000735 }
736
Ben Murdochf3b273f2017-01-17 12:11:28 +0000737 return InlineCall(node, new_target, context, frame_state, start, end,
738 exception_target, uncaught_subcalls);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000739}
Emily Bernier958fae72015-03-24 16:35:39 -0400740
Ben Murdoch13e2dad2016-09-16 13:49:30 +0100741Graph* JSInliner::graph() const { return jsgraph()->graph(); }
742
Ben Murdochf91f0612016-11-29 16:50:11 +0000743JSOperatorBuilder* JSInliner::javascript() const {
744 return jsgraph()->javascript();
745}
746
747CommonOperatorBuilder* JSInliner::common() const { return jsgraph()->common(); }
748
749SimplifiedOperatorBuilder* JSInliner::simplified() const {
750 return jsgraph()->simplified();
751}
752
Ben Murdoch014dc512016-03-22 12:00:34 +0000753} // namespace compiler
754} // namespace internal
755} // namespace v8