js-intrinsic-lowering.cc 12.9 KB
Newer Older
1 2 3 4 5 6
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/compiler/js-intrinsic-lowering.h"

7 8
#include <stack>

9
#include "src/code-factory.h"
10 11
#include "src/compiler/access-builder.h"
#include "src/compiler/js-graph.h"
12
#include "src/compiler/linkage.h"
13
#include "src/compiler/node-matchers.h"
14
#include "src/compiler/node-properties.h"
15
#include "src/compiler/operator-properties.h"
16 17
#include "src/counters.h"
#include "src/objects-inl.h"
18
#include "src/objects/js-generator.h"
19 20 21 22 23

namespace v8 {
namespace internal {
namespace compiler {

24 25
JSIntrinsicLowering::JSIntrinsicLowering(Editor* editor, JSGraph* jsgraph)
    : AdvancedReducer(editor), jsgraph_(jsgraph) {}
26 27 28 29 30

Reduction JSIntrinsicLowering::Reduce(Node* node) {
  if (node->opcode() != IrOpcode::kJSCallRuntime) return NoChange();
  const Runtime::Function* const f =
      Runtime::FunctionForId(CallRuntimeParametersOf(node->op()).id());
31
  if (f->intrinsic_type != Runtime::IntrinsicType::INLINE) return NoChange();
32
  switch (f->function_id) {
33 34
    case Runtime::kInlineCreateIterResultObject:
      return ReduceCreateIterResultObject(node);
35
    case Runtime::kInlineDeoptimizeNow:
36
      return ReduceDeoptimizeNow(node);
37 38
    case Runtime::kInlineGeneratorClose:
      return ReduceGeneratorClose(node);
39 40
    case Runtime::kInlineCreateJSGeneratorObject:
      return ReduceCreateJSGeneratorObject(node);
41 42 43 44
    case Runtime::kInlineAsyncFunctionAwaitCaught:
      return ReduceAsyncFunctionAwaitCaught(node);
    case Runtime::kInlineAsyncFunctionAwaitUncaught:
      return ReduceAsyncFunctionAwaitUncaught(node);
45 46 47 48 49 50
    case Runtime::kInlineAsyncFunctionEnter:
      return ReduceAsyncFunctionEnter(node);
    case Runtime::kInlineAsyncFunctionReject:
      return ReduceAsyncFunctionReject(node);
    case Runtime::kInlineAsyncFunctionResolve:
      return ReduceAsyncFunctionResolve(node);
51 52 53 54
    case Runtime::kInlineAsyncGeneratorAwaitCaught:
      return ReduceAsyncGeneratorAwaitCaught(node);
    case Runtime::kInlineAsyncGeneratorAwaitUncaught:
      return ReduceAsyncGeneratorAwaitUncaught(node);
55 56 57 58
    case Runtime::kInlineAsyncGeneratorReject:
      return ReduceAsyncGeneratorReject(node);
    case Runtime::kInlineAsyncGeneratorResolve:
      return ReduceAsyncGeneratorResolve(node);
59 60
    case Runtime::kInlineAsyncGeneratorYield:
      return ReduceAsyncGeneratorYield(node);
61 62
    case Runtime::kInlineGeneratorGetResumeMode:
      return ReduceGeneratorGetResumeMode(node);
63
    case Runtime::kInlineIsArray:
64
      return ReduceIsInstanceType(node, JS_ARRAY_TYPE);
65 66
    case Runtime::kInlineIsTypedArray:
      return ReduceIsInstanceType(node, JS_TYPED_ARRAY_TYPE);
67 68
    case Runtime::kInlineIsJSReceiver:
      return ReduceIsJSReceiver(node);
69 70
    case Runtime::kInlineIsSmi:
      return ReduceIsSmi(node);
71 72
    case Runtime::kInlineToLength:
      return ReduceToLength(node);
73 74
    case Runtime::kInlineToObject:
      return ReduceToObject(node);
75 76
    case Runtime::kInlineToString:
      return ReduceToString(node);
77 78
    case Runtime::kInlineCall:
      return ReduceCall(node);
79 80 81 82 83 84 85
    default:
      break;
  }
  return NoChange();
}


86 87 88 89 90 91 92 93 94
Reduction JSIntrinsicLowering::ReduceCreateIterResultObject(Node* node) {
  Node* const value = NodeProperties::GetValueInput(node, 0);
  Node* const done = NodeProperties::GetValueInput(node, 1);
  Node* const context = NodeProperties::GetContextInput(node);
  Node* const effect = NodeProperties::GetEffectInput(node);
  return Change(node, javascript()->CreateIterResultObject(), value, done,
                context, effect);
}

95
Reduction JSIntrinsicLowering::ReduceDeoptimizeNow(Node* node) {
96
  Node* const frame_state = NodeProperties::GetFrameStateInput(node);
97 98
  Node* const effect = NodeProperties::GetEffectInput(node);
  Node* const control = NodeProperties::GetControlInput(node);
99

100
  // TODO(bmeurer): Move MergeControlToEnd() to the AdvancedReducer.
101
  Node* deoptimize = graph()->NewNode(
102 103
      common()->Deoptimize(DeoptimizeKind::kEager,
                           DeoptimizeReason::kDeoptimizeNow, VectorSlotPair()),
104
      frame_state, effect, control);
105
  NodeProperties::MergeControlToEnd(graph(), common(), deoptimize);
106
  Revisit(graph()->end());
107

108
  node->TrimInputCount(0);
109
  NodeProperties::ChangeOp(node, common()->Dead());
110
  return Changed(node);
111 112
}

113 114 115 116 117 118 119 120 121 122 123 124 125
Reduction JSIntrinsicLowering::ReduceCreateJSGeneratorObject(Node* node) {
  Node* const closure = NodeProperties::GetValueInput(node, 0);
  Node* const receiver = NodeProperties::GetValueInput(node, 1);
  Node* const context = NodeProperties::GetContextInput(node);
  Node* const effect = NodeProperties::GetEffectInput(node);
  Node* const control = NodeProperties::GetControlInput(node);
  Operator const* const op = javascript()->CreateGeneratorObject();
  Node* create_generator =
      graph()->NewNode(op, closure, receiver, context, effect, control);
  ReplaceWithValue(node, create_generator, create_generator);
  return Changed(create_generator);
}

126 127 128 129 130 131 132 133 134 135 136 137 138
Reduction JSIntrinsicLowering::ReduceGeneratorClose(Node* node) {
  Node* const generator = NodeProperties::GetValueInput(node, 0);
  Node* const effect = NodeProperties::GetEffectInput(node);
  Node* const control = NodeProperties::GetControlInput(node);
  Node* const closed = jsgraph()->Constant(JSGeneratorObject::kGeneratorClosed);
  Node* const undefined = jsgraph()->UndefinedConstant();
  Operator const* const op = simplified()->StoreField(
      AccessBuilder::ForJSGeneratorObjectContinuation());

  ReplaceWithValue(node, undefined, node);
  NodeProperties::RemoveType(node);
  return Change(node, op, generator, closed, effect, control);
}
139

140 141 142 143 144 145 146 147 148 149 150 151 152
Reduction JSIntrinsicLowering::ReduceAsyncFunctionAwaitCaught(Node* node) {
  return Change(
      node,
      Builtins::CallableFor(isolate(), Builtins::kAsyncFunctionAwaitCaught), 0);
}

Reduction JSIntrinsicLowering::ReduceAsyncFunctionAwaitUncaught(Node* node) {
  return Change(
      node,
      Builtins::CallableFor(isolate(), Builtins::kAsyncFunctionAwaitUncaught),
      0);
}

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
Reduction JSIntrinsicLowering::ReduceAsyncFunctionEnter(Node* node) {
  NodeProperties::ChangeOp(node, javascript()->AsyncFunctionEnter());
  return Changed(node);
}

Reduction JSIntrinsicLowering::ReduceAsyncFunctionReject(Node* node) {
  RelaxControls(node);
  NodeProperties::ChangeOp(node, javascript()->AsyncFunctionReject());
  return Changed(node);
}

Reduction JSIntrinsicLowering::ReduceAsyncFunctionResolve(Node* node) {
  RelaxControls(node);
  NodeProperties::ChangeOp(node, javascript()->AsyncFunctionResolve());
  return Changed(node);
}

170 171 172 173 174 175 176 177 178 179 180 181 182 183
Reduction JSIntrinsicLowering::ReduceAsyncGeneratorAwaitCaught(Node* node) {
  return Change(
      node,
      Builtins::CallableFor(isolate(), Builtins::kAsyncGeneratorAwaitCaught),
      0);
}

Reduction JSIntrinsicLowering::ReduceAsyncGeneratorAwaitUncaught(Node* node) {
  return Change(
      node,
      Builtins::CallableFor(isolate(), Builtins::kAsyncGeneratorAwaitUncaught),
      0);
}

184
Reduction JSIntrinsicLowering::ReduceAsyncGeneratorReject(Node* node) {
185 186 187
  return Change(
      node, Builtins::CallableFor(isolate(), Builtins::kAsyncGeneratorReject),
      0);
188 189 190
}

Reduction JSIntrinsicLowering::ReduceAsyncGeneratorResolve(Node* node) {
191 192 193
  return Change(
      node, Builtins::CallableFor(isolate(), Builtins::kAsyncGeneratorResolve),
      0);
194 195
}

196 197 198 199 200 201
Reduction JSIntrinsicLowering::ReduceAsyncGeneratorYield(Node* node) {
  return Change(
      node, Builtins::CallableFor(isolate(), Builtins::kAsyncGeneratorYield),
      0);
}

202 203 204 205 206 207 208 209 210 211
Reduction JSIntrinsicLowering::ReduceGeneratorGetResumeMode(Node* node) {
  Node* const generator = NodeProperties::GetValueInput(node, 0);
  Node* const effect = NodeProperties::GetEffectInput(node);
  Node* const control = NodeProperties::GetControlInput(node);
  Operator const* const op =
      simplified()->LoadField(AccessBuilder::ForJSGeneratorObjectResumeMode());

  return Change(node, op, generator, effect, control);
}

212
Reduction JSIntrinsicLowering::ReduceIsInstanceType(
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
    Node* node, InstanceType instance_type) {
  // if (%_IsSmi(value)) {
  //   return false;
  // } else {
  //   return %_GetInstanceType(%_GetMap(value)) == instance_type;
  // }
  Node* value = NodeProperties::GetValueInput(node, 0);
  Node* effect = NodeProperties::GetEffectInput(node);
  Node* control = NodeProperties::GetControlInput(node);

  Node* check = graph()->NewNode(simplified()->ObjectIsSmi(), value);
  Node* branch = graph()->NewNode(common()->Branch(), check, control);

  Node* if_true = graph()->NewNode(common()->IfTrue(), branch);
  Node* etrue = effect;
  Node* vtrue = jsgraph()->FalseConstant();

  Node* if_false = graph()->NewNode(common()->IfFalse(), branch);
231 232
  Node* efalse = effect;
  Node* map = efalse =
233
      graph()->NewNode(simplified()->LoadField(AccessBuilder::ForMap()), value,
234 235 236 237 238 239 240
                       efalse, if_false);
  Node* map_instance_type = efalse = graph()->NewNode(
      simplified()->LoadField(AccessBuilder::ForMapInstanceType()), map, efalse,
      if_false);
  Node* vfalse =
      graph()->NewNode(simplified()->NumberEqual(), map_instance_type,
                       jsgraph()->Constant(instance_type));
241 242 243 244 245

  Node* merge = graph()->NewNode(common()->Merge(2), if_true, if_false);

  // Replace all effect uses of {node} with the {ephi}.
  Node* ephi = graph()->NewNode(common()->EffectPhi(2), etrue, efalse, merge);
246
  ReplaceWithValue(node, node, ephi, merge);
247 248

  // Turn the {node} into a Phi.
249 250
  return Change(node, common()->Phi(MachineRepresentation::kTagged, 2), vtrue,
                vfalse, merge);
251 252 253
}


254
Reduction JSIntrinsicLowering::ReduceIsJSReceiver(Node* node) {
255
  return Change(node, simplified()->ObjectIsReceiver());
256 257 258
}


259 260 261 262
Reduction JSIntrinsicLowering::ReduceIsSmi(Node* node) {
  return Change(node, simplified()->ObjectIsSmi());
}

263
Reduction JSIntrinsicLowering::Change(Node* node, const Operator* op) {
264
  // Replace all effect uses of {node} with the effect dependency.
265
  RelaxEffectsAndControls(node);
266 267 268
  // Remove the inputs corresponding to context, effect and control.
  NodeProperties::RemoveNonValueInputs(node);
  // Finally update the operator to the new one.
269
  NodeProperties::ChangeOp(node, op);
270 271 272
  return Changed(node);
}

273 274

Reduction JSIntrinsicLowering::ReduceToLength(Node* node) {
275 276
  NodeProperties::ChangeOp(node, javascript()->ToLength());
  return Changed(node);
277 278 279
}


280
Reduction JSIntrinsicLowering::ReduceToObject(Node* node) {
281
  NodeProperties::ChangeOp(node, javascript()->ToObject());
282 283 284 285
  return Changed(node);
}


286
Reduction JSIntrinsicLowering::ReduceToString(Node* node) {
287 288 289 290 291 292
  // ToString is unnecessary if the input is a string.
  HeapObjectMatcher m(NodeProperties::GetValueInput(node, 0));
  if (m.HasValue() && m.Value()->IsString()) {
    ReplaceWithValue(node, m.node());
    return Replace(m.node());
  }
293 294 295 296 297
  NodeProperties::ChangeOp(node, javascript()->ToString());
  return Changed(node);
}


298
Reduction JSIntrinsicLowering::ReduceCall(Node* node) {
299
  size_t const arity = CallRuntimeParametersOf(node->op()).arity();
300
  NodeProperties::ChangeOp(node, javascript()->Call(arity));
301 302 303
  return Changed(node);
}

304 305
Reduction JSIntrinsicLowering::Change(Node* node, const Operator* op, Node* a,
                                      Node* b) {
306
  RelaxControls(node);
307 308 309
  node->ReplaceInput(0, a);
  node->ReplaceInput(1, b);
  node->TrimInputCount(2);
310
  NodeProperties::ChangeOp(node, op);
311 312 313 314
  return Changed(node);
}


315 316
Reduction JSIntrinsicLowering::Change(Node* node, const Operator* op, Node* a,
                                      Node* b, Node* c) {
317
  RelaxControls(node);
318 319 320 321
  node->ReplaceInput(0, a);
  node->ReplaceInput(1, b);
  node->ReplaceInput(2, c);
  node->TrimInputCount(3);
322
  NodeProperties::ChangeOp(node, op);
323 324 325 326
  return Changed(node);
}


327 328
Reduction JSIntrinsicLowering::Change(Node* node, const Operator* op, Node* a,
                                      Node* b, Node* c, Node* d) {
329
  RelaxControls(node);
330 331 332 333 334
  node->ReplaceInput(0, a);
  node->ReplaceInput(1, b);
  node->ReplaceInput(2, c);
  node->ReplaceInput(3, d);
  node->TrimInputCount(4);
335
  NodeProperties::ChangeOp(node, op);
336 337 338 339
  return Changed(node);
}


340 341
Reduction JSIntrinsicLowering::Change(Node* node, Callable const& callable,
                                      int stack_parameter_count) {
342
  auto call_descriptor = Linkage::GetStubCallDescriptor(
343
      graph()->zone(), callable.descriptor(), stack_parameter_count,
344 345 346
      CallDescriptor::kNeedsFrameState, node->op()->properties());
  node->InsertInput(graph()->zone(), 0,
                    jsgraph()->HeapConstant(callable.code()));
347
  NodeProperties::ChangeOp(node, common()->Call(call_descriptor));
348 349 350 351
  return Changed(node);
}


352 353 354
Graph* JSIntrinsicLowering::graph() const { return jsgraph()->graph(); }


355 356 357
Isolate* JSIntrinsicLowering::isolate() const { return jsgraph()->isolate(); }


358 359 360 361
CommonOperatorBuilder* JSIntrinsicLowering::common() const {
  return jsgraph()->common();
}

362 363 364 365
JSOperatorBuilder* JSIntrinsicLowering::javascript() const {
  return jsgraph_->javascript();
}

366 367 368 369
SimplifiedOperatorBuilder* JSIntrinsicLowering::simplified() const {
  return jsgraph()->simplified();
}

370 371 372
}  // namespace compiler
}  // namespace internal
}  // namespace v8