Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1 | // 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 5 | #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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 12 | #include "src/compiler/ast-graph-builder.h" |
| 13 | #include "src/compiler/common-operator.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 14 | #include "src/compiler/graph-reducer.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 15 | #include "src/compiler/js-operator.h" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 16 | #include "src/compiler/node-matchers.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 17 | #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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 22 | |
| 23 | namespace v8 { |
| 24 | namespace internal { |
| 25 | namespace compiler { |
| 26 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 27 | #define TRACE(...) \ |
| 28 | do { \ |
| 29 | if (FLAG_trace_turbo_inlining) PrintF(__VA_ARGS__); \ |
| 30 | } while (false) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 31 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 32 | |
| 33 | // Provides convenience accessors for the common layout of nodes having either |
| 34 | // the {JSCallFunction} or the {JSCallConstruct} operator. |
| 35 | class 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | private: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 74 | Node* call_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 78 | class CopyVisitor { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 79 | public: |
| 80 | CopyVisitor(Graph* source_graph, Graph* target_graph, Zone* temp_zone) |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 81 | : 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 85 | source_graph_(source_graph), |
| 86 | target_graph_(target_graph), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 87 | temp_zone_(temp_zone) {} |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 88 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 89 | Node* GetCopy(Node* orig) { return copies_[orig->id()]; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 90 | |
| 91 | void CopyGraph() { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 92 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 123 | const NodeVector& copies() const { return copies_; } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 124 | |
| 125 | private: |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 126 | Operator const sentinel_op_; |
| 127 | Node* const sentinel_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 128 | NodeVector copies_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 129 | Graph* const source_graph_; |
| 130 | Graph* const target_graph_; |
| 131 | Zone* const temp_zone_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 135 | Reduction JSInliner::InlineCall(Node* call, Node* new_target, Node* context, |
| 136 | Node* frame_state, Node* start, Node* end) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 137 | // The scheduler is smart enough to place our code; we just ensure {control} |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 138 | // becomes the control input of the start of the inlinee, and {effect} becomes |
| 139 | // the effect input of the start of the inlinee. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 140 | Node* control = NodeProperties::GetControlInput(call); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 141 | Node* effect = NodeProperties::GetEffectInput(call); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 142 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 143 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 149 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 150 | // {inliner_inputs} counts JSFunction, receiver, arguments, but not |
| 151 | // new target value, argument count, context, effect or control. |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 152 | int inliner_inputs = call->op()->ValueInputCount(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 153 | // Iterate over all uses of the start node. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 154 | for (Edge edge : start->use_edges()) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 155 | Node* use = edge.from(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 156 | switch (use->opcode()) { |
| 157 | case IrOpcode::kParameter: { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 158 | int index = 1 + ParameterIndexOf(use->op()); |
| 159 | DCHECK_LE(index, inlinee_context_index); |
| 160 | if (index < inliner_inputs && index < inlinee_new_target_index) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 161 | // There is an input from the call, and the index is a value |
| 162 | // projection but not the context, so rewire the input. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 163 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 170 | } else if (index == inlinee_context_index) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 171 | // The projection is requesting the inlinee function context. |
| 172 | Replace(use, context); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 173 | } else { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 174 | // Call has fewer arguments than required, fill with undefined. |
| 175 | Replace(use, jsgraph_->UndefinedConstant()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 176 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 177 | break; |
| 178 | } |
| 179 | default: |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 180 | if (NodeProperties::IsEffectEdge(edge)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 181 | edge.UpdateTo(effect); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 182 | } else if (NodeProperties::IsControlEdge(edge)) { |
| 183 | edge.UpdateTo(control); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 184 | } else if (NodeProperties::IsFrameStateEdge(edge)) { |
| 185 | edge.UpdateTo(frame_state); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 186 | } else { |
| 187 | UNREACHABLE(); |
| 188 | } |
| 189 | break; |
| 190 | } |
| 191 | } |
| 192 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 193 | 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 Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 208 | Revisit(jsgraph_->graph()->end()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 209 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 241 | Node* 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 Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 247 | frame_state_type, parameter_count + 1, 0, shared); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 248 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 249 | const Operator* op = jsgraph_->common()->FrameState( |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 250 | BailoutId(-1), OutputFrameStateCombine::Ignore(), state_info); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 251 | const Operator* op0 = jsgraph_->common()->StateValues(0); |
| 252 | Node* node0 = jsgraph_->graph()->NewNode(op0); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 253 | NodeVector params(local_zone_); |
| 254 | for (int parameter = 0; parameter < parameter_count + 1; ++parameter) { |
| 255 | params.push_back(node->InputAt(1 + parameter)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 256 | } |
| 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()), ¶ms.front()); |
| 261 | return jsgraph_->graph()->NewNode(op, params_node, node0, node0, |
| 262 | jsgraph_->UndefinedConstant(), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 263 | node->InputAt(0), outer_frame_state); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 266 | Node* 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 295 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 296 | namespace { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 297 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 298 | // TODO(mstarzinger,verwaest): Move this predicate onto SharedFunctionInfo? |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 299 | bool 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 Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 303 | return construct_stub != *isolate->builtins()->JSBuiltinsConstructStub() && |
| 304 | construct_stub != |
| 305 | *isolate->builtins()->JSBuiltinsConstructStubForDerived() && |
| 306 | construct_stub != *isolate->builtins()->JSConstructStubApi(); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | bool 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | } // namespace |
| 317 | |
| 318 | |
| 319 | Reduction 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 | |
| 334 | Reduction JSInliner::ReduceJSCall(Node* node, Handle<JSFunction> function) { |
| 335 | DCHECK(IrOpcode::IsInlineeOpcode(node->opcode())); |
| 336 | JSCallAccessor call(node); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 337 | Handle<SharedFunctionInfo> shared_info(function->shared()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 338 | |
| 339 | // Function must be inlineable. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 340 | if (!shared_info->IsInlineable()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 341 | TRACE("Not inlining %s into %s because callee is not inlineable\n", |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 342 | shared_info->DebugName()->ToCString().get(), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 343 | info_->shared_info()->DebugName()->ToCString().get()); |
| 344 | return NoChange(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 345 | } |
| 346 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 347 | // Constructor must be constructable. |
| 348 | if (node->opcode() == IrOpcode::kJSCallConstruct && |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 349 | IsNonConstructible(shared_info)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 350 | TRACE("Not inlining %s into %s because constructor is not constructable.\n", |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 351 | shared_info->DebugName()->ToCString().get(), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 352 | info_->shared_info()->DebugName()->ToCString().get()); |
| 353 | return NoChange(); |
| 354 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 355 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 356 | // 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 Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 359 | IsClassConstructor(shared_info->kind())) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 360 | TRACE("Not inlining %s into %s because callee is a class constructor.\n", |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 361 | shared_info->DebugName()->ToCString().get(), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 362 | info_->shared_info()->DebugName()->ToCString().get()); |
| 363 | return NoChange(); |
| 364 | } |
| 365 | |
| 366 | // Function contains break points. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 367 | if (shared_info->HasDebugInfo()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 368 | TRACE("Not inlining %s into %s because callee may contain break points\n", |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 369 | shared_info->DebugName()->ToCString().get(), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 370 | 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 Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 385 | shared_info->DebugName()->ToCString().get(), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 386 | 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 Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 396 | 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 400 | TRACE("Not inlining %s into %s because call is recursive\n", |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 401 | shared_info->DebugName()->ToCString().get(), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 402 | info_->shared_info()->DebugName()->ToCString().get()); |
| 403 | return NoChange(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 404 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 407 | // 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 Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 410 | shared_info->DebugName()->ToCString().get(), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 411 | info_->shared_info()->DebugName()->ToCString().get()); |
| 412 | return NoChange(); |
| 413 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 414 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 415 | Zone zone(info_->isolate()->allocator()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 416 | ParseInfo parse_info(&zone, function); |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 417 | CompilationInfo info(&parse_info, function); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 418 | if (info_->is_deoptimization_enabled()) info.MarkAsDeoptimizationEnabled(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 419 | |
| 420 | if (!Compiler::ParseAndAnalyze(info.parse_info())) { |
| 421 | TRACE("Not inlining %s into %s because parsing failed\n", |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 422 | shared_info->DebugName()->ToCString().get(), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 423 | info_->shared_info()->DebugName()->ToCString().get()); |
| 424 | if (info_->isolate()->has_pending_exception()) { |
| 425 | info_->isolate()->clear_pending_exception(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 426 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 427 | return NoChange(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 430 | if (!Compiler::EnsureDeoptimizationSupport(&info)) { |
| 431 | TRACE("Not inlining %s into %s because deoptimization support failed\n", |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 432 | shared_info->DebugName()->ToCString().get(), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 433 | 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 Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 439 | info_->AddInlinedFunction(shared_info); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 440 | |
| 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 Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 446 | shared_info->DebugName()->ToCString().get(), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 447 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 455 | jsgraph_->machine()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 456 | AstGraphBuilder graph_builder(local_zone_, &info, &jsgraph); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 457 | graph_builder.CreateGraph(false); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 458 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 459 | CopyVisitor visitor(&graph, jsgraph_->graph(), &zone); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 460 | visitor.CopyGraph(); |
| 461 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 462 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 466 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 467 | // 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 Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 473 | NeedsImplicitReceiver(shared_info)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 474 | 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 Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 495 | // 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 Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 522 | is_sloppy(parse_info.language_mode()) && !shared_info->native()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 523 | 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 Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 532 | // 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 Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 546 | // 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 Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 550 | int parameter_count = info.literal()->parameter_count(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 551 | 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 Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 555 | FrameStateType::kArgumentsAdaptor, shared_info); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | return InlineCall(node, new_target, context, frame_state, start, end); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 559 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 560 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 561 | } // namespace compiler |
| 562 | } // namespace internal |
| 563 | } // namespace v8 |