43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2011, 2011, 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.graph;
|
|
24 |
|
|
25 |
import java.util.Arrays;
|
47084
|
26 |
import java.util.ConcurrentModificationException;
|
43972
|
27 |
import java.util.Iterator;
|
47084
|
28 |
import java.util.NoSuchElementException;
|
43972
|
29 |
|
|
30 |
import org.graalvm.compiler.graph.iterators.NodeIterable;
|
|
31 |
|
47084
|
32 |
public final class NodeBitMap extends NodeIdAccessor implements NodeIterable<Node> {
|
43972
|
33 |
private static final int SHIFT = 6;
|
|
34 |
|
|
35 |
private long[] bits;
|
|
36 |
private int nodeCount;
|
|
37 |
private int counter;
|
|
38 |
|
|
39 |
public NodeBitMap(Graph graph) {
|
47084
|
40 |
super(graph);
|
43972
|
41 |
this.nodeCount = graph.nodeIdCount();
|
|
42 |
this.bits = new long[sizeForNodeCount(nodeCount)];
|
|
43 |
}
|
|
44 |
|
|
45 |
private static int sizeForNodeCount(int nodeCount) {
|
|
46 |
return (nodeCount + Long.SIZE - 1) >> SHIFT;
|
|
47 |
}
|
|
48 |
|
|
49 |
public int getCounter() {
|
|
50 |
return counter;
|
|
51 |
}
|
|
52 |
|
|
53 |
private NodeBitMap(NodeBitMap other) {
|
47084
|
54 |
super(other.graph);
|
43972
|
55 |
this.bits = other.bits.clone();
|
|
56 |
this.nodeCount = other.nodeCount;
|
|
57 |
}
|
|
58 |
|
|
59 |
public Graph graph() {
|
|
60 |
return graph;
|
|
61 |
}
|
|
62 |
|
|
63 |
public boolean isNew(Node node) {
|
47084
|
64 |
return getNodeId(node) >= nodeCount;
|
43972
|
65 |
}
|
|
66 |
|
|
67 |
public boolean isMarked(Node node) {
|
|
68 |
assert check(node, false);
|
47084
|
69 |
return isMarked(getNodeId(node));
|
43972
|
70 |
}
|
|
71 |
|
|
72 |
public boolean checkAndMarkInc(Node node) {
|
|
73 |
if (!isMarked(node)) {
|
|
74 |
this.counter++;
|
|
75 |
this.mark(node);
|
|
76 |
return true;
|
|
77 |
} else {
|
|
78 |
return false;
|
|
79 |
}
|
|
80 |
}
|
|
81 |
|
|
82 |
public boolean isMarked(int id) {
|
|
83 |
return (bits[id >> SHIFT] & (1L << id)) != 0;
|
|
84 |
}
|
|
85 |
|
|
86 |
public boolean isMarkedAndGrow(Node node) {
|
|
87 |
assert check(node, true);
|
47084
|
88 |
int id = getNodeId(node);
|
43972
|
89 |
checkGrow(id);
|
|
90 |
return isMarked(id);
|
|
91 |
}
|
|
92 |
|
|
93 |
public void mark(Node node) {
|
|
94 |
assert check(node, false);
|
47084
|
95 |
int id = getNodeId(node);
|
43972
|
96 |
bits[id >> SHIFT] |= (1L << id);
|
|
97 |
}
|
|
98 |
|
|
99 |
public void markAndGrow(Node node) {
|
|
100 |
assert check(node, true);
|
47084
|
101 |
int id = getNodeId(node);
|
43972
|
102 |
checkGrow(id);
|
|
103 |
bits[id >> SHIFT] |= (1L << id);
|
|
104 |
}
|
|
105 |
|
|
106 |
public void clear(Node node) {
|
|
107 |
assert check(node, false);
|
47084
|
108 |
int id = getNodeId(node);
|
43972
|
109 |
bits[id >> SHIFT] &= ~(1L << id);
|
|
110 |
}
|
|
111 |
|
|
112 |
public void clearAndGrow(Node node) {
|
|
113 |
assert check(node, true);
|
47084
|
114 |
int id = getNodeId(node);
|
43972
|
115 |
checkGrow(id);
|
|
116 |
bits[id >> SHIFT] &= ~(1L << id);
|
|
117 |
}
|
|
118 |
|
|
119 |
private void checkGrow(int id) {
|
|
120 |
if (id >= nodeCount) {
|
|
121 |
if ((id >> SHIFT) >= bits.length) {
|
|
122 |
grow();
|
|
123 |
} else {
|
|
124 |
nodeCount = id + 1;
|
|
125 |
}
|
|
126 |
}
|
|
127 |
}
|
|
128 |
|
|
129 |
public void clearAll() {
|
|
130 |
Arrays.fill(bits, 0);
|
|
131 |
}
|
|
132 |
|
|
133 |
public void intersect(NodeBitMap other) {
|
|
134 |
assert graph() == other.graph();
|
|
135 |
int commonLength = Math.min(bits.length, other.bits.length);
|
|
136 |
for (int i = commonLength; i < bits.length; i++) {
|
|
137 |
bits[i] = 0;
|
|
138 |
}
|
|
139 |
for (int i = 0; i < commonLength; i++) {
|
|
140 |
bits[i] &= other.bits[i];
|
|
141 |
}
|
|
142 |
}
|
|
143 |
|
|
144 |
public void subtract(NodeBitMap other) {
|
|
145 |
assert graph() == other.graph();
|
|
146 |
int commonLength = Math.min(bits.length, other.bits.length);
|
|
147 |
for (int i = 0; i < commonLength; i++) {
|
|
148 |
bits[i] &= ~other.bits[i];
|
|
149 |
}
|
|
150 |
}
|
|
151 |
|
|
152 |
public void union(NodeBitMap other) {
|
|
153 |
assert graph() == other.graph();
|
|
154 |
grow();
|
|
155 |
if (bits.length < other.bits.length) {
|
|
156 |
bits = Arrays.copyOf(bits, other.bits.length);
|
|
157 |
}
|
46371
|
158 |
for (int i = 0; i < Math.min(bits.length, other.bits.length); i++) {
|
43972
|
159 |
bits[i] |= other.bits[i];
|
|
160 |
}
|
|
161 |
}
|
|
162 |
|
48861
|
163 |
public void invert() {
|
|
164 |
for (int i = 0; i < bits.length; i++) {
|
|
165 |
bits[i] = ~bits[i];
|
|
166 |
}
|
|
167 |
}
|
|
168 |
|
43972
|
169 |
public void grow() {
|
|
170 |
nodeCount = Math.max(nodeCount, graph().nodeIdCount());
|
|
171 |
int newLength = sizeForNodeCount(nodeCount);
|
|
172 |
if (newLength > bits.length) {
|
|
173 |
newLength = Math.max(newLength, (bits.length * 3 / 2) + 1);
|
|
174 |
bits = Arrays.copyOf(bits, newLength);
|
|
175 |
}
|
|
176 |
}
|
|
177 |
|
|
178 |
private boolean check(Node node, boolean grow) {
|
|
179 |
assert node.graph() == graph() : "this node is not part of the graph: " + node;
|
|
180 |
assert grow || !isNew(node) : "node was added to the graph after creating the node bitmap: " + node;
|
|
181 |
assert node.isAlive() : "node is deleted!" + node;
|
|
182 |
return true;
|
|
183 |
}
|
|
184 |
|
|
185 |
public <T extends Node> void markAll(Iterable<T> nodes) {
|
|
186 |
for (Node node : nodes) {
|
|
187 |
mark(node);
|
|
188 |
}
|
|
189 |
}
|
|
190 |
|
47084
|
191 |
protected Node nextMarkedNode(int fromNodeId) {
|
46371
|
192 |
assert fromNodeId >= 0;
|
|
193 |
int wordIndex = fromNodeId >> SHIFT;
|
|
194 |
int wordsInUse = bits.length;
|
|
195 |
if (wordIndex < wordsInUse) {
|
47084
|
196 |
long word = getPartOfWord(bits[wordIndex], fromNodeId);
|
46371
|
197 |
while (true) {
|
47084
|
198 |
while (word != 0) {
|
|
199 |
int bitIndex = Long.numberOfTrailingZeros(word);
|
|
200 |
int nodeId = wordIndex * Long.SIZE + bitIndex;
|
|
201 |
Node result = graph.getNode(nodeId);
|
|
202 |
if (result == null) {
|
|
203 |
// node was deleted -> clear the bit and continue searching
|
47667
|
204 |
bits[wordIndex] = bits[wordIndex] & ~(1L << bitIndex);
|
47084
|
205 |
int nextNodeId = nodeId + 1;
|
|
206 |
if ((nextNodeId & (Long.SIZE - 1)) == 0) {
|
|
207 |
// we reached the end of this word
|
|
208 |
break;
|
|
209 |
} else {
|
|
210 |
word = getPartOfWord(word, nextNodeId);
|
|
211 |
}
|
|
212 |
} else {
|
|
213 |
return result;
|
|
214 |
}
|
46371
|
215 |
}
|
|
216 |
if (++wordIndex == wordsInUse) {
|
|
217 |
break;
|
|
218 |
}
|
|
219 |
word = bits[wordIndex];
|
|
220 |
}
|
|
221 |
}
|
47084
|
222 |
return null;
|
|
223 |
}
|
|
224 |
|
|
225 |
private static long getPartOfWord(long word, int firstNodeIdToInclude) {
|
|
226 |
return word & (0xFFFFFFFFFFFFFFFFL << firstNodeIdToInclude);
|
46371
|
227 |
}
|
43972
|
228 |
|
47084
|
229 |
/**
|
|
230 |
* This iterator only returns nodes that are marked in the {@link NodeBitMap} and are alive in
|
|
231 |
* the corresponding {@link Graph}.
|
|
232 |
*/
|
46371
|
233 |
private class MarkedNodeIterator implements Iterator<Node> {
|
47084
|
234 |
private int currentNodeId;
|
|
235 |
private Node currentNode;
|
43972
|
236 |
|
46371
|
237 |
MarkedNodeIterator() {
|
47084
|
238 |
currentNodeId = -1;
|
43972
|
239 |
forward();
|
|
240 |
}
|
|
241 |
|
|
242 |
private void forward() {
|
47084
|
243 |
assert currentNode == null;
|
|
244 |
currentNode = NodeBitMap.this.nextMarkedNode(currentNodeId + 1);
|
|
245 |
if (currentNode != null) {
|
|
246 |
assert currentNode.isAlive();
|
|
247 |
currentNodeId = getNodeId(currentNode);
|
|
248 |
} else {
|
|
249 |
currentNodeId = -1;
|
|
250 |
}
|
43972
|
251 |
}
|
|
252 |
|
|
253 |
@Override
|
|
254 |
public boolean hasNext() {
|
47084
|
255 |
if (currentNode == null && currentNodeId >= 0) {
|
|
256 |
forward();
|
|
257 |
}
|
|
258 |
return currentNodeId >= 0;
|
43972
|
259 |
}
|
|
260 |
|
|
261 |
@Override
|
|
262 |
public Node next() {
|
47084
|
263 |
if (!hasNext()) {
|
|
264 |
throw new NoSuchElementException();
|
|
265 |
}
|
|
266 |
if (!currentNode.isAlive()) {
|
|
267 |
throw new ConcurrentModificationException("NodeBitMap was modified between the calls to hasNext() and next()");
|
|
268 |
}
|
|
269 |
|
|
270 |
Node result = currentNode;
|
|
271 |
currentNode = null;
|
46371
|
272 |
return result;
|
43972
|
273 |
}
|
|
274 |
|
|
275 |
@Override
|
|
276 |
public void remove() {
|
|
277 |
throw new UnsupportedOperationException();
|
|
278 |
}
|
|
279 |
|
|
280 |
}
|
|
281 |
|
|
282 |
@Override
|
|
283 |
public Iterator<Node> iterator() {
|
46371
|
284 |
return new MarkedNodeIterator();
|
43972
|
285 |
}
|
|
286 |
|
|
287 |
public NodeBitMap copy() {
|
|
288 |
return new NodeBitMap(this);
|
|
289 |
}
|
|
290 |
|
|
291 |
@Override
|
|
292 |
public int count() {
|
|
293 |
int count = 0;
|
|
294 |
for (long l : bits) {
|
|
295 |
count += Long.bitCount(l);
|
|
296 |
}
|
|
297 |
return count;
|
|
298 |
}
|
|
299 |
|
|
300 |
@Override
|
|
301 |
public boolean contains(Node node) {
|
|
302 |
return isMarked(node);
|
|
303 |
}
|
|
304 |
|
|
305 |
@Override
|
|
306 |
public String toString() {
|
|
307 |
return snapshot().toString();
|
|
308 |
}
|
|
309 |
}
|