Node* control = graph()->start();
switch (node->opcode()) {
case IrOpcode::kChangeBitToBool:
- return ChangeBitToBool(node->InputAt(0), control);
+ return ChangeBitToBool(node->InputAt(0));
case IrOpcode::kChangeBoolToBit:
return ChangeBoolToBit(node->InputAt(0));
+ case IrOpcode::kChangeWord32ToBit:
+ return ChangeWord32ToBit(node->InputAt(0));
+ case IrOpcode::kChangeWord64ToBit:
+ return ChangeWord64ToBit(node->InputAt(0));
case IrOpcode::kChangeFloat64ToTagged:
return ChangeFloat64ToTagged(node->InputAt(0), control);
case IrOpcode::kChangeInt32ToTagged:
}
-Reduction ChangeLowering::ChangeBitToBool(Node* val, Node* control) {
+Reduction ChangeLowering::ChangeBitToBool(Node* value) {
MachineType const type = static_cast<MachineType>(kTypeBool | kRepTagged);
- return Replace(graph()->NewNode(common()->Select(type), val,
+ return Replace(graph()->NewNode(common()->Select(type), value,
jsgraph()->TrueConstant(),
jsgraph()->FalseConstant()));
}
-Reduction ChangeLowering::ChangeBoolToBit(Node* val) {
+Reduction ChangeLowering::ChangeBoolToBit(Node* value) {
+ return Replace(graph()->NewNode(machine()->WordEqual(), value,
+ jsgraph()->TrueConstant()));
+}
+
+
+Reduction ChangeLowering::ChangeWord32ToBit(Node* value) {
+ return Replace(
+ graph()->NewNode(machine()->Word32Equal(),
+ graph()->NewNode(machine()->Word32Equal(), value,
+ jsgraph()->Int32Constant(0)),
+ jsgraph()->Int32Constant(0)));
+}
+
+
+Reduction ChangeLowering::ChangeWord64ToBit(Node* value) {
return Replace(
- graph()->NewNode(machine()->WordEqual(), val, jsgraph()->TrueConstant()));
+ graph()->NewNode(machine()->Word32Equal(),
+ graph()->NewNode(machine()->Word64Equal(), value,
+ jsgraph()->Int64Constant(0)),
+ jsgraph()->Int32Constant(0)));
}
class Linkage;
class MachineOperatorBuilder;
+
class ChangeLowering FINAL : public Reducer {
public:
ChangeLowering(JSGraph* jsgraph, Linkage* linkage)
Node* TestNotSmi(Node* value);
Node* Uint32LessThanOrEqual(Node* lhs, Node* rhs);
- Reduction ChangeBitToBool(Node* value, Node* control);
+ Reduction ChangeBitToBool(Node* value);
Reduction ChangeBoolToBit(Node* value);
+ Reduction ChangeWord32ToBit(Node* value);
+ Reduction ChangeWord64ToBit(Node* value);
Reduction ChangeFloat64ToTagged(Node* value, Node* control);
Reduction ChangeInt32ToTagged(Node* value, Node* control);
Reduction ChangeTaggedToFloat64(Node* value, Node* control);
}
+Type* JSGraph::ZeroOneRangeType() {
+ if (!zero_one_range_type_.is_set()) {
+ zero_one_range_type_.set(
+ Type::Range(factory()->NewNumber(0), factory()->NewNumber(1), zone()));
+ }
+ return zero_one_range_type_.get();
+}
+
+
void JSGraph::GetCachedNodes(NodeVector* nodes) {
cache_.GetCachedNodes(nodes);
SetOncePointer<Node>* ptrs[] = {
namespace internal {
namespace compiler {
+// Forward declarations.
class Typer;
// Implements a facade on a Graph, enhancing the graph with JS-specific
// stubs and runtime functions that do not require a context.
Node* NoContextConstant() { return ZeroConstant(); }
+ // Cached common types.
+ Type* ZeroOneRangeType();
+
JSOperatorBuilder* javascript() { return javascript_; }
CommonOperatorBuilder* common() { return common_; }
MachineOperatorBuilder* machine() { return machine_; }
SetOncePointer<Node> one_constant_;
SetOncePointer<Node> nan_constant_;
+ SetOncePointer<Type> zero_one_range_type_;
+
CommonNodeCache cache_;
Node* ImmovableHeapConstant(Handle<HeapObject> value);
JS_CONTEXT_OP_LIST(V) \
JS_OTHER_OP_LIST(V)
-// Opcodes for VirtuaMachine-level operators.
+// Opcodes for VirtualMachine-level operators.
#define SIMPLIFIED_OP_LIST(V) \
V(AnyToBoolean) \
V(BooleanNot) \
V(ChangeUint32ToTagged) \
V(ChangeFloat64ToTagged) \
V(ChangeBoolToBit) \
+ V(ChangeWord32ToBit) \
+ V(ChangeWord64ToBit) \
V(ChangeBitToBool) \
V(LoadField) \
V(LoadBuffer) \
: jsgraph_(jsgraph),
simplified_(simplified),
isolate_(isolate),
+ bit_range_(Type::Range(isolate->factory()->NewNumber(0),
+ isolate->factory()->NewNumber(1),
+ jsgraph->zone())),
testing_type_errors_(false),
type_error_(false) {}
// TODO(titzer): should Word64 also be implicitly convertable to others?
- static const MachineTypeUnion rWord =
- kRepBit | kRepWord8 | kRepWord16 | kRepWord32;
+ static const MachineTypeUnion rWord = kRepWord8 | kRepWord16 | kRepWord32;
Node* GetRepresentationFor(Node* node, MachineTypeUnion output_type,
MachineTypeUnion use_type) {
break;
}
// Select the correct X -> Word32 operator.
- const Operator* op = NULL;
- if (output_type & kRepFloat64) {
+ const Operator* op;
+ if (output_type & kRepBit) {
+ return node; // No change necessary.
+ } else if (output_type & kRepFloat64) {
if (output_type & kTypeUint32 || use_unsigned) {
op = machine()->ChangeFloat64ToUint32();
} else {
}
case IrOpcode::kNumberConstant: {
double value = OpParameter<double>(node);
- if (std::isnan(value) || value == 0.0) {
- return jsgraph()->Int32Constant(0);
- }
- return jsgraph()->Int32Constant(1);
+ if (value == 0 || std::isnan(value)) return jsgraph()->Int32Constant(0);
+ return jsgraph()->Int32Constant(1); // value != +0.0, -0.0, NaN
}
case IrOpcode::kHeapConstant: {
- Handle<Object> handle = OpParameter<Unique<Object> >(node).handle();
- DCHECK(*handle == isolate()->heap()->true_value() ||
- *handle == isolate()->heap()->false_value());
- return jsgraph()->Int32Constant(
- *handle == isolate()->heap()->true_value() ? 1 : 0);
+ Handle<Object> object = OpParameter<Unique<Object>>(node).handle();
+ return jsgraph()->Int32Constant(object->BooleanValue() ? 1 : 0);
}
default:
break;
// Select the correct X -> Bit operator.
const Operator* op;
if (output_type & rWord) {
- return node; // No change necessary.
+ op = simplified()->ChangeWord32ToBit();
} else if (output_type & kRepWord64) {
- return node; // TODO(titzer): No change necessary, on 64-bit.
+ op = simplified()->ChangeWord64ToBit();
} else if (output_type & kRepTagged) {
- op = simplified()->ChangeBoolToBit();
+ Type* upper = NodeProperties::GetBounds(node).upper;
+ if (upper->Is(Type::Boolean())) {
+ op = simplified()->ChangeBoolToBit();
+ } else if (upper->Is(Type::Signed32())) {
+ // Tagged -> Int32 -> Bit
+ node = InsertChangeTaggedToInt32(node);
+ op = simplified()->ChangeWord32ToBit();
+ } else if (upper->Is(Type::Unsigned32())) {
+ // Tagged -> Uint32 -> Bit
+ node = InsertChangeTaggedToUint32(node);
+ op = simplified()->ChangeWord32ToBit();
+ } else {
+ return TypeError(node, output_type, kRepBit);
+ }
} else {
return TypeError(node, output_type, kRepBit);
}
- return jsgraph()->graph()->NewNode(op, node);
+ return graph()->NewNode(op, node);
}
Node* GetWord64RepresentationFor(Node* node, MachineTypeUnion output_type) {
JSGraph* jsgraph_;
SimplifiedOperatorBuilder* simplified_;
Isolate* isolate_;
+ Type* bit_range_;
friend class RepresentationChangerTester; // accesses the below fields.
}
Node* InsertChangeFloat32ToFloat64(Node* node) {
- return jsgraph()->graph()->NewNode(machine()->ChangeFloat32ToFloat64(),
- node);
+ return graph()->NewNode(machine()->ChangeFloat32ToFloat64(), node);
}
Node* InsertChangeTaggedToFloat64(Node* node) {
- return jsgraph()->graph()->NewNode(simplified()->ChangeTaggedToFloat64(),
- node);
+ return graph()->NewNode(simplified()->ChangeTaggedToFloat64(), node);
+ }
+
+ Node* InsertChangeTaggedToInt32(Node* node) {
+ return graph()->NewNode(simplified()->ChangeTaggedToInt32(), node);
+ }
+
+ Node* InsertChangeTaggedToUint32(Node* node) {
+ return graph()->NewNode(simplified()->ChangeTaggedToUint32(), node);
}
- JSGraph* jsgraph() { return jsgraph_; }
- Isolate* isolate() { return isolate_; }
- SimplifiedOperatorBuilder* simplified() { return simplified_; }
- MachineOperatorBuilder* machine() { return jsgraph()->machine(); }
+ Graph* graph() const { return jsgraph()->graph(); }
+ JSGraph* jsgraph() const { return jsgraph_; }
+ Isolate* isolate() const { return isolate_; }
+ SimplifiedOperatorBuilder* simplified() const { return simplified_; }
+ MachineOperatorBuilder* machine() const { return jsgraph()->machine(); }
};
} // namespace compiler
if (m.IsChangeBitToBool()) return Replace(m.node()->InputAt(0));
break;
}
+ case IrOpcode::kChangeWord32ToBit:
+ return ReduceChangeWord32ToBit(node);
case IrOpcode::kChangeFloat64ToTagged: {
Float64Matcher m(node->InputAt(0));
if (m.HasValue()) return ReplaceNumber(m.Value());
}
+Reduction SimplifiedOperatorReducer::ReduceChangeWord32ToBit(Node* node) {
+ Node* const input = NodeProperties::GetValueInput(node, 0);
+ Type* const input_type = NodeProperties::GetBounds(input).upper;
+ if (input_type->Is(jsgraph()->ZeroOneRangeType())) {
+ // ChangeWord32ToBit(x:bit) => x
+ return Replace(input);
+ }
+ return NoChange();
+}
+
+
Reduction SimplifiedOperatorReducer::Change(Node* node, const Operator* op,
Node* a) {
DCHECK_EQ(node->InputCount(), OperatorProperties::GetTotalInputCount(op));
// Forward declarations.
class Factory;
-class Heap;
namespace compiler {
private:
Reduction ReduceAnyToBoolean(Node* node);
+ Reduction ReduceChangeWord32ToBit(Node* node);
Reduction Change(Node* node, const Operator* op, Node* a);
Reduction ReplaceFloat64(double value);
V(ChangeInt32ToTagged, Operator::kNoProperties, 1) \
V(ChangeUint32ToTagged, Operator::kNoProperties, 1) \
V(ChangeFloat64ToTagged, Operator::kNoProperties, 1) \
- V(ChangeBoolToBit, Operator::kNoProperties, 1) \
V(ChangeBitToBool, Operator::kNoProperties, 1) \
+ V(ChangeBoolToBit, Operator::kNoProperties, 1) \
+ V(ChangeWord32ToBit, Operator::kNoProperties, 1) \
+ V(ChangeWord64ToBit, Operator::kNoProperties, 1) \
V(ObjectIsSmi, Operator::kNoProperties, 1) \
V(ObjectIsNonNegativeSmi, Operator::kNoProperties, 1)
const Operator* ChangeInt32ToTagged();
const Operator* ChangeUint32ToTagged();
const Operator* ChangeFloat64ToTagged();
- const Operator* ChangeBoolToBit();
const Operator* ChangeBitToBool();
+ const Operator* ChangeBoolToBit();
+ const Operator* ChangeWord32ToBit();
+ const Operator* ChangeWord64ToBit();
const Operator* ObjectIsSmi();
const Operator* ObjectIsNonNegativeSmi();
}
+Bounds Typer::Visitor::TypeChangeBitToBool(Node* node) {
+ Bounds arg = Operand(node, 0);
+ // TODO(neis): DCHECK(arg.upper->Is(Type::Boolean()));
+ return Bounds(ChangeRepresentation(arg.lower, Type::TaggedPointer(), zone()),
+ ChangeRepresentation(arg.upper, Type::TaggedPointer(), zone()));
+}
+
+
Bounds Typer::Visitor::TypeChangeBoolToBit(Node* node) {
Bounds arg = Operand(node, 0);
// TODO(neis): DCHECK(arg.upper->Is(Type::Boolean()));
}
-Bounds Typer::Visitor::TypeChangeBitToBool(Node* node) {
- Bounds arg = Operand(node, 0);
- // TODO(neis): DCHECK(arg.upper->Is(Type::Boolean()));
+Bounds Typer::Visitor::TypeChangeWord32ToBit(Node* node) {
+ return Bounds(
+ ChangeRepresentation(Type::Boolean(), Type::UntaggedBit(), zone()));
+}
+
+
+Bounds Typer::Visitor::TypeChangeWord64ToBit(Node* node) {
return Bounds(
- ChangeRepresentation(arg.lower, Type::TaggedPointer(), zone()),
- ChangeRepresentation(arg.upper, Type::TaggedPointer(), zone()));
+ ChangeRepresentation(Type::Boolean(), Type::UntaggedBit(), zone()));
}
// CheckUpperIs(node, to));
break;
}
+ case IrOpcode::kChangeBitToBool: {
+ // Boolean /\ UntaggedInt1 -> Boolean /\ TaggedPtr
+ // TODO(neis): Activate once ChangeRepresentation works in typer.
+ // Type* from = Type::Intersect(Type::Boolean(), Type::UntaggedInt1());
+ // Type* to = Type::Intersect(Type::Boolean(), Type::TaggedPtr());
+ // CheckValueInputIs(node, 0, from));
+ // CheckUpperIs(node, to));
+ break;
+ }
case IrOpcode::kChangeBoolToBit: {
// Boolean /\ TaggedPtr -> Boolean /\ UntaggedInt1
// TODO(neis): Activate once ChangeRepresentation works in typer.
// CheckUpperIs(node, to));
break;
}
- case IrOpcode::kChangeBitToBool: {
- // Boolean /\ UntaggedInt1 -> Boolean /\ TaggedPtr
- // TODO(neis): Activate once ChangeRepresentation works in typer.
- // Type* from = Type::Intersect(Type::Boolean(), Type::UntaggedInt1());
- // Type* to = Type::Intersect(Type::Boolean(), Type::TaggedPtr());
- // CheckValueInputIs(node, 0, from));
- // CheckUpperIs(node, to));
+ case IrOpcode::kChangeWord32ToBit: {
+ // TODO(rossberg): Check.
+ CheckValueInputIs(node, 0, Type::Integral32());
+ break;
+ }
+ case IrOpcode::kChangeWord64ToBit: {
+ // TODO(rossberg): Check.
break;
}
CHECK_EQ(expected, m.Value());
}
- Node* Parameter(int index = 0) {
- return graph()->NewNode(common()->Parameter(index), graph()->start());
+ Node* Parameter(Type* type, int index = 0) {
+ Node* node = graph()->NewNode(common()->Parameter(index), graph()->start());
+ NodeProperties::SetBounds(node, Bounds(type));
+ return node;
}
+ Node* Parameter(int index = 0) { return Parameter(Type::Any(), index); }
+
void CheckTypeError(MachineTypeUnion from, MachineTypeUnion to) {
changer()->testing_type_errors_ = true;
changer()->type_error_ = false;
CHECK_EQ(n, c);
}
};
-}
-}
-} // namespace v8::internal::compiler
+
+} // namespace compiler
+} // namespace internal
+} // namespace v8
static const MachineType all_reps[] = {kRepBit, kRepWord32, kRepWord64,
kRepFloat32, kRepFloat64, kRepTagged};
-TEST(BoolToBit_constant) {
+TEST(ToBit_constant) {
RepresentationChangerTester r;
Node* true_node = r.jsgraph()->TrueConstant();
Node* false_bit =
r.changer()->GetRepresentationFor(false_node, kRepTagged, kRepBit);
r.CheckInt32Constant(false_bit, 0);
+
+ {
+ FOR_FLOAT64_INPUTS(i) {
+ Node* node = r.jsgraph()->Constant(*i);
+ Node* bit = r.changer()->GetRepresentationFor(node, kRepTagged, kRepBit);
+ r.CheckInt32Constant(bit, DoubleToBoolean(*i) ? 1 : 0);
+ }
+ }
+
+ {
+ FOR_INT32_INPUTS(i) {
+ Node* node = r.jsgraph()->Int32Constant(*i);
+ Node* bit = r.changer()->GetRepresentationFor(node, kRepWord32, kRepBit);
+ r.CheckInt32Constant(bit, *i == 0 ? 0 : 1);
+ }
+ }
}
static void CheckChange(IrOpcode::Value expected, MachineTypeUnion from,
- MachineTypeUnion to) {
+ MachineTypeUnion to, Type* from_type = Type::Any()) {
RepresentationChangerTester r;
- Node* n = r.Parameter();
+ Node* n = r.Parameter(from_type);
Node* c = r.changer()->GetRepresentationFor(n, from, to);
CHECK_NE(c, n);
static void CheckTwoChanges(IrOpcode::Value expected2,
IrOpcode::Value expected1, MachineTypeUnion from,
- MachineTypeUnion to) {
+ MachineTypeUnion to,
+ Type* from_type = Type::Any()) {
RepresentationChangerTester r;
- Node* n = r.Parameter();
+ Node* n = r.Parameter(from_type);
Node* c1 = r.changer()->GetRepresentationFor(n, from, to);
CHECK_NE(c1, n);
TEST(SingleChanges) {
- CheckChange(IrOpcode::kChangeBoolToBit, kRepTagged, kRepBit);
+ CheckChange(IrOpcode::kChangeBoolToBit, kRepTagged, kRepBit, Type::Boolean());
+ CheckTwoChanges(IrOpcode::kChangeTaggedToInt32, IrOpcode::kChangeWord32ToBit,
+ kRepTagged, kRepBit, Type::Signed32());
+ CheckTwoChanges(IrOpcode::kChangeTaggedToUint32, IrOpcode::kChangeWord32ToBit,
+ kRepTagged, kRepBit, Type::Unsigned32());
+ CheckChange(IrOpcode::kChangeWord32ToBit, kRepWord8, kRepBit);
+ CheckChange(IrOpcode::kChangeWord32ToBit, kRepWord16, kRepBit);
+ CheckChange(IrOpcode::kChangeWord32ToBit, kRepWord32, kRepBit);
+ CheckChange(IrOpcode::kChangeWord64ToBit, kRepWord64, kRepBit);
CheckChange(IrOpcode::kChangeBitToBool, kRepBit, kRepTagged);
CheckChange(IrOpcode::kChangeInt32ToTagged, kRepWord32 | kTypeInt32,
r.CheckNop(kRepFloat32 | kTypeNumber, kRepFloat32);
r.CheckNop(kRepFloat32, kRepFloat32 | kTypeNumber);
- // 32-bit or 64-bit words can be used as branch conditions (kRepBit).
- r.CheckNop(kRepWord32, kRepBit);
- r.CheckNop(kRepWord32, kRepBit | kTypeBool);
- r.CheckNop(kRepWord64, kRepBit);
- r.CheckNop(kRepWord64, kRepBit | kTypeBool);
-
// 32-bit words can be used as smaller word sizes and vice versa, because
// loads from memory implicitly sign or zero extend the value to the
// full machine word size, and stores implicitly truncate.
})(stdlib, foreign, buffer).foo;
assertFalse(foo(1));
+assertTrue(foo(0));
Node* node = graph()->NewNode(simplified()->ChangeBoolToBit(), val);
Reduction reduction = Reduce(node);
ASSERT_TRUE(reduction.Changed());
-
EXPECT_THAT(reduction.replacement(), IsWordEqual(val, IsTrueConstant()));
}
+TARGET_TEST_P(ChangeLoweringCommonTest, ChangeWord32ToBit) {
+ Node* val = Parameter(0);
+ Node* node = graph()->NewNode(simplified()->ChangeWord32ToBit(), val);
+ Reduction reduction = Reduce(node);
+ ASSERT_TRUE(reduction.Changed());
+ EXPECT_THAT(reduction.replacement(),
+ IsWord32Equal(IsWord32Equal(val, IsInt32Constant(0)),
+ IsInt32Constant(0)));
+}
+
+
+TARGET_TEST_P(ChangeLoweringCommonTest, ChangeWord64ToBit) {
+ Node* val = Parameter(0);
+ Node* node = graph()->NewNode(simplified()->ChangeWord64ToBit(), val);
+ Reduction reduction = Reduce(node);
+ ASSERT_TRUE(reduction.Changed());
+ EXPECT_THAT(reduction.replacement(),
+ IsWord32Equal(IsWord64Equal(val, IsInt64Constant(0)),
+ IsInt32Constant(0)));
+}
+
+
TARGET_TEST_P(ChangeLoweringCommonTest, ChangeFloat64ToTagged) {
Node* val = Parameter(0);
Node* node = graph()->NewNode(simplified()->ChangeFloat64ToTagged(), val);
// -----------------------------------------------------------------------------
+// ChangeWord32ToBit
+
+
+TEST_F(SimplifiedOperatorReducerTest, ChangeWord32ToBitWithBitType) {
+ Handle<Object> zero = factory()->NewNumber(0);
+ Handle<Object> one = factory()->NewNumber(1);
+ Type* const kBitTypes[] = {
+ Type::Constant(zero, zone()), Type::Constant(one, zone()),
+ Type::Range(zero, zero, zone()), Type::Range(one, one, zone()),
+ Type::Range(zero, one, zone())};
+ TRACED_FOREACH(Type*, type, kBitTypes) {
+ Node* param0 = Parameter(type, 0);
+ Reduction reduction =
+ Reduce(graph()->NewNode(simplified()->ChangeWord32ToBit(), param0));
+ ASSERT_TRUE(reduction.Changed());
+ EXPECT_EQ(param0, reduction.replacement());
+ }
+}
+
+
+// -----------------------------------------------------------------------------
// ChangeFloat64ToTagged
PURE(ChangeUint32ToTagged, Operator::kNoProperties, 1),
PURE(ChangeFloat64ToTagged, Operator::kNoProperties, 1),
PURE(ChangeBoolToBit, Operator::kNoProperties, 1),
+ PURE(ChangeWord32ToBit, Operator::kNoProperties, 1),
+ PURE(ChangeWord64ToBit, Operator::kNoProperties, 1),
PURE(ChangeBitToBool, Operator::kNoProperties, 1),
PURE(ObjectIsSmi, Operator::kNoProperties, 1),
PURE(ObjectIsNonNegativeSmi, Operator::kNoProperties, 1)