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 | |
| 266 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 267 | namespace { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 268 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 269 | // TODO(mstarzinger,verwaest): Move this predicate onto SharedFunctionInfo? |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 270 | bool NeedsImplicitReceiver(Handle<SharedFunctionInfo> shared_info) { |
| 271 | DisallowHeapAllocation no_gc; |
| 272 | Isolate* const isolate = shared_info->GetIsolate(); |
| 273 | Code* const construct_stub = shared_info->construct_stub(); |
| 274 | return construct_stub != *isolate->builtins()->JSBuiltinsConstructStub(); |
| 275 | } |
| 276 | |
| 277 | bool IsNonConstructible(Handle<SharedFunctionInfo> shared_info) { |
| 278 | DisallowHeapAllocation no_gc; |
| 279 | Isolate* const isolate = shared_info->GetIsolate(); |
| 280 | Code* const construct_stub = shared_info->construct_stub(); |
| 281 | return construct_stub == *isolate->builtins()->ConstructedNonConstructable(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | } // namespace |
| 285 | |
| 286 | |
| 287 | Reduction JSInliner::Reduce(Node* node) { |
| 288 | if (!IrOpcode::IsInlineeOpcode(node->opcode())) return NoChange(); |
| 289 | |
| 290 | // This reducer can handle both normal function calls as well a constructor |
| 291 | // calls whenever the target is a constant function object, as follows: |
| 292 | // - JSCallFunction(target:constant, receiver, args...) |
| 293 | // - JSCallConstruct(target:constant, args..., new.target) |
| 294 | HeapObjectMatcher match(node->InputAt(0)); |
| 295 | if (!match.HasValue() || !match.Value()->IsJSFunction()) return NoChange(); |
| 296 | Handle<JSFunction> function = Handle<JSFunction>::cast(match.Value()); |
| 297 | |
| 298 | return ReduceJSCall(node, function); |
| 299 | } |
| 300 | |
| 301 | |
| 302 | Reduction JSInliner::ReduceJSCall(Node* node, Handle<JSFunction> function) { |
| 303 | DCHECK(IrOpcode::IsInlineeOpcode(node->opcode())); |
| 304 | JSCallAccessor call(node); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 305 | Handle<SharedFunctionInfo> shared_info(function->shared()); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 306 | |
| 307 | // Function must be inlineable. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 308 | if (!shared_info->IsInlineable()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 309 | TRACE("Not inlining %s into %s because callee is not inlineable\n", |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 310 | shared_info->DebugName()->ToCString().get(), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 311 | info_->shared_info()->DebugName()->ToCString().get()); |
| 312 | return NoChange(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 315 | // Constructor must be constructable. |
| 316 | if (node->opcode() == IrOpcode::kJSCallConstruct && |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 317 | IsNonConstructible(shared_info)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 318 | TRACE("Not inlining %s into %s because constructor is not constructable.\n", |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 319 | shared_info->DebugName()->ToCString().get(), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 320 | info_->shared_info()->DebugName()->ToCString().get()); |
| 321 | return NoChange(); |
| 322 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 323 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 324 | // Class constructors are callable, but [[Call]] will raise an exception. |
| 325 | // See ES6 section 9.2.1 [[Call]] ( thisArgument, argumentsList ). |
| 326 | if (node->opcode() == IrOpcode::kJSCallFunction && |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 327 | IsClassConstructor(shared_info->kind())) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 328 | 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^] | 329 | shared_info->DebugName()->ToCString().get(), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 330 | info_->shared_info()->DebugName()->ToCString().get()); |
| 331 | return NoChange(); |
| 332 | } |
| 333 | |
| 334 | // Function contains break points. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 335 | if (shared_info->HasDebugInfo()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 336 | 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^] | 337 | shared_info->DebugName()->ToCString().get(), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 338 | info_->shared_info()->DebugName()->ToCString().get()); |
| 339 | return NoChange(); |
| 340 | } |
| 341 | |
| 342 | // Disallow cross native-context inlining for now. This means that all parts |
| 343 | // of the resulting code will operate on the same global object. |
| 344 | // This also prevents cross context leaks for asm.js code, where we could |
| 345 | // inline functions from a different context and hold on to that context (and |
| 346 | // closure) from the code object. |
| 347 | // TODO(turbofan): We might want to revisit this restriction later when we |
| 348 | // have a need for this, and we know how to model different native contexts |
| 349 | // in the same graph in a compositional way. |
| 350 | if (function->context()->native_context() != |
| 351 | info_->context()->native_context()) { |
| 352 | TRACE("Not inlining %s into %s because of different native contexts\n", |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 353 | shared_info->DebugName()->ToCString().get(), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 354 | info_->shared_info()->DebugName()->ToCString().get()); |
| 355 | return NoChange(); |
| 356 | } |
| 357 | |
| 358 | // TODO(turbofan): TranslatedState::GetAdaptedArguments() currently relies on |
| 359 | // not inlining recursive functions. We might want to relax that at some |
| 360 | // point. |
| 361 | for (Node* frame_state = call.frame_state_after(); |
| 362 | frame_state->opcode() == IrOpcode::kFrameState; |
| 363 | frame_state = frame_state->InputAt(kFrameStateOuterStateInput)) { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 364 | FrameStateInfo const& frame_info = OpParameter<FrameStateInfo>(frame_state); |
| 365 | Handle<SharedFunctionInfo> frame_shared_info; |
| 366 | if (frame_info.shared_info().ToHandle(&frame_shared_info) && |
| 367 | *frame_shared_info == *shared_info) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 368 | TRACE("Not inlining %s into %s because call is recursive\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(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 372 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 375 | // TODO(turbofan): Inlining into a try-block is not yet supported. |
| 376 | if (NodeProperties::IsExceptionalCall(node)) { |
| 377 | TRACE("Not inlining %s into %s because of surrounding try-block\n", |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 378 | shared_info->DebugName()->ToCString().get(), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 379 | info_->shared_info()->DebugName()->ToCString().get()); |
| 380 | return NoChange(); |
| 381 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 382 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 383 | Zone zone; |
| 384 | ParseInfo parse_info(&zone, function); |
| 385 | CompilationInfo info(&parse_info); |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 386 | if (info_->is_deoptimization_enabled()) info.MarkAsDeoptimizationEnabled(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 387 | |
| 388 | if (!Compiler::ParseAndAnalyze(info.parse_info())) { |
| 389 | TRACE("Not inlining %s into %s because parsing failed\n", |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 390 | shared_info->DebugName()->ToCString().get(), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 391 | info_->shared_info()->DebugName()->ToCString().get()); |
| 392 | if (info_->isolate()->has_pending_exception()) { |
| 393 | info_->isolate()->clear_pending_exception(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 394 | } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 395 | return NoChange(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 396 | } |
| 397 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 398 | // In strong mode, in case of too few arguments we need to throw a TypeError |
| 399 | // so we must not inline this call. |
| 400 | int parameter_count = info.literal()->parameter_count(); |
| 401 | if (is_strong(info.language_mode()) && |
| 402 | call.formal_arguments() < parameter_count) { |
| 403 | TRACE("Not inlining %s into %s because too few arguments for strong mode\n", |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 404 | shared_info->DebugName()->ToCString().get(), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 405 | info_->shared_info()->DebugName()->ToCString().get()); |
| 406 | return NoChange(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 409 | if (!Compiler::EnsureDeoptimizationSupport(&info)) { |
| 410 | TRACE("Not inlining %s into %s because deoptimization support failed\n", |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 411 | shared_info->DebugName()->ToCString().get(), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 412 | info_->shared_info()->DebugName()->ToCString().get()); |
| 413 | return NoChange(); |
| 414 | } |
| 415 | // Remember that we inlined this function. This needs to be called right |
| 416 | // after we ensure deoptimization support so that the code flusher |
| 417 | // does not remove the code with the deoptimization support. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 418 | info_->AddInlinedFunction(shared_info); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 419 | |
| 420 | // ---------------------------------------------------------------- |
| 421 | // After this point, we've made a decision to inline this function. |
| 422 | // We shall not bailout from inlining if we got here. |
| 423 | |
| 424 | TRACE("Inlining %s into %s\n", |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 425 | shared_info->DebugName()->ToCString().get(), |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 426 | info_->shared_info()->DebugName()->ToCString().get()); |
| 427 | |
| 428 | // TODO(mstarzinger): We could use the temporary zone for the graph because |
| 429 | // nodes are copied. This however leads to Zone-Types being allocated in the |
| 430 | // wrong zone and makes the engine explode at high speeds. Explosion bad! |
| 431 | Graph graph(jsgraph_->zone()); |
| 432 | JSGraph jsgraph(info.isolate(), &graph, jsgraph_->common(), |
| 433 | jsgraph_->javascript(), jsgraph_->simplified(), |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 434 | jsgraph_->machine()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 435 | AstGraphBuilder graph_builder(local_zone_, &info, &jsgraph); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 436 | graph_builder.CreateGraph(false); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 437 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 438 | CopyVisitor visitor(&graph, jsgraph_->graph(), &zone); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 439 | visitor.CopyGraph(); |
| 440 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 441 | Node* start = visitor.GetCopy(graph.start()); |
| 442 | Node* end = visitor.GetCopy(graph.end()); |
| 443 | Node* frame_state = call.frame_state_after(); |
| 444 | Node* new_target = jsgraph_->UndefinedConstant(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 445 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 446 | // Insert nodes around the call that model the behavior required for a |
| 447 | // constructor dispatch (allocate implicit receiver and check return value). |
| 448 | // This models the behavior usually accomplished by our {JSConstructStub}. |
| 449 | // Note that the context has to be the callers context (input to call node). |
| 450 | Node* receiver = jsgraph_->UndefinedConstant(); // Implicit receiver. |
| 451 | if (node->opcode() == IrOpcode::kJSCallConstruct && |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 452 | NeedsImplicitReceiver(shared_info)) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 453 | Node* effect = NodeProperties::GetEffectInput(node); |
| 454 | Node* context = NodeProperties::GetContextInput(node); |
| 455 | Node* create = jsgraph_->graph()->NewNode( |
| 456 | jsgraph_->javascript()->Create(), call.target(), call.new_target(), |
| 457 | context, call.frame_state_before(), effect); |
| 458 | NodeProperties::ReplaceEffectInput(node, create); |
| 459 | // Insert a check of the return value to determine whether the return value |
| 460 | // or the implicit receiver should be selected as a result of the call. |
| 461 | Node* check = jsgraph_->graph()->NewNode( |
| 462 | jsgraph_->javascript()->CallRuntime(Runtime::kInlineIsJSReceiver, 1), |
| 463 | node, context, node, start); |
| 464 | Node* select = jsgraph_->graph()->NewNode( |
| 465 | jsgraph_->common()->Select(MachineRepresentation::kTagged), check, node, |
| 466 | create); |
| 467 | NodeProperties::ReplaceUses(node, select, check, node, node); |
| 468 | NodeProperties::ReplaceValueInput(select, node, 1); |
| 469 | NodeProperties::ReplaceValueInput(check, node, 0); |
| 470 | NodeProperties::ReplaceEffectInput(check, node); |
| 471 | receiver = create; // The implicit receiver. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 474 | // Swizzle the inputs of the {JSCallConstruct} node to look like inputs to a |
| 475 | // normal {JSCallFunction} node so that the rest of the inlining machinery |
| 476 | // behaves as if we were dealing with a regular function invocation. |
| 477 | if (node->opcode() == IrOpcode::kJSCallConstruct) { |
| 478 | new_target = call.new_target(); // Retrieve new target value input. |
| 479 | node->RemoveInput(call.formal_arguments() + 1); // Drop new target. |
| 480 | node->InsertInput(jsgraph_->graph()->zone(), 1, receiver); |
| 481 | // Insert a construct stub frame into the chain of frame states. This will |
| 482 | // reconstruct the proper frame when deoptimizing within the constructor. |
| 483 | frame_state = CreateArtificialFrameState( |
| 484 | node, frame_state, call.formal_arguments(), |
| 485 | FrameStateType::kConstructStub, info.shared_info()); |
| 486 | } |
| 487 | |
| 488 | // The inlinee specializes to the context from the JSFunction object. |
| 489 | // TODO(turbofan): We might want to load the context from the JSFunction at |
| 490 | // runtime in case we only know the SharedFunctionInfo once we have dynamic |
| 491 | // type feedback in the compiler. |
| 492 | Node* context = jsgraph_->Constant(handle(function->context())); |
| 493 | |
| 494 | // Insert a JSConvertReceiver node for sloppy callees. Note that the context |
| 495 | // passed into this node has to be the callees context (loaded above). Note |
| 496 | // that the frame state passed to the JSConvertReceiver must be the frame |
| 497 | // state _before_ the call; it is not necessary to fiddle with the receiver |
| 498 | // in that frame state tho, as the conversion of the receiver can be repeated |
| 499 | // any number of times, it's not observable. |
| 500 | if (node->opcode() == IrOpcode::kJSCallFunction && |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 501 | is_sloppy(info.language_mode()) && !shared_info->native()) { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 502 | const CallFunctionParameters& p = CallFunctionParametersOf(node->op()); |
| 503 | Node* effect = NodeProperties::GetEffectInput(node); |
| 504 | Node* convert = jsgraph_->graph()->NewNode( |
| 505 | jsgraph_->javascript()->ConvertReceiver(p.convert_mode()), |
| 506 | call.receiver(), context, call.frame_state_before(), effect, start); |
| 507 | NodeProperties::ReplaceValueInput(node, convert, 1); |
| 508 | NodeProperties::ReplaceEffectInput(node, convert); |
| 509 | } |
| 510 | |
| 511 | // Insert argument adaptor frame if required. The callees formal parameter |
| 512 | // count (i.e. value outputs of start node minus target, receiver, new target, |
| 513 | // arguments count and context) have to match the number of arguments passed |
| 514 | // to the call. |
| 515 | DCHECK_EQ(parameter_count, start->op()->ValueOutputCount() - 5); |
| 516 | if (call.formal_arguments() != parameter_count) { |
| 517 | frame_state = CreateArtificialFrameState( |
| 518 | node, frame_state, call.formal_arguments(), |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame^] | 519 | FrameStateType::kArgumentsAdaptor, shared_info); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | return InlineCall(node, new_target, context, frame_state, start, end); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 523 | } |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 524 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 525 | } // namespace compiler |
| 526 | } // namespace internal |
| 527 | } // namespace v8 |