js-generic-lowering.cc 17.7 KB
Newer Older
1 2 3 4
// Copyright 2014 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.

5
#include "src/code-factory.h"
6 7 8 9
#include "src/code-stubs.h"
#include "src/compiler/common-operator.h"
#include "src/compiler/js-generic-lowering.h"
#include "src/compiler/machine-operator.h"
10
#include "src/compiler/node-matchers.h"
11 12
#include "src/compiler/node-properties.h"
#include "src/compiler/operator-properties.h"
13 14 15 16 17 18
#include "src/unique.h"

namespace v8 {
namespace internal {
namespace compiler {

19 20
JSGenericLowering::JSGenericLowering(bool is_typing_enabled, JSGraph* jsgraph)
    : is_typing_enabled_(is_typing_enabled), jsgraph_(jsgraph) {}
21 22


23
void JSGenericLowering::PatchOperator(Node* node, const Operator* op) {
24 25 26 27 28 29 30 31 32
  node->set_op(op);
}


void JSGenericLowering::PatchInsertInput(Node* node, int index, Node* input) {
  node->InsertInput(zone(), index, input);
}


33
Reduction JSGenericLowering::Reduce(Node* node) {
34
  switch (node->opcode()) {
35 36 37 38
#define DECLARE_CASE(x)  \
    case IrOpcode::k##x: \
      Lower##x(node);    \
      break;
39 40
    JS_OP_LIST(DECLARE_CASE)
#undef DECLARE_CASE
41 42 43 44
    case IrOpcode::kBranch:
      // TODO(mstarzinger): If typing is enabled then simplified lowering will
      // have inserted the correct ChangeBoolToBit, otherwise we need to perform
      // poor-man's representation inference here and insert manual change.
45
      if (!is_typing_enabled_) {
46 47 48 49 50 51
        Node* condition = node->InputAt(0);
        if (condition->opcode() != IrOpcode::kAlways) {
          Node* test = graph()->NewNode(machine()->WordEqual(), condition,
                                        jsgraph()->TrueConstant());
          node->ReplaceInput(0, test);
        }
52 53 54
        break;
      }
      // Fall-through.
55 56
    default:
      // Nothing to see.
57
      return NoChange();
58
  }
59
  return Changed(node);
60 61 62
}


63
#define REPLACE_BINARY_OP_IC_CALL(op, token)                             \
64
  void JSGenericLowering::Lower##op(Node* node) {                        \
65 66
    ReplaceWithStubCall(node, CodeFactory::BinaryOpIC(isolate(), token), \
                        CallDescriptor::kPatchableCallSiteWithNop);      \
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
  }
REPLACE_BINARY_OP_IC_CALL(JSBitwiseOr, Token::BIT_OR)
REPLACE_BINARY_OP_IC_CALL(JSBitwiseXor, Token::BIT_XOR)
REPLACE_BINARY_OP_IC_CALL(JSBitwiseAnd, Token::BIT_AND)
REPLACE_BINARY_OP_IC_CALL(JSShiftLeft, Token::SHL)
REPLACE_BINARY_OP_IC_CALL(JSShiftRight, Token::SAR)
REPLACE_BINARY_OP_IC_CALL(JSShiftRightLogical, Token::SHR)
REPLACE_BINARY_OP_IC_CALL(JSAdd, Token::ADD)
REPLACE_BINARY_OP_IC_CALL(JSSubtract, Token::SUB)
REPLACE_BINARY_OP_IC_CALL(JSMultiply, Token::MUL)
REPLACE_BINARY_OP_IC_CALL(JSDivide, Token::DIV)
REPLACE_BINARY_OP_IC_CALL(JSModulus, Token::MOD)
#undef REPLACE_BINARY_OP_IC_CALL


82
#define REPLACE_COMPARE_IC_CALL(op, token)        \
83
  void JSGenericLowering::Lower##op(Node* node) { \
84
    ReplaceWithCompareIC(node, token);            \
85
  }
86 87 88 89 90 91 92 93
REPLACE_COMPARE_IC_CALL(JSEqual, Token::EQ)
REPLACE_COMPARE_IC_CALL(JSNotEqual, Token::NE)
REPLACE_COMPARE_IC_CALL(JSStrictEqual, Token::EQ_STRICT)
REPLACE_COMPARE_IC_CALL(JSStrictNotEqual, Token::NE_STRICT)
REPLACE_COMPARE_IC_CALL(JSLessThan, Token::LT)
REPLACE_COMPARE_IC_CALL(JSGreaterThan, Token::GT)
REPLACE_COMPARE_IC_CALL(JSLessThanOrEqual, Token::LTE)
REPLACE_COMPARE_IC_CALL(JSGreaterThanOrEqual, Token::GTE)
94 95 96
#undef REPLACE_COMPARE_IC_CALL


97 98 99
#define REPLACE_RUNTIME_CALL(op, fun)             \
  void JSGenericLowering::Lower##op(Node* node) { \
    ReplaceWithRuntimeCall(node, fun);            \
100 101 102 103 104 105 106
  }
