Commit 6b2001b6 authored by bmeurer's avatar bmeurer Committed by Commit bot

[turbofan] Introduce proper ObjectIsReceiver operator.

Avoid the hacking in JSIntrinsicLowering and provide a proper simplified
operator ObjectIsReceiver instead that is used to implement %_IsJSReceiver
which is used by our JavaScript builtins and the JSInliner.

R=jarin@chromium.org
BUG=v8:4544
LOG=n

Review URL: https://codereview.chromium.org/1657863004

Cr-Commit-Position: refs/heads/master@{#33675}
parent 6a118774
......@@ -228,46 +228,7 @@ Reduction JSIntrinsicLowering::ReduceIsInstanceType(
Reduction JSIntrinsicLowering::ReduceIsJSReceiver(Node* node) {
Node* value = NodeProperties::GetValueInput(node, 0);
Type* value_type = NodeProperties::GetType(value);
Node* effect = NodeProperties::GetEffectInput(node);
Node* control = NodeProperties::GetControlInput(node);
if (value_type->Is(Type::Receiver())) {
value = jsgraph()->TrueConstant();
} else if (!value_type->Maybe(Type::Receiver())) {
value = jsgraph()->FalseConstant();
} else {
// if (%_IsSmi(value)) {
// return false;
// } else {
// return FIRST_JS_RECEIVER_TYPE <= %_GetInstanceType(%_GetMap(value))
// }
STATIC_ASSERT(LAST_TYPE == LAST_JS_RECEIVER_TYPE);
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);
Node* efalse = graph()->NewNode(
simplified()->LoadField(AccessBuilder::ForMapInstanceType()),
graph()->NewNode(simplified()->LoadField(AccessBuilder::ForMap()),
value, effect, if_false),
effect, if_false);
Node* vfalse = graph()->NewNode(
machine()->Uint32LessThanOrEqual(),
jsgraph()->Int32Constant(FIRST_JS_RECEIVER_TYPE), efalse);
control = graph()->NewNode(common()->Merge(2), if_true, if_false);
effect = graph()->NewNode(common()->EffectPhi(2), etrue, efalse, control);
value = graph()->NewNode(common()->Phi(MachineRepresentation::kTagged, 2),
vtrue, vfalse, control);
}
ReplaceWithValue(node, node, effect, control);
return Replace(value);
return Change(node, simplified()->ObjectIsReceiver());
}
......
......@@ -705,7 +705,7 @@ Reduction JSNativeContextSpecialization::ReduceElementAccess(
Type* element_type = Type::Any();
MachineType element_machine_type = MachineType::AnyTagged();
if (IsFastDoubleElementsKind(elements_kind)) {
element_type = type_cache_.kFloat64;
element_type = Type::Number();
element_machine_type = MachineType::Float64();
} else if (IsFastSmiElementsKind(elements_kind)) {
element_type = type_cache_.kSmi;
......
......@@ -202,6 +202,7 @@
V(StoreBuffer) \
V(StoreElement) \
V(ObjectIsNumber) \
V(ObjectIsReceiver) \
V(ObjectIsSmi)
// Opcodes for Machine-level operators.
......
......@@ -1240,6 +1240,12 @@ class RepresentationSelector {
if (lower()) lowering->DoObjectIsNumber(node);
break;
}
case IrOpcode::kObjectIsReceiver: {
ProcessInput(node, 0, UseInfo::AnyTagged());
SetOutput(node, NodeOutputInfo::Bool());
if (lower()) lowering->DoObjectIsReceiver(node);
break;
}
case IrOpcode::kObjectIsSmi: {
ProcessInput(node, 0, UseInfo::AnyTagged());
SetOutput(node, NodeOutputInfo::Bool());
......@@ -1583,21 +1589,13 @@ void SimplifiedLowering::DoStoreBuffer(Node* node) {
void SimplifiedLowering::DoObjectIsNumber(Node* node) {
Node* input = NodeProperties::GetValueInput(node, 0);
// TODO(bmeurer): Optimize somewhat based on input type.
Node* check =
graph()->NewNode(machine()->WordEqual(),
graph()->NewNode(machine()->WordAnd(), input,
jsgraph()->IntPtrConstant(kSmiTagMask)),
jsgraph()->IntPtrConstant(kSmiTag));
Node* check = IsSmi(input);
Node* branch = graph()->NewNode(common()->Branch(), check, graph()->start());
Node* if_true = graph()->NewNode(common()->IfTrue(), branch);
Node* vtrue = jsgraph()->Int32Constant(1);
Node* if_false = graph()->NewNode(common()->IfFalse(), branch);
Node* vfalse = graph()->NewNode(
machine()->WordEqual(),
graph()->NewNode(
machine()->Load(MachineType::AnyTagged()), input,
jsgraph()->IntPtrConstant(HeapObject::kMapOffset - kHeapObjectTag),
graph()->start(), if_false),
machine()->WordEqual(), LoadHeapObjectMap(input, if_false),
jsgraph()->HeapConstant(isolate()->factory()->heap_number_map()));
Node* control = graph()->NewNode(common()->Merge(2), if_true, if_false);
node->ReplaceInput(0, vtrue);
......@@ -1607,6 +1605,27 @@ void SimplifiedLowering::DoObjectIsNumber(Node* node) {
}
void SimplifiedLowering::DoObjectIsReceiver(Node* node) {
Node* input = NodeProperties::GetValueInput(node, 0);
// TODO(bmeurer): Optimize somewhat based on input type.
STATIC_ASSERT(LAST_TYPE == LAST_JS_RECEIVER_TYPE);
Node* check = IsSmi(input);
Node* branch = graph()->NewNode(common()->Branch(), check, graph()->start());
Node* if_true = graph()->NewNode(common()->IfTrue(), branch);
Node* vtrue = jsgraph()->Int32Constant(0);
Node* if_false = graph()->NewNode(common()->IfFalse(), branch);
Node* vfalse =
graph()->NewNode(machine()->Uint32LessThanOrEqual(),
jsgraph()->Uint32Constant(FIRST_JS_RECEIVER_TYPE),
LoadMapInstanceType(LoadHeapObjectMap(input, if_false)));
Node* control = graph()->NewNode(common()->Merge(2), if_true, if_false);
node->ReplaceInput(0, vtrue);
node->AppendInput(graph()->zone(), vfalse);
node->AppendInput(graph()->zone(), control);
NodeProperties::ChangeOp(node, common()->Phi(MachineRepresentation::kBit, 2));
}
void SimplifiedLowering::DoObjectIsSmi(Node* node) {
node->ReplaceInput(0,
graph()->NewNode(machine()->WordAnd(), node->InputAt(0),
......@@ -1616,6 +1635,31 @@ void SimplifiedLowering::DoObjectIsSmi(Node* node) {
}
Node* SimplifiedLowering::IsSmi(Node* value) {
return graph()->NewNode(
machine()->WordEqual(),
graph()->NewNode(machine()->WordAnd(), value,
jsgraph()->IntPtrConstant(kSmiTagMask)),
jsgraph()->IntPtrConstant(kSmiTag));
}
Node* SimplifiedLowering::LoadHeapObjectMap(Node* object, Node* control) {
return graph()->NewNode(
machine()->Load(MachineType::AnyTagged()), object,
jsgraph()->IntPtrConstant(HeapObject::kMapOffset - kHeapObjectTag),
graph()->start(), control);
}
Node* SimplifiedLowering::LoadMapInstanceType(Node* map) {
return graph()->NewNode(
machine()->Load(MachineType::Uint8()), map,
jsgraph()->IntPtrConstant(Map::kInstanceTypeOffset - kHeapObjectTag),
graph()->start(), graph()->start());
}
Node* SimplifiedLowering::StringComparison(Node* node) {
Operator::Properties properties = node->op()->properties();
Callable callable = CodeFactory::StringCompare(isolate());
......
......@@ -37,6 +37,7 @@ class SimplifiedLowering final {
RepresentationChanger* changer);
void DoStoreBuffer(Node* node);
void DoObjectIsNumber(Node* node);
void DoObjectIsReceiver(Node* node);
void DoObjectIsSmi(Node* node);
void DoShift(Node* node, Operator const* op, Type* rhs_type);
void DoStringEqual(Node* node);
......@@ -55,6 +56,9 @@ class SimplifiedLowering final {
// position information via the SourcePositionWrapper like all other reducers.
SourcePositionTable* source_positions_;
Node* IsSmi(Node* value);
Node* LoadHeapObjectMap(Node* object, Node* control);
Node* LoadMapInstanceType(Node* map);
Node* StringComparison(Node* node);
Node* Int32Div(Node* const node);
Node* Int32Mod(Node* const node);
......
......@@ -187,6 +187,7 @@ const ElementAccess& ElementAccessOf(const Operator* op) {
V(ChangeBoolToBit, Operator::kNoProperties, 1) \
V(ChangeBitToBool, Operator::kNoProperties, 1) \
V(ObjectIsNumber, Operator::kNoProperties, 1) \
V(ObjectIsReceiver, Operator::kNoProperties, 1) \
V(ObjectIsSmi, Operator::kNoProperties, 1)
#define NO_THROW_OP_LIST(V) \
......
......@@ -165,6 +165,7 @@ class SimplifiedOperatorBuilder final : public ZoneObject {
const Operator* ChangeBitToBool();
const Operator* ObjectIsNumber();
const Operator* ObjectIsReceiver();
const Operator* ObjectIsSmi();
const Operator* Allocate(PretenureFlag pretenure = NOT_TENURED);
......
......@@ -241,6 +241,10 @@ class Typer::Visitor : public Reducer {
static Type* NumberToInt32(Type*, Typer*);
static Type* NumberToUint32(Type*, Typer*);
static Type* ObjectIsNumber(Type*, Typer*);
static Type* ObjectIsReceiver(Type*, Typer*);
static Type* ObjectIsSmi(Type*, Typer*);
static Type* JSAddRanger(RangeType*, RangeType*, Typer*);
static Type* JSSubtractRanger(RangeType*, RangeType*, Typer*);
static Type* JSDivideRanger(RangeType*, RangeType*, Typer*);
......@@ -505,6 +509,30 @@ Type* Typer::Visitor::NumberToUint32(Type* type, Typer* t) {
}
// Type checks.
Type* Typer::Visitor::ObjectIsNumber(Type* type, Typer* t) {
if (type->Is(Type::Number())) return t->singleton_true_;
if (!type->Maybe(Type::Number())) return t->singleton_false_;
return Type::Boolean();
}
Type* Typer::Visitor::ObjectIsReceiver(Type* type, Typer* t) {
if (type->Is(Type::Receiver())) return t->singleton_true_;
if (!type->Maybe(Type::Receiver())) return t->singleton_false_;
return Type::Boolean();
}
Type* Typer::Visitor::ObjectIsSmi(Type* type, Typer* t) {
if (type->Is(Type::TaggedSigned())) return t->singleton_true_;
if (type->Is(Type::TaggedPointer())) return t->singleton_false_;
return Type::Boolean();
}
// -----------------------------------------------------------------------------
......@@ -1529,13 +1557,15 @@ Type* Typer::Visitor::TypeJSCallFunction(Node* node) {
Type* Typer::Visitor::TypeJSCallRuntime(Node* node) {
switch (CallRuntimeParametersOf(node->op()).id()) {
case Runtime::kInlineIsJSReceiver:
return TypeUnaryOp(node, ObjectIsReceiver);
case Runtime::kInlineIsSmi:
return TypeUnaryOp(node, ObjectIsSmi);
case Runtime::kInlineIsArray:
case Runtime::kInlineIsDate:
case Runtime::kInlineIsTypedArray:
case Runtime::kInlineIsMinusZero:
case Runtime::kInlineIsRegExp:
case Runtime::kInlineIsJSReceiver:
return Type::Boolean();
case Runtime::kInlineDoubleLo:
case Runtime::kInlineDoubleHi:
......@@ -1878,20 +1908,17 @@ Type* Typer::Visitor::TypeStoreElement(Node* node) {
Type* Typer::Visitor::TypeObjectIsNumber(Node* node) {
Type* arg = Operand(node, 0);
if (arg->Is(Type::None())) return Type::None();
if (arg->Is(Type::Number())) return typer_->singleton_true_;
if (!arg->Maybe(Type::Number())) return typer_->singleton_false_;
return Type::Boolean();
return TypeUnaryOp(node, ObjectIsNumber);
}
Type* Typer::Visitor::TypeObjectIsReceiver(Node* node) {
return TypeUnaryOp(node, ObjectIsReceiver);
}
Type* Typer::Visitor::TypeObjectIsSmi(Node* node) {
Type* arg = Operand(node, 0);
if (arg->Is(Type::None())) return Type::None();
if (arg->Is(Type::TaggedSigned())) return typer_->singleton_true_;
if (arg->Is(Type::TaggedPointer())) return typer_->singleton_false_;
return Type::Boolean();
return TypeUnaryOp(node, ObjectIsSmi);
}
......
......@@ -713,6 +713,7 @@ void Verifier::Visitor::Check(Node* node) {
break;
}
case IrOpcode::kObjectIsNumber:
case IrOpcode::kObjectIsReceiver:
case IrOpcode::kObjectIsSmi:
CheckValueInputIs(node, 0, Type::Any());
CheckUpperIs(node, Type::Boolean());
......
// 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.
// Flags: --allow-natives-syntax
var a = [-0, 0];
var b;
function foo(a) {
for (var i = 0; i < 2; ++i) {
b = a[i];
}
}
foo(a);
foo(a);
%OptimizeFunctionOnNextCall(foo);
foo(a);
......@@ -23,9 +23,9 @@ namespace v8 {
namespace internal {
namespace compiler {
class JSIntrinsicLoweringTest : public TypedGraphTest {
class JSIntrinsicLoweringTest : public GraphTest {
public:
JSIntrinsicLoweringTest() : TypedGraphTest(3), javascript_(zone()) {}
JSIntrinsicLoweringTest() : GraphTest(3), javascript_(zone()) {}
~JSIntrinsicLoweringTest() override {}
protected:
......@@ -257,57 +257,16 @@ TEST_F(JSIntrinsicLoweringTest, InlineIsRegExp) {
// %_IsJSReceiver
TEST_F(JSIntrinsicLoweringTest, InlineIsJSReceiverWithAny) {
Node* const input = Parameter(Type::Any());
Node* const context = Parameter(Type::Any());
Node* const effect = graph()->start();
Node* const control = graph()->start();
Reduction const r = Reduce(graph()->NewNode(
javascript()->CallRuntime(Runtime::kInlineIsJSReceiver, 1), input,
context, effect, control));
ASSERT_TRUE(r.Changed());
Node* phi = r.replacement();
Capture<Node *> branch, if_false;
EXPECT_THAT(
phi,
IsPhi(
MachineRepresentation::kTagged, IsFalseConstant(),
IsUint32LessThanOrEqual(
IsInt32Constant(FIRST_JS_RECEIVER_TYPE),
IsLoadField(AccessBuilder::ForMapInstanceType(),
IsLoadField(AccessBuilder::ForMap(), input, effect,
CaptureEq(&if_false)),
effect, _)),
IsMerge(IsIfTrue(AllOf(CaptureEq(&branch),
IsBranch(IsObjectIsSmi(input), control))),
AllOf(CaptureEq(&if_false), IsIfFalse(CaptureEq(&branch))))));
}
TEST_F(JSIntrinsicLoweringTest, InlineIsJSReceiverWithReceiver) {
Node* const input = Parameter(Type::Receiver());
Node* const context = Parameter(Type::Any());
Node* const effect = graph()->start();
Node* const control = graph()->start();
Reduction const r = Reduce(graph()->NewNode(
javascript()->CallRuntime(Runtime::kInlineIsJSReceiver, 1), input,
context, effect, control));
ASSERT_TRUE(r.Changed());
EXPECT_THAT(r.replacement(), IsTrueConstant());
}
TEST_F(JSIntrinsicLoweringTest, InlineIsJSReceiverWithUndefined) {
Node* const input = Parameter(Type::Undefined());
Node* const context = Parameter(Type::Any());
TEST_F(JSIntrinsicLoweringTest, InlineIsJSReceiver) {
Node* const input = Parameter(0);
Node* const context = Parameter(1);
Node* const effect = graph()->start();
Node* const control = graph()->start();
Reduction const r = Reduce(graph()->NewNode(
javascript()->CallRuntime(Runtime::kInlineIsJSReceiver, 1), input,
context, effect, control));
ASSERT_TRUE(r.Changed());
EXPECT_THAT(r.replacement(), IsFalseConstant());
EXPECT_THAT(r.replacement(), IsObjectIsReceiver(input));
}
......
......@@ -2106,6 +2106,7 @@ IS_UNOP_MATCHER(Float64ExtractLowWord32)
IS_UNOP_MATCHER(Float64ExtractHighWord32)
IS_UNOP_MATCHER(NumberToInt32)
IS_UNOP_MATCHER(NumberToUint32)
IS_UNOP_MATCHER(ObjectIsReceiver)
IS_UNOP_MATCHER(ObjectIsSmi)
IS_UNOP_MATCHER(Word32Clz)
#undef IS_UNOP_MATCHER
......
......@@ -243,6 +243,7 @@ Matcher<Node*> IsStoreElement(const Matcher<ElementAccess>& access_matcher,
const Matcher<Node*>& value_matcher,
const Matcher<Node*>& effect_matcher,
const Matcher<Node*>& control_matcher);
Matcher<Node*> IsObjectIsReceiver(const Matcher<Node*>& value_matcher);
Matcher<Node*> IsObjectIsSmi(const Matcher<Node*>& value_matcher);
Matcher<Node*> IsLoad(const Matcher<LoadRepresentation>& rep_matcher,
......
......@@ -65,6 +65,8 @@ const PureOperator kPureOperators[] = {
PURE(ChangeFloat64ToTagged, Operator::kNoProperties, 1),
PURE(ChangeBoolToBit, Operator::kNoProperties, 1),
PURE(ChangeBitToBool, Operator::kNoProperties, 1),
PURE(ObjectIsNumber, Operator::kNoProperties, 1),
PURE(ObjectIsReceiver, Operator::kNoProperties, 1),
PURE(ObjectIsSmi, Operator::kNoProperties, 1)
#undef PURE
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment