Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(98)

Unified Diff: src/compiler/control-reducer.cc

Issue 1155683004: [turbofan] Connect loops to end via Terminate during graph building. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Also remove redundant EffectPhis now. Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/compiler/common-operator.cc ('k') | src/compiler/js-inlining.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/compiler/control-reducer.cc
diff --git a/src/compiler/control-reducer.cc b/src/compiler/control-reducer.cc
index a92b4a1708f31df8f952cb0c71424814a915365d..3fb3055a1f5b9fcb8a9760463e7988b6b4dba646 100644
--- a/src/compiler/control-reducer.cc
+++ b/src/compiler/control-reducer.cc
@@ -33,18 +33,12 @@ class ReachabilityMarker : public NodeMarker<uint8_t> {
return before & kFromEnd;
}
bool IsReachableFromEnd(Node* node) { return Get(node) & kFromEnd; }
- bool SetReachableFromStart(Node* node) {
- uint8_t before = Get(node);
- Set(node, before | kFromStart);
- return before & kFromStart;
- }
- bool IsReachableFromStart(Node* node) { return Get(node) & kFromStart; }
void Push(Node* node) { Set(node, Get(node) | kFwStack); }
void Pop(Node* node) { Set(node, Get(node) & ~kFwStack); }
bool IsOnStack(Node* node) { return Get(node) & kFwStack; }
private:
- enum Bit { kFromEnd = 1, kFromStart = 2, kFwStack = 4 };
+ enum Bit { kFromEnd = 1, kFwStack = 2 };
};
@@ -64,134 +58,11 @@ class ControlReducerImpl final : public AdvancedReducer {
CommonOperatorBuilder* common() { return jsgraph_->common(); }
Node* dead() { return jsgraph_->DeadControl(); }
- // Finish reducing the graph by trimming nodes and/or connecting NTLs.
+ // Finish reducing the graph by trimming nodes.
bool Finish() final {
- bool done = true;
- // Gather all nodes backwards-reachable from end (through inputs).
- ReachabilityMarker marked(graph());
- NodeVector nodes(zone_);
- AddNodesReachableFromRoots(marked, nodes);
-
- // Walk forward through control nodes, looking for back edges to nodes
- // that are not connected to end. Those are non-terminating loops (NTLs).
- Node* start = graph()->start();
- marked.Push(start);
- marked.SetReachableFromStart(start);
-
- // We use a stack of (Node, Node::UseEdges::iterator) pairs to avoid
- // O(n^2) traversal.
- typedef std::pair<Node*, Node::UseEdges::iterator> FwIter;
- ZoneVector<FwIter> fw_stack(zone_);
- fw_stack.push_back(FwIter(start, start->use_edges().begin()));
-
- while (!fw_stack.empty()) {
- Node* node = fw_stack.back().first;
- TRACE("ControlFw: #%d:%s\n", node->id(), node->op()->mnemonic());
- bool pop = true;
- while (fw_stack.back().second != node->use_edges().end()) {
- Edge edge = *(fw_stack.back().second);
- Node* succ = edge.from();
- if (NodeProperties::IsControlEdge(edge) &&
- succ->op()->ControlOutputCount() > 0) {
- // Only walk control edges to control nodes.
- if (marked.IsOnStack(succ) && !marked.IsReachableFromEnd(succ)) {
- // {succ} is on stack and not reachable from end.
- Node* added = ConnectNTL(succ);
- nodes.push_back(added);
- marked.SetReachableFromEnd(added);
- AddBackwardsReachableNodes(marked, nodes, nodes.size() - 1);
-
- // Reset the use iterators for the entire stack.
- for (size_t i = 0; i < fw_stack.size(); i++) {
- FwIter& iter = fw_stack[i];
- fw_stack[i] = FwIter(iter.first, iter.first->use_edges().begin());
- }
- pop = false; // restart traversing successors of this node.
- break;
- }
- if (!marked.IsReachableFromStart(succ)) {
- // {succ} is not yet reached from start.
- marked.SetReachableFromStart(succ);
- if (succ->opcode() != IrOpcode::kOsrLoopEntry) {
- // Skip OsrLoopEntry; forms a confusing irredducible loop.
- marked.Push(succ);
- fw_stack.push_back(FwIter(succ, succ->use_edges().begin()));
- pop = false; // "recurse" into successor control node.
- break;
- }
- }
- }
- ++fw_stack.back().second;
- }
- if (pop) {
- marked.Pop(node);
- fw_stack.pop_back();
- }
- }
-
- // Trim references from dead nodes to live nodes first.
- TrimNodes(marked, nodes);
-
- // Any control nodes not reachable from start are dead, even loops.
- for (size_t i = 0; i < nodes.size(); i++) {
- Node* node = nodes[i];
- if (node->op()->ControlOutputCount() > 0 &&
- !marked.IsReachableFromStart(node) &&
- node->opcode() != IrOpcode::kDead) {
- TRACE("Dead: #%d:%s\n", node->id(), node->op()->mnemonic());
- node->ReplaceUses(dead());
- done = false;
- }
- }
-
- return done;
- }
-
- // Connect {loop}, the header of a non-terminating loop, to the end node.
- Node* ConnectNTL(Node* loop) {
- TRACE("ConnectNTL: #%d:%s\n", loop->id(), loop->op()->mnemonic());
- DCHECK_EQ(IrOpcode::kLoop, loop->opcode());
-
- // Collect all loop effects.
- NodeVector effects(zone_);
- for (auto edge : loop->use_edges()) {
- DCHECK_EQ(loop, edge.to());
- DCHECK(NodeProperties::IsControlEdge(edge));
- switch (edge.from()->opcode()) {
- case IrOpcode::kPhi:
- break;
- case IrOpcode::kEffectPhi:
- effects.push_back(edge.from());
- break;
- default:
- break;
- }
- }
-
- // Compute effects for the Return.
- Node* effect = graph()->start();
- int const effects_count = static_cast<int>(effects.size());
- if (effects_count == 1) {
- effect = effects[0];
- } else if (effects_count > 1) {
- effect = graph()->NewNode(common()->EffectSet(effects_count),
- effects_count, &effects.front());
- }
-
- // Add a terminate to connect the NTL to the end.
- Node* terminate = graph()->NewNode(common()->Terminate(), effect, loop);
-
- Node* end = graph()->end();
- if (end->opcode() == IrOpcode::kDead) {
- // End is actually the dead node. Make a new end.
- end = graph()->NewNode(common()->End(1), terminate);
- graph()->SetEnd(end);
- return end;
- }
- // Append a new input to the end.
- end->AppendInput(graph()->zone(), terminate);
- end->set_op(common()->End(end->InputCount()));
- return terminate;
+ // TODO(bmeurer): Move this to the GraphReducer.
+ Trim();
+ return true;
}
void AddNodesReachableFromRoots(ReachabilityMarker& marked,
@@ -366,14 +237,6 @@ class ControlReducerImpl final : public AdvancedReducer {
if (n <= 1) return dead(); // No non-control inputs.
if (n == 2) return node->InputAt(0); // Only one non-control input.
- // Never remove an effect phi from a (potentially non-terminating) loop.
- // Otherwise, we might end up eliminating effect nodes, such as calls,
- // before the loop.
- if (node->opcode() == IrOpcode::kEffectPhi &&
- NodeProperties::GetControlInput(node)->opcode() == IrOpcode::kLoop) {
- return node;
- }
-
Node* replacement = NULL;
auto const inputs = node->inputs();
for (auto it = inputs.begin(); n > 1; --n, ++it) {
« no previous file with comments | « src/compiler/common-operator.cc ('k') | src/compiler/js-inlining.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698