Skip to content

Commit b9501f6

Browse files
tkrodriguezelkorchi
authored andcommitted
Remove sanity checking from SparseBitSet and add javadoc
(cherry picked from commit 4df4fa4)
1 parent f0335e3 commit b9501f6

File tree

3 files changed

+59
-113
lines changed

3 files changed

+59
-113
lines changed

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/lir/alloc/lsra/LinearScanLifetimeAnalysisPhase.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ protected void computeGlobalLiveSets() {
362362
// block has successors
363363
for (int j = 0; j < block.getSuccessorCount(); j++) {
364364
BasicBlock<?> successor = block.getSuccessorAt(j);
365-
scratch.or(allocator.getBlockData(successor).liveIn);
365+
scratch.addAll(allocator.getBlockData(successor).liveIn);
366366
}
367367

368368
if (!blockSets.liveOut.equals(scratch)) {
@@ -388,9 +388,9 @@ protected void computeGlobalLiveSets() {
388388
* SparseBitSet#or will call SparseBitSet#ensureSize (since the bit set
389389
* is of length 0 initially) and set sticky to false
390390
*/
391-
liveIn.or(blockSets.liveOut);
392-
liveIn.andNot(blockSets.liveKill);
393-
liveIn.or(blockSets.liveGen);
391+
liveIn.addAll(blockSets.liveOut);
392+
liveIn.removeAll(blockSets.liveKill);
393+
liveIn.addAll(blockSets.liveGen);
394394

395395
if (debug.isLogEnabled()) {
396396
debug.log("block %d: livein = %s, liveout = %s", block.getId(), liveIn, blockSets.liveOut);
@@ -429,7 +429,7 @@ protected void computeGlobalLiveSets() {
429429
}
430430
SparseBitSet bs = allocator.getBlockData(startBlock).liveIn;
431431
StringBuilder sb = new StringBuilder();
432-
for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i + 1)) {
432+
for (int i = bs.iterateValues(0); i >= 0; i = bs.iterateValues(i + 1)) {
433433
int variableNumber = allocator.getVariableNumber(i);
434434
if (variableNumber >= 0) {
435435
sb.append("v").append(variableNumber);
@@ -462,7 +462,7 @@ protected void reportFailure(int numBlocks) {
462462

463463
SparseBitSet startBlockLiveIn = allocator.getBlockData(allocator.getLIR().getControlFlowGraph().getStartBlock()).liveIn;
464464
try (Indent indent2 = debug.logAndIndent("Error: liveIn set of first block must be empty (when this fails, variables are used before they are defined):")) {
465-
for (int operandNum = startBlockLiveIn.nextSetBit(0); operandNum >= 0; operandNum = startBlockLiveIn.nextSetBit(operandNum + 1)) {
465+
for (int operandNum = startBlockLiveIn.iterateValues(0); operandNum >= 0; operandNum = startBlockLiveIn.iterateValues(operandNum + 1)) {
466466
Interval interval = allocator.intervalFor(operandNum);
467467
if (interval != null) {
468468
Value operand = interval.operand;
@@ -474,7 +474,7 @@ protected void reportFailure(int numBlocks) {
474474
}
475475

476476
// print some additional information to simplify debugging
477-
for (int operandNum = startBlockLiveIn.nextSetBit(0); operandNum >= 0; operandNum = startBlockLiveIn.nextSetBit(operandNum + 1)) {
477+
for (int operandNum = startBlockLiveIn.iterateValues(0); operandNum >= 0; operandNum = startBlockLiveIn.iterateValues(operandNum + 1)) {
478478
Interval interval = allocator.intervalFor(operandNum);
479479
Value operand = null;
480480
Object valueForOperandFromDebugContext = null;
@@ -855,7 +855,7 @@ protected void buildIntervals() {
855855

856856
// Update intervals for operands live at the end of this block;
857857
SparseBitSet live = allocator.getBlockData(block).liveOut;
858-
for (int operandNum = live.nextSetBit(0); operandNum >= 0; operandNum = live.nextSetBit(operandNum + 1)) {
858+
for (int operandNum = live.iterateValues(0); operandNum >= 0; operandNum = live.iterateValues(operandNum + 1)) {
859859
assert live.get(operandNum) : "should not stop here otherwise";
860860
AllocatableValue operand = allocator.intervalFor(operandNum).operand;
861861
if (debug.isLogEnabled()) {

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/lir/alloc/lsra/LinearScanResolveDataFlowPhase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ protected void resolveCollectMappings(BasicBlock<?> fromBlock, BasicBlock<?> toB
6868
SparseBitSet liveAtEdge = allocator.getBlockData(toBlock).liveIn;
6969

7070
// visit all variables for which the liveAtEdge bit is set
71-
for (int operandNum = liveAtEdge.nextSetBit(0); operandNum >= 0; operandNum = liveAtEdge.nextSetBit(operandNum + 1)) {
71+
for (int operandNum = liveAtEdge.iterateValues(0); operandNum >= 0; operandNum = liveAtEdge.iterateValues(operandNum + 1)) {
7272
assert operandNum < numOperands : "live information set for not exisiting interval";
7373
assert allocator.getBlockData(fromBlock).liveOut.get(operandNum) && allocator.getBlockData(toBlock).liveIn.get(operandNum) : "interval not live at this edge";
7474

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/lir/alloc/lsra/SparseBitSet.java

Lines changed: 50 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -25,26 +25,18 @@
2525
package jdk.graal.compiler.lir.alloc.lsra;
2626

2727
import java.util.Arrays;
28-
import java.util.BitSet;
29-
30-
import jdk.graal.compiler.debug.Assertions;
3128

3229
/**
3330
* This class implements a very simple sparse bitset that's optimized for the measured usage by
3431
* linear scan. In practice the bitset cardinality is at most 60 elements while the range of values
3532
* in the set can be quite large, reaching over 100000 in extreme cases. Since lifetime analysis
3633
* uses 4 bitsets per block and the number of blocks can also be very large, space efficiency can
37-
* plays a major role in performance. A simple sorted list of integers performs significantly better
34+
* play a major role in performance. A simple sorted list of integers performs significantly better
3835
* for large numbers of intervals and blocks while only modestly affecting the perform for small
3936
* values.
4037
*/
4138
public class SparseBitSet {
4239

43-
/**
44-
* A bitset used to verify the operation of this set.
45-
*/
46-
private BitSet sanity;
47-
4840
/**
4941
* The number of elements in the set.
5042
*/
@@ -55,51 +47,25 @@ public class SparseBitSet {
5547
*/
5648
protected int[] elements;
5749

58-
private boolean checkBitSet() {
59-
if (sanity != null) {
60-
assert sanity.cardinality() == cardinality() : sanity + " " + this;
61-
for (int bit = sanity.nextSetBit(0); bit >= 0; bit = sanity.nextSetBit(bit + 1)) {
62-
boolean found = false;
63-
for (int i = 0; i < size; i++) {
64-
if (elements[i] == bit) {
65-
found = true;
66-
break;
67-
}
68-
}
69-
assert found : sanity + " " + this;
70-
}
71-
}
72-
return true;
73-
}
50+
/**
51+
* Internal element iterator value used by {@link #iterateValues(int)}.
52+
*/
53+
private int iterator = 0;
7454

7555
public SparseBitSet() {
7656
elements = new int[4];
77-
if (Assertions.assertionsEnabled()) {
78-
sanity = new BitSet();
79-
}
8057
}
8158

8259
public SparseBitSet(SparseBitSet other) {
8360
elements = other.elements.clone();
8461
size = other.size;
85-
if (other.sanity != null) {
86-
sanity = (BitSet) other.sanity.clone();
87-
}
8862
iterator = 0;
8963
}
9064

91-
private boolean checkOr(SparseBitSet other) {
92-
if (sanity != null) {
93-
sanity.or(other.sanity);
94-
checkBitSet();
95-
}
96-
return true;
97-
}
98-
99-
public void or(SparseBitSet other) {
100-
assert checkBitSet();
101-
assert other.checkBitSet();
102-
65+
/**
66+
* Adds all elements from {@code other} to this set.
67+
*/
68+
public void addAll(SparseBitSet other) {
10369
if (size == 0) {
10470
if (elements.length < other.size) {
10571
elements = other.elements.clone();
@@ -138,8 +104,6 @@ public void or(SparseBitSet other) {
138104
size += newElements;
139105
}
140106
}
141-
142-
assert checkOr(other);
143107
}
144108

145109
private void ensureCapacity(int minCapacity) {
@@ -148,30 +112,15 @@ private void ensureCapacity(int minCapacity) {
148112
}
149113
}
150114

151-
/**
152-
* Increases the capacity to ensure that it can hold at least the number of elements specified
153-
* by the minimum capacity argument.
154-
*
155-
* @param minCapacity the desired minimum capacity
156-
* @throws OutOfMemoryError if minCapacity is less than zero
157-
*/
158-
private int[] grow(int minCapacity) {
115+
private void grow(int minCapacity) {
159116
int oldCapacity = elements.length;
160-
/* minimum growth */
161-
/* preferred growth */
162-
int newCapacity = oldCapacity + Math.max(minCapacity - oldCapacity, oldCapacity >> 1);
163-
return elements = Arrays.copyOf(elements, newCapacity);
164-
}
165-
166-
private boolean checkAndNot(SparseBitSet other) {
167-
if (sanity != null) {
168-
sanity.andNot(other.sanity);
169-
checkBitSet();
170-
}
171-
return true;
117+
elements = Arrays.copyOf(elements, oldCapacity + Math.max(minCapacity - oldCapacity, oldCapacity >> 1));
172118
}
173119

174-
public void andNot(SparseBitSet other) {
120+
/**
121+
* Remove all the value that are seting {@ocde other} from this set.
122+
*/
123+
public void removeAll(SparseBitSet other) {
175124
int otherIndex = 0;
176125
int firstDeleted = -1;
177126
for (int index = 0; index < size && otherIndex < other.size;) {
@@ -202,48 +151,47 @@ public void andNot(SparseBitSet other) {
202151
}
203152
size -= shift;
204153
}
205-
assert checkAndNot(other);
206154
}
207155

156+
/**
157+
* Returns the number of bits set to {@code true} in this set.
158+
*/
208159
public int cardinality() {
209160
return size;
210161
}
211162

212-
public boolean get(int value) {
213-
return Arrays.binarySearch(elements, 0, size, value) >= 0;
214-
}
215-
216-
private boolean checkSet(int value) {
217-
if (value < 0) {
218-
throw new IllegalArgumentException();
219-
}
220-
if (sanity != null) {
221-
sanity.set(value);
222-
checkBitSet();
223-
}
224-
return true;
163+
/**
164+
* Returns the bitIndex of the bit with the specified index. The bitIndex is {@code true} if the
165+
* bit with the index {@code bitIndex} is currently set in this {@code BitSet}; otherwise, the
166+
* result is {@code false}.
167+
*/
168+
public boolean get(int bitIndex) {
169+
return Arrays.binarySearch(elements, 0, size, bitIndex) >= 0;
225170
}
226171

227-
public void set(int value) {
228-
int location = Arrays.binarySearch(elements, 0, size, value);
172+
/**
173+
* Sets the bit at the specified index to {@code true}.
174+
*/
175+
public void set(int bitIndex) {
176+
int location = Arrays.binarySearch(elements, 0, size, bitIndex);
229177
if (location < 0) {
230-
insertAt(value, -(location + 1));
231-
}
232-
assert checkSet(value);
233-
}
234-
235-
private void insertAt(int value, int insertPoint) {
236-
if (size == elements.length) {
237-
grow(size + 1);
178+
int insertPoint = -(location + 1);
179+
if (size == elements.length) {
180+
grow(size + 1);
181+
}
182+
System.arraycopy(elements, insertPoint, elements, insertPoint + 1, size - insertPoint);
183+
elements[insertPoint] = bitIndex;
184+
size++;
238185
}
239-
System.arraycopy(elements, insertPoint, elements, insertPoint + 1, size - insertPoint);
240-
elements[insertPoint] = value;
241-
size++;
242186
}
243187

244-
private int iterator = 0;
245-
246-
public int nextSetBit(int i) {
188+
/**
189+
* Uses an internal iterator to visit the values in the set in order. {@code iterateValues(0)}
190+
* starts the iteration and returns the first value. Repeated calls of {@code iterateValues}
191+
* with non-zero values return the next element in the set. It returns -1 when there are no more
192+
* elements to visit.
193+
*/
194+
public int iterateValues(int i) {
247195
if (i == 0) {
248196
iterator = 0;
249197
}
@@ -253,11 +201,11 @@ public int nextSetBit(int i) {
253201
return -1;
254202
}
255203

204+
/**
205+
* Remove all values from the bit set.
206+
*/
256207
public void clear() {
257208
size = 0;
258-
if (Assertions.assertionsEnabled() && sanity != null) {
259-
sanity.clear();
260-
}
261209
}
262210

263211
@Override
@@ -266,24 +214,22 @@ public boolean equals(Object obj) {
266214
if (size != bs.size) {
267215
return false;
268216
}
269-
int result = Arrays.compare(elements, 0, size, bs.elements, 0, size);
270-
assert sanity == null || sanity.equals(bs.sanity) == (result == 0) : sanity + " " + bs.sanity;
271-
return result == 0;
217+
return Arrays.compare(elements, 0, size, bs.elements, 0, size) == 0;
272218
}
273219
return false;
274220
}
275221

276222
@Override
277223
public String toString() {
278224
StringBuilder sb = new StringBuilder();
279-
sb.append("set(size=").append(size).append(", {");
225+
sb.append("{");
280226
for (int i = 0; i < size; i++) {
281227
if (i > 0) {
282228
sb.append(',');
283229
}
284230
sb.append(elements[i]);
285231
}
286-
sb.append("})");
232+
sb.append("}");
287233
return sb.toString();
288234
}
289235

0 commit comments

Comments
 (0)