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);
|
|
|
|
}
|
|
|
|
|
2015-04-27 10:44:47 +00:00
|
|
|
Node* MathFunction(const char* name) {
|
2014-10-08 14:42:31 +00:00
|
|
|
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());
|
2015-04-27 10:44:47 +00:00
|
|
|
return HeapConstant(Unique<JSFunction>::CreateUninitialized(f));
|
2014-10-08 14:42:31 +00:00
|
|
|
}
|
|
|
|
|
2014-09-23 11:26:49 +00:00
|
|
|
JSOperatorBuilder* javascript() { return &javascript_; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
JSOperatorBuilder javascript_;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2015-04-27 10:44:47 +00:00
|
|
|
Type* const kIntegral32Types[] = {Type::UnsignedSmall(), Type::Negative32(),
|
|
|
|
Type::Unsigned31(), Type::SignedSmall(),
|
|
|
|
Type::Signed32(), Type::Unsigned32(),
|
|
|
|
Type::Integral32()};
|
|
|
|
|
|
|
|
|
|
|
|
const LanguageMode kLanguageModes[] = {SLOPPY, STRICT, STRONG};
|
|
|
|
|
|
|
|
|
2014-09-23 11:26:49 +00:00
|
|
|
// 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) {
|
2015-04-27 10:44:47 +00:00
|
|
|
Node* function = MathFunction("max");
|
2014-09-23 11:40:00 +00:00
|
|
|
|
2015-04-27 10:44:47 +00:00
|
|
|
TRACED_FOREACH(LanguageMode, language_mode, kLanguageModes) {
|
|
|
|
Node* call = graph()->NewNode(
|
|
|
|
javascript()->CallFunction(2, NO_CALL_FUNCTION_FLAGS, language_mode),
|
|
|
|
function, 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());
|
2015-04-27 10:44:47 +00:00
|
|
|
EXPECT_THAT(r.replacement(), IsNumberConstant(-V8_INFINITY));
|
2014-09-23 11:40:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-27 10:44:47 +00:00
|
|
|
TEST_F(JSBuiltinReducerTest, MathMax1) {
|
|
|
|
Node* function = MathFunction("max");
|
2014-09-23 11:40:00 +00:00
|
|
|
|
2015-04-27 10:44:47 +00:00
|
|
|
TRACED_FOREACH(LanguageMode, language_mode, kLanguageModes) {
|
|
|
|
TRACED_FOREACH(Type*, t0, kNumberTypes) {
|
2014-09-23 11:40:00 +00:00
|
|
|
Node* p0 = Parameter(t0, 0);
|
2014-09-30 10:42:44 +00:00
|
|
|
Node* call = graph()->NewNode(
|
2015-04-27 10:44:47 +00:00
|
|
|
javascript()->CallFunction(3, NO_CALL_FUNCTION_FLAGS, language_mode),
|
|
|
|
function, UndefinedConstant(), p0);
|
2014-09-23 11:40:00 +00:00
|
|
|
Reduction r = Reduce(call);
|
|
|
|
|
2015-04-27 10:44:47 +00:00
|
|
|
ASSERT_TRUE(r.Changed());
|
|
|
|
EXPECT_THAT(r.replacement(), p0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(JSBuiltinReducerTest, MathMax2) {
|
|
|
|
Node* function = MathFunction("max");
|
|
|
|
|
|
|
|
TRACED_FOREACH(LanguageMode, language_mode, kLanguageModes) {
|
|
|
|
TRACED_FOREACH(Type*, t0, kIntegral32Types) {
|
|
|
|
TRACED_FOREACH(Type*, t1, kIntegral32Types) {
|
|
|
|
Node* p0 = Parameter(t0, 0);
|
|
|
|
Node* p1 = Parameter(t1, 1);
|
|
|
|
Node* call =
|
|
|
|
graph()->NewNode(javascript()->CallFunction(
|
|
|
|
4, NO_CALL_FUNCTION_FLAGS, language_mode),
|
|
|
|
function, UndefinedConstant(), p0, p1);
|
|
|
|
Reduction r = Reduce(call);
|
|
|
|
|
2014-09-24 10:24:19 +00:00
|
|
|
ASSERT_TRUE(r.Changed());
|
2014-11-06 09:09:50 +00:00
|
|
|
EXPECT_THAT(r.replacement(),
|
2015-03-20 12:05:06 +00:00
|
|
|
IsSelect(kMachNone, IsNumberLessThan(p1, p0), p0, p1));
|
2014-09-23 11:40:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-09-23 11:26:49 +00:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Math.imul
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(JSBuiltinReducerTest, MathImul) {
|
2015-04-27 10:44:47 +00:00
|
|
|
Node* function = MathFunction("imul");
|
|
|
|
|
|
|
|
TRACED_FOREACH(LanguageMode, language_mode, kLanguageModes) {
|
|
|
|
TRACED_FOREACH(Type*, t0, kIntegral32Types) {
|
|
|
|
TRACED_FOREACH(Type*, t1, kIntegral32Types) {
|
|
|
|
Node* p0 = Parameter(t0, 0);
|
|
|
|
Node* p1 = Parameter(t1, 1);
|
|
|
|
Node* call =
|
|
|
|
graph()->NewNode(javascript()->CallFunction(
|
|
|
|
4, NO_CALL_FUNCTION_FLAGS, language_mode),
|
|
|
|
function, UndefinedConstant(), p0, p1);
|
|
|
|
Reduction r = Reduce(call);
|
2014-09-23 11:26:49 +00:00
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-24 14:55:13 +00:00
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Math.fround
|
|
|
|
|
|
|
|
|
|
|
|
TEST_F(JSBuiltinReducerTest, MathFround) {
|
2015-04-27 10:44:47 +00:00
|
|
|
Node* function = MathFunction("fround");
|
2014-09-24 14:55:13 +00:00
|
|
|
|
2015-04-27 10:44:47 +00:00
|
|
|
TRACED_FOREACH(LanguageMode, language_mode, kLanguageModes) {
|
|
|
|
TRACED_FOREACH(Type*, t0, kNumberTypes) {
|
|
|
|
Node* p0 = Parameter(t0, 0);
|
|
|
|
Node* call = graph()->NewNode(
|
|
|
|
javascript()->CallFunction(3, NO_CALL_FUNCTION_FLAGS, language_mode),
|
|
|
|
function, UndefinedConstant(), p0);
|
|
|
|
Reduction r = Reduce(call);
|
|
|
|
|
|
|
|
ASSERT_TRUE(r.Changed());
|
|
|
|
EXPECT_THAT(r.replacement(), IsTruncateFloat64ToFloat32(p0));
|
|
|
|
}
|
2014-09-24 14:55:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-23 11:26:49 +00:00
|
|
|
} // namespace compiler
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|