REPLACE_RUNTIME_CALL(JSTypeOf, Runtime::kTypeof)
REPLACE_RUNTIME_CALL(JSCreate, Runtime::kAbort)
REPLACE_RUNTIME_CALL(JSCreateFunctionContext, Runtime::kNewFunctionContext)
REPLACE_RUNTIME_CALL(JSCreateWithContext, Runtime::kPushWithContext)
REPLACE_RUNTIME_CALL(JSCreateBlockContext, Runtime::kPushBlockContext)
REPLACE_RUNTIME_CALL(JSCreateModuleContext, Runtime::kPushModuleContext)
107
REPLACE_RUNTIME_CALL(JSCreateScriptContext, Runtime::kAbort)
108 109 110
#undef REPLACE_RUNTIME


111 112
#define REPLACE_UNIMPLEMENTED(op) \
  void JSGenericLowering::Lower##op(Node* node) { UNIMPLEMENTED(); }
113 114 115 116 117
REPLACE_UNIMPLEMENTED(JSYield)
REPLACE_UNIMPLEMENTED(JSDebugger)
#undef REPLACE_UNIMPLEMENTED


118 119
static CallDescriptor::Flags FlagsForNode(Node* node) {
  CallDescriptor::Flags result = CallDescriptor::kNoFlags;
120 121 122
  if (OperatorProperties::HasFrameStateInput(node->op())) {
    result |= CallDescriptor::kNeedsFrameState;
  }
123
  return result;
124 125 126
}


127
void JSGenericLowering::ReplaceWithCompareIC(Node* node, Token::Value token) {
128
  Callable callable = CodeFactory::CompareIC(isolate(), token);
129
  bool has_frame_state = OperatorProperties::HasFrameStateInput(node->op());
130 131
  CallDescriptor* desc_compare = Linkage::GetStubCallDescriptor(
      isolate(), zone(), callable.descriptor(), 0,
132
      CallDescriptor::kPatchableCallSiteWithNop | FlagsForNode(node));
133 134
  NodeVector inputs(zone());
  inputs.reserve(node->InputCount() + 1);
135
  inputs.push_back(jsgraph()->HeapConstant(callable.code()));
136 137 138
  inputs.push_back(NodeProperties::GetValueInput(node, 0));
  inputs.push_back(NodeProperties::GetValueInput(node, 1));
  inputs.push_back(NodeProperties::GetContextInput(node));
139
  if (node->op()->HasProperty(Operator::kPure)) {
140 141 142 143 144
    // A pure (strict) comparison doesn't have an effect, control or frame
    // state.  But for the graph, we need to add control and effect inputs.
    DCHECK(!has_frame_state);
    inputs.push_back(graph()->start());
    inputs.push_back(graph()->start());
145
  } else {
146 147 148 149 150 151
    DCHECK(has_frame_state == FLAG_turbo_deoptimization);
    if (FLAG_turbo_deoptimization) {
      inputs.push_back(NodeProperties::GetFrameStateInput(node));
    }
    inputs.push_back(NodeProperties::GetEffectInput(node));
    inputs.push_back(NodeProperties::GetControlInput(node));
152
  }
153 154 155
  Node* compare =
      graph()->NewNode(common()->Call(desc_compare),
                       static_cast<int>(inputs.size()), &inputs.front());
156

157
  node->ReplaceInput(0, compare);
158
  node->ReplaceInput(1, jsgraph()->SmiConstant(token));
159 160 161

  if (has_frame_state) {
    // Remove the frame state from inputs.
162
    node->RemoveInput(NodeProperties::FirstFrameStateIndex(node));
163 164
  }

165 166 167 168
  ReplaceWithRuntimeCall(node, Runtime::kBooleanize);
}


