Commit d641cc45 authored by bmeurer's avatar bmeurer Committed by Commit bot

[turbofan] Split ControlEquivalence implementation and add trace flag.

Split interface and implementation of ControlEquivalence and add a
dedicated trace flag --trace-turbo-ceq to make it reusable outside the
scheduler.

R=jarin@chromium.org

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

Cr-Commit-Position: refs/heads/master@{#27862}
parent 4d337014
...@@ -561,6 +561,7 @@ source_set("v8_base") { ...@@ -561,6 +561,7 @@ source_set("v8_base") {
"src/compiler/common-operator.h", "src/compiler/common-operator.h",
"src/compiler/control-builders.cc", "src/compiler/control-builders.cc",
"src/compiler/control-builders.h", "src/compiler/control-builders.h",
"src/compiler/control-equivalence.cc",
"src/compiler/control-equivalence.h", "src/compiler/control-equivalence.h",
"src/compiler/control-flow-optimizer.cc", "src/compiler/control-flow-optimizer.cc",
"src/compiler/control-flow-optimizer.h", "src/compiler/control-flow-optimizer.h",
......
// Copyright 2015 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/node-properties.h"
#define TRACE(...) \
do { \
if (FLAG_trace_turbo_ceq) PrintF(__VA_ARGS__); \
} while (false)
namespace v8 {
namespace internal {
namespace compiler {
void ControlEquivalence::Run(Node* exit) {
if (GetClass(exit) == kInvalidClass) {
DetermineParticipation(exit);
RunUndirectedDFS(exit);
}
}
// static
STATIC_CONST_MEMBER_DEFINITION const size_t ControlEquivalence::kInvalidClass;
void ControlEquivalence::VisitPre(Node* node) {
TRACE("CEQ: Pre-visit of #%d:%s\n", node->id(), node->op()->mnemonic());
// Dispense a new pre-order number.
SetNumber(node, NewDFSNumber());
TRACE(" Assigned DFS number is %zu\n", GetNumber(node));
}
void ControlEquivalence::VisitMid(Node* node, DFSDirection direction) {
TRACE("CEQ: Mid-visit of #%d:%s\n", node->id(), node->op()->mnemonic());
BracketList& blist = GetBracketList(node);
// Remove brackets pointing to this node [line:19].
BracketListDelete(blist, node, direction);
// Potentially introduce artificial dependency from start to end.
if (blist.empty()) {
DCHECK_EQ(kInputDirection, direction);
VisitBackedge(node, graph_->end(), kInputDirection);
}
// Potentially start a new equivalence class [line:37].
BracketListTRACE(blist);
Bracket* recent = &blist.back();
if (recent->recent_size != blist.size()) {
recent->recent_size = blist.size();
recent->recent_class = NewClassNumber();
}
// Assign equivalence class to node.
SetClass(node, recent->recent_class);
TRACE(" Assigned class number is %zu\n", GetClass(node));
}
void ControlEquivalence::VisitPost(Node* node, Node* parent_node,
DFSDirection direction) {
TRACE("CEQ: Post-visit of #%d:%s\n", node->id(), node->op()->mnemonic());
BracketList& blist = GetBracketList(node);
// Remove brackets pointing to this node [line:19].
BracketListDelete(blist, node, direction);
// Propagate bracket list up the DFS tree [line:13].
if (parent_node != NULL) {
BracketList& parent_blist = GetBracketList(parent_node);
parent_blist.splice(parent_blist.end(), blist);
}
}
void ControlEquivalence::VisitBackedge(Node* from, Node* to,
DFSDirection direction) {
TRACE("CEQ: Backedge from #%d:%s to #%d:%s\n", from->id(),
from->op()->mnemonic(), to->id(), to->op()->mnemonic());
// Push backedge onto the bracket list [line:25].
Bracket bracket = {direction, kInvalidClass, 0, from, to};
GetBracketList(from).push_back(bracket);
}
void ControlEquivalence::RunUndirectedDFS(Node* exit) {
ZoneStack<DFSStackEntry> stack(zone_);
DFSPush(stack, exit, NULL, kInputDirection);
VisitPre(exit);
while (!stack.empty()) { // Undirected depth-first backwards traversal.
DFSStackEntry& entry = stack.top();
Node* node = entry.node;
if (entry.direction == kInputDirection) {
if (entry.input != node->input_edges().end()) {
Edge edge = *entry.input;
Node* input = edge.to();
++(entry.input);
if (NodeProperties::IsControlEdge(edge)) {
// Visit next control input.
if (!GetData(input)->participates) continue;
if (GetData(input)->visited) continue;
if (GetData(input)->on_stack) {
// Found backedge if input is on stack.
if (input != entry.parent_node) {
VisitBackedge(node, input, kInputDirection);
}
} else {
// Push input onto stack.
DFSPush(stack, input, node, kInputDirection);
VisitPre(input);
}
}
continue;
}
if (entry.use != node->use_edges().end()) {
// Switch direction to uses.
entry.direction = kUseDirection;
VisitMid(node, kInputDirection);
continue;
}
}
if (entry.direction == kUseDirection) {
if (entry.use != node->use_edges().end()) {
Edge edge = *entry.use;
Node* use = edge.from();
++(entry.use);
if (NodeProperties::IsControlEdge(edge)) {
// Visit next control use.
if (!GetData(use)->participates) continue;
if (GetData(use)->visited) continue;
if (GetData(use)->on_stack) {
// Found backedge if use is on stack.
if (use != entry.parent_node) {
VisitBackedge(node, use, kUseDirection);
}
} else {
// Push use onto stack.
DFSPush(stack, use, node, kUseDirection);
VisitPre(use);
}
}
continue;
}
if (entry.input != node->input_edges().end()) {
// Switch direction to inputs.
entry.direction = kInputDirection;
VisitMid(node, kUseDirection);
continue;
}
}
// Pop node from stack when done with all inputs and uses.
DCHECK(entry.input == node->input_edges().end());
DCHECK(entry.use == node->use_edges().end());
DFSPop(stack, node);
VisitPost(node, entry.parent_node, entry.direction);
}
}
void ControlEquivalence::DetermineParticipationEnqueue(ZoneQueue<Node*>& queue,
Node* node) {
if (!GetData(node)->participates) {
GetData(node)->participates = true;
queue.push(node);
}
}
void ControlEquivalence::DetermineParticipation(Node* exit) {
ZoneQueue<Node*> queue(zone_);
DetermineParticipationEnqueue(queue, exit);
while (!queue.empty()) { // Breadth-first backwards traversal.
Node* node = queue.front();
queue.pop();
int max = NodeProperties::PastControlIndex(node);
for (int i = NodeProperties::FirstControlIndex(node); i < max; i++) {
DetermineParticipationEnqueue(queue, node->InputAt(i));
}
}
}
void ControlEquivalence::DFSPush(DFSStack& stack, Node* node, Node* from,
DFSDirection dir) {
DCHECK(GetData(node)->participates);
DCHECK(!GetData(node)->visited);
GetData(node)->on_stack = true;
Node::InputEdges::iterator input = node->input_edges().begin();
Node::UseEdges::iterator use = node->use_edges().begin();
stack.push({dir, input, use, from, node});
}
void ControlEquivalence::DFSPop(DFSStack& stack, Node* node) {
DCHECK_EQ(stack.top().node, node);
GetData(node)->on_stack = false;
GetData(node)->visited = true;
stack.pop();
}
void ControlEquivalence::BracketListDelete(BracketList& blist, Node* to,
DFSDirection direction) {
// TODO(mstarzinger): Optimize this to avoid linear search.
for (BracketList::iterator i = blist.begin(); i != blist.end(); /*nop*/) {
if (i->to == to && i->direction != direction) {
TRACE(" BList erased: {%d->%d}\n", i->from->id(), i->to->id());
i = blist.erase(i);
} else {
++i;
}
}
}
void ControlEquivalence::BracketListTRACE(BracketList& blist) {
if (FLAG_trace_turbo_ceq) {
TRACE(" BList: ");
for (Bracket bracket : blist) {
TRACE("{%d->%d} ", bracket.from->id(), bracket.to->id());
}
TRACE("\n");
}
}
} // namespace compiler
} // namespace internal
} // namespace v8
...@@ -7,18 +7,12 @@ ...@@ -7,18 +7,12 @@
#include "src/compiler/graph.h" #include "src/compiler/graph.h"
#include "src/compiler/node.h" #include "src/compiler/node.h"
#include "src/compiler/node-properties.h"
#include "src/zone-containers.h" #include "src/zone-containers.h"
namespace v8 { namespace v8 {
namespace internal { namespace internal {
namespace compiler { namespace compiler {
#define TRACE(...) \
do { \
if (FLAG_trace_turbo_scheduler) PrintF(__VA_ARGS__); \
} while (false)
// Determines control dependence equivalence classes for control nodes. Any two // Determines control dependence equivalence classes for control nodes. Any two
// nodes having the same set of control dependences land in one class. These // nodes having the same set of control dependences land in one class. These
// classes can in turn be used to: // classes can in turn be used to:
...@@ -34,7 +28,7 @@ namespace compiler { ...@@ -34,7 +28,7 @@ namespace compiler {
// control regions in linear time" by Johnson, Pearson & Pingali (PLDI94) which // control regions in linear time" by Johnson, Pearson & Pingali (PLDI94) which
// also contains proofs for the aforementioned equivalence. References to line // also contains proofs for the aforementioned equivalence. References to line
// numbers in the algorithm from figure 4 have been added [line:x]. // numbers in the algorithm from figure 4 have been added [line:x].
class ControlEquivalence : public ZoneObject { class ControlEquivalence FINAL : public ZoneObject {
public: public:
ControlEquivalence(Zone* zone, Graph* graph) ControlEquivalence(Zone* zone, Graph* graph)
: zone_(zone), : zone_(zone),
...@@ -49,15 +43,11 @@ class ControlEquivalence : public ZoneObject { ...@@ -49,15 +43,11 @@ class ControlEquivalence : public ZoneObject {
// participate in the next step. Takes O(E) time and O(N) space. // participate in the next step. Takes O(E) time and O(N) space.
// 2) An undirected depth-first backwards traversal that determines class // 2) An undirected depth-first backwards traversal that determines class
// numbers for all participating nodes. Takes O(E) time and O(N) space. // numbers for all participating nodes. Takes O(E) time and O(N) space.
void Run(Node* exit) { void Run(Node* exit);
if (GetClass(exit) != kInvalidClass) return;
DetermineParticipation(exit);
RunUndirectedDFS(exit);
}
// Retrieves a previously computed class number. // Retrieves a previously computed class number.
size_t ClassOf(Node* node) { size_t ClassOf(Node* node) {
DCHECK(GetClass(node) != kInvalidClass); DCHECK_NE(kInvalidClass, GetClass(node));
return GetClass(node); return GetClass(node);
} }
...@@ -100,65 +90,16 @@ class ControlEquivalence : public ZoneObject { ...@@ -100,65 +90,16 @@ class ControlEquivalence : public ZoneObject {
typedef ZoneVector<NodeData> Data; typedef ZoneVector<NodeData> Data;
// Called at pre-visit during DFS walk. // Called at pre-visit during DFS walk.
void VisitPre(Node* node) { void VisitPre(Node* node);
TRACE("CEQ: Pre-visit of #%d:%s\n", node->id(), node->op()->mnemonic());
// Dispense a new pre-order number.
SetNumber(node, NewDFSNumber());
TRACE(" Assigned DFS number is %zu\n", GetNumber(node));
}
// Called at mid-visit during DFS walk. // Called at mid-visit during DFS walk.
void VisitMid(Node* node, DFSDirection direction) { void VisitMid(Node* node, DFSDirection direction);
TRACE("CEQ: Mid-visit of #%d:%s\n", node->id(), node->op()->mnemonic());
BracketList& blist = GetBracketList(node);
// Remove brackets pointing to this node [line:19].
BracketListDelete(blist, node, direction);
// Potentially introduce artificial dependency from start to end.
if (blist.empty()) {
DCHECK_EQ(kInputDirection, direction);
VisitBackedge(node, graph_->end(), kInputDirection);
}
// Potentially start a new equivalence class [line:37].
BracketListTRACE(blist);
Bracket* recent = &blist.back();
if (recent->recent_size != blist.size()) {
recent->recent_size = blist.size();
recent->recent_class = NewClassNumber();
}
// Assign equivalence class to node.
SetClass(node, recent->recent_class);
TRACE(" Assigned class number is %zu\n", GetClass(node));
}
// Called at post-visit during DFS walk. // Called at post-visit during DFS walk.
void VisitPost(Node* node, Node* parent_node, DFSDirection direction) { void VisitPost(Node* node, Node* parent_node, DFSDirection direction);
TRACE("CEQ: Post-visit of #%d:%s\n", node->id(), node->op()->mnemonic());
BracketList& blist = GetBracketList(node);
// Remove brackets pointing to this node [line:19].
BracketListDelete(blist, node, direction);
// Propagate bracket list up the DFS tree [line:13].
if (parent_node != NULL) {
BracketList& parent_blist = GetBracketList(parent_node);
parent_blist.splice(parent_blist.end(), blist);
}
}
// Called when hitting a back edge in the DFS walk. // Called when hitting a back edge in the DFS walk.
void VisitBackedge(Node* from, Node* to, DFSDirection direction) { void VisitBackedge(Node* from, Node* to, DFSDirection direction);
TRACE("CEQ: Backedge from #%d:%s to #%d:%s\n", from->id(),
from->op()->mnemonic(), to->id(), to->op()->mnemonic());
// Push backedge onto the bracket list [line:25].
Bracket bracket = {direction, kInvalidClass, 0, from, to};
GetBracketList(from).push_back(bracket);
}
// Performs and undirected DFS walk of the graph. Conceptually all nodes are // Performs and undirected DFS walk of the graph. Conceptually all nodes are
// expanded, splitting "input" and "use" out into separate nodes. During the // expanded, splitting "input" and "use" out into separate nodes. During the
...@@ -174,102 +115,10 @@ class ControlEquivalence : public ZoneObject { ...@@ -174,102 +115,10 @@ class ControlEquivalence : public ZoneObject {
// //
// This will yield a true spanning tree (without cross or forward edges) and // This will yield a true spanning tree (without cross or forward edges) and
// also discover proper back edges in both directions. // also discover proper back edges in both directions.
void RunUndirectedDFS(Node* exit) { void RunUndirectedDFS(Node* exit);
ZoneStack<DFSStackEntry> stack(zone_);
DFSPush(stack, exit, NULL, kInputDirection);
VisitPre(exit);
while (!stack.empty()) { // Undirected depth-first backwards traversal. void DetermineParticipationEnqueue(ZoneQueue<Node*>& queue, Node* node);
DFSStackEntry& entry = stack.top(); void DetermineParticipation(Node* exit);
Node* node = entry.node;
if (entry.direction == kInputDirection) {
if (entry.input != node->input_edges().end()) {
Edge edge = *entry.input;
Node* input = edge.to();
++(entry.input);
if (NodeProperties::IsControlEdge(edge)) {
// Visit next control input.
if (!GetData(input)->participates) continue;
if (GetData(input)->visited) continue;
if (GetData(input)->on_stack) {
// Found backedge if input is on stack.
if (input != entry.parent_node) {
VisitBackedge(node, input, kInputDirection);
}
} else {
// Push input onto stack.
DFSPush(stack, input, node, kInputDirection);
VisitPre(input);
}
}
continue;
}
if (entry.use != node->use_edges().end()) {
// Switch direction to uses.
entry.direction = kUseDirection;
VisitMid(node, kInputDirection);
continue;
}
}
if (entry.direction == kUseDirection) {
if (entry.use != node->use_edges().end()) {
Edge edge = *entry.use;
Node* use = edge.from();
++(entry.use);
if (NodeProperties::IsControlEdge(edge)) {
// Visit next control use.
if (!GetData(use)->participates) continue;
if (GetData(use)->visited) continue;
if (GetData(use)->on_stack) {
// Found backedge if use is on stack.
if (use != entry.parent_node) {
VisitBackedge(node, use, kUseDirection);
}
} else {
// Push use onto stack.
DFSPush(stack, use, node, kUseDirection);
VisitPre(use);
}
}
continue;
}
if (entry.input != node->input_edges().end()) {
// Switch direction to inputs.
entry.direction = kInputDirection;
VisitMid(node, kUseDirection);
continue;
}
}
// Pop node from stack when done with all inputs and uses.
DCHECK(entry.input == node->input_edges().end());
DCHECK(entry.use == node->use_edges().end());
DFSPop(stack, node);
VisitPost(node, entry.parent_node, entry.direction);
}
}
void DetermineParticipationEnqueue(ZoneQueue<Node*>& queue, Node* node) {
if (!GetData(node)->participates) {
GetData(node)->participates = true;
queue.push(node);
}
}
void DetermineParticipation(Node* exit) {
ZoneQueue<Node*> queue(zone_);
DetermineParticipationEnqueue(queue, exit);
while (!queue.empty()) { // Breadth-first backwards traversal.
Node* node = queue.front();
queue.pop();
int max = NodeProperties::PastControlIndex(node);
for (int i = NodeProperties::FirstControlIndex(node); i < max; i++) {
DetermineParticipationEnqueue(queue, node->InputAt(i));
}
}
}
private: private:
NodeData* GetData(Node* node) { return &node_data_[node->id()]; } NodeData* GetData(Node* node) { return &node_data_[node->id()]; }
...@@ -300,54 +149,21 @@ class ControlEquivalence : public ZoneObject { ...@@ -300,54 +149,21 @@ class ControlEquivalence : public ZoneObject {
} }
// Mutates the DFS stack by pushing an entry. // Mutates the DFS stack by pushing an entry.
void DFSPush(DFSStack& stack, Node* node, Node* from, DFSDirection dir) { void DFSPush(DFSStack& stack, Node* node, Node* from, DFSDirection dir);
DCHECK(GetData(node)->participates);
DCHECK(!GetData(node)->visited);
GetData(node)->on_stack = true;
Node::InputEdges::iterator input = node->input_edges().begin();
Node::UseEdges::iterator use = node->use_edges().begin();
stack.push({dir, input, use, from, node});
}
// Mutates the DFS stack by popping an entry. // Mutates the DFS stack by popping an entry.
void DFSPop(DFSStack& stack, Node* node) { void DFSPop(DFSStack& stack, Node* node);
DCHECK_EQ(stack.top().node, node);
GetData(node)->on_stack = false;
GetData(node)->visited = true;
stack.pop();
}
// TODO(mstarzinger): Optimize this to avoid linear search. void BracketListDelete(BracketList& blist, Node* to, DFSDirection direction);
void BracketListDelete(BracketList& blist, Node* to, DFSDirection direction) { void BracketListTRACE(BracketList& blist);
for (BracketList::iterator i = blist.begin(); i != blist.end(); /*nop*/) {
if (i->to == to && i->direction != direction) {
TRACE(" BList erased: {%d->%d}\n", i->from->id(), i->to->id());
i = blist.erase(i);
} else {
++i;
}
}
}
void BracketListTRACE(BracketList& blist) { Zone* const zone_;
if (FLAG_trace_turbo_scheduler) { Graph* const graph_;
TRACE(" BList: ");
for (Bracket bracket : blist) {
TRACE("{%d->%d} ", bracket.from->id(), bracket.to->id());
}
TRACE("\n");
}
}
Zone* zone_;
Graph* graph_;
int dfs_number_; // Generates new DFS pre-order numbers on demand. int dfs_number_; // Generates new DFS pre-order numbers on demand.
int class_number_; // Generates new equivalence class numbers on demand. int class_number_; // Generates new equivalence class numbers on demand.
Data node_data_; // Per-node data stored as a side-table. Data node_data_; // Per-node data stored as a side-table.
}; };
#undef TRACE
} // namespace compiler } // namespace compiler
} // namespace internal } // namespace internal
} // namespace v8 } // namespace v8
......
...@@ -390,6 +390,7 @@ DEFINE_BOOL(trace_turbo_types, true, "trace TurboFan's types") ...@@ -390,6 +390,7 @@ DEFINE_BOOL(trace_turbo_types, true, "trace TurboFan's types")
DEFINE_BOOL(trace_turbo_scheduler, false, "trace TurboFan's scheduler") DEFINE_BOOL(trace_turbo_scheduler, false, "trace TurboFan's scheduler")
DEFINE_BOOL(trace_turbo_reduction, false, "trace TurboFan's various reducers") DEFINE_BOOL(trace_turbo_reduction, false, "trace TurboFan's various reducers")
DEFINE_BOOL(trace_turbo_jt, false, "trace TurboFan's jump threading") DEFINE_BOOL(trace_turbo_jt, false, "trace TurboFan's jump threading")
DEFINE_BOOL(trace_turbo_ceq, false, "trace TurboFan's control equivalence")
DEFINE_BOOL(turbo_asm, true, "enable TurboFan for asm.js code") DEFINE_BOOL(turbo_asm, true, "enable TurboFan for asm.js code")
DEFINE_BOOL(turbo_verify, DEBUG_BOOL, "verify TurboFan graphs at each phase") DEFINE_BOOL(turbo_verify, DEBUG_BOOL, "verify TurboFan graphs at each phase")
DEFINE_BOOL(turbo_stats, false, "print TurboFan statistics") DEFINE_BOOL(turbo_stats, false, "print TurboFan statistics")
......
...@@ -447,6 +447,7 @@ ...@@ -447,6 +447,7 @@
'../../src/compiler/common-operator.h', '../../src/compiler/common-operator.h',
'../../src/compiler/control-builders.cc', '../../src/compiler/control-builders.cc',
'../../src/compiler/control-builders.h', '../../src/compiler/control-builders.h',
'../../src/compiler/control-equivalence.cc',
'../../src/compiler/control-equivalence.h', '../../src/compiler/control-equivalence.h',
'../../src/compiler/control-flow-optimizer.cc', '../../src/compiler/control-flow-optimizer.cc',
'../../src/compiler/control-flow-optimizer.h', '../../src/compiler/control-flow-optimizer.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