2014-09-23 11:26:49 +00:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
#include "src/compiler/js-builtin-reducer.h"
|
|
|
|
#include "src/compiler/js-graph.h"
|
|
|
|
#include "src/compiler/node-properties-inl.h"
|
|
|
|
#include "src/compiler/typer.h"
|
2014-10-20 11:26:23 +00:00
|
|
|
#include "test/unittests/compiler/graph-unittest.h"
|
|
|
|
#include "test/unittests/compiler/node-test-utils.h"
|
2014-09-23 11:40:00 +00:00
|
|
|
#include "testing/gmock-support.h"
|
|
|
|
|
|
|
|
using testing::Capture;
|
2014-09-23 11:26:49 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace compiler {
|
|
|
|
|
2014-10-15 11:38:04 +00:00
|
|
|
class JSBuiltinReducerTest : public TypedGraphTest {
|
2014-09-23 11:26:49 +00:00
|
|
|
public:
|
|
|
|
JSBuiltinReducerTest() : javascript_(zone()) {}
|
|
|
|
|
|
|
|
protected:
|
Add floor, ceil, round (truncate) instructions for ia32, x64 (if SSE4.1) and
add floor, ceil, round (truncate and away from zero) for arm64.
R=bmeurer@chromium.org, dcarney@chromium.org, mstarzinger@chromium.org, rodolph.perfetta@arm.com
TEST=test/mjsunit/asm/math-floor.js,test/mjsunit/asm/math-ceil.js,test/unittest/compiler/js-builtin-reducer-unittest.cc
Review URL: https://codereview.chromium.org/677433002
Cr-Commit-Position: refs/heads/master@{#25018}
git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@25018 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2014-10-30 14:15:20 +00:00
|
|
|
Reduction Reduce(Node* node, MachineOperatorBuilder::Flags flags =
|
|
|
|
MachineOperatorBuilder::Flag::kNoFlags) {
|
2014-11-10 14:28:09 +00:00
|
|
|
MachineOperatorBuilder machine(zone(), kMachPtr, flags);
|
2014-10-15 11:38:04 +00:00
|
|
|
JSGraph jsgraph(graph(), common(), javascript(), &machine);
|
2014-09-23 11:26:49 +00:00
|
|
|
JSBuiltinReducer reducer(&jsgraph);
|
|
|
|
return reducer.Reduce(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* Parameter(Type* t, int32_t index = 0) {
|
|
|
|
Node* n = graph()->NewNode(common()->Parameter(index), graph()->start());
|
|
|
|
NodeProperties::SetBounds(n, Bounds(Type::None(), t));
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2014-10-08 14:42:31 +00:00
|
|
|
Handle<JSFunction> MathFunction(const char* name) {
|
|
|
|
Handle<Object> m =
|
|
|
|
JSObject::GetProperty(isolate()->global_object(),
|
|
|
|
isolate()->factory()->NewStringFromAsciiChecked(
|
|
|
|
"Math")).ToHandleChecked();
|
|
|
|
Handle<JSFunction> f = Handle<JSFunction>::cast(
|
|
|
|
JSObject::GetProperty(
|
|
|
|
m, isolate()->factory()->NewStringFromAsciiChecked(name))
|
|
|
|
.ToHandleChecked());
|
|
|
|
return f;
|
|
|
|
}
|
|
|
|
|
2014-09-23 11:26:49 +00:00
|
|
|
JSOperatorBuilder* javascript() { return &javascript_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
JSOperatorBuilder javascript_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// TODO(mstarzinger): Find a common place and unify with test-js-typed-lowering.
|
|
|
|
Type* const kNumberTypes[] = {
|
|
|
|
Type::UnsignedSmall(), Type::OtherSignedSmall(), Type::OtherUnsigned31(),
|
|
|
|
Type::OtherUnsigned32(), Type::OtherSigned32(), Type::SignedSmall(),
|
|
|
|
Type::Signed32(), Type::Unsigned32(), Type::Integral32(),
|
|
|
|
Type::MinusZero(), Type::NaN(), Type::OtherNumber(),
|
|
|
|
Type::OrderedNumber(), Type::Number()};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
2014-09-26 14:06:56 +00:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Math.abs
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(JSBuiltinReducerTest, MathAbs) {
|
2014-10-08 14:42:31 +00:00
|
|
|
Handle<JSFunction> f = MathFunction("abs");
|
2014-09-26 14:06:56 +00:00
|
|
|
|
|
|
|
TRACED_FOREACH(Type*, t0, kNumberTypes) {
|
|
|
|
Node* p0 = Parameter(t0, 0);
|
|
|
|
Node* fun = HeapConstant(Unique<HeapObject>::CreateUninitialized(f));
|
2014-09-30 10:42:44 +00:00
|
|
|
Node* call =
|
|
|
|
graph()->NewNode(javascript()->CallFunction(3, NO_CALL_FUNCTION_FLAGS),
|
|
|
|
fun, UndefinedConstant(), p0);
|
2014-09-26 14:06:56 +00:00
|
|
|
Reduction r = Reduce(call);
|
|
|
|
|
|
|
|
if (t0->Is(Type::Unsigned32())) {
|
|
|
|
ASSERT_TRUE(r.Changed());
|
|
|
|
EXPECT_THAT(r.replacement(), p0);
|
|
|
|
} else {
|
|
|
|
Capture<Node*> branch;
|
|
|
|
ASSERT_TRUE(r.Changed());
|
2014-11-06 09:09:50 +00:00
|
|
|
EXPECT_THAT(r.replacement(),
|
|
|
|
IsSelect(kMachNone, IsNumberLessThan(IsNumberConstant(0), p0),
|
|
|
|
p0, IsNumberSubtract(IsNumberConstant(0), p0)));
|
2014-09-26 14:06:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-24 10:24:19 +00:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Math.sqrt
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(JSBuiltinReducerTest, MathSqrt) {
|
2014-10-08 14:42:31 +00:00
|
|
|
Handle<JSFunction> f = MathFunction("sqrt");
|
2014-09-24 10:24:19 +00:00
|
|
|
|
|
|
|
TRACED_FOREACH(Type*, t0, kNumberTypes) {
|
|
|
|
Node* p0 = Parameter(t0, 0);
|
|
|
|
Node* fun = HeapConstant(Unique<HeapObject>::CreateUninitialized(f));
|
2014-09-30 10:42:44 +00:00
|
|
|
Node* call =
|
|
|
|
graph()->NewNode(javascript()->CallFunction(3, NO_CALL_FUNCTION_FLAGS),
|
|
|
|
fun, UndefinedConstant(), p0);
|
2014-09-24 10:24:19 +00:00
|
|
|
Reduction r = Reduce(call);
|
|
|
|
|
|
|
|
ASSERT_TRUE(r.Changed());
|
|
|
|
EXPECT_THAT(r.replacement(), IsFloat64Sqrt(p0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-23 11:40:00 +00:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Math.max
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(JSBuiltinReducerTest, MathMax0) {
|
2014-10-08 14:42:31 +00:00
|
|
|
Handle<JSFunction> f = MathFunction("max");
|
2014-09-23 11:40:00 +00:00
|
|
|
|
|
|
|
Node* fun = HeapConstant(Unique<HeapObject>::CreateUninitialized(f));
|
2014-09-30 10:42:44 +00:00
|
|
|
Node* call =
|
|
|
|
graph()->NewNode(javascript()->CallFunction(2, NO_CALL_FUNCTION_FLAGS),
|
|
|
|
fun, UndefinedConstant());
|
2014-09-23 11:40:00 +00:00
|
|
|
Reduction r = Reduce(call);
|
|
|
|
|
2014-09-24 10:24:19 +00:00
|
|
|
ASSERT_TRUE(r.Changed());
|
2014-09-23 11:40:00 +00:00
|
|
|
EXPECT_THAT(r.replacement(), IsNumberConstant(-V8_INFINITY));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(JSBuiltinReducerTest, MathMax1) {
|
2014-10-08 14:42:31 +00:00
|
|
|
Handle<JSFunction> f = MathFunction("max");
|
2014-09-23 11:40:00 +00:00
|
|
|
|
|
|
|
TRACED_FOREACH(Type*, t0, kNumberTypes) {
|
|
|
|
Node* p0 = Parameter(t0, 0);
|
|
|
|
Node* fun = HeapConstant(Unique<HeapObject>::CreateUninitialized(f));
|
2014-09-30 10:42:44 +00:00
|
|
|
Node* call =
|
|
|
|
graph()->NewNode(javascript()->CallFunction(3, NO_CALL_FUNCTION_FLAGS),
|
|
|
|
fun, UndefinedConstant(), p0);
|
2014-09-23 11:40:00 +00:00
|
|
|
Reduction r = Reduce(call);
|
|
|
|
|
2014-09-24 10:24:19 +00:00
|
|
|
ASSERT_TRUE(r.Changed());
|
2014-09-23 11:40:00 +00:00
|
|
|
EXPECT_THAT(r.replacement(), p0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(JSBuiltinReducerTest, MathMax2) {
|
2014-10-08 14:42:31 +00:00
|
|
|
Handle<JSFunction> f = MathFunction("max");
|
2014-09-23 11:40:00 +00:00
|
|
|
|
|
|
|
TRACED_FOREACH(Type*, t0, kNumberTypes) {
|
|
|
|
TRACED_FOREACH(Type*, t1, kNumberTypes) {
|
|
|
|
Node* p0 = Parameter(t0, 0);
|
|
|
|
Node* p1 = Parameter(t1, 1);
|
|
|
|
Node* fun = HeapConstant(Unique<HeapObject>::CreateUninitialized(f));
|
2014-09-30 10:42:44 +00:00
|
|
|
Node* call = graph()->NewNode(
|
|
|
|
javascript()->CallFunction(4, NO_CALL_FUNCTION_FLAGS), fun,
|
|
|
|
UndefinedConstant(), p0, p1);
|
2014-09-23 11:40:00 +00:00
|
|
|
Reduction r = Reduce(call);
|
|
|
|
|
|
|
|
if (t0->Is(Type::Integral32()) && t1->Is(Type::Integral32())) {
|
2014-09-24 10:24:19 +00:00
|
|
|
ASSERT_TRUE(r.Changed());
|
2014-11-06 09:09:50 +00:00
|
|
|
EXPECT_THAT(r.replacement(),
|
|
|
|
IsSelect(kMachNone, IsNumberLessThan(p1, p0), p1, p0));
|
2014-09-23 11:40:00 +00:00
|
|
|
} else {
|
2014-09-24 10:24:19 +00:00
|
|
|
ASSERT_FALSE(r.Changed());
|
2014-09-23 11:40:00 +00:00
|
|
|
EXPECT_EQ(IrOpcode::kJSCallFunction, call->opcode());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-23 11:26:49 +00:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Math.imul
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(JSBuiltinReducerTest, MathImul) {
|
2014-10-08 14:42:31 +00:00
|
|
|
Handle<JSFunction> f = MathFunction("imul");
|
2014-09-23 11:26:49 +00:00
|
|
|
|
|
|
|
TRACED_FOREACH(Type*, t0, kNumberTypes) {
|
|
|
|
TRACED_FOREACH(Type*, t1, kNumberTypes) {
|
|
|
|
Node* p0 = Parameter(t0, 0);
|
|
|
|
Node* p1 = Parameter(t1, 1);
|
|
|
|
Node* fun = HeapConstant(Unique<HeapObject>::CreateUninitialized(f));
|
2014-09-30 10:42:44 +00:00
|
|
|
Node* call = graph()->NewNode(
|
|
|
|
javascript()->CallFunction(4, NO_CALL_FUNCTION_FLAGS), fun,
|
|
|
|
UndefinedConstant(), p0, p1);
|
2014-09-23 11:26:49 +00:00
|
|
|
Reduction r = Reduce(call);
|
|
|
|
|
|
|
|
if (t0->Is(Type::Integral32()) && t1->Is(Type::Integral32())) {
|
2014-09-24 10:24:19 +00:00
|
|
|
ASSERT_TRUE(r.Changed());
|
2014-09-23 11:26:49 +00:00
|
|
|
EXPECT_THAT(r.replacement(), IsInt32Mul(p0, p1));
|
|
|
|
} else {
|
2014-09-24 10:24:19 +00:00
|
|
|
ASSERT_FALSE(r.Changed());
|
2014-09-23 11:26:49 +00:00
|
|
|
EXPECT_EQ(IrOpcode::kJSCallFunction, call->opcode());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-24 14:55:13 +00:00
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Math.fround
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(JSBuiltinReducerTest, MathFround) {
|
2014-10-08 14:42:31 +00:00
|
|
|
Handle<JSFunction> f = MathFunction("fround");
|
2014-09-24 14:55:13 +00:00
|
|
|
|
|
|
|
TRACED_FOREACH(Type*, t0, kNumberTypes) {
|
|
|
|
Node* p0 = Parameter(t0, 0);
|
|
|
|
Node* fun = HeapConstant(Unique<HeapObject>::CreateUninitialized(f));
|
2014-09-30 10:42:44 +00:00
|
|
|
Node* call =
|
|
|
|
graph()->NewNode(javascript()->CallFunction(3, NO_CALL_FUNCTION_FLAGS),
|
|
|
|
fun, UndefinedConstant(), p0);
|
2014-09-24 14:55:13 +00:00
|
|
|
Reduction r = Reduce(call);
|
|
|
|
|
|
|
|
ASSERT_TRUE(r.Changed());
|
|
|
|
EXPECT_THAT(r.replacement(), IsTruncateFloat64ToFloat32(p0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Add floor, ceil, round (truncate) instructions for ia32, x64 (if SSE4.1) and
add floor, ceil, round (truncate and away from zero) for arm64.
R=bmeurer@chromium.org, dcarney@chromium.org, mstarzinger@chromium.org, rodolph.perfetta@arm.com
TEST=test/mjsunit/asm/math-floor.js,test/mjsunit/asm/math-ceil.js,test/unittest/compiler/js-builtin-reducer-unittest.cc
Review URL: https://codereview.chromium.org/677433002
Cr-Commit-Position: refs/heads/master@{#25018}
git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@25018 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
2014-10-30 14:15:20 +00:00
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Math.floor
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(JSBuiltinReducerTest, MathFloorAvailable) {
|
|
|
|
Handle<JSFunction> f = MathFunction("floor");
|
|
|
|
|
|
|
|
TRACED_FOREACH(Type*, t0, kNumberTypes) {
|
|
|
|
Node* p0 = Parameter(t0, 0);
|
|
|
|
Node* fun = HeapConstant(Unique<HeapObject>::CreateUninitialized(f));
|
|
|
|
Node* call =
|
|
|
|
graph()->NewNode(javascript()->CallFunction(3, NO_CALL_FUNCTION_FLAGS),
|
|
|
|
fun, UndefinedConstant(), p0);
|
|
|
|
Reduction r = Reduce(call, MachineOperatorBuilder::Flag::kFloat64Floor);
|
|
|
|
|
|
|
|
ASSERT_TRUE(r.Changed());
|
|
|
|
EXPECT_THAT(r.replacement(), IsFloat64Floor(p0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(JSBuiltinReducerTest, MathFloorUnavailable) {
|
|
|
|
Handle<JSFunction> f = MathFunction("floor");
|
|
|
|
|
|
|
|
TRACED_FOREACH(Type*, t0, kNumberTypes) {
|
|
|
|
Node* p0 = Parameter(t0, 0);
|
|
|
|
Node* fun = HeapConstant(Unique<HeapObject>::CreateUninitialized(f));
|
|
|
|
Node* call =
|
|
|
|
graph()->NewNode(javascript()->CallFunction(3, NO_CALL_FUNCTION_FLAGS),
|
|
|
|
fun, UndefinedConstant(), p0);
|
|
|
|
Reduction r = Reduce(call, MachineOperatorBuilder::Flag::kNoFlags);
|
|
|
|
|
|
|
|
ASSERT_FALSE(r.Changed());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Math.ceil
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(JSBuiltinReducerTest, MathCeilAvailable) {
|
|
|
|
Handle<JSFunction> f = MathFunction("ceil");
|
|
|
|
|
|
|
|
TRACED_FOREACH(Type*, t0, kNumberTypes) {
|
|
|
|
Node* p0 = Parameter(t0, 0);
|
|
|
|
Node* fun = HeapConstant(Unique<HeapObject>::CreateUninitialized(f));
|
|
|
|
Node* call =
|
|
|
|
graph()->NewNode(javascript()->CallFunction(3, NO_CALL_FUNCTION_FLAGS),
|
|
|
|
fun, UndefinedConstant(), p0);
|
|
|
|
Reduction r = Reduce(call, MachineOperatorBuilder::Flag::kFloat64Ceil);
|
|
|
|
|
|
|
|
ASSERT_TRUE(r.Changed());
|
|
|
|
EXPECT_THAT(r.replacement(), IsFloat64Ceil(p0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(JSBuiltinReducerTest, MathCeilUnavailable) {
|
|
|
|
Handle<JSFunction> f = MathFunction("ceil");
|
|
|
|
|
|
|
|
TRACED_FOREACH(Type*, t0, kNumberTypes) {
|
|
|
|
Node* p0 = Parameter(t0, 0);
|
|
|
|
Node* fun = HeapConstant(Unique<HeapObject>::CreateUninitialized(f));
|
|
|
|
Node* call =
|
|
|
|
graph()->NewNode(javascript()->CallFunction(3, NO_CALL_FUNCTION_FLAGS),
|
|
|
|
fun, UndefinedConstant(), p0);
|
|
|
|
Reduction r = Reduce(call, MachineOperatorBuilder::Flag::kNoFlags);
|
|
|
|
|
|
|
|
ASSERT_FALSE(r.Changed());
|
|
|
|
}
|
|
|
|
}
|
2014-09-23 11:26:49 +00:00
|
|
|
} // namespace compiler
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|