169
void JSGenericLowering::ReplaceWithStubCall(Node* node, Callable callable,
170
                                            CallDescriptor::Flags flags) {
171
  Operator::Properties properties = node->op()->properties();
172 173 174
  CallDescriptor* desc =
      Linkage::GetStubCallDescriptor(isolate(), zone(), callable.descriptor(),
                                     0, flags | FlagsForNode(node), properties);
175
  Node* stub_code = jsgraph()->HeapConstant(callable.code());
176 177 178 179 180 181 182 183
  PatchInsertInput(node, 0, stub_code);
  PatchOperator(node, common()->Call(desc));
}


void JSGenericLowering::ReplaceWithBuiltinCall(Node* node,
                                               Builtins::JavaScript id,
                                               int nargs) {
184
  Operator::Properties properties = node->op()->properties();
185 186
  Callable callable =
      CodeFactory::CallFunction(isolate(), nargs - 1, NO_CALL_FUNCTION_FLAGS);
187 188 189
  CallDescriptor* desc =
      Linkage::GetStubCallDescriptor(isolate(), zone(), callable.descriptor(),
                                     nargs, FlagsForNode(node), properties);
190 191 192 193 194 195 196 197 198 199 200 201 202 203
  Node* global_object = graph()->NewNode(
      machine()->Load(kMachAnyTagged), NodeProperties::GetContextInput(node),
      jsgraph()->IntPtrConstant(
          Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)),
      NodeProperties::GetEffectInput(node), graph()->start());
  Node* builtins_object = graph()->NewNode(
      machine()->Load(kMachAnyTagged), global_object,
      jsgraph()->IntPtrConstant(GlobalObject::kBuiltinsOffset - kHeapObjectTag),
      NodeProperties::GetEffectInput(node), graph()->start());
  Node* function = graph()->NewNode(
      machine()->Load(kMachAnyTagged), builtins_object,
      jsgraph()->IntPtrConstant(JSBuiltinsObject::OffsetOfFunctionWithId(id) -
                                kHeapObjectTag),
      NodeProperties::GetEffectInput(node), graph()->start());
204
  Node* stub_code = jsgraph()->HeapConstant(callable.code());
205
  PatchInsertInput(node, 0, stub_code);
206
  PatchInsertInput(node, 1, function);
207 208 209 210 211 212 213
  PatchOperator(node, common()->Call(desc));
}


void JSGenericLowering::ReplaceWithRuntimeCall(Node* node,
                                               Runtime::FunctionId f,
                                               int nargs_override) {
214
  Operator::Properties properties = node->op()->properties();
215 216
  const Runtime::Function* fun = Runtime::FunctionForId(f);
  int nargs = (nargs_override < 0) ? fun->nargs : nargs_override;
217
  CallDescriptor* desc =
218
      Linkage::GetRuntimeCallDescriptor(zone(), f, nargs, properties);
219 220
  Node* ref = jsgraph()->ExternalConstant(ExternalReference(f, isolate()));
  Node* arity = jsgraph()->Int32Constant(nargs);
221
  PatchInsertInput(node, 0, jsgraph()->CEntryStubConstant(fun->result_size));
222 223 224 225 226 227
  PatchInsertInput(node, nargs + 1, ref);
  PatchInsertInput(node, nargs + 2, arity);
  PatchOperator(node, common()->Call(desc));
}


