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"
|
2015-10-19 08:05:05 +00:00
|
|
|
#include "src/compiler/simplified-operator.h"
|
2014-09-23 11:26:49 +00:00
|
|
|
#include "src/compiler/typer.h"
|
2015-09-01 09:25:19 +00:00
|
|
|
#include "src/isolate-inl.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) {
|
2015-12-10 09:03:30 +00:00
|
|
|
MachineOperatorBuilder machine(zone(), MachineType::PointerRepresentation(),
|
|
|
|
flags);
|
2015-10-19 08:05:05 +00:00
|
|
|
SimplifiedOperatorBuilder simplified(zone());
|
|
|
|
JSGraph jsgraph(isolate(), graph(), common(), javascript(), &simplified,
|
2015-10-16 12:38:46 +00:00
|
|
|
&machine);
|
2015-06-05 12:37:43 +00:00
|
|
|
// TODO(titzer): mock the GraphReducer here for better unit testing.
|
|
|
|
GraphReducer graph_reducer(zone(), graph());
|
|
|
|
JSBuiltinReducer reducer(&graph_reducer, &jsgraph);
|
2014-09-23 11:26:49 +00:00
|
|
|
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-08-31 08:24:52 +00:00
|
|
|
return HeapConstant(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-05-21 10:59:54 +00:00
|
|
|
Node* effect = graph()->start();
|
|
|
|
Node* control = graph()->start();
|
2015-10-30 06:58:56 +00:00
|
|
|
Node* context = UndefinedConstant();
|
2015-05-21 10:59:54 +00:00
|
|
|
Node* frame_state = graph()->start();
|
2015-04-27 10:44:47 +00:00
|
|
|
TRACED_FOREACH(LanguageMode, language_mode, kLanguageModes) {
|
2015-11-04 15:04:19 +00:00
|
|
|
Node* call = graph()->NewNode(javascript()->CallFunction(2, language_mode),
|
|
|
|
function, UndefinedConstant(), context,
|
|
|
|
frame_state, frame_state, effect, control);
|
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-05-21 10:59:54 +00:00
|
|
|
Node* effect = graph()->start();
|
|
|
|
Node* control = graph()->start();
|
2015-10-30 06:58:56 +00:00
|
|
|
Node* context = UndefinedConstant();
|
2015-05-21 10:59:54 +00:00
|
|
|
Node* frame_state = graph()->start();
|
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);
|
2015-11-04 15:04:19 +00:00
|
|
|
Node* call =
|
|
|
|
graph()->NewNode(javascript()->CallFunction(3, language_mode),
|
|
|
|
function, UndefinedConstant(), p0, context,
|
|
|
|
frame_state, frame_state, effect, control);
|
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");
|
|
|
|
|
2015-05-21 10:59:54 +00:00
|
|
|
Node* effect = graph()->start();
|
|
|
|
Node* control = graph()->start();
|
2015-10-30 06:58:56 +00:00
|
|
|
Node* context = UndefinedConstant();
|
2015-05-21 10:59:54 +00:00
|
|
|
Node* frame_state = graph()->start();
|
2015-04-27 10:44:47 +00:00
|
|
|
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 =
|
2015-11-04 15:04:19 +00:00
|
|
|
graph()->NewNode(javascript()->CallFunction(4, language_mode),
|
2015-10-30 06:58:56 +00:00
|
|
|
function, UndefinedConstant(), p0, p1, context,
|
|
|
|
frame_state, frame_state, effect, control);
|
2015-04-27 10:44:47 +00:00
|
|
|
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-12-10 09:03:30 +00:00
|
|
|
IsSelect(MachineRepresentation::kNone,
|
|
|
|
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");
|
|
|
|
|
2015-05-21 10:59:54 +00:00
|
|
|
Node* effect = graph()->start();
|
|
|
|
Node* control = graph()->start();
|
2015-10-30 06:58:56 +00:00
|
|
|
Node* context = UndefinedConstant();
|
2015-05-21 10:59:54 +00:00
|
|
|
Node* frame_state = graph()->start();
|
2015-04-27 10:44:47 +00:00
|
|
|
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 =
|
2015-11-04 15:04:19 +00:00
|
|
|
graph()->NewNode(javascript()->CallFunction(4, language_mode),
|
2015-10-30 06:58:56 +00:00
|
|
|
function, UndefinedConstant(), p0, p1, context,
|
|
|
|
frame_state, frame_state, effect, control);
|
2015-04-27 10:44:47 +00:00
|
|
|
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-05-21 10:59:54 +00:00
|
|
|
Node* effect = graph()->start();
|
|
|
|
Node* control = graph()->start();
|
2015-10-30 06:58:56 +00:00
|
|
|
Node* context = UndefinedConstant();
|
2015-05-21 10:59:54 +00:00
|
|
|
Node* frame_state = graph()->start();
|
2015-04-27 10:44:47 +00:00
|
|
|
TRACED_FOREACH(LanguageMode, language_mode, kLanguageModes) {
|
|
|
|
TRACED_FOREACH(Type*, t0, kNumberTypes) {
|
|
|
|
Node* p0 = Parameter(t0, 0);
|
2015-11-04 15:04:19 +00:00
|
|
|
Node* call =
|
|
|
|
graph()->NewNode(javascript()->CallFunction(3, language_mode),
|
|
|
|
function, UndefinedConstant(), p0, context,
|
|
|
|
frame_state, frame_state, effect, control);
|
2015-04-27 10:44:47 +00:00
|
|
|
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
|