Commit f9e4527f authored by mstarzinger's avatar mstarzinger Committed by Commit bot

Restrict floating control to minimal control-connected component.

R=jarin@chromium.org
TEST=cctest/test-scheduler/NestedFloatingDiamondWithChain

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

Cr-Commit-Position: refs/heads/master@{#25621}
parent 0672b64d
......@@ -491,6 +491,7 @@ source_set("v8_base") {
"src/compiler/common-operator.h",
"src/compiler/control-builders.cc",
"src/compiler/control-builders.h",
"src/compiler/control-equivalence.h",
"src/compiler/control-reducer.cc",
"src/compiler/control-reducer.h",
"src/compiler/diamond.h",
......
This diff is collapsed.
......@@ -8,6 +8,7 @@
#include "src/compiler/scheduler.h"
#include "src/bit-vector.h"
#include "src/compiler/control-equivalence.h"
#include "src/compiler/graph.h"
#include "src/compiler/graph-inl.h"
#include "src/compiler/node.h"
......@@ -58,7 +59,7 @@ Schedule* Scheduler::ComputeSchedule(Zone* zone, Graph* graph) {
Scheduler::SchedulerData Scheduler::DefaultSchedulerData() {
SchedulerData def = {schedule_->start(), 0, false, false, kUnknown};
SchedulerData def = {schedule_->start(), 0, false, kUnknown};
return def;
}
......@@ -85,17 +86,12 @@ Scheduler::Placement Scheduler::GetPlacement(Node* node) {
data->placement_ = (p == kFixed ? kFixed : kCoupled);
break;
}
#define DEFINE_FLOATING_CONTROL_CASE(V) case IrOpcode::k##V:
CONTROL_OP_LIST(DEFINE_FLOATING_CONTROL_CASE)
#undef DEFINE_FLOATING_CONTROL_CASE
#define DEFINE_CONTROL_CASE(V) case IrOpcode::k##V:
CONTROL_OP_LIST(DEFINE_CONTROL_CASE)
#undef DEFINE_CONTROL_CASE
{
// Control nodes that were not control-reachable from end may float.
data->placement_ = kSchedulable;
if (!data->is_connected_control_) {
data->is_floating_control_ = true;
Trace("Floating control found: #%d:%s\n", node->id(),
node->op()->mnemonic());
}
break;
}
default:
......@@ -125,9 +121,9 @@ void Scheduler::UpdatePlacement(Node* node, Placement placement) {
schedule_->AddNode(block, node);
break;
}
#define DEFINE_FLOATING_CONTROL_CASE(V) case IrOpcode::k##V:
CONTROL_OP_LIST(DEFINE_FLOATING_CONTROL_CASE)
#undef DEFINE_FLOATING_CONTROL_CASE
#define DEFINE_CONTROL_CASE(V) case IrOpcode::k##V:
CONTROL_OP_LIST(DEFINE_CONTROL_CASE)
#undef DEFINE_CONTROL_CASE
{
// Control nodes force coupled uses to be placed.
Node::Uses uses = node->uses();
......@@ -241,7 +237,7 @@ class CFGBuilder : public ZoneObject {
schedule_(scheduler->schedule_),
queue_(zone),
control_(zone),
component_head_(NULL),
component_entry_(NULL),
component_start_(NULL),
component_end_(NULL) {}
......@@ -267,31 +263,37 @@ class CFGBuilder : public ZoneObject {
}
// Run the control flow graph construction for a minimal control-connected
// component ending in {node} and merge that component into an existing
// component ending in {exit} and merge that component into an existing
// control flow graph at the bottom of {block}.
void Run(BasicBlock* block, Node* node) {
void Run(BasicBlock* block, Node* exit) {
ResetDataStructures();
Queue(node);
Queue(exit);
component_entry_ = NULL;
component_start_ = block;
component_end_ = schedule_->block(node);
component_end_ = schedule_->block(exit);
scheduler_->equivalence_->Run(exit);
while (!queue_.empty()) { // Breadth-first backwards traversal.
Node* node = queue_.front();
queue_.pop();
bool is_dom = true;
// Use control dependence equivalence to find a canonical single-entry
// single-exit region that makes up a minimal component to be scheduled.
if (IsSingleEntrySingleExitRegion(node, exit)) {
Trace("Found SESE at #%d:%s\n", node->id(), node->op()->mnemonic());
DCHECK_EQ(NULL, component_entry_);
component_entry_ = node;
continue;
}
int max = NodeProperties::PastControlIndex(node);
for (int i = NodeProperties::FirstControlIndex(node); i < max; i++) {
is_dom = is_dom &&
!scheduler_->GetData(node->InputAt(i))->is_floating_control_;
Queue(node->InputAt(i));
}
// TODO(mstarzinger): This is a hacky way to find component dominator.
if (is_dom) component_head_ = node;
}
DCHECK_NOT_NULL(component_head_);
DCHECK_NE(NULL, component_entry_);
for (NodeVector::iterator i = control_.begin(); i != control_.end(); ++i) {
scheduler_->GetData(*i)->is_floating_control_ = false;
ConnectBlocks(*i); // Connect block to its predecessor/successors.
}
}
......@@ -316,7 +318,6 @@ class CFGBuilder : public ZoneObject {
}
}
void BuildBlocks(Node* node) {
switch (node->opcode()) {
case IrOpcode::kEnd:
......@@ -390,14 +391,14 @@ class CFGBuilder : public ZoneObject {
IrOpcode::Value false_opcode) {
buffer[0] = NULL;
buffer[1] = NULL;
for (UseIter i = node->uses().begin(); i != node->uses().end(); ++i) {
if ((*i)->opcode() == true_opcode) {
for (Node* use : node->uses()) {
if (use->opcode() == true_opcode) {
DCHECK_EQ(NULL, buffer[0]);
buffer[0] = *i;
buffer[0] = use;
}
if ((*i)->opcode() == false_opcode) {
if (use->opcode() == false_opcode) {
DCHECK_EQ(NULL, buffer[1]);
buffer[1] = *i;
buffer[1] = use;
}
}
DCHECK_NE(NULL, buffer[0]);
......@@ -430,7 +431,7 @@ class CFGBuilder : public ZoneObject {
break;
}
if (branch == component_head_) {
if (branch == component_entry_) {
TraceConnect(branch, component_start_, successor_blocks[0]);
TraceConnect(branch, component_start_, successor_blocks[1]);
schedule_->InsertBranch(component_start_, component_end_, branch,
......@@ -455,8 +456,8 @@ class CFGBuilder : public ZoneObject {
DCHECK(block != NULL);
// For all of the merge's control inputs, add a goto at the end to the
// merge's basic block.
for (Node* const j : merge->inputs()) {
BasicBlock* predecessor_block = schedule_->block(j);
for (Node* const input : merge->inputs()) {
BasicBlock* predecessor_block = schedule_->block(input);
TraceConnect(merge, predecessor_block, block);
schedule_->AddGoto(predecessor_block, block);
}
......@@ -485,6 +486,12 @@ class CFGBuilder : public ZoneObject {
node == scheduler_->graph_->end()->InputAt(0));
}
bool IsSingleEntrySingleExitRegion(Node* entry, Node* exit) const {
size_t entry_class = scheduler_->equivalence_->ClassOf(entry);
size_t exit_class = scheduler_->equivalence_->ClassOf(exit);
return entry != exit && entry_class == exit_class;
}
void ResetDataStructures() {
control_.clear();
DCHECK(queue_.empty());
......@@ -495,7 +502,7 @@ class CFGBuilder : public ZoneObject {
Schedule* schedule_;
ZoneQueue<Node*> queue_;
NodeVector control_;
Node* component_head_;
Node* component_entry_;
BasicBlock* component_start_;
BasicBlock* component_end_;
};
......@@ -504,6 +511,9 @@ class CFGBuilder : public ZoneObject {
void Scheduler::BuildCFG() {
Trace("--- CREATING CFG -------------------------------------------\n");
// Instantiate a new control equivalence algorithm for the graph.
equivalence_ = new (zone_) ControlEquivalence(zone_, graph_);
// Build a control-flow graph for the main control-connected component that
// is being spanned by the graph's start and end nodes.
control_flow_builder_ = new (zone_) CFGBuilder(zone_, this);
......@@ -1363,7 +1373,6 @@ class ScheduleLateNodeVisitor {
}
void ScheduleFloatingControl(BasicBlock* block, Node* node) {
DCHECK(scheduler_->GetData(node)->is_floating_control_);
scheduler_->FuseFloatingControl(block, node);
}
......
......@@ -17,6 +17,7 @@ namespace internal {
namespace compiler {
class CFGBuilder;
class ControlEquivalence;
class SpecialRPONumberer;
// Computes a schedule from a graph, placing nodes into basic blocks and
......@@ -49,8 +50,6 @@ class Scheduler {
BasicBlock* minimum_block_; // Minimum legal RPO placement.
int unscheduled_count_; // Number of unscheduled uses of this node.
bool is_connected_control_; // {true} if control-connected to the end node.
bool is_floating_control_; // {true} if control, but not control-connected
// to the end node.
Placement placement_; // Whether the node is fixed, schedulable,
// coupled to another node, or not yet known.
};
......@@ -64,6 +63,7 @@ class Scheduler {
ZoneVector<SchedulerData> node_data_; // Per-node data for all nodes.
CFGBuilder* control_flow_builder_; // Builds basic blocks for controls.
SpecialRPONumberer* special_rpo_; // Special RPO numbering of blocks.
ControlEquivalence* equivalence_; // Control dependence equivalence.
Scheduler(Zone* zone, Graph* graph, Schedule* schedule);
......
......@@ -50,10 +50,10 @@ class zone_allocator {
}
void destroy(pointer p) { p->~T(); }
bool operator==(zone_allocator const& other) {
bool operator==(zone_allocator const& other) const {
return zone_ == other.zone_;
}
bool operator!=(zone_allocator const& other) {
bool operator!=(zone_allocator const& other) const {
return zone_ != other.zone_;
}
......
......@@ -6,6 +6,7 @@
#define V8_ZONE_CONTAINERS_H_
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
......@@ -18,34 +19,45 @@ namespace internal {
// A wrapper subclass for std::vector to make it easy to construct one
// that uses a zone allocator.
template <typename T>
class ZoneVector : public std::vector<T, zone_allocator<T> > {
class ZoneVector : public std::vector<T, zone_allocator<T>> {
public:
// Constructs an empty vector.
explicit ZoneVector(Zone* zone)
: std::vector<T, zone_allocator<T> >(zone_allocator<T>(zone)) {}
: std::vector<T, zone_allocator<T>>(zone_allocator<T>(zone)) {}
// Constructs a new vector and fills it with {size} elements, each
// constructed via the default constructor.
ZoneVector(int size, Zone* zone)
: std::vector<T, zone_allocator<T> >(size, T(), zone_allocator<T>(zone)) {
}
: std::vector<T, zone_allocator<T>>(size, T(), zone_allocator<T>(zone)) {}
// Constructs a new vector and fills it with {size} elements, each
// having the value {def}.
ZoneVector(int size, T def, Zone* zone)
: std::vector<T, zone_allocator<T> >(size, def, zone_allocator<T>(zone)) {
}
: std::vector<T, zone_allocator<T>>(size, def, zone_allocator<T>(zone)) {}
};
// A wrapper subclass std::deque to make it easy to construct one
// that uses a zone allocator.
template <typename T>
class ZoneDeque : public std::deque<T, zone_allocator<T> > {
class ZoneDeque : public std::deque<T, zone_allocator<T>> {
public:
// Constructs an empty deque.
explicit ZoneDeque(Zone* zone)
: std::deque<T, zone_allocator<T> >(zone_allocator<T>(zone)) {}
: std::deque<T, zone_allocator<T>>(zone_allocator<T>(zone)) {}
};
// A wrapper subclass std::list to make it easy to construct one
// that uses a zone allocator.
// TODO(mstarzinger): This should be renamed to ZoneList once we got rid of our
// own home-grown ZoneList that actually is a ZoneVector.
template <typename T>
class ZoneLinkedList : public std::list<T, zone_allocator<T>> {
public:
// Constructs an empty list.
explicit ZoneLinkedList(Zone* zone)
: std::list<T, zone_allocator<T>>(zone_allocator<T>(zone)) {}
};
......
......@@ -1804,6 +1804,52 @@ TEST(NestedFloatingDiamonds) {
}
TEST(NestedFloatingDiamondWithChain) {
HandleAndZoneScope scope;
Graph graph(scope.main_zone());
CommonOperatorBuilder common(scope.main_zone());
Node* start = graph.NewNode(common.Start(2));
graph.SetStart(start);
Node* p0 = graph.NewNode(common.Parameter(0), start);
Node* p1 = graph.NewNode(common.Parameter(1), start);
Node* c = graph.NewNode(common.Int32Constant(7));
Node* brA1 = graph.NewNode(common.Branch(), p0, graph.start());
Node* tA1 = graph.NewNode(common.IfTrue(), brA1);
Node* fA1 = graph.NewNode(common.IfFalse(), brA1);
Node* mA1 = graph.NewNode(common.Merge(2), tA1, fA1);
Node* phiA1 = graph.NewNode(common.Phi(kMachAnyTagged, 2), p0, p1, mA1);
Node* brB1 = graph.NewNode(common.Branch(), p1, graph.start());
Node* tB1 = graph.NewNode(common.IfTrue(), brB1);
Node* fB1 = graph.NewNode(common.IfFalse(), brB1);
Node* mB1 = graph.NewNode(common.Merge(2), tB1, fB1);
Node* phiB1 = graph.NewNode(common.Phi(kMachAnyTagged, 2), p0, p1, mB1);
Node* brA2 = graph.NewNode(common.Branch(), phiB1, mA1);
Node* tA2 = graph.NewNode(common.IfTrue(), brA2);
Node* fA2 = graph.NewNode(common.IfFalse(), brA2);
Node* mA2 = graph.NewNode(common.Merge(2), tA2, fA2);
Node* phiA2 = graph.NewNode(common.Phi(kMachAnyTagged, 2), phiB1, c, mA2);
Node* brB2 = graph.NewNode(common.Branch(), phiA1, mB1);
Node* tB2 = graph.NewNode(common.IfTrue(), brB2);
Node* fB2 = graph.NewNode(common.IfFalse(), brB2);
Node* mB2 = graph.NewNode(common.Merge(2), tB2, fB2);
Node* phiB2 = graph.NewNode(common.Phi(kMachAnyTagged, 2), phiA1, c, mB2);
Node* add = graph.NewNode(&kIntAdd, phiA2, phiB2);
Node* ret = graph.NewNode(common.Return(), add, start, start);
Node* end = graph.NewNode(common.End(), ret, start);
graph.SetEnd(end);
ComputeAndVerifySchedule(35, &graph);
}
TEST(NestedFloatingDiamondWithLoop) {
HandleAndZoneScope scope;
Graph graph(scope.main_zone());
......
// 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/control-equivalence.h"
#include "src/compiler/graph-visualizer.h"
#include "src/compiler/node-properties-inl.h"
#include "src/zone-containers.h"
#include "test/unittests/compiler/graph-unittest.h"
namespace v8 {
namespace internal {
namespace compiler {
#define ASSERT_EQUIVALENCE(...) \
do { \
Node* __n[] = {__VA_ARGS__}; \
ASSERT_TRUE(IsEquivalenceClass(arraysize(__n), __n)); \
} while (false);
class ControlEquivalenceTest : public GraphTest {
public:
ControlEquivalenceTest() : all_nodes_(zone()), classes_(zone()) {
Store(graph()->start());
}
protected:
void ComputeEquivalence(Node* node) {
graph()->SetEnd(graph()->NewNode(common()->End(), node));
if (FLAG_trace_turbo) {
OFStream os(stdout);
os << AsDOT(*graph());
}
ControlEquivalence equivalence(zone(), graph());
equivalence.Run(node);
classes_.resize(graph()->NodeCount());
for (Node* node : all_nodes_) {
classes_[node->id()] = equivalence.ClassOf(node);
}
}
bool IsEquivalenceClass(size_t length, Node** nodes) {
BitVector in_class(graph()->NodeCount(), zone());
size_t expected_class = classes_[nodes[0]->id()];
for (size_t i = 0; i < length; ++i) {
in_class.Add(nodes[i]->id());
}
for (Node* node : all_nodes_) {
if (in_class.Contains(node->id())) {
if (classes_[node->id()] != expected_class) return false;
} else {
if (classes_[node->id()] == expected_class) return false;
}
}
return true;
}
Node* Value() { return NumberConstant(0.0); }
Node* Branch(Node* control) {
return Store(graph()->NewNode(common()->Branch(), Value(), control));
}
Node* IfTrue(Node* control) {
return Store(graph()->NewNode(common()->IfTrue(), control));
}
Node* IfFalse(Node* control) {
return Store(graph()->NewNode(common()->IfFalse(), control));
}
Node* Merge2(Node* control1, Node* control2) {
return Store(graph()->NewNode(common()->Merge(2), control1, control2));
}
Node* Loop2(Node* control) {
return Store(graph()->NewNode(common()->Loop(2), control, control));
}
Node* End(Node* control) {
return Store(graph()->NewNode(common()->End(), control));
}
private:
Node* Store(Node* node) {
all_nodes_.push_back(node);
return node;
}
ZoneVector<Node*> all_nodes_;
ZoneVector<size_t> classes_;
};
// -----------------------------------------------------------------------------
// Test cases.
TEST_F(ControlEquivalenceTest, Empty1) {
Node* start = graph()->start();
ComputeEquivalence(start);
ASSERT_EQUIVALENCE(start);
}
TEST_F(ControlEquivalenceTest, Empty2) {
Node* start = graph()->start();
Node* end = End(start);
ComputeEquivalence(end);
ASSERT_EQUIVALENCE(start, end);
}
TEST_F(ControlEquivalenceTest, Diamond1) {
Node* start = graph()->start();
Node* b = Branch(start);
Node* t = IfTrue(b);
Node* f = IfFalse(b);
Node* m = Merge2(t, f);
ComputeEquivalence(m);
ASSERT_EQUIVALENCE(b, m, start);
ASSERT_EQUIVALENCE(f);
ASSERT_EQUIVALENCE(t);
}
TEST_F(ControlEquivalenceTest, Diamond2) {
Node* start = graph()->start();
Node* b1 = Branch(start);
Node* t1 = IfTrue(b1);
Node* f1 = IfFalse(b1);
Node* b2 = Branch(f1);
Node* t2 = IfTrue(b2);
Node* f2 = IfFalse(b2);
Node* m2 = Merge2(t2, f2);
Node* m1 = Merge2(t1, m2);
ComputeEquivalence(m1);
ASSERT_EQUIVALENCE(b1, m1, start);
ASSERT_EQUIVALENCE(t1);
ASSERT_EQUIVALENCE(f1, b2, m2);
ASSERT_EQUIVALENCE(t2);
ASSERT_EQUIVALENCE(f2);
}
TEST_F(ControlEquivalenceTest, Diamond3) {
Node* start = graph()->start();
Node* b1 = Branch(start);
Node* t1 = IfTrue(b1);
Node* f1 = IfFalse(b1);
Node* m1 = Merge2(t1, f1);
Node* b2 = Branch(m1);
Node* t2 = IfTrue(b2);
Node* f2 = IfFalse(b2);
Node* m2 = Merge2(t2, f2);
ComputeEquivalence(m2);
ASSERT_EQUIVALENCE(b1, m1, b2, m2, start);
ASSERT_EQUIVALENCE(t1);
ASSERT_EQUIVALENCE(f1);
ASSERT_EQUIVALENCE(t2);
ASSERT_EQUIVALENCE(f2);
}
TEST_F(ControlEquivalenceTest, Switch1) {
Node* start = graph()->start();
Node* b1 = Branch(start);
Node* t1 = IfTrue(b1);
Node* f1 = IfFalse(b1);
Node* b2 = Branch(f1);
Node* t2 = IfTrue(b2);
Node* f2 = IfFalse(b2);
Node* b3 = Branch(f2);
Node* t3 = IfTrue(b3);
Node* f3 = IfFalse(b3);
Node* m1 = Merge2(t1, t2);
Node* m2 = Merge2(m1, t3);
Node* m3 = Merge2(m2, f3);
ComputeEquivalence(m3);
ASSERT_EQUIVALENCE(b1, m3, start);
ASSERT_EQUIVALENCE(t1);
ASSERT_EQUIVALENCE(f1, b2);
ASSERT_EQUIVALENCE(t2);
ASSERT_EQUIVALENCE(f2, b3);
ASSERT_EQUIVALENCE(t3);
ASSERT_EQUIVALENCE(f3);
ASSERT_EQUIVALENCE(m1);
ASSERT_EQUIVALENCE(m2);
}
TEST_F(ControlEquivalenceTest, Loop1) {
Node* start = graph()->start();
Node* l = Loop2(start);
l->ReplaceInput(1, l);
ComputeEquivalence(l);
ASSERT_EQUIVALENCE(start);
ASSERT_EQUIVALENCE(l);
}
TEST_F(ControlEquivalenceTest, Loop2) {
Node* start = graph()->start();
Node* l = Loop2(start);
Node* b = Branch(l);
Node* t = IfTrue(b);
Node* f = IfFalse(b);
l->ReplaceInput(1, t);
ComputeEquivalence(f);
ASSERT_EQUIVALENCE(f, start);
ASSERT_EQUIVALENCE(t);
ASSERT_EQUIVALENCE(l, b);
}
TEST_F(ControlEquivalenceTest, Irreducible) {
Node* start = graph()->start();
Node* b1 = Branch(start);
Node* t1 = IfTrue(b1);
Node* f1 = IfFalse(b1);
Node* lp = Loop2(f1);
Node* m1 = Merge2(t1, lp);
Node* b2 = Branch(m1);
Node* t2 = IfTrue(b2);
Node* f2 = IfFalse(b2);
Node* m2 = Merge2(t2, f2);
Node* b3 = Branch(m2);
Node* t3 = IfTrue(b3);
Node* f3 = IfFalse(b3);
lp->ReplaceInput(1, f3);
ComputeEquivalence(t3);
ASSERT_EQUIVALENCE(b1, t3, start);
ASSERT_EQUIVALENCE(t1);
ASSERT_EQUIVALENCE(f1);
ASSERT_EQUIVALENCE(m1, b2, m2, b3);
ASSERT_EQUIVALENCE(t2);
ASSERT_EQUIVALENCE(f2);
ASSERT_EQUIVALENCE(f3);
ASSERT_EQUIVALENCE(lp);
}
} // namespace compiler
} // namespace internal
} // namespace v8
......@@ -39,6 +39,7 @@
'compiler/change-lowering-unittest.cc',
'compiler/common-operator-unittest.cc',
'compiler/compiler-test-utils.h',
'compiler/control-equivalence-unittest.cc',
'compiler/diamond-unittest.cc',
'compiler/graph-reducer-unittest.cc',
'compiler/graph-unittest.cc',
......
......@@ -423,6 +423,7 @@
'../../src/compiler/common-operator.h',
'../../src/compiler/control-builders.cc',
'../../src/compiler/control-builders.h',
'../../src/compiler/control-equivalence.h',
'../../src/compiler/control-reducer.cc',
'../../src/compiler/control-reducer.h',
'../../src/compiler/diamond.h',
......
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