228
void JSGenericLowering::LowerJSUnaryNot(Node* node) {
229 230 231
  Callable callable = CodeFactory::ToBoolean(
      isolate(), ToBooleanStub::RESULT_AS_INVERSE_ODDBALL);
  ReplaceWithStubCall(node, callable, CallDescriptor::kPatchableCallSite);
232 233 234
}


235
void JSGenericLowering::LowerJSToBoolean(Node* node) {
236 237 238
  Callable callable =
      CodeFactory::ToBoolean(isolate(), ToBooleanStub::RESULT_AS_ODDBALL);
  ReplaceWithStubCall(node, callable, CallDescriptor::kPatchableCallSite);
239 240 241
}


242 243
void JSGenericLowering::LowerJSToNumber(Node* node) {
  Callable callable = CodeFactory::ToNumber(isolate());
244
  ReplaceWithStubCall(node, callable, FlagsForNode(node));
245 246 247 248
}


void JSGenericLowering::LowerJSToString(Node* node) {
249 250 251 252
  ReplaceWithBuiltinCall(node, Builtins::TO_STRING, 1);
}


253 254 255 256 257
void JSGenericLowering::LowerJSToName(Node* node) {
  ReplaceWithBuiltinCall(node, Builtins::TO_NAME, 1);
}


258
void JSGenericLowering::LowerJSToObject(Node* node) {
259 260 261 262
  ReplaceWithBuiltinCall(node, Builtins::TO_OBJECT, 1);
}


263
void JSGenericLowering::LowerJSLoadProperty(Node* node) {
264 265 266
  const LoadPropertyParameters& p = LoadPropertyParametersOf(node->op());
  Callable callable = CodeFactory::KeyedLoadICInOptimizedCode(isolate());
  if (FLAG_vector_ics) {
267
    PatchInsertInput(node, 2, jsgraph()->SmiConstant(p.feedback().index()));
268 269
    PatchInsertInput(node, 3, jsgraph()->HeapConstant(p.feedback().vector()));
  }
270
  ReplaceWithStubCall(node, callable, CallDescriptor::kPatchableCallSite);
271 272 273
}


274
void JSGenericLowering::LowerJSLoadNamed(Node* node) {
275
  const LoadNamedParameters& p = LoadNamedParametersOf(node->op());
276 277
  Callable callable =
      CodeFactory::LoadICInOptimizedCode(isolate(), p.contextual_mode());
278
  PatchInsertInput(node, 1, jsgraph()->HeapConstant(p.name()));
279
  if (FLAG_vector_ics) {
280
    PatchInsertInput(node, 2, jsgraph()->SmiConstant(p.feedback().index()));
281 282
    PatchInsertInput(node, 3, jsgraph()->HeapConstant(p.feedback().vector()));
  }
283
  ReplaceWithStubCall(node, callable, CallDescriptor::kPatchableCallSite);
284 285 286
}


287
void JSGenericLowering::LowerJSStoreProperty(Node* node) {
288 289
  LanguageMode language_mode = OpParameter<LanguageMode>(node);
  Callable callable = CodeFactory::KeyedStoreIC(isolate(), language_mode);
290
  ReplaceWithStubCall(node, callable, CallDescriptor::kPatchableCallSite);
291 292 293
}


294
void JSGenericLowering::LowerJSStoreNamed(Node* node) {
295
  const StoreNamedParameters& p = StoreNamedParametersOf(node->op());
296
  Callable callable = CodeFactory::StoreIC(isolate(), p.language_mode());
297
  PatchInsertInput(node, 1, jsgraph()->HeapConstant(p.name()));
298
  ReplaceWithStubCall(node, callable, CallDescriptor::kPatchableCallSite);
299 300 301
}


