--- /dev/null 2017-01-22 10:16:57.869617664 -0800 +++ new/src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.phases/src/org/graalvm/compiler/phases/graph/SinglePassNodeIterator.java 2017-02-15 17:08:28.762453605 -0800 @@ -0,0 +1,395 @@ +/* + * Copyright (c) 2011, 2011, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package org.graalvm.compiler.phases.graph; + +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Deque; +import java.util.List; +import java.util.Map; + +import org.graalvm.compiler.graph.Node; +import org.graalvm.compiler.graph.NodeBitMap; +import org.graalvm.compiler.nodes.AbstractBeginNode; +import org.graalvm.compiler.nodes.AbstractMergeNode; +import org.graalvm.compiler.nodes.ControlSinkNode; +import org.graalvm.compiler.nodes.ControlSplitNode; +import org.graalvm.compiler.nodes.EndNode; +import org.graalvm.compiler.nodes.FixedNode; +import org.graalvm.compiler.nodes.FixedWithNextNode; +import org.graalvm.compiler.nodes.Invoke; +import org.graalvm.compiler.nodes.InvokeWithExceptionNode; +import org.graalvm.compiler.nodes.LoopBeginNode; +import org.graalvm.compiler.nodes.LoopEndNode; +import org.graalvm.compiler.nodes.StartNode; +import org.graalvm.compiler.nodes.StructuredGraph; + +/** + * A SinglePassNodeIterator iterates the fixed nodes of the graph in post order starting from its + * start node. Unlike in iterative dataflow analysis, a single pass is performed, which allows + * keeping a smaller working set of pending {@link MergeableState}. This iteration scheme requires: + *
true
(an
+ * assertion checks this)+ * For this iterator the CFG is defined by the classical CFG nodes ( + * {@link org.graalvm.compiler.nodes.ControlSplitNode}, + * {@link org.graalvm.compiler.nodes.AbstractMergeNode} ...) and the + * {@link org.graalvm.compiler.nodes.FixedWithNextNode#next() next} pointers of + * {@link org.graalvm.compiler.nodes.FixedWithNextNode}. + *
+ * + *+ * The lifecycle that single-pass node iterators go through is described in {@link #apply()} + *
+ * + * @param+ * It's tricky to answer whether the state an entry contains is the pre-state or the post-state + * for the key in question, because states are mutable. Thus an entry may be created to contain + * a pre-state (at the time, as done for a loop-begin in {@link #apply()}) only to make it a + * post-state soon after (continuing with the loop-begin example, also in {@link #apply()}). In + * any case, given that keys are limited to the nodes mentioned in the previous paragraph, in + * all cases an entry can be considered to hold a post-state by the time such entry is + * retrieved. + *
+ * + *+ * The only method that makes this map grow is {@link #keepForLater(FixedNode, MergeableState)} + * and the only one that shrinks it is {@link #pruneEntry(FixedNode)}. To make sure no entry is + * left behind inadvertently, asserts in {@link #finished()} are in place. + *
+ */ + private final Map+ * Correspondingly each item may stand for: + *
+ * After this method has been invoked, the {@link SinglePassNodeIterator} instance can't be used + * again. This saves clearing up fields in {@link #finished()}, the assumption being that this + * instance will be garbage-collected soon afterwards. + *
+ */ + public void apply() { + FixedNode current = start; + + do { + if (current instanceof InvokeWithExceptionNode) { + invoke((Invoke) current); + queueSuccessors(current); + current = nextQueuedNode(); + } else if (current instanceof LoopBeginNode) { + state.loopBegin((LoopBeginNode) current); + keepForLater(current, state); + state = state.clone(); + loopBegin((LoopBeginNode) current); + current = ((LoopBeginNode) current).next(); + assert current != null; + } else if (current instanceof LoopEndNode) { + loopEnd((LoopEndNode) current); + finishLoopEnds((LoopEndNode) current); + current = nextQueuedNode(); + } else if (current instanceof AbstractMergeNode) { + merge((AbstractMergeNode) current); + current = ((AbstractMergeNode) current).next(); + assert current != null; + } else if (current instanceof FixedWithNextNode) { + FixedNode next = ((FixedWithNextNode) current).next(); + assert next != null : current; + node(current); + current = next; + } else if (current instanceof EndNode) { + end((EndNode) current); + queueMerge((EndNode) current); + current = nextQueuedNode(); + } else if (current instanceof ControlSinkNode) { + node(current); + current = nextQueuedNode(); + } else if (current instanceof ControlSplitNode) { + controlSplit((ControlSplitNode) current); + queueSuccessors(current); + current = nextQueuedNode(); + } else { + assert false : current; + } + } while (current != null); + finished(); + } + + /** + * Two methods enqueue items in {@link #nodeQueue}. Of them, only this method enqueues items + * with non-null state (the other method being {@link #queueMerge(EndNode)}). + * + *+ * A space optimization is made: the state is cloned for all successors except the first. Given + * that right after invoking this method, {@link #nextQueuedNode()} is invoked, that single + * non-cloned state instance is in effect "handed over" to its next owner (thus realizing an + * owner-is-mutator access protocol). + *
+ */ + private void queueSuccessors(FixedNode x) { + T startState = state; + T curState = startState; + for (Node succ : x.successors()) { + if (succ != null) { + if (curState == null) { + // the current state isn't cloned for the first successor + // conceptually, the state is handed over to it + curState = startState.clone(); + } + AbstractBeginNode begin = (AbstractBeginNode) succ; + nodeQueue.addFirst(new PathStart<>(begin, curState)); + } + } + } + + /** + * This method is invoked upon not having a (single) next {@link FixedNode} to visit. This + * method picks such next-node-to-visit from {@link #nodeQueue} and updates {@link #state} with + * the pre-state for that node. + * + *+ * Upon reaching a {@link AbstractMergeNode}, some entries are pruned from {@link #nodeStates} + * (ie, the entries associated to forward-ends for that merge-node). + *
+ */ + private FixedNode nextQueuedNode() { + if (nodeQueue.isEmpty()) { + return null; + } + PathStart+ * The entries removed by this method were inserted: + *
+ * {@link #nextQueuedNode()} is in charge of pruning entries (held by {@link #nodeStates}) for + * the forward-ends inserted by this method. + *
+ */ + private void queueMerge(EndNode end) { + assert !visitedEnds.isMarked(end); + visitedEnds.mark(end); + keepForLater(end, state); + AbstractMergeNode merge = end.merge(); + boolean endsVisited = true; + for (int i = 0; i < merge.forwardEndCount(); i++) { + if (!visitedEnds.isMarked(merge.forwardEndAt(i))) { + endsVisited = false; + break; + } + } + if (endsVisited) { + nodeQueue.add(new PathStart<>(merge, null)); + } + } + + protected abstract void node(FixedNode node); + + protected void end(EndNode endNode) { + node(endNode); + } + + protected void merge(AbstractMergeNode merge) { + node(merge); + } + + protected void loopBegin(LoopBeginNode loopBegin) { + node(loopBegin); + } + + protected void loopEnd(LoopEndNode loopEnd) { + node(loopEnd); + } + + protected void controlSplit(ControlSplitNode controlSplit) { + node(controlSplit); + } + + protected void invoke(Invoke invoke) { + node(invoke.asNode()); + } + + /** + * The lifecycle that single-pass node iterators go through is described in {@link #apply()} + * + *+ * When overriding this method don't forget to invoke this implementation, otherwise the + * assertions will be skipped. + *
+ */ + protected void finished() { + assert nodeQueue.isEmpty(); + assert nodeStates.isEmpty(); + } + + private void keepForLater(FixedNode x, T s) { + assert !nodeStates.containsKey(x); + assert (x instanceof LoopBeginNode) || (x instanceof LoopEndNode) || (x instanceof EndNode); + assert s != null; + nodeStates.put(x, s); + } + + private T pruneEntry(FixedNode x) { + T result = nodeStates.remove(x); + assert result != null; + return result; + } +}