1014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch// Copyright 2015 the V8 project authors. All rights reserved.
2014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch// Use of this source code is governed by a BSD-style license that can be
3014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch// found in the LICENSE file.
4014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
5014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch#include "src/compiler/js-intrinsic-lowering.h"
6014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
7014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch#include <stack>
8014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
9014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch#include "src/code-factory.h"
10014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch#include "src/compiler/access-builder.h"
11014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch#include "src/compiler/js-graph.h"
12014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch#include "src/compiler/linkage.h"
13014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch#include "src/compiler/node-matchers.h"
14014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch#include "src/compiler/node-properties.h"
15014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch#include "src/compiler/operator-properties.h"
16014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch#include "src/counters.h"
17014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch#include "src/objects-inl.h"
18014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
19014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochnamespace v8 {
20014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochnamespace internal {
21014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdochnamespace compiler {
22014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
23014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochJSIntrinsicLowering::JSIntrinsicLowering(Editor* editor, JSGraph* jsgraph,
24014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                                         DeoptimizationMode mode)
251b268ca467c924004286c97bac133db489cf43d0Ben Murdoch    : AdvancedReducer(editor), jsgraph_(jsgraph), mode_(mode) {}
26014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
27014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochReduction JSIntrinsicLowering::Reduce(Node* node) {
28014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  if (node->opcode() != IrOpcode::kJSCallRuntime) return NoChange();
29014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  const Runtime::Function* const f =
30014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      Runtime::FunctionForId(CallRuntimeParametersOf(node->op()).id());
31014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  if (f->intrinsic_type != Runtime::IntrinsicType::INLINE) return NoChange();
32014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  switch (f->function_id) {
33014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    case Runtime::kInlineCreateIterResultObject:
34014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      return ReduceCreateIterResultObject(node);
35014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    case Runtime::kInlineDeoptimizeNow:
36014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      return ReduceDeoptimizeNow(node);
37014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    case Runtime::kInlineDoubleHi:
38014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      return ReduceDoubleHi(node);
39014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    case Runtime::kInlineDoubleLo:
40014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      return ReduceDoubleLo(node);
4121efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch    case Runtime::kInlineGeneratorClose:
4221efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch      return ReduceGeneratorClose(node);
4321efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch    case Runtime::kInlineGeneratorGetInputOrDebugPos:
4421efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch      return ReduceGeneratorGetInputOrDebugPos(node);
4521efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch    case Runtime::kInlineGeneratorGetResumeMode:
4621efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch      return ReduceGeneratorGetResumeMode(node);
47014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    case Runtime::kInlineIsArray:
48014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      return ReduceIsInstanceType(node, JS_ARRAY_TYPE);
49014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    case Runtime::kInlineIsTypedArray:
50014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      return ReduceIsInstanceType(node, JS_TYPED_ARRAY_TYPE);
51014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    case Runtime::kInlineIsRegExp:
52014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      return ReduceIsInstanceType(node, JS_REGEXP_TYPE);
53014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    case Runtime::kInlineIsJSReceiver:
54014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      return ReduceIsJSReceiver(node);
55014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    case Runtime::kInlineIsSmi:
56014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      return ReduceIsSmi(node);
57014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    case Runtime::kInlineValueOf:
58014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      return ReduceValueOf(node);
59014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    case Runtime::kInlineFixedArrayGet:
60014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      return ReduceFixedArrayGet(node);
61014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    case Runtime::kInlineFixedArraySet:
62014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      return ReduceFixedArraySet(node);
63014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    case Runtime::kInlineRegExpConstructResult:
64014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      return ReduceRegExpConstructResult(node);
65014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    case Runtime::kInlineRegExpExec:
66014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      return ReduceRegExpExec(node);
67014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    case Runtime::kInlineRegExpFlags:
68014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      return ReduceRegExpFlags(node);
69014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    case Runtime::kInlineRegExpSource:
70014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      return ReduceRegExpSource(node);
71014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    case Runtime::kInlineSubString:
72014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      return ReduceSubString(node);
73014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    case Runtime::kInlineToInteger:
74014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      return ReduceToInteger(node);
75014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    case Runtime::kInlineToLength:
76014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      return ReduceToLength(node);
77014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    case Runtime::kInlineToName:
78014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      return ReduceToName(node);
79014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    case Runtime::kInlineToNumber:
80014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      return ReduceToNumber(node);
81014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    case Runtime::kInlineToObject:
82014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      return ReduceToObject(node);
83014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    case Runtime::kInlineToPrimitive:
84014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      return ReduceToPrimitive(node);
85014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    case Runtime::kInlineToString:
86014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      return ReduceToString(node);
87014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    case Runtime::kInlineCall:
88014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      return ReduceCall(node);
891b268ca467c924004286c97bac133db489cf43d0Ben Murdoch    case Runtime::kInlineNewObject:
901b268ca467c924004286c97bac133db489cf43d0Ben Murdoch      return ReduceNewObject(node);
91014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    case Runtime::kInlineGetSuperConstructor:
92014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      return ReduceGetSuperConstructor(node);
93014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    default:
94014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      break;
95014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  }
96014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return NoChange();
97014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
98014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
99014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
100014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochReduction JSIntrinsicLowering::ReduceCreateIterResultObject(Node* node) {
101014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* const value = NodeProperties::GetValueInput(node, 0);
102014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* const done = NodeProperties::GetValueInput(node, 1);
103014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* const context = NodeProperties::GetContextInput(node);
104014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* const effect = NodeProperties::GetEffectInput(node);
105014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return Change(node, javascript()->CreateIterResultObject(), value, done,
106014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                context, effect);
107014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
108014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
109014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
110014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochReduction JSIntrinsicLowering::ReduceDeoptimizeNow(Node* node) {
111014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  if (mode() != kDeoptimizationEnabled) return NoChange();
112014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* const frame_state = NodeProperties::GetFrameStateInput(node, 0);
113014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* const effect = NodeProperties::GetEffectInput(node);
114014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* const control = NodeProperties::GetControlInput(node);
115014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
116014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // TODO(bmeurer): Move MergeControlToEnd() to the AdvancedReducer.
117014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* deoptimize =
118014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      graph()->NewNode(common()->Deoptimize(DeoptimizeKind::kEager),
119014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                       frame_state, effect, control);
120014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  NodeProperties::MergeControlToEnd(graph(), common(), deoptimize);
121342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch  Revisit(graph()->end());
122014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
123014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  node->TrimInputCount(0);
124014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  NodeProperties::ChangeOp(node, common()->Dead());
125014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return Changed(node);
126014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
127014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
128014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
129014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochReduction JSIntrinsicLowering::ReduceDoubleHi(Node* node) {
1301b268ca467c924004286c97bac133db489cf43d0Ben Murdoch  // Tell the compiler to assume number input.
131537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch  Node* renamed = graph()->NewNode(simplified()->TypeGuard(Type::Number()),
1321b268ca467c924004286c97bac133db489cf43d0Ben Murdoch                                   node->InputAt(0), graph()->start());
1331b268ca467c924004286c97bac133db489cf43d0Ben Murdoch  node->ReplaceInput(0, renamed);
134014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return Change(node, machine()->Float64ExtractHighWord32());
135014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
136014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
137014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
138014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochReduction JSIntrinsicLowering::ReduceDoubleLo(Node* node) {
1391b268ca467c924004286c97bac133db489cf43d0Ben Murdoch  // Tell the compiler to assume number input.
140537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch  Node* renamed = graph()->NewNode(simplified()->TypeGuard(Type::Number()),
1411b268ca467c924004286c97bac133db489cf43d0Ben Murdoch                                   node->InputAt(0), graph()->start());
1421b268ca467c924004286c97bac133db489cf43d0Ben Murdoch  node->ReplaceInput(0, renamed);
143014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return Change(node, machine()->Float64ExtractLowWord32());
144014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
145014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
14621efce637eb329c94f1323b6a2334a1c977e1a9dBen MurdochReduction JSIntrinsicLowering::ReduceGeneratorClose(Node* node) {
14721efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch  Node* const generator = NodeProperties::GetValueInput(node, 0);
14821efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch  Node* const effect = NodeProperties::GetEffectInput(node);
14921efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch  Node* const control = NodeProperties::GetControlInput(node);
15021efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch  Node* const closed = jsgraph()->Constant(JSGeneratorObject::kGeneratorClosed);
15121efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch  Node* const undefined = jsgraph()->UndefinedConstant();
15221efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch  Operator const* const op = simplified()->StoreField(
15321efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch      AccessBuilder::ForJSGeneratorObjectContinuation());
15421efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch
15521efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch  ReplaceWithValue(node, undefined, node);
15621efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch  NodeProperties::RemoveType(node);
15721efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch  return Change(node, op, generator, closed, effect, control);
15821efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch}
15921efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch
16021efce637eb329c94f1323b6a2334a1c977e1a9dBen MurdochReduction JSIntrinsicLowering::ReduceGeneratorGetInputOrDebugPos(Node* node) {
16121efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch  Node* const generator = NodeProperties::GetValueInput(node, 0);
16221efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch  Node* const effect = NodeProperties::GetEffectInput(node);
16321efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch  Node* const control = NodeProperties::GetControlInput(node);
16421efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch  Operator const* const op = simplified()->LoadField(
16521efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch      AccessBuilder::ForJSGeneratorObjectInputOrDebugPos());
16621efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch
16721efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch  return Change(node, op, generator, effect, control);
16821efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch}
16921efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch
17021efce637eb329c94f1323b6a2334a1c977e1a9dBen MurdochReduction JSIntrinsicLowering::ReduceGeneratorGetResumeMode(Node* node) {
17121efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch  Node* const generator = NodeProperties::GetValueInput(node, 0);
17221efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch  Node* const effect = NodeProperties::GetEffectInput(node);
17321efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch  Node* const control = NodeProperties::GetControlInput(node);
17421efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch  Operator const* const op =
17521efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch      simplified()->LoadField(AccessBuilder::ForJSGeneratorObjectResumeMode());
17621efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch
17721efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch  return Change(node, op, generator, effect, control);
17821efce637eb329c94f1323b6a2334a1c977e1a9dBen Murdoch}
179014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
180014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochReduction JSIntrinsicLowering::ReduceIsInstanceType(
181014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    Node* node, InstanceType instance_type) {
182014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // if (%_IsSmi(value)) {
183014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  //   return false;
184014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // } else {
185014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  //   return %_GetInstanceType(%_GetMap(value)) == instance_type;
186014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // }
187014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* value = NodeProperties::GetValueInput(node, 0);
188014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* effect = NodeProperties::GetEffectInput(node);
189014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* control = NodeProperties::GetControlInput(node);
190014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
191014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* check = graph()->NewNode(simplified()->ObjectIsSmi(), value);
192014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* branch = graph()->NewNode(common()->Branch(), check, control);
193014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
194014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* if_true = graph()->NewNode(common()->IfTrue(), branch);
195014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* etrue = effect;
196014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* vtrue = jsgraph()->FalseConstant();
197014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
198014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* if_false = graph()->NewNode(common()->IfFalse(), branch);
199014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* efalse = graph()->NewNode(
200014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      simplified()->LoadField(AccessBuilder::ForMapInstanceType()),
201014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      graph()->NewNode(simplified()->LoadField(AccessBuilder::ForMap()), value,
202014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                       effect, if_false),
203014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      effect, if_false);
204014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* vfalse = graph()->NewNode(machine()->Word32Equal(), efalse,
205014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                                  jsgraph()->Int32Constant(instance_type));
206014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
207014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* merge = graph()->NewNode(common()->Merge(2), if_true, if_false);
208014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
209014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // Replace all effect uses of {node} with the {ephi}.
210014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* ephi = graph()->NewNode(common()->EffectPhi(2), etrue, efalse, merge);
211014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  ReplaceWithValue(node, node, ephi);
212014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
213014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // Turn the {node} into a Phi.
214014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return Change(node, common()->Phi(MachineRepresentation::kTagged, 2), vtrue,
215014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                vfalse, merge);
216014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
217014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
218014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
219014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochReduction JSIntrinsicLowering::ReduceIsJSReceiver(Node* node) {
220342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch  return Change(node, simplified()->ObjectIsReceiver());
221014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
222014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
223014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
224014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochReduction JSIntrinsicLowering::ReduceIsSmi(Node* node) {
225014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return Change(node, simplified()->ObjectIsSmi());
226014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
227014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
228014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
229014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochReduction JSIntrinsicLowering::ReduceValueOf(Node* node) {
230014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // if (%_IsSmi(value)) {
231014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  //   return value;
232014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // } else if (%_GetInstanceType(%_GetMap(value)) == JS_VALUE_TYPE) {
233014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  //   return %_GetValue(value);
234014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // } else {
235014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  //   return value;
236014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // }
237014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  const Operator* const merge_op = common()->Merge(2);
238014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  const Operator* const ephi_op = common()->EffectPhi(2);
239014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  const Operator* const phi_op =
240014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      common()->Phi(MachineRepresentation::kTagged, 2);
241014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
242014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* value = NodeProperties::GetValueInput(node, 0);
243014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* effect = NodeProperties::GetEffectInput(node);
244014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* control = NodeProperties::GetControlInput(node);
245014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
246014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* check0 = graph()->NewNode(simplified()->ObjectIsSmi(), value);
247014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* branch0 = graph()->NewNode(common()->Branch(), check0, control);
248014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
249014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* if_true0 = graph()->NewNode(common()->IfTrue(), branch0);
250014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* etrue0 = effect;
251014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* vtrue0 = value;
252014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
253014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* if_false0 = graph()->NewNode(common()->IfFalse(), branch0);
254014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* efalse0;
255014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* vfalse0;
256014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  {
257014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    Node* check1 = graph()->NewNode(
258014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch        machine()->Word32Equal(),
259014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch        graph()->NewNode(
260014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch            simplified()->LoadField(AccessBuilder::ForMapInstanceType()),
261014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch            graph()->NewNode(simplified()->LoadField(AccessBuilder::ForMap()),
262014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                             value, effect, if_false0),
263014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch            effect, if_false0),
264014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch        jsgraph()->Int32Constant(JS_VALUE_TYPE));
265014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    Node* branch1 = graph()->NewNode(common()->Branch(), check1, if_false0);
266014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
267014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    Node* if_true1 = graph()->NewNode(common()->IfTrue(), branch1);
268014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    Node* etrue1 =
269014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch        graph()->NewNode(simplified()->LoadField(AccessBuilder::ForValue()),
270014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                         value, effect, if_true1);
271014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    Node* vtrue1 = etrue1;
272014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
273014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    Node* if_false1 = graph()->NewNode(common()->IfFalse(), branch1);
274014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    Node* efalse1 = effect;
275014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    Node* vfalse1 = value;
276014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
277014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    Node* merge1 = graph()->NewNode(merge_op, if_true1, if_false1);
278014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    efalse0 = graph()->NewNode(ephi_op, etrue1, efalse1, merge1);
279014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    vfalse0 = graph()->NewNode(phi_op, vtrue1, vfalse1, merge1);
280014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  }
281014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
282014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* merge0 = graph()->NewNode(merge_op, if_true0, if_false0);
283014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
284014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // Replace all effect uses of {node} with the {ephi0}.
285014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* ephi0 = graph()->NewNode(ephi_op, etrue0, efalse0, merge0);
286014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  ReplaceWithValue(node, node, ephi0);
287014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
288014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // Turn the {node} into a Phi.
289014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return Change(node, phi_op, vtrue0, vfalse0, merge0);
290014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
291014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
292014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
293014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochReduction JSIntrinsicLowering::Change(Node* node, const Operator* op) {
294014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // Replace all effect uses of {node} with the effect dependency.
295014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  RelaxEffectsAndControls(node);
296014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // Remove the inputs corresponding to context, effect and control.
297014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  NodeProperties::RemoveNonValueInputs(node);
298014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // Finally update the operator to the new one.
299014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  NodeProperties::ChangeOp(node, op);
300014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return Changed(node);
301014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
302014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
303014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
304014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochReduction JSIntrinsicLowering::ReduceFixedArrayGet(Node* node) {
305014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* base = node->InputAt(0);
306014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* index = node->InputAt(1);
307014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* effect = NodeProperties::GetEffectInput(node);
308014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* control = NodeProperties::GetControlInput(node);
309014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return Change(
310014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      node, simplified()->LoadElement(AccessBuilder::ForFixedArrayElement()),
311014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      base, index, effect, control);
312014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
313014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
314014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
315014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochReduction JSIntrinsicLowering::ReduceFixedArraySet(Node* node) {
316014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* base = node->InputAt(0);
317014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* index = node->InputAt(1);
318014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* value = node->InputAt(2);
319014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* effect = NodeProperties::GetEffectInput(node);
320014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* control = NodeProperties::GetControlInput(node);
321014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* store = (graph()->NewNode(
322014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      simplified()->StoreElement(AccessBuilder::ForFixedArrayElement()), base,
323014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      index, value, effect, control));
324014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  ReplaceWithValue(node, value, store);
325014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return Changed(store);
326014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
327014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
328014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
329014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochReduction JSIntrinsicLowering::ReduceRegExpConstructResult(Node* node) {
330014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  // TODO(bmeurer): Introduce JSCreateRegExpResult?
331014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return Change(node, CodeFactory::RegExpConstructResult(isolate()), 0);
332014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
333014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
334014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
335014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochReduction JSIntrinsicLowering::ReduceRegExpExec(Node* node) {
336014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return Change(node, CodeFactory::RegExpExec(isolate()), 4);
337014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
338014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
339014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
340014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochReduction JSIntrinsicLowering::ReduceRegExpFlags(Node* node) {
341014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* const receiver = NodeProperties::GetValueInput(node, 0);
342014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* const effect = NodeProperties::GetEffectInput(node);
343014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* const control = NodeProperties::GetControlInput(node);
344014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Operator const* const op =
345014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      simplified()->LoadField(AccessBuilder::ForJSRegExpFlags());
346014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return Change(node, op, receiver, effect, control);
347014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
348014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
349014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
350014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochReduction JSIntrinsicLowering::ReduceRegExpSource(Node* node) {
351014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* const receiver = NodeProperties::GetValueInput(node, 0);
352014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* const effect = NodeProperties::GetEffectInput(node);
353014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* const control = NodeProperties::GetControlInput(node);
354014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Operator const* const op =
355014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      simplified()->LoadField(AccessBuilder::ForJSRegExpSource());
356014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return Change(node, op, receiver, effect, control);
357014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
358014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
359014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
360014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochReduction JSIntrinsicLowering::ReduceSubString(Node* node) {
361014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return Change(node, CodeFactory::SubString(isolate()), 3);
362014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
363014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
364014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
365014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochReduction JSIntrinsicLowering::ReduceToInteger(Node* node) {
3661b268ca467c924004286c97bac133db489cf43d0Ben Murdoch  NodeProperties::ChangeOp(node, javascript()->ToInteger());
3671b268ca467c924004286c97bac133db489cf43d0Ben Murdoch  return Changed(node);
368014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
369014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
370014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
371014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochReduction JSIntrinsicLowering::ReduceToName(Node* node) {
372014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  NodeProperties::ChangeOp(node, javascript()->ToName());
373014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return Changed(node);
374014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
375014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
376014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
377014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochReduction JSIntrinsicLowering::ReduceToNumber(Node* node) {
378014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  NodeProperties::ChangeOp(node, javascript()->ToNumber());
379014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return Changed(node);
380014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
381014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
382014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
383014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochReduction JSIntrinsicLowering::ReduceToLength(Node* node) {
3841b268ca467c924004286c97bac133db489cf43d0Ben Murdoch  NodeProperties::ChangeOp(node, javascript()->ToLength());
3851b268ca467c924004286c97bac133db489cf43d0Ben Murdoch  return Changed(node);
386014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
387014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
388014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
389014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochReduction JSIntrinsicLowering::ReduceToObject(Node* node) {
390014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  NodeProperties::ChangeOp(node, javascript()->ToObject());
391014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return Changed(node);
392014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
393014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
394014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
395014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochReduction JSIntrinsicLowering::ReduceToPrimitive(Node* node) {
396014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* value = NodeProperties::GetValueInput(node, 0);
397014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Type* value_type = NodeProperties::GetType(value);
398014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  if (value_type->Is(Type::Primitive())) {
399014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    ReplaceWithValue(node, value);
400014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch    return Replace(value);
401014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  }
402014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return NoChange();
403014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
404014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
405014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
406014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochReduction JSIntrinsicLowering::ReduceToString(Node* node) {
407014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  NodeProperties::ChangeOp(node, javascript()->ToString());
408014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return Changed(node);
409014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
410014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
411014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
412014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochReduction JSIntrinsicLowering::ReduceCall(Node* node) {
413014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  size_t const arity = CallRuntimeParametersOf(node->op()).arity();
414342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch  NodeProperties::ChangeOp(node,
415342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch                           javascript()->CallFunction(arity, VectorSlotPair(),
416342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch                                                      ConvertReceiverMode::kAny,
417342c50ce1624b485728b9a4fc41d8bbf37eb46cfBen Murdoch                                                      TailCallMode::kDisallow));
418014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return Changed(node);
419014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
420014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
4211b268ca467c924004286c97bac133db489cf43d0Ben MurdochReduction JSIntrinsicLowering::ReduceNewObject(Node* node) {
422537ba893e2530051ec7f296e769fdd37bb4ae4a0Ben Murdoch  return Change(node, CodeFactory::FastNewObject(isolate()), 0);
423014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
424014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
425014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochReduction JSIntrinsicLowering::ReduceGetSuperConstructor(Node* node) {
426014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* active_function = NodeProperties::GetValueInput(node, 0);
427014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* effect = NodeProperties::GetEffectInput(node);
428014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* control = NodeProperties::GetControlInput(node);
429014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  Node* active_function_map = effect =
430014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      graph()->NewNode(simplified()->LoadField(AccessBuilder::ForMap()),
431014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                       active_function, effect, control);
432014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return Change(node, simplified()->LoadField(AccessBuilder::ForMapPrototype()),
433014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                active_function_map, effect, control);
434014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
435014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
436014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochReduction JSIntrinsicLowering::Change(Node* node, const Operator* op, Node* a,
437014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                                      Node* b) {
438014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  RelaxControls(node);
439014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  node->ReplaceInput(0, a);
440014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  node->ReplaceInput(1, b);
441014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  node->TrimInputCount(2);
442014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  NodeProperties::ChangeOp(node, op);
443014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return Changed(node);
444014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
445014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
446014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
447014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochReduction JSIntrinsicLowering::Change(Node* node, const Operator* op, Node* a,
448014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                                      Node* b, Node* c) {
449014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  RelaxControls(node);
450014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  node->ReplaceInput(0, a);
451014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  node->ReplaceInput(1, b);
452014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  node->ReplaceInput(2, c);
453014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  node->TrimInputCount(3);
454014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  NodeProperties::ChangeOp(node, op);
455014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return Changed(node);
456014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
457014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
458014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
459014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochReduction JSIntrinsicLowering::Change(Node* node, const Operator* op, Node* a,
460014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                                      Node* b, Node* c, Node* d) {
461014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  RelaxControls(node);
462014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  node->ReplaceInput(0, a);
463014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  node->ReplaceInput(1, b);
464014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  node->ReplaceInput(2, c);
465014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  node->ReplaceInput(3, d);
466014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  node->TrimInputCount(4);
467014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  NodeProperties::ChangeOp(node, op);
468014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return Changed(node);
469014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
470014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
471014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
472014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochReduction JSIntrinsicLowering::Change(Node* node, Callable const& callable,
473014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                                      int stack_parameter_count) {
474014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  CallDescriptor const* const desc = Linkage::GetStubCallDescriptor(
475014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      isolate(), graph()->zone(), callable.descriptor(), stack_parameter_count,
476014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch      CallDescriptor::kNeedsFrameState, node->op()->properties());
477014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  node->InsertInput(graph()->zone(), 0,
478014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch                    jsgraph()->HeapConstant(callable.code()));
479014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  NodeProperties::ChangeOp(node, common()->Call(desc));
480014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return Changed(node);
481014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
482014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
483014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
484014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochGraph* JSIntrinsicLowering::graph() const { return jsgraph()->graph(); }
485014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
486014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
487014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochIsolate* JSIntrinsicLowering::isolate() const { return jsgraph()->isolate(); }
488014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
489014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
490014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochCommonOperatorBuilder* JSIntrinsicLowering::common() const {
491014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return jsgraph()->common();
492014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
493014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
494014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochJSOperatorBuilder* JSIntrinsicLowering::javascript() const {
495014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return jsgraph_->javascript();
496014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
497014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
498014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
499014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochMachineOperatorBuilder* JSIntrinsicLowering::machine() const {
500014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return jsgraph()->machine();
501014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
502014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
503014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
504014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben MurdochSimplifiedOperatorBuilder* JSIntrinsicLowering::simplified() const {
505014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch  return jsgraph()->simplified();
506014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}
507014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch
508014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}  // namespace compiler
509014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}  // namespace internal
510014dc512cdd3e367bee49a713fdc5ed92584a3e5Ben Murdoch}  // namespace v8
511