302
void JSGenericLowering::LowerJSDeleteProperty(Node* node) {
303
  LanguageMode language_mode = OpParameter<LanguageMode>(node);
304
  ReplaceWithBuiltinCall(node, Builtins::DELETE, 3);
305
  PatchInsertInput(node, 4, jsgraph()->SmiConstant(language_mode));
306 307 308
}


309
void JSGenericLowering::LowerJSHasProperty(Node* node) {
310 311 312 313
  ReplaceWithBuiltinCall(node, Builtins::IN, 2);
}


314
void JSGenericLowering::LowerJSInstanceOf(Node* node) {
315 316 317 318
  InstanceofStub::Flags flags = static_cast<InstanceofStub::Flags>(
      InstanceofStub::kReturnTrueFalseObject |
      InstanceofStub::kArgsInRegisters);
  InstanceofStub stub(isolate(), flags);
319
  CallInterfaceDescriptor d = stub.GetCallInterfaceDescriptor();
320 321
  CallDescriptor* desc = Linkage::GetStubCallDescriptor(isolate(), zone(), d, 0,
                                                        FlagsForNode(node));
322
  Node* stub_code = jsgraph()->HeapConstant(stub.GetCode());
323 324 325 326 327
  PatchInsertInput(node, 0, stub_code);
  PatchOperator(node, common()->Call(desc));
}


328
void JSGenericLowering::LowerJSLoadContext(Node* node) {
329 330
  const ContextAccess& access = ContextAccessOf(node->op());
  for (size_t i = 0; i < access.depth(); ++i) {
331
    node->ReplaceInput(
332 333 334 335 336 337
        0, graph()->NewNode(machine()->Load(kMachAnyTagged),
                            NodeProperties::GetValueInput(node, 0),
                            jsgraph()->Int32Constant(
                                Context::SlotOffset(Context::PREVIOUS_INDEX)),
                            NodeProperties::GetEffectInput(node),
                            graph()->start()));
338
  }
339 340
  node->ReplaceInput(1, jsgraph()->Int32Constant(Context::SlotOffset(
                            static_cast<int>(access.index()))));
341
  node->AppendInput(zone(), graph()->start());
342
  PatchOperator(node, machine()->Load(kMachAnyTagged));
343 344 345
}


346
void JSGenericLowering::LowerJSStoreContext(Node* node) {
347 348
  const ContextAccess& access = ContextAccessOf(node->op());
  for (size_t i = 0; i < access.depth(); ++i) {
349
    node->ReplaceInput(
350 351 352 353 354 355
        0, graph()->NewNode(machine()->Load(kMachAnyTagged),
                            NodeProperties::GetValueInput(node, 0),
                            jsgraph()->Int32Constant(
                                Context::SlotOffset(Context::PREVIOUS_INDEX)),
                            NodeProperties::GetEffectInput(node),
                            graph()->start()));
356 357
  }
  node->ReplaceInput(2, NodeProperties::GetValueInput(node, 1));
358 359
  node->ReplaceInput(1, jsgraph()->Int32Constant(Context::SlotOffset(
                            static_cast<int>(access.index()))));
360 361
  PatchOperator(node, machine()->Store(StoreRepresentation(kMachAnyTagged,
                                                           kFullWriteBarrier)));
362 363 364
}


365 366 367 368 369 370 371
void JSGenericLowering::LowerJSCreateCatchContext(Node* node) {
  Unique<String> name = OpParameter<Unique<String>>(node);
  PatchInsertInput(node, 0, jsgraph()->HeapConstant(name));
  ReplaceWithRuntimeCall(node, Runtime::kPushCatchContext);
}


