43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
package org.graalvm.compiler.phases.graph;
|
|
24 |
|
|
25 |
import java.util.ArrayDeque;
|
|
26 |
import java.util.ArrayList;
|
|
27 |
import java.util.Deque;
|
|
28 |
import java.util.Iterator;
|
|
29 |
import java.util.List;
|
|
30 |
|
48861
|
31 |
import org.graalvm.collections.EconomicMap;
|
|
32 |
import org.graalvm.collections.Equivalence;
|
|
33 |
import org.graalvm.collections.MapCursor;
|
43972
|
34 |
import org.graalvm.compiler.graph.Node;
|
|
35 |
import org.graalvm.compiler.nodes.AbstractBeginNode;
|
|
36 |
import org.graalvm.compiler.nodes.AbstractEndNode;
|
|
37 |
import org.graalvm.compiler.nodes.AbstractMergeNode;
|
|
38 |
import org.graalvm.compiler.nodes.EndNode;
|
|
39 |
import org.graalvm.compiler.nodes.FixedNode;
|
|
40 |
import org.graalvm.compiler.nodes.FixedWithNextNode;
|
|
41 |
import org.graalvm.compiler.nodes.LoopBeginNode;
|
|
42 |
import org.graalvm.compiler.nodes.LoopEndNode;
|
|
43 |
import org.graalvm.compiler.nodes.LoopExitNode;
|
|
44 |
|
|
45 |
public final class ReentrantNodeIterator {
|
|
46 |
|
|
47 |
public static class LoopInfo<StateT> {
|
|
48 |
|
46344
|
49 |
public final EconomicMap<LoopEndNode, StateT> endStates;
|
|
50 |
public final EconomicMap<LoopExitNode, StateT> exitStates;
|
43972
|
51 |
|
|
52 |
public LoopInfo(int endCount, int exitCount) {
|
46344
|
53 |
endStates = EconomicMap.create(Equivalence.IDENTITY, endCount);
|
|
54 |
exitStates = EconomicMap.create(Equivalence.IDENTITY, exitCount);
|
43972
|
55 |
}
|
|
56 |
}
|
|
57 |
|
|
58 |
public abstract static class NodeIteratorClosure<StateT> {
|
|
59 |
|
|
60 |
protected abstract StateT processNode(FixedNode node, StateT currentState);
|
|
61 |
|
|
62 |
protected abstract StateT merge(AbstractMergeNode merge, List<StateT> states);
|
|
63 |
|
|
64 |
protected abstract StateT afterSplit(AbstractBeginNode node, StateT oldState);
|
|
65 |
|
46344
|
66 |
protected abstract EconomicMap<LoopExitNode, StateT> processLoop(LoopBeginNode loop, StateT initialState);
|
43972
|
67 |
|
|
68 |
/**
|
|
69 |
* Determine whether iteration should continue in the current state.
|
|
70 |
*
|
|
71 |
* @param currentState
|
|
72 |
*/
|
|
73 |
protected boolean continueIteration(StateT currentState) {
|
|
74 |
return true;
|
|
75 |
}
|
|
76 |
}
|
|
77 |
|
|
78 |
private ReentrantNodeIterator() {
|
|
79 |
// no instances allowed
|
|
80 |
}
|
|
81 |
|
|
82 |
public static <StateT> LoopInfo<StateT> processLoop(NodeIteratorClosure<StateT> closure, LoopBeginNode loop, StateT initialState) {
|
46344
|
83 |
EconomicMap<FixedNode, StateT> blockEndStates = apply(closure, loop, initialState, loop);
|
43972
|
84 |
|
|
85 |
LoopInfo<StateT> info = new LoopInfo<>(loop.loopEnds().count(), loop.loopExits().count());
|
|
86 |
for (LoopEndNode end : loop.loopEnds()) {
|
|
87 |
if (blockEndStates.containsKey(end)) {
|
|
88 |
info.endStates.put(end, blockEndStates.get(end));
|
|
89 |
}
|
|
90 |
}
|
|
91 |
for (LoopExitNode exit : loop.loopExits()) {
|
|
92 |
if (blockEndStates.containsKey(exit)) {
|
|
93 |
info.exitStates.put(exit, blockEndStates.get(exit));
|
|
94 |
}
|
|
95 |
}
|
|
96 |
return info;
|
|
97 |
}
|
|
98 |
|
|
99 |
public static <StateT> void apply(NodeIteratorClosure<StateT> closure, FixedNode start, StateT initialState) {
|
|
100 |
apply(closure, start, initialState, null);
|
|
101 |
}
|
|
102 |
|
46344
|
103 |
private static <StateT> EconomicMap<FixedNode, StateT> apply(NodeIteratorClosure<StateT> closure, FixedNode start, StateT initialState, LoopBeginNode boundary) {
|
43972
|
104 |
assert start != null;
|
|
105 |
Deque<AbstractBeginNode> nodeQueue = new ArrayDeque<>();
|
46344
|
106 |
EconomicMap<FixedNode, StateT> blockEndStates = EconomicMap.create(Equivalence.IDENTITY);
|
43972
|
107 |
|
|
108 |
StateT state = initialState;
|
|
109 |
FixedNode current = start;
|
|
110 |
do {
|
|
111 |
while (current instanceof FixedWithNextNode) {
|
|
112 |
if (boundary != null && current instanceof LoopExitNode && ((LoopExitNode) current).loopBegin() == boundary) {
|
|
113 |
blockEndStates.put(current, state);
|
|
114 |
current = null;
|
|
115 |
} else {
|
|
116 |
FixedNode next = ((FixedWithNextNode) current).next();
|
|
117 |
state = closure.processNode(current, state);
|
|
118 |
current = closure.continueIteration(state) ? next : null;
|
|
119 |
}
|
|
120 |
}
|
|
121 |
|
|
122 |
if (current != null) {
|
|
123 |
state = closure.processNode(current, state);
|
|
124 |
|
|
125 |
if (closure.continueIteration(state)) {
|
|
126 |
Iterator<Node> successors = current.successors().iterator();
|
|
127 |
if (!successors.hasNext()) {
|
|
128 |
if (current instanceof LoopEndNode) {
|
|
129 |
blockEndStates.put(current, state);
|
|
130 |
} else if (current instanceof EndNode) {
|
|
131 |
// add the end node and see if the merge is ready for processing
|
|
132 |
AbstractMergeNode merge = ((EndNode) current).merge();
|
|
133 |
if (merge instanceof LoopBeginNode) {
|
46344
|
134 |
EconomicMap<LoopExitNode, StateT> loopExitState = closure.processLoop((LoopBeginNode) merge, state);
|
|
135 |
MapCursor<LoopExitNode, StateT> entry = loopExitState.getEntries();
|
|
136 |
while (entry.advance()) {
|
43972
|
137 |
blockEndStates.put(entry.getKey(), entry.getValue());
|
|
138 |
nodeQueue.add(entry.getKey());
|
|
139 |
}
|
|
140 |
} else {
|
|
141 |
boolean endsVisited = true;
|
|
142 |
for (AbstractEndNode forwardEnd : merge.forwardEnds()) {
|
|
143 |
if (forwardEnd != current && !blockEndStates.containsKey(forwardEnd)) {
|
|
144 |
endsVisited = false;
|
|
145 |
break;
|
|
146 |
}
|
|
147 |
}
|
|
148 |
if (endsVisited) {
|
|
149 |
ArrayList<StateT> states = new ArrayList<>(merge.forwardEndCount());
|
|
150 |
for (int i = 0; i < merge.forwardEndCount(); i++) {
|
|
151 |
AbstractEndNode forwardEnd = merge.forwardEndAt(i);
|
|
152 |
assert forwardEnd == current || blockEndStates.containsKey(forwardEnd);
|
46344
|
153 |
StateT other = forwardEnd == current ? state : blockEndStates.removeKey(forwardEnd);
|
43972
|
154 |
states.add(other);
|
|
155 |
}
|
|
156 |
state = closure.merge(merge, states);
|
|
157 |
current = closure.continueIteration(state) ? merge : null;
|
|
158 |
continue;
|
|
159 |
} else {
|
|
160 |
assert !blockEndStates.containsKey(current);
|
|
161 |
blockEndStates.put(current, state);
|
|
162 |
}
|
|
163 |
}
|
|
164 |
}
|
|
165 |
} else {
|
|
166 |
FixedNode firstSuccessor = (FixedNode) successors.next();
|
|
167 |
if (!successors.hasNext()) {
|
|
168 |
current = firstSuccessor;
|
|
169 |
continue;
|
|
170 |
} else {
|
|
171 |
do {
|
|
172 |
AbstractBeginNode successor = (AbstractBeginNode) successors.next();
|
|
173 |
StateT successorState = closure.afterSplit(successor, state);
|
|
174 |
if (closure.continueIteration(successorState)) {
|
|
175 |
blockEndStates.put(successor, successorState);
|
|
176 |
nodeQueue.add(successor);
|
|
177 |
}
|
|
178 |
} while (successors.hasNext());
|
|
179 |
|
|
180 |
state = closure.afterSplit((AbstractBeginNode) firstSuccessor, state);
|
|
181 |
current = closure.continueIteration(state) ? firstSuccessor : null;
|
|
182 |
continue;
|
|
183 |
}
|
|
184 |
}
|
|
185 |
}
|
|
186 |
}
|
|
187 |
|
|
188 |
// get next queued block
|
|
189 |
if (nodeQueue.isEmpty()) {
|
|
190 |
return blockEndStates;
|
|
191 |
} else {
|
|
192 |
current = nodeQueue.removeFirst();
|
|
193 |
assert blockEndStates.containsKey(current);
|
46344
|
194 |
state = blockEndStates.removeKey(current);
|
43972
|
195 |
assert !(current instanceof AbstractMergeNode) && current instanceof AbstractBeginNode;
|
|
196 |
}
|
|
197 |
} while (true);
|
|
198 |
}
|
|
199 |
}
|