2014-08-01 08:16:19 +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 <limits>
|
|
|
|
|
2014-08-27 11:14:10 +00:00
|
|
|
#include "src/compiler/change-lowering.h"
|
2014-08-01 08:16:19 +00:00
|
|
|
#include "src/compiler/control-builders.h"
|
2014-08-27 11:14:10 +00:00
|
|
|
#include "src/compiler/js-graph.h"
|
2015-01-29 09:17:45 +00:00
|
|
|
#include "src/compiler/node-properties.h"
|
2014-08-01 08:16:19 +00:00
|
|
|
#include "src/compiler/pipeline.h"
|
2014-11-06 09:09:50 +00:00
|
|
|
#include "src/compiler/select-lowering.h"
|
2014-10-15 11:38:04 +00:00
|
|
|
#include "src/compiler/simplified-lowering.h"
|
2014-11-17 13:55:27 +00:00
|
|
|
#include "src/compiler/typer.h"
|
2014-08-01 08:16:19 +00:00
|
|
|
#include "src/compiler/verifier.h"
|
|
|
|
#include "src/execution.h"
|
2014-08-27 13:49:30 +00:00
|
|
|
#include "src/globals.h"
|
2014-08-01 08:16:19 +00:00
|
|
|
#include "src/parser.h"
|
|
|
|
#include "src/rewriter.h"
|
|
|
|
#include "src/scopes.h"
|
|
|
|
#include "test/cctest/cctest.h"
|
|
|
|
#include "test/cctest/compiler/codegen-tester.h"
|
2014-10-08 09:23:33 +00:00
|
|
|
#include "test/cctest/compiler/function-tester.h"
|
2014-08-01 08:16:19 +00:00
|
|
|
#include "test/cctest/compiler/graph-builder-tester.h"
|
|
|
|
#include "test/cctest/compiler/value-helper.h"
|
|
|
|
|
|
|
|
using namespace v8::internal;
|
|
|
|
using namespace v8::internal::compiler;
|
|
|
|
|
|
|
|
template <typename ReturnType>
|
|
|
|
class ChangesLoweringTester : public GraphBuilderTester<ReturnType> {
|
|
|
|
public:
|
2014-08-14 09:19:54 +00:00
|
|
|
explicit ChangesLoweringTester(MachineType p0 = kMachNone)
|
2014-08-01 08:16:19 +00:00
|
|
|
: GraphBuilderTester<ReturnType>(p0),
|
2014-09-12 11:06:37 +00:00
|
|
|
javascript(this->zone()),
|
2015-01-23 15:19:34 +00:00
|
|
|
jsgraph(this->isolate(), this->graph(), this->common(), &javascript,
|
|
|
|
this->machine()),
|
2014-08-01 08:16:19 +00:00
|
|
|
function(Handle<JSFunction>::null()) {}
|
|
|
|
|
2014-09-12 11:06:37 +00:00
|
|
|
JSOperatorBuilder javascript;
|
2014-08-01 08:16:19 +00:00
|
|
|
JSGraph jsgraph;
|
|
|
|
Handle<JSFunction> function;
|
|
|
|
|
|
|
|
Node* start() { return this->graph()->start(); }
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T* CallWithPotentialGC() {
|
2014-10-08 09:23:33 +00:00
|
|
|
// TODO(titzer): we wrap the code in a JSFunction here to reuse the
|
|
|
|
// JSEntryStub; that could be done with a special prologue or other stub.
|
2014-08-01 08:16:19 +00:00
|
|
|
if (function.is_null()) {
|
2014-10-08 09:23:33 +00:00
|
|
|
function = FunctionTester::ForMachineGraph(this->graph());
|
2014-08-01 08:16:19 +00:00
|
|
|
}
|
|
|
|
Handle<Object>* args = NULL;
|
|
|
|
MaybeHandle<Object> result =
|
|
|
|
Execution::Call(this->isolate(), function, factory()->undefined_value(),
|
|
|
|
0, args, false);
|
|
|
|
return T::cast(*result.ToHandleChecked());
|
|
|
|
}
|
|
|
|
|
|
|
|
void StoreFloat64(Node* node, double* ptr) {
|
|
|
|
Node* ptr_node = this->PointerConstant(ptr);
|
2014-08-14 09:19:54 +00:00
|
|
|
this->Store(kMachFloat64, ptr_node, node);
|
2014-08-01 08:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Node* LoadInt32(int32_t* ptr) {
|
|
|
|
Node* ptr_node = this->PointerConstant(ptr);
|
2014-08-14 09:19:54 +00:00
|
|
|
return this->Load(kMachInt32, ptr_node);
|
2014-08-01 08:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Node* LoadUint32(uint32_t* ptr) {
|
|
|
|
Node* ptr_node = this->PointerConstant(ptr);
|
2014-08-14 09:19:54 +00:00
|
|
|
return this->Load(kMachUint32, ptr_node);
|
2014-08-01 08:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Node* LoadFloat64(double* ptr) {
|
|
|
|
Node* ptr_node = this->PointerConstant(ptr);
|
2014-08-14 09:19:54 +00:00
|
|
|
return this->Load(kMachFloat64, ptr_node);
|
2014-08-01 08:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CheckNumber(double expected, Object* number) {
|
|
|
|
CHECK(this->isolate()->factory()->NewNumber(expected)->SameValue(number));
|
|
|
|
}
|
|
|
|
|
2014-09-10 12:23:45 +00:00
|
|
|
void BuildAndLower(const Operator* op) {
|
2014-08-01 08:16:19 +00:00
|
|
|
// We build a graph by hand here, because the raw machine assembler
|
|
|
|
// does not add the correct control and effect nodes.
|
|
|
|
Node* p0 = this->Parameter(0);
|
|
|
|
Node* change = this->graph()->NewNode(op, p0);
|
|
|
|
Node* ret = this->graph()->NewNode(this->common()->Return(), change,
|
|
|
|
this->start(), this->start());
|
|
|
|
Node* end = this->graph()->NewNode(this->common()->End(), ret);
|
|
|
|
this->graph()->SetEnd(end);
|
2014-08-27 11:14:10 +00:00
|
|
|
LowerChange(change);
|
2014-08-01 08:16:19 +00:00
|
|
|
}
|
|
|
|
|
2014-09-10 12:23:45 +00:00
|
|
|
void BuildStoreAndLower(const Operator* op, const Operator* store_op,
|
|
|
|
void* location) {
|
2014-08-01 08:16:19 +00:00
|
|
|
// We build a graph by hand here, because the raw machine assembler
|
|
|
|
// does not add the correct control and effect nodes.
|
|
|
|
Node* p0 = this->Parameter(0);
|
|
|
|
Node* change = this->graph()->NewNode(op, p0);
|
|
|
|
Node* store = this->graph()->NewNode(
|
|
|
|
store_op, this->PointerConstant(location), this->Int32Constant(0),
|
|
|
|
change, this->start(), this->start());
|
|
|
|
Node* ret = this->graph()->NewNode(
|
|
|
|
this->common()->Return(), this->Int32Constant(0), store, this->start());
|
|
|
|
Node* end = this->graph()->NewNode(this->common()->End(), ret);
|
|
|
|
this->graph()->SetEnd(end);
|
2014-08-27 11:14:10 +00:00
|
|
|
LowerChange(change);
|
2014-08-01 08:16:19 +00:00
|
|
|
}
|
|
|
|
|
2014-09-10 12:23:45 +00:00
|
|
|
void BuildLoadAndLower(const Operator* op, const Operator* load_op,
|
|
|
|
void* location) {
|
2014-08-01 08:16:19 +00:00
|
|
|
// We build a graph by hand here, because the raw machine assembler
|
|
|
|
// does not add the correct control and effect nodes.
|
2014-10-01 11:08:37 +00:00
|
|
|
Node* load = this->graph()->NewNode(
|
|
|
|
load_op, this->PointerConstant(location), this->Int32Constant(0),
|
|
|
|
this->start(), this->start());
|
2014-08-01 08:16:19 +00:00
|
|
|
Node* change = this->graph()->NewNode(op, load);
|
|
|
|
Node* ret = this->graph()->NewNode(this->common()->Return(), change,
|
|
|
|
this->start(), this->start());
|
|
|
|
Node* end = this->graph()->NewNode(this->common()->End(), ret);
|
|
|
|
this->graph()->SetEnd(end);
|
2014-08-27 11:14:10 +00:00
|
|
|
LowerChange(change);
|
|
|
|
}
|
|
|
|
|
|
|
|
void LowerChange(Node* change) {
|
|
|
|
// Run the graph reducer with changes lowering on a single node.
|
2015-02-11 14:12:03 +00:00
|
|
|
Typer typer(this->isolate(), this->graph(), Handle<Context>());
|
2014-11-17 13:55:27 +00:00
|
|
|
typer.Run();
|
2015-02-11 14:12:03 +00:00
|
|
|
ChangeLowering change_lowering(&jsgraph);
|
2014-11-06 09:09:50 +00:00
|
|
|
SelectLowering select_lowering(this->graph(), this->common());
|
2014-11-17 12:12:24 +00:00
|
|
|
GraphReducer reducer(this->graph(), this->zone());
|
2014-11-06 09:09:50 +00:00
|
|
|
reducer.AddReducer(&change_lowering);
|
|
|
|
reducer.AddReducer(&select_lowering);
|
2014-08-27 11:14:10 +00:00
|
|
|
reducer.ReduceNode(change);
|
2014-10-15 11:38:04 +00:00
|
|
|
Verifier::Run(this->graph(), Verifier::UNTYPED);
|
2014-08-01 08:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Factory* factory() { return this->isolate()->factory(); }
|
|
|
|
Heap* heap() { return this->isolate()->heap(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
TEST(RunChangeTaggedToInt32) {
|
|
|
|
// Build and lower a graph by hand.
|
2014-08-14 09:19:54 +00:00
|
|
|
ChangesLoweringTester<int32_t> t(kMachAnyTagged);
|
2014-08-01 08:16:19 +00:00
|
|
|
t.BuildAndLower(t.simplified()->ChangeTaggedToInt32());
|
|
|
|
|
|
|
|
if (Pipeline::SupportedTarget()) {
|
|
|
|
FOR_INT32_INPUTS(i) {
|
|
|
|
int32_t input = *i;
|
|
|
|
|
|
|
|
if (Smi::IsValid(input)) {
|
|
|
|
int32_t result = t.Call(Smi::FromInt(input));
|
|
|
|
CHECK_EQ(input, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
Handle<Object> number = t.factory()->NewNumber(input);
|
|
|
|
int32_t result = t.Call(*number);
|
|
|
|
CHECK_EQ(input, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
Handle<HeapNumber> number = t.factory()->NewHeapNumber(input);
|
|
|
|
int32_t result = t.Call(*number);
|
|
|
|
CHECK_EQ(input, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(RunChangeTaggedToUint32) {
|
|
|
|
// Build and lower a graph by hand.
|
2014-08-14 09:19:54 +00:00
|
|
|
ChangesLoweringTester<uint32_t> t(kMachAnyTagged);
|
2014-08-01 08:16:19 +00:00
|
|
|
t.BuildAndLower(t.simplified()->ChangeTaggedToUint32());
|
|
|
|
|
|
|
|
if (Pipeline::SupportedTarget()) {
|
|
|
|
FOR_UINT32_INPUTS(i) {
|
|
|
|
uint32_t input = *i;
|
|
|
|
|
|
|
|
if (Smi::IsValid(input)) {
|
|
|
|
uint32_t result = t.Call(Smi::FromInt(input));
|
|
|
|
CHECK_EQ(static_cast<int32_t>(input), static_cast<int32_t>(result));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
Handle<Object> number = t.factory()->NewNumber(input);
|
|
|
|
uint32_t result = t.Call(*number);
|
|
|
|
CHECK_EQ(static_cast<int32_t>(input), static_cast<int32_t>(result));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
Handle<HeapNumber> number = t.factory()->NewHeapNumber(input);
|
|
|
|
uint32_t result = t.Call(*number);
|
|
|
|
CHECK_EQ(static_cast<int32_t>(input), static_cast<int32_t>(result));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(RunChangeTaggedToFloat64) {
|
2014-08-14 09:19:54 +00:00
|
|
|
ChangesLoweringTester<int32_t> t(kMachAnyTagged);
|
2014-08-01 08:16:19 +00:00
|
|
|
double result;
|
|
|
|
|
2014-09-11 10:37:49 +00:00
|
|
|
t.BuildStoreAndLower(
|
|
|
|
t.simplified()->ChangeTaggedToFloat64(),
|
|
|
|
t.machine()->Store(StoreRepresentation(kMachFloat64, kNoWriteBarrier)),
|
|
|
|
&result);
|
2014-08-01 08:16:19 +00:00
|
|
|
|
|
|
|
if (Pipeline::SupportedTarget()) {
|
|
|
|
FOR_INT32_INPUTS(i) {
|
|
|
|
int32_t input = *i;
|
|
|
|
|
|
|
|
if (Smi::IsValid(input)) {
|
|
|
|
t.Call(Smi::FromInt(input));
|
|
|
|
CHECK_EQ(input, static_cast<int32_t>(result));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
Handle<Object> number = t.factory()->NewNumber(input);
|
|
|
|
t.Call(*number);
|
|
|
|
CHECK_EQ(input, static_cast<int32_t>(result));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
Handle<HeapNumber> number = t.factory()->NewHeapNumber(input);
|
|
|
|
t.Call(*number);
|
|
|
|
CHECK_EQ(input, static_cast<int32_t>(result));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Pipeline::SupportedTarget()) {
|
|
|
|
FOR_FLOAT64_INPUTS(i) {
|
|
|
|
double input = *i;
|
|
|
|
{
|
|
|
|
Handle<Object> number = t.factory()->NewNumber(input);
|
|
|
|
t.Call(*number);
|
2015-01-30 09:29:25 +00:00
|
|
|
CheckDoubleEq(input, result);
|
2014-08-01 08:16:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
Handle<HeapNumber> number = t.factory()->NewHeapNumber(input);
|
|
|
|
t.Call(*number);
|
2015-01-30 09:29:25 +00:00
|
|
|
CheckDoubleEq(input, result);
|
2014-08-01 08:16:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(RunChangeBoolToBit) {
|
2014-08-14 09:19:54 +00:00
|
|
|
ChangesLoweringTester<int32_t> t(kMachAnyTagged);
|
2014-08-01 08:16:19 +00:00
|
|
|
t.BuildAndLower(t.simplified()->ChangeBoolToBit());
|
|
|
|
|
|
|
|
if (Pipeline::SupportedTarget()) {
|
|
|
|
Object* true_obj = t.heap()->true_value();
|
|
|
|
int32_t result = t.Call(true_obj);
|
|
|
|
CHECK_EQ(1, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Pipeline::SupportedTarget()) {
|
|
|
|
Object* false_obj = t.heap()->false_value();
|
|
|
|
int32_t result = t.Call(false_obj);
|
|
|
|
CHECK_EQ(0, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(RunChangeBitToBool) {
|
2014-08-14 09:19:54 +00:00
|
|
|
ChangesLoweringTester<Object*> t(kMachInt32);
|
2014-08-01 08:16:19 +00:00
|
|
|
t.BuildAndLower(t.simplified()->ChangeBitToBool());
|
|
|
|
|
|
|
|
if (Pipeline::SupportedTarget()) {
|
|
|
|
Object* result = t.Call(1);
|
|
|
|
Object* true_obj = t.heap()->true_value();
|
|
|
|
CHECK_EQ(true_obj, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Pipeline::SupportedTarget()) {
|
|
|
|
Object* result = t.Call(0);
|
|
|
|
Object* false_obj = t.heap()->false_value();
|
|
|
|
CHECK_EQ(false_obj, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-27 16:17:16 +00:00
|
|
|
#if V8_TURBOFAN_BACKEND
|
2014-08-27 13:49:30 +00:00
|
|
|
// TODO(titzer): disabled on ARM
|
|
|
|
|
|
|
|
TEST(RunChangeInt32ToTaggedSmi) {
|
2014-08-01 08:16:19 +00:00
|
|
|
ChangesLoweringTester<Object*> t;
|
|
|
|
int32_t input;
|
|
|
|
t.BuildLoadAndLower(t.simplified()->ChangeInt32ToTagged(),
|
2014-08-14 09:19:54 +00:00
|
|
|
t.machine()->Load(kMachInt32), &input);
|
2014-08-01 08:16:19 +00:00
|
|
|
|
|
|
|
if (Pipeline::SupportedTarget()) {
|
|
|
|
FOR_INT32_INPUTS(i) {
|
|
|
|
input = *i;
|
2014-08-27 13:49:30 +00:00
|
|
|
if (!Smi::IsValid(input)) continue;
|
|
|
|
Object* result = t.Call();
|
2014-08-27 11:14:10 +00:00
|
|
|
t.CheckNumber(static_cast<double>(input), result);
|
2014-08-01 08:16:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-08-27 13:49:30 +00:00
|
|
|
TEST(RunChangeUint32ToTaggedSmi) {
|
2014-08-01 08:16:19 +00:00
|
|
|
ChangesLoweringTester<Object*> t;
|
|
|
|
uint32_t input;
|
|
|
|
t.BuildLoadAndLower(t.simplified()->ChangeUint32ToTagged(),
|
2014-08-14 09:19:54 +00:00
|
|
|
t.machine()->Load(kMachUint32), &input);
|
2014-08-01 08:16:19 +00:00
|
|
|
|
|
|
|
if (Pipeline::SupportedTarget()) {
|
|
|
|
FOR_UINT32_INPUTS(i) {
|
|
|
|
input = *i;
|
2014-08-27 13:49:30 +00:00
|
|
|
if (input > static_cast<uint32_t>(Smi::kMaxValue)) continue;
|
|
|
|
Object* result = t.Call();
|
2014-08-01 08:16:19 +00:00
|
|
|
double expected = static_cast<double>(input);
|
2014-08-27 11:14:10 +00:00
|
|
|
t.CheckNumber(expected, result);
|
2014-08-01 08:16:19 +00:00
|
|
|
}
|
|
|
|
}
|
2014-08-27 13:49:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(RunChangeInt32ToTagged) {
|
|
|
|
ChangesLoweringTester<Object*> t;
|
|
|
|
int32_t input;
|
|
|
|
t.BuildLoadAndLower(t.simplified()->ChangeInt32ToTagged(),
|
|
|
|
t.machine()->Load(kMachInt32), &input);
|
2014-08-01 08:16:19 +00:00
|
|
|
|
|
|
|
if (Pipeline::SupportedTarget()) {
|
2014-08-27 13:49:30 +00:00
|
|
|
for (int m = 0; m < 3; m++) { // Try 3 GC modes.
|
|
|
|
FOR_INT32_INPUTS(i) {
|
|
|
|
if (m == 0) CcTest::heap()->EnableInlineAllocation();
|
|
|
|
if (m == 1) CcTest::heap()->DisableInlineAllocation();
|
|
|
|
if (m == 2) SimulateFullSpace(CcTest::heap()->new_space());
|
|
|
|
|
|
|
|
input = *i;
|
|
|
|
Object* result = t.CallWithPotentialGC<Object>();
|
|
|
|
t.CheckNumber(static_cast<double>(input), result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(RunChangeUint32ToTagged) {
|
|
|
|
ChangesLoweringTester<Object*> t;
|
|
|
|
uint32_t input;
|
|
|
|
t.BuildLoadAndLower(t.simplified()->ChangeUint32ToTagged(),
|
|
|
|
t.machine()->Load(kMachUint32), &input);
|
|
|
|
|
|
|
|
if (Pipeline::SupportedTarget()) {
|
|
|
|
for (int m = 0; m < 3; m++) { // Try 3 GC modes.
|
|
|
|
FOR_UINT32_INPUTS(i) {
|
|
|
|
if (m == 0) CcTest::heap()->EnableInlineAllocation();
|
|
|
|
if (m == 1) CcTest::heap()->DisableInlineAllocation();
|
|
|
|
if (m == 2) SimulateFullSpace(CcTest::heap()->new_space());
|
|
|
|
|
|
|
|
input = *i;
|
|
|
|
Object* result = t.CallWithPotentialGC<Object>();
|
|
|
|
double expected = static_cast<double>(input);
|
|
|
|
t.CheckNumber(expected, result);
|
|
|
|
}
|
2014-08-01 08:16:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(RunChangeFloat64ToTagged) {
|
|
|
|
ChangesLoweringTester<Object*> t;
|
|
|
|
double input;
|
|
|
|
t.BuildLoadAndLower(t.simplified()->ChangeFloat64ToTagged(),
|
2014-08-14 09:19:54 +00:00
|
|
|
t.machine()->Load(kMachFloat64), &input);
|
2014-08-01 08:16:19 +00:00
|
|
|
|
2014-08-27 13:49:30 +00:00
|
|
|
if (Pipeline::SupportedTarget()) {
|
|
|
|
for (int m = 0; m < 3; m++) { // Try 3 GC modes.
|
|
|
|
FOR_FLOAT64_INPUTS(i) {
|
|
|
|
if (m == 0) CcTest::heap()->EnableInlineAllocation();
|
|
|
|
if (m == 1) CcTest::heap()->DisableInlineAllocation();
|
|
|
|
if (m == 2) SimulateFullSpace(CcTest::heap()->new_space());
|
|
|
|
|
|
|
|
input = *i;
|
|
|
|
Object* result = t.CallWithPotentialGC<Object>();
|
|
|
|
t.CheckNumber(input, result);
|
|
|
|
}
|
2014-08-01 08:16:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-08-27 13:49:30 +00:00
|
|
|
|
2014-08-27 16:17:16 +00:00
|
|
|
#endif // V8_TURBOFAN_BACKEND
|