372
void JSGenericLowering::LowerJSCallConstruct(Node* node) {
373 374
  int arity = OpParameter<int>(node);
  CallConstructStub stub(isolate(), NO_CALL_CONSTRUCTOR_FLAGS);
375
  CallInterfaceDescriptor d = stub.GetCallInterfaceDescriptor();
376 377
  CallDescriptor* desc = Linkage::GetStubCallDescriptor(
      isolate(), zone(), d, arity, FlagsForNode(node));
378
  Node* stub_code = jsgraph()->HeapConstant(stub.GetCode());
379 380
  Node* construct = NodeProperties::GetValueInput(node, 0);
  PatchInsertInput(node, 0, stub_code);
381
  PatchInsertInput(node, 1, jsgraph()->Int32Constant(arity - 1));
382 383 384 385 386 387
  PatchInsertInput(node, 2, construct);
  PatchInsertInput(node, 3, jsgraph()->UndefinedConstant());
  PatchOperator(node, common()->Call(desc));
}


388 389 390 391 392 393 394 395 396 397 398
bool JSGenericLowering::TryLowerDirectJSCall(Node* node) {
  // Lower to a direct call to a constant JSFunction if legal.
  const CallFunctionParameters& p = CallFunctionParametersOf(node->op());
  int arg_count = static_cast<int>(p.arity() - 2);

  // Check the function is a constant and is really a JSFunction.
  HeapObjectMatcher<Object> function_const(node->InputAt(0));
  if (!function_const.HasValue()) return false;  // not a constant.
  Handle<Object> func = function_const.Value().handle();
  if (!func->IsJSFunction()) return false;  // not a function.
  Handle<JSFunction> function = Handle<JSFunction>::cast(func);
399 400 401
  if (arg_count != function->shared()->internal_formal_parameter_count()) {
    return false;
  }
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419

  // Check the receiver doesn't need to be wrapped.
  Node* receiver = node->InputAt(1);
  if (!NodeProperties::IsTyped(receiver)) return false;
  Type* ok_receiver = Type::Union(Type::Undefined(), Type::Receiver(), zone());
  if (!NodeProperties::GetBounds(receiver).upper->Is(ok_receiver)) return false;

  int index = NodeProperties::FirstContextIndex(node);

  // TODO(titzer): total hack to share function context constants.
  // Remove this when the JSGraph canonicalizes heap constants.
  Node* context = node->InputAt(index);
  HeapObjectMatcher<Context> context_const(context);
  if (!context_const.HasValue() ||
      *(context_const.Value().handle()) != function->context()) {
    context = jsgraph()->HeapConstant(Handle<Context>(function->context()));
  }
  node->ReplaceInput(index, context);
420 421
  CallDescriptor* desc = Linkage::GetJSCallDescriptor(
      zone(), false, 1 + arg_count, FlagsForNode(node));
422 423 424 425 426
  PatchOperator(node, common()->Call(desc));
  return true;
}


427
void JSGenericLowering::LowerJSCallFunction(Node* node) {
428 429 430 431
  // Fast case: call function directly.
  if (TryLowerDirectJSCall(node)) return;

  // General case: CallFunctionStub.
432
  const CallFunctionParameters& p = CallFunctionParametersOf(node->op());
433 434
  int arg_count = static_cast<int>(p.arity() - 2);
  CallFunctionStub stub(isolate(), arg_count, p.flags());
435
  CallInterfaceDescriptor d = stub.GetCallInterfaceDescriptor();
436 437 438
  CallDescriptor* desc = Linkage::GetStubCallDescriptor(
      isolate(), zone(), d, static_cast<int>(p.arity() - 1),
      FlagsForNode(node));
439
  Node* stub_code = jsgraph()->HeapConstant(stub.GetCode());
440
  PatchInsertInput(node, 0, stub_code);
441 442 443 444
  PatchOperator(node, common()->Call(desc));
}


445
void JSGenericLowering::LowerJSCallRuntime(Node* node) {
446 447
  const CallRuntimeParameters& p = CallRuntimeParametersOf(node->op());
  ReplaceWithRuntimeCall(node, p.id(), static_cast<int>(p.arity()));
448
}
449 450 451 452

}  // namespace compiler
}  // namespace internal
}  // namespace v8