Commit 7300a2a3 authored by bmeurer@chromium.org's avatar bmeurer@chromium.org

[turbofan] Fix MachineOperatorReducer to use JSGraph as well.

TEST=compiler-unittests,cctest
R=jarin@chromium.org

Review URL: https://codereview.chromium.org/526083002

git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@23577 ce2b1a6d-e550-0410-aec6-3dcde31c8c00
parent 3d763dfd
......@@ -4,7 +4,9 @@
#include "src/base/bits.h"
#include "src/compiler/graph-unittest.h"
#include "src/compiler/js-graph.h"
#include "src/compiler/machine-operator-reducer.h"
#include "src/compiler/typer.h"
namespace v8 {
namespace internal {
......@@ -18,7 +20,9 @@ class MachineOperatorReducerTest : public GraphTest {
protected:
Reduction Reduce(Node* node) {
MachineOperatorReducer reducer(graph());
Typer typer(zone());
JSGraph jsgraph(graph(), common(), &typer);
MachineOperatorReducer reducer(&jsgraph);
return reducer.Reduce(node);
}
......
......@@ -5,50 +5,34 @@
#include "src/compiler/machine-operator-reducer.h"
#include "src/base/bits.h"
#include "src/compiler/common-node-cache.h"
#include "src/compiler/generic-node-inl.h"
#include "src/compiler/graph.h"
#include "src/compiler/js-graph.h"
#include "src/compiler/node-matchers.h"
namespace v8 {
namespace internal {
namespace compiler {
MachineOperatorReducer::MachineOperatorReducer(Graph* graph)
: graph_(graph),
cache_(new (graph->zone()) CommonNodeCache(graph->zone())),
common_(graph->zone()),
machine_(graph->zone()) {}
MachineOperatorReducer::MachineOperatorReducer(JSGraph* jsgraph)
: jsgraph_(jsgraph), machine_(jsgraph->zone()) {}
MachineOperatorReducer::MachineOperatorReducer(Graph* graph,
CommonNodeCache* cache)
: graph_(graph),
cache_(cache),
common_(graph->zone()),
machine_(graph->zone()) {}
MachineOperatorReducer::~MachineOperatorReducer() {}
Node* MachineOperatorReducer::Float64Constant(volatile double value) {
Node** loc = cache_->FindFloat64Constant(value);
if (*loc == NULL) {
*loc = graph_->NewNode(common_.Float64Constant(value));
}
return *loc;
return jsgraph()->Float64Constant(value);
}
Node* MachineOperatorReducer::Int32Constant(int32_t value) {
Node** loc = cache_->FindInt32Constant(value);
if (*loc == NULL) {
*loc = graph_->NewNode(common_.Int32Constant(value));
}
return *loc;
return jsgraph()->Int32Constant(value);
}
Node* MachineOperatorReducer::Int64Constant(int64_t value) {
return graph_->NewNode(common_.Int64Constant(value));
return graph()->NewNode(common()->Int64Constant(value));
}
......@@ -82,7 +66,7 @@ Reduction MachineOperatorReducer::Reduce(Node* node) {
Int32BinopMatcher mrightright(mright.right().node());
if (mrightright.left().Is(32) &&
mrightright.right().node() == mleft.right().node()) {
graph_->ChangeOperator(node, machine_.Word32Ror());
graph()->ChangeOperator(node, machine()->Word32Ror());
node->ReplaceInput(0, mleft.left().node());
node->ReplaceInput(1, mleft.right().node());
return Changed(node);
......@@ -91,7 +75,7 @@ Reduction MachineOperatorReducer::Reduce(Node* node) {
// (x << K) | (x >> (32 - K)) => x ror K
if (mleft.right().IsInRange(0, 31) &&
mright.right().Is(32 - mleft.right().Value())) {
graph_->ChangeOperator(node, machine_.Word32Ror());
graph()->ChangeOperator(node, machine()->Word32Ror());
node->ReplaceInput(0, mleft.left().node());
node->ReplaceInput(1, mleft.right().node());
return Changed(node);
......@@ -107,7 +91,7 @@ Reduction MachineOperatorReducer::Reduce(Node* node) {
Int32BinopMatcher mleftright(mleft.right().node());
if (mleftright.left().Is(32) &&
mleftright.right().node() == mright.right().node()) {
graph_->ChangeOperator(node, machine_.Word32Ror());
graph()->ChangeOperator(node, machine()->Word32Ror());
node->ReplaceInput(0, mright.left().node());
node->ReplaceInput(1, mright.right().node());
return Changed(node);
......@@ -116,7 +100,7 @@ Reduction MachineOperatorReducer::Reduce(Node* node) {
// (x >> (32 - K)) | (x << K) => x ror K
if (mright.right().IsInRange(0, 31) &&
mleft.right().Is(32 - mright.right().Value())) {
graph_->ChangeOperator(node, machine_.Word32Ror());
graph()->ChangeOperator(node, machine()->Word32Ror());
node->ReplaceInput(0, mright.left().node());
node->ReplaceInput(1, mright.right().node());
return Changed(node);
......@@ -209,13 +193,13 @@ Reduction MachineOperatorReducer::Reduce(Node* node) {
return ReplaceInt32(m.left().Value() * m.right().Value());
}
if (m.right().Is(-1)) { // x * -1 => 0 - x
graph_->ChangeOperator(node, machine_.Int32Sub());
graph()->ChangeOperator(node, machine()->Int32Sub());
node->ReplaceInput(0, Int32Constant(0));
node->ReplaceInput(1, m.left().node());
return Changed(node);
}
if (m.right().IsPowerOf2()) { // x * 2^n => x << n
graph_->ChangeOperator(node, machine_.Word32Shl());
graph()->ChangeOperator(node, machine()->Word32Shl());
node->ReplaceInput(1, Int32Constant(WhichPowerOf2(m.right().Value())));
return Changed(node);
}
......@@ -233,7 +217,7 @@ Reduction MachineOperatorReducer::Reduce(Node* node) {
return ReplaceInt32(m.left().Value() / m.right().Value());
}
if (m.right().Is(-1)) { // x / -1 => 0 - x
graph_->ChangeOperator(node, machine_.Int32Sub());
graph()->ChangeOperator(node, machine()->Int32Sub());
node->ReplaceInput(0, Int32Constant(0));
node->ReplaceInput(1, m.left().node());
return Changed(node);
......@@ -250,7 +234,7 @@ Reduction MachineOperatorReducer::Reduce(Node* node) {
return ReplaceInt32(m.left().Value() / m.right().Value());
}
if (m.right().IsPowerOf2()) { // x / 2^n => x >> n
graph_->ChangeOperator(node, machine_.Word32Shr());
graph()->ChangeOperator(node, machine()->Word32Shr());
node->ReplaceInput(1, Int32Constant(WhichPowerOf2(m.right().Value())));
return Changed(node);
}
......@@ -279,7 +263,7 @@ Reduction MachineOperatorReducer::Reduce(Node* node) {
return ReplaceInt32(m.left().Value() % m.right().Value());
}
if (m.right().IsPowerOf2()) { // x % 2^n => x & 2^n-1
graph_->ChangeOperator(node, machine_.Word32And());
graph()->ChangeOperator(node, machine()->Word32And());
node->ReplaceInput(1, Int32Constant(m.right().Value() - 1));
return Changed(node);
}
......@@ -447,6 +431,15 @@ Reduction MachineOperatorReducer::Reduce(Node* node) {
}
return NoChange();
}
CommonOperatorBuilder* MachineOperatorReducer::common() const {
return jsgraph()->common();
}
}
} // namespace v8::internal::compiler
Graph* MachineOperatorReducer::graph() const { return jsgraph()->graph(); }
} // namespace compiler
} // namespace internal
} // namespace v8
......@@ -5,7 +5,6 @@
#ifndef V8_COMPILER_MACHINE_OPERATOR_REDUCER_H_
#define V8_COMPILER_MACHINE_OPERATOR_REDUCER_H_
#include "src/compiler/common-operator.h"
#include "src/compiler/graph-reducer.h"
#include "src/compiler/machine-operator.h"
......@@ -14,24 +13,20 @@ namespace internal {
namespace compiler {
// Forward declarations.
class CommonNodeCache;
class CommonOperatorBuilder;
class JSGraph;
// Performs constant folding and strength reduction on nodes that have
// machine operators.
class MachineOperatorReducer : public Reducer {
class MachineOperatorReducer V8_FINAL : public Reducer {
public:
explicit MachineOperatorReducer(Graph* graph);
MachineOperatorReducer(Graph* graph, CommonNodeCache* cache);
explicit MachineOperatorReducer(JSGraph* jsgraph);
~MachineOperatorReducer();
virtual Reduction Reduce(Node* node);
virtual Reduction Reduce(Node* node) V8_OVERRIDE;
private:
Graph* graph_;
CommonNodeCache* cache_;
CommonOperatorBuilder common_;
MachineOperatorBuilder machine_;
Node* Float64Constant(volatile double value);
Node* Int32Constant(int32_t value);
Node* Int64Constant(int64_t value);
......@@ -46,9 +41,18 @@ class MachineOperatorReducer : public Reducer {
Reduction ReplaceInt64(int64_t value) {
return Replace(Int64Constant(value));
}
Graph* graph() const;
JSGraph* jsgraph() const { return jsgraph_; }
CommonOperatorBuilder* common() const;
MachineOperatorBuilder* machine() { return &machine_; }
JSGraph* jsgraph_;
MachineOperatorBuilder machine_;
};
}
}
} // namespace v8::internal::compiler
} // namespace compiler
} // namespace internal
} // namespace v8
#endif // V8_COMPILER_MACHINE_OPERATOR_REDUCER_H_
......@@ -257,7 +257,7 @@ Handle<Code> Pipeline::GenerateCode() {
MachineOperatorBuilder machine(zone());
SimplifiedOperatorReducer simple_reducer(&jsgraph, &machine);
ChangeLowering lowering(&jsgraph, &linkage, &machine);
MachineOperatorReducer mach_reducer(&graph);
MachineOperatorReducer mach_reducer(&jsgraph);
GraphReducer graph_reducer(&graph);
// TODO(titzer): Figure out if we should run all reducers at once here.
graph_reducer.AddReducer(&simple_reducer);
......
......@@ -6,7 +6,9 @@
#include "src/base/utils/random-number-generator.h"
#include "src/compiler/graph-inl.h"
#include "src/compiler/js-graph.h"
#include "src/compiler/machine-operator-reducer.h"
#include "src/compiler/typer.h"
#include "test/cctest/compiler/value-helper.h"
using namespace v8::internal;
......@@ -37,6 +39,8 @@ class ReducerTester : public HandleAndZoneScope {
machine(main_zone()),
common(main_zone()),
graph(main_zone()),
typer(main_zone()),
jsgraph(&graph, &common, &typer),
maxuint32(Constant<int32_t>(kMaxUInt32)) {
Node* s = graph.NewNode(common.Start(num_parameters));
graph.SetStart(s);
......@@ -48,6 +52,8 @@ class ReducerTester : public HandleAndZoneScope {
MachineOperatorBuilder machine;
CommonOperatorBuilder common;
Graph graph;
Typer typer;
JSGraph jsgraph;
Node* maxuint32;
template <typename T>
......@@ -68,7 +74,7 @@ class ReducerTester : public HandleAndZoneScope {
void CheckFoldBinop(volatile T expect, Node* a, Node* b) {
CHECK_NE(NULL, binop);
Node* n = graph.NewNode(binop, a, b);
MachineOperatorReducer reducer(&graph);
MachineOperatorReducer reducer(&jsgraph);
Reduction reduction = reducer.Reduce(n);
CHECK(reduction.Changed());
CHECK_NE(n, reduction.replacement());
......@@ -80,7 +86,7 @@ class ReducerTester : public HandleAndZoneScope {
void CheckBinop(Node* expect, Node* a, Node* b) {
CHECK_NE(NULL, binop);
Node* n = graph.NewNode(binop, a, b);
MachineOperatorReducer reducer(&graph);
MachineOperatorReducer reducer(&jsgraph);
Reduction reduction = reducer.Reduce(n);
CHECK(reduction.Changed());
CHECK_EQ(expect, reduction.replacement());
......@@ -92,7 +98,7 @@ class ReducerTester : public HandleAndZoneScope {
Node* right) {
CHECK_NE(NULL, binop);
Node* n = graph.NewNode(binop, left, right);
MachineOperatorReducer reducer(&graph);
MachineOperatorReducer reducer(&jsgraph);
Reduction reduction = reducer.Reduce(n);
CHECK(reduction.Changed());
CHECK_EQ(binop, reduction.replacement()->op());
......@@ -107,7 +113,7 @@ class ReducerTester : public HandleAndZoneScope {
Node* right_expect, Node* left, Node* right) {
CHECK_NE(NULL, binop);
Node* n = graph.NewNode(binop, left, right);
MachineOperatorReducer reducer(&graph);
MachineOperatorReducer reducer(&jsgraph);
Reduction r = reducer.Reduce(n);
CHECK(r.Changed());
CHECK_EQ(op_expect->opcode(), r.replacement()->op()->opcode());
......@@ -122,7 +128,7 @@ class ReducerTester : public HandleAndZoneScope {
volatile T right_expect, Node* left, Node* right) {
CHECK_NE(NULL, binop);
Node* n = graph.NewNode(binop, left, right);
MachineOperatorReducer reducer(&graph);
MachineOperatorReducer reducer(&jsgraph);
Reduction r = reducer.Reduce(n);
CHECK(r.Changed());
CHECK_EQ(op_expect->opcode(), r.replacement()->op()->opcode());
......@@ -139,7 +145,7 @@ class ReducerTester : public HandleAndZoneScope {
Node* k = Constant<T>(constant);
{
Node* n = graph.NewNode(binop, k, p);
MachineOperatorReducer reducer(&graph);
MachineOperatorReducer reducer(&jsgraph);
Reduction reduction = reducer.Reduce(n);
CHECK(!reduction.Changed() || reduction.replacement() == n);
CHECK_EQ(p, n->InputAt(0));
......@@ -147,7 +153,7 @@ class ReducerTester : public HandleAndZoneScope {
}
{
Node* n = graph.NewNode(binop, p, k);
MachineOperatorReducer reducer(&graph);
MachineOperatorReducer reducer(&jsgraph);
Reduction reduction = reducer.Reduce(n);
CHECK(!reduction.Changed());
CHECK_EQ(p, n->InputAt(0));
......@@ -163,7 +169,7 @@ class ReducerTester : public HandleAndZoneScope {
Node* p = Parameter();
Node* k = Constant<T>(constant);
Node* n = graph.NewNode(binop, k, p);
MachineOperatorReducer reducer(&graph);
MachineOperatorReducer reducer(&jsgraph);
Reduction reduction = reducer.Reduce(n);
CHECK(!reduction.Changed());
CHECK_EQ(k, n->InputAt(0));
......@@ -633,7 +639,7 @@ TEST(ReduceLoadStore) {
Node* load = R.graph.NewNode(R.machine.Load(kMachInt32), base, index);
{
MachineOperatorReducer reducer(&R.graph);
MachineOperatorReducer reducer(&R.jsgraph);
Reduction reduction = reducer.Reduce(load);
CHECK(!reduction.Changed()); // loads should not be reduced.
}
......@@ -641,7 +647,7 @@ TEST(ReduceLoadStore) {
{
Node* store = R.graph.NewNode(R.machine.Store(kMachInt32, kNoWriteBarrier),
base, index, load);
MachineOperatorReducer reducer(&R.graph);
MachineOperatorReducer reducer(&R.jsgraph);
Reduction reduction = reducer.Reduce(store);
CHECK(!reduction.Changed()); // stores should not be reduced.
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment