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"
|
2015-01-29 09:17:45 +00:00
|
|
|
#include "src/compiler/node-properties.h"
|
2014-09-23 11:26:49 +00:00
|
|
|
#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"
|
|
|
|
|
2015-01-05 12:29:04 +00:00
|
|
|
using testing::BitEq;
|
2014-09-23 11:40:00 +00:00
|
|
|
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);
|
2015-01-23 15:19:34 +00:00
|
|
|
JSGraph jsgraph(isolate(), graph(), common(), javascript(), &machine);
|
2014-09-23 11:26:49 +00:00
|
|
|
JSBuiltinReducer reducer(&jsgraph);
|
|
|
|
return reducer.Reduce(node);
|
|
|
|
}
|
|
|
|
|
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[] = {
|
2015-01-28 08:42:15 +00:00
|
|
|
Type::UnsignedSmall(), Type::Negative32(), Type::Unsigned31(),
|
|
|
|
Type::SignedSmall(), Type::Signed32(), Type::Unsigned32(),
|
|
|
|
Type::Integral32(), Type::MinusZero(), Type::NaN(),
|
|
|
|
Type::OrderedNumber(), Type::PlainNumber(), Type::Number()};
|
2014-09-23 11:26:49 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
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-12-20 13:17:20 +00:00
|
|
|
EXPECT_THAT(r.replacement(), IsNumberConstant(-V8_INFINITY));
|
2014-09-23 11:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-23 11:26:49 +00:00
|
|
|
} // namespace compiler
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|