Commit 5a0ff8e6 authored by neis@chromium.org's avatar neis@chromium.org

Refine expression typing, esp. by propagating range information.

R=rossberg@chromium.org
BUG=

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

git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@24552 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent c0504eef
......@@ -383,16 +383,6 @@ Reduction JSTypedLowering::ReduceJSStrictEqual(Node* node, bool invert) {
: jsgraph()->TrueConstant());
}
}
/* TODO(neis): This is currently unsound.
if (!r.left_type()->Maybe(r.right_type())) {
// Type intersection is empty; === is always false unless both
// inputs could be strings (one internalized and one not).
if (r.OneInputCannotBe(Type::String())) {
return ReplaceEagerly(node, invert ? jsgraph()->TrueConstant()
: jsgraph()->FalseConstant());
}
}
*/
if (r.OneInputIs(Type::Undefined())) {
return r.ChangeToPureOperator(
simplified()->ReferenceEqual(Type::Undefined()), invert);
......
This diff is collapsed.
......@@ -36,6 +36,17 @@ class Typer {
class WidenVisitor;
Zone* zone_;
Type* negative_signed32;
Type* non_negative_signed32;
Type* undefined_or_null;
Type* singleton_false;
Type* singleton_true;
Type* singleton_zero;
Type* singleton_one;
Type* zero_or_one;
Type* zeroish;
Type* falsish;
Type* integer;
Type* number_fun0_;
Type* number_fun1_;
Type* number_fun2_;
......
......@@ -86,6 +86,7 @@
'compiler/test-schedule.cc',
'compiler/test-scheduler.cc',
'compiler/test-simplified-lowering.cc',
'compiler/test-typer.cc',
'cctest.cc',
'gay-fixed.cc',
'gay-precision.cc',
......
......@@ -227,7 +227,7 @@ TEST(NumberTypes) {
FOR_FLOAT64_INPUTS(i) {
double value = *i;
Node* node = T.Constant(value);
CHECK(T.upper(node)->Equals(Type::Of(value, T.main_zone())));
CHECK(T.upper(node)->Is(Type::Of(value, T.main_zone())));
}
}
......
......@@ -262,16 +262,15 @@ TEST(NumberBinops) {
static void CheckToI32(Node* old_input, Node* new_input, bool is_signed) {
Type* old_type = NodeProperties::GetBounds(old_input).upper;
Type* new_type = NodeProperties::GetBounds(new_input).upper;
Type* expected_type = I32Type(is_signed);
CHECK(new_type->Is(expected_type));
if (old_type->Is(expected_type)) {
CHECK_EQ(old_input, new_input);
} else if (new_input->opcode() == IrOpcode::kNumberConstant) {
CHECK(NodeProperties::GetBounds(new_input).upper->Is(expected_type));
double v = OpParameter<double>(new_input);
double e = static_cast<double>(is_signed ? FastD2I(v) : FastD2UI(v));
CHECK_EQ(e, v);
} else {
CHECK_EQ(NumberToI32(is_signed), new_input->opcode());
}
}
......
// 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.
// This tests the correctness of the typer.
//
// For simplicity, it currently only tests it on expression operators that have
// a direct equivalent in C++. Also, testing is currently limited to ranges as
// input types.
#include <functional>
#include "src/compiler/node-properties-inl.h"
#include "src/compiler/typer.h"
#include "test/cctest/cctest.h"
#include "test/cctest/compiler/graph-builder-tester.h"
using namespace v8::internal;
using namespace v8::internal::compiler;
class TyperTester : public HandleAndZoneScope, public GraphAndBuilders {
public:
TyperTester()
: GraphAndBuilders(main_zone()),
typer_(main_zone()),
javascript_(main_zone()) {
Node* s = graph()->NewNode(common()->Start(3));
graph()->SetStart(s);
context_node_ = graph()->NewNode(common()->Parameter(2), graph()->start());
rng_ = isolate()->random_number_generator();
integers.push_back(0);
integers.push_back(0);
integers.push_back(-1);
integers.push_back(+1);
integers.push_back(-V8_INFINITY);
integers.push_back(+V8_INFINITY);
for (int i = 0; i < 5; ++i) {
double x = rng_->NextInt();
integers.push_back(x);
x *= rng_->NextInt();
if (!IsMinusZero(x)) integers.push_back(x);
}
int32s.push_back(0);
int32s.push_back(0);
int32s.push_back(-1);
int32s.push_back(+1);
int32s.push_back(kMinInt);
int32s.push_back(kMaxInt);
for (int i = 0; i < 10; ++i) {
int32s.push_back(rng_->NextInt());
}
}
Typer typer_;
JSOperatorBuilder javascript_;
Node* context_node_;
v8::base::RandomNumberGenerator* rng_;
std::vector<double> integers;
std::vector<double> int32s;
Isolate* isolate() { return main_isolate(); }
Graph* graph() { return main_graph_; }
CommonOperatorBuilder* common() { return &main_common_; }
Node* Parameter(int index = 0) {
return graph()->NewNode(common()->Parameter(index), graph()->start());
}
Type* TypeBinaryOp(const Operator* op, Type* lhs, Type* rhs) {
Node* p0 = Parameter(0);
Node* p1 = Parameter(1);
NodeProperties::SetBounds(p0, Bounds(lhs));
NodeProperties::SetBounds(p1, Bounds(rhs));
Node* n = graph()->NewNode(
op, p0, p1, context_node_, graph()->start(), graph()->start());
typer_.Init(n);
return NodeProperties::GetBounds(n).upper;
}
Type* RandomRange(bool int32 = false) {
std::vector<double>& numbers = int32 ? int32s : integers;
Factory* f = isolate()->factory();
int i = rng_->NextInt(static_cast<int>(numbers.size()));
int j = rng_->NextInt(static_cast<int>(numbers.size()));
i::Handle<i::Object> min = f->NewNumber(numbers[i]);
i::Handle<i::Object> max = f->NewNumber(numbers[j]);
if (min->Number() > max->Number()) std::swap(min, max);
return Type::Range(min, max, main_zone());
}
double RandomInt(double min, double max) {
switch (rng_->NextInt(4)) {
case 0: return min;
case 1: return max;
default: break;
}
if (min == +V8_INFINITY) return +V8_INFINITY;
if (max == -V8_INFINITY) return -V8_INFINITY;
if (min == -V8_INFINITY && max == +V8_INFINITY) {
return rng_->NextInt() * static_cast<double>(rng_->NextInt());
}
double result = nearbyint(min + (max - min) * rng_->NextDouble());
if (IsMinusZero(result)) return 0;
if (std::isnan(result)) return rng_->NextInt(2) ? min : max;
DCHECK(min <= result && result <= max);
return result;
}
double RandomInt(Type::RangeType* range) {
return RandomInt(range->Min()->Number(), range->Max()->Number());
}
template <class BinaryFunction>
void TestBinaryArithOp(const Operator* op, BinaryFunction opfun) {
for (int i = 0; i < 100; ++i) {
Type::RangeType* r1 = RandomRange()->AsRange();
Type::RangeType* r2 = RandomRange()->AsRange();
Type* expected_type = TypeBinaryOp(op, r1, r2);
double x1 = RandomInt(r1);
double x2 = RandomInt(r2);
double result_value = opfun(x1, x2);
Type* result_type = Type::Constant(
isolate()->factory()->NewNumber(result_value), main_zone());
CHECK(result_type->Is(expected_type));
}
}
template <class BinaryFunction>
void TestBinaryCompareOp(const Operator* op, BinaryFunction opfun) {
for (int i = 0; i < 100; ++i) {
Type::RangeType* r1 = RandomRange()->AsRange();
Type::RangeType* r2 = RandomRange()->AsRange();
Type* expected_type = TypeBinaryOp(op, r1, r2);
double x1 = RandomInt(r1);
double x2 = RandomInt(r2);
bool result_value = opfun(x1, x2);
Type* result_type = Type::Constant(result_value ?
isolate()->factory()->true_value() :
isolate()->factory()->false_value(), main_zone());
CHECK(result_type->Is(expected_type));
}
}
template <class BinaryFunction>
void TestBinaryBitOp(const Operator* op, BinaryFunction opfun) {
for (int i = 0; i < 100; ++i) {
Type::RangeType* r1 = RandomRange(true)->AsRange();
Type::RangeType* r2 = RandomRange(true)->AsRange();
Type* expected_type = TypeBinaryOp(op, r1, r2);
int32_t x1 = RandomInt(r1);
int32_t x2 = RandomInt(r2);
double result_value = opfun(x1, x2);
Type* result_type = Type::Constant(
isolate()->factory()->NewNumber(result_value), main_zone());
CHECK(result_type->Is(expected_type));
}
}
};
static int32_t shift_left(int32_t x, int32_t y) { return x << y; }
static int32_t shift_right(int32_t x, int32_t y) { return x >> y; }
TEST(TypeJSAdd) {
TyperTester t;
t.TestBinaryArithOp(t.javascript_.Subtract(), std::plus<double>());
}
TEST(TypeJSSubtract) {
TyperTester t;
t.TestBinaryArithOp(t.javascript_.Subtract(), std::minus<double>());
}
TEST(TypeJSMultiply) {
TyperTester t;
t.TestBinaryArithOp(t.javascript_.Multiply(), std::multiplies<double>());
}
TEST(TypeJSDivide) {
TyperTester t;
t.TestBinaryArithOp(t.javascript_.Divide(), std::divides<double>());
}
TEST(TypeJSBitwiseOr) {
TyperTester t;
t.TestBinaryBitOp(t.javascript_.BitwiseOr(), std::bit_or<int32_t>());
}
TEST(TypeJSBitwiseAnd) {
TyperTester t;
t.TestBinaryBitOp(t.javascript_.BitwiseAnd(), std::bit_and<int32_t>());
}
TEST(TypeJSBitwiseXor) {
TyperTester t;
t.TestBinaryBitOp(t.javascript_.BitwiseXor(), std::bit_xor<int32_t>());
}
TEST(TypeJSShiftLeft) {
TyperTester t;
t.TestBinaryBitOp(t.javascript_.ShiftLeft(), shift_left);
}
TEST(TypeJSShiftRight) {
TyperTester t;
t.TestBinaryBitOp(t.javascript_.ShiftRight(), shift_right);
}
TEST(TypeJSLessThan) {
TyperTester t;
t.TestBinaryCompareOp(t.javascript_.LessThan(), std::less<double>());
}
TEST(TypeJSLessThanOrEqual) {
TyperTester t;
t.TestBinaryCompareOp(
t.javascript_.LessThanOrEqual(), std::less_equal<double>());
}
TEST(TypeJSGreaterThan) {
TyperTester t;
t.TestBinaryCompareOp(t.javascript_.GreaterThan(), std::greater<double>());
}
TEST(TypeJSGreaterThanOrEqual) {
TyperTester t;
t.TestBinaryCompareOp(
t.javascript_.GreaterThanOrEqual(), std::greater_equal<double>());
}
TEST(TypeJSEqual) {
TyperTester t;
t.TestBinaryCompareOp(t.javascript_.Equal(), std::equal_to<double>());
}
TEST(TypeJSNotEqual) {
TyperTester t;
t.TestBinaryCompareOp(t.javascript_.NotEqual(), std::not_equal_to<double>());
}
// For numbers there's no difference between strict and non-strict equality.
TEST(TypeJSStrictEqual) {
TyperTester t;
t.TestBinaryCompareOp(t.javascript_.StrictEqual(), std::equal_to<double>());
}
TEST(TypeJSStrictNotEqual) {
TyperTester t;
t.TestBinaryCompareOp(
t.javascript_.StrictNotEqual(), std::not_equal_to<double>());
}
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