/// CHECK-START: int[] Main.linearCopy(int[]) BCE (before) /// CHECK-DAG: BoundsCheck // /// CHECK-START: int[] Main.linearCopy(int[]) BCE (after) /// CHECK-NOT: BoundsCheck /// CHECK-NOT: Deoptimize privatestaticint[] linearCopy(int x[]) { int n = x.length; int y[] = newint[n]; for (int i = 0; i < n; i++) {
y[i] = x[i];
} return y;
}
/// CHECK-START: int Main.linearByTwo(int[]) BCE (before) /// CHECK-DAG: BoundsCheck /// CHECK-DAG: BoundsCheck // /// CHECK-START: int Main.linearByTwo(int[]) BCE (after) /// CHECK-NOT: BoundsCheck /// CHECK-NOT: Deoptimize privatestaticint linearByTwo(int x[]) { int n = x.length / 2; int result = 0; for (int i = 0; i < n; i++) { int ii = i << 1;
result += x[ii];
result += x[ii + 1];
} return result;
}
/// CHECK-START: int Main.linearByTwoSkip1(int[]) BCE (before) /// CHECK-DAG: BoundsCheck // /// CHECK-START: int Main.linearByTwoSkip1(int[]) BCE (after) /// CHECK-NOT: BoundsCheck /// CHECK-NOT: Deoptimize privatestaticint linearByTwoSkip1(int x[]) { int result = 0; for (int i = 0; i < x.length / 2; i++) {
result += x[2 * i];
} return result;
}
/// CHECK-START: int Main.linearByTwoSkip2(int[]) BCE (before) /// CHECK-DAG: BoundsCheck // /// CHECK-START: int Main.linearByTwoSkip2(int[]) BCE (after) /// CHECK-DAG: BoundsCheck // /// CHECK-START: int Main.linearByTwoSkip2(int[]) BCE (after) /// CHECK-NOT: Deoptimize privatestaticint linearByTwoSkip2(int x[]) { int result = 0; // This case is not optimized. for (int i = 0; i < x.length; i+=2) {
result += x[i];
} return result;
}
/// CHECK-START: int Main.linearWithCompoundStride() BCE (before) /// CHECK-DAG: BoundsCheck // /// CHECK-START: int Main.linearWithCompoundStride() BCE (after) /// CHECK-NOT: BoundsCheck /// CHECK-NOT: Deoptimize privatestaticint linearWithCompoundStride() { int[] x = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 }; int result = 0; for (int i = 0; i <= 12; ) {
i++;
result += x[i];
i++;
} return result;
}
/// CHECK-START: int Main.linearWithLargePositiveStride() BCE (before) /// CHECK-DAG: BoundsCheck // /// CHECK-START: int Main.linearWithLargePositiveStride() BCE (after) /// CHECK-NOT: BoundsCheck /// CHECK-NOT: Deoptimize privatestaticint linearWithLargePositiveStride() { int[] x = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; int result = 0; int k = 0; // Range analysis has no problem with a trip-count defined by a // reasonably large positive stride far away from upper bound. for (int i = 1; i <= 10 * 10000000 + 1; i += 10000000) {
result += x[k++];
} return result;
}
/// CHECK-START: int Main.linearWithVeryLargePositiveStride() BCE (before) /// CHECK-DAG: BoundsCheck // /// CHECK-START: int Main.linearWithVeryLargePositiveStride() BCE (after) /// CHECK-DAG: BoundsCheck // /// CHECK-START: int Main.linearWithVeryLargePositiveStride() BCE (after) /// CHECK-NOT: Deoptimize privatestaticint linearWithVeryLargePositiveStride() { int[] x = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; int result = 0; int k = 0; // Range analysis conservatively bails due to potential of wrap-around // arithmetic while computing the trip-count for this very large stride. for (int i = 1; i < Integer.MAX_VALUE; i += 195225786) {
result += x[k++];
} return result;
}
/// CHECK-START: int Main.linearWithLargeNegativeStride() BCE (before) /// CHECK-DAG: BoundsCheck // /// CHECK-START: int Main.linearWithLargeNegativeStride() BCE (after) /// CHECK-NOT: BoundsCheck /// CHECK-NOT: Deoptimize privatestaticint linearWithLargeNegativeStride() { int[] x = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; int result = 0; int k = 0; // Range analysis has no problem with a trip-count defined by a // reasonably large negative stride far away from lower bound. for (int i = -1; i >= -10 * 10000000 - 1; i -= 10000000) {
result += x[k++];
} return result;
}
/// CHECK-START: int Main.linearWithVeryLargeNegativeStride() BCE (before) /// CHECK-DAG: BoundsCheck // /// CHECK-START: int Main.linearWithVeryLargeNegativeStride() BCE (after) /// CHECK-DAG: BoundsCheck // /// CHECK-START: int Main.linearWithVeryLargeNegativeStride() BCE (after) /// CHECK-NOT: Deoptimize privatestaticint linearWithVeryLargeNegativeStride() { int[] x = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; int result = 0; int k = 0; // Range analysis conservatively bails due to potential of wrap-around // arithmetic while computing the trip-count for this very large stride. for (int i = -2; i > Integer.MIN_VALUE; i -= 195225786) {
result += x[k++];
} return result;
}
/// CHECK-START: int Main.linearForNEUp() BCE (before) /// CHECK-DAG: BoundsCheck // /// CHECK-START: int Main.linearForNEUp() BCE (after) /// CHECK-NOT: BoundsCheck /// CHECK-NOT: Deoptimize privatestaticint linearForNEUp() { int[] x = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int result = 0; for (int i = 0; i != 10; i++) {
result += x[i];
} return result;
}
/// CHECK-START: int Main.linearForNEDown() BCE (before) /// CHECK-DAG: BoundsCheck // /// CHECK-START: int Main.linearForNEDown() BCE (after) /// CHECK-NOT: BoundsCheck /// CHECK-NOT: Deoptimize privatestaticint linearForNEDown() { int[] x = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int result = 0; for (int i = 9; i != -1; i--) {
result += x[i];
} return result;
}
/// CHECK-START: int Main.linearForNEArrayLengthUp(int[]) BCE (before) /// CHECK-DAG: BoundsCheck // /// CHECK-START: int Main.linearForNEArrayLengthUp(int[]) BCE (after) /// CHECK-NOT: BoundsCheck /// CHECK-NOT: Deoptimize privatestaticint linearForNEArrayLengthUp(int[] x) { int result = 0; for (int i = 0; i != x.length; i++) {
result += x[i];
} return result;
}
/// CHECK-START: int Main.linearForNEArrayLengthDown(int[]) BCE (before) /// CHECK-DAG: BoundsCheck // /// CHECK-START: int Main.linearForNEArrayLengthDown(int[]) BCE (after) /// CHECK-NOT: BoundsCheck /// CHECK-NOT: Deoptimize privatestaticint linearForNEArrayLengthDown(int[] x) { int result = 0; for (int i = x.length - 1; i != -1; i--) {
result += x[i];
} return result;
}
/// CHECK-START: int Main.linearDoWhileUp() BCE (before) /// CHECK-DAG: BoundsCheck // /// CHECK-START: int Main.linearDoWhileUp() BCE (after) /// CHECK-NOT: BoundsCheck /// CHECK-NOT: Deoptimize privatestaticint linearDoWhileUp() { int[] x = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int result = 0; int i = 0; do {
result += x[i++];
} while (i < 10); return result;
}
/// CHECK-START: int Main.linearDoWhileDown() BCE (before) /// CHECK-DAG: BoundsCheck // /// CHECK-START: int Main.linearDoWhileDown() BCE (after) /// CHECK-NOT: BoundsCheck /// CHECK-NOT: Deoptimize privatestaticint linearDoWhileDown() { int[] x = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int result = 0; int i = 9; do {
result += x[i--];
} while (0 <= i); return result;
}
/// CHECK-START: int Main.linearLong() BCE (before) /// CHECK-DAG: BoundsCheck // /// CHECK-START: int Main.linearLong() BCE (after) /// CHECK-NOT: BoundsCheck /// CHECK-NOT: Deoptimize privatestaticint linearLong() { int[] x = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int result = 0; // Induction on constant interval is done in higher precision than necessary, // but truncated at the use as subscript. for (long i = 0; i < 10; i++) {
result += x[(int)i];
} return result;
}
/// CHECK-START: int Main.linearLongAlt(int[]) BCE (before) /// CHECK-DAG: BoundsCheck // /// CHECK-START: int Main.linearLongAlt(int[]) BCE (after) /// CHECK-NOT: BoundsCheck /// CHECK-NOT: Deoptimize privatestaticint linearLongAlt(int[] x) { int result = 0; // Induction on array length is done in higher precision than necessary, // but truncated at the use as subscript. for (long i = 0; i < x.length; i++) {
result += x[(int)i];
} return result;
}
/// CHECK-START: int Main.linearShort() BCE (before) /// CHECK-DAG: BoundsCheck // /// CHECK-START: int Main.linearShort() BCE (after) /// CHECK-NOT: BoundsCheck /// CHECK-NOT: Deoptimize privatestaticint linearShort() { int[] x = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int result = 0; // Induction is done in short precision, but fits. for (short i = 0; i < 10; i++) {
result += x[i];
} return result;
}
/// CHECK-START: int Main.linearChar() BCE (before) /// CHECK-DAG: BoundsCheck // /// CHECK-START: int Main.linearChar() BCE (after) /// CHECK-NOT: BoundsCheck /// CHECK-NOT: Deoptimize privatestaticint linearChar() { int[] x = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int result = 0; // Induction is done in char precision, but fits. for (char i = 0; i < 10; i++) {
result += x[i];
} return result;
}
/// CHECK-START: int Main.linearByte() BCE (before) /// CHECK-DAG: BoundsCheck // /// CHECK-START: int Main.linearByte() BCE (after) /// CHECK-NOT: BoundsCheck /// CHECK-NOT: Deoptimize privatestaticint linearByte() { int[] x = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int result = 0; // Induction is done in byte precision, but fits. for (byte i = 0; i < 10; i++) {
result += x[i];
} return result;
}
/// CHECK-START: int Main.invariantFromPreLoop(int[], int) BCE (before) /// CHECK-DAG: BoundsCheck // /// CHECK-START: int Main.invariantFromPreLoop(int[], int) BCE (after) /// CHECK-NOT: BoundsCheck /// CHECK-NOT: Deoptimize privatestaticint invariantFromPreLoop(int[] x, int y) { int result = 0; // Strange pre-loop that sets upper bound. int hi; while (true) {
y = y % 3;
hi = x.length; if (y != 123) break;
} for (int i = 0; i < hi; i++) {
result += x[i];
} return result;
}
/// CHECK-START: void Main.linearTriangularOnTwoArrayLengths(int) BCE (before) /// CHECK-DAG: BoundsCheck /// CHECK-DAG: BoundsCheck // /// CHECK-START: void Main.linearTriangularOnTwoArrayLengths(int) BCE (after) /// CHECK-NOT: BoundsCheck /// CHECK-NOT: Deoptimize privatestaticvoid linearTriangularOnTwoArrayLengths(int n) { int[] a = newint[n]; for (int i = 0; i < a.length; i++) { int[] b = newint[i]; for (int j = 0; j < b.length; j++) { // Need to know j < b.length < a.length for static bce.
a[j] += 1; // Need to know just j < b.length for static bce.
b[j] += 1;
}
verifyTriangular(a, b, i, n);
}
}
/// CHECK-START: void Main.linearTriangularOnOneArrayLength(int) BCE (before) /// CHECK-DAG: BoundsCheck /// CHECK-DAG: BoundsCheck // /// CHECK-START: void Main.linearTriangularOnOneArrayLength(int) BCE (after) /// CHECK-NOT: BoundsCheck /// CHECK-NOT: Deoptimize privatestaticvoid linearTriangularOnOneArrayLength(int n) { int[] a = newint[n]; for (int i = 0; i < a.length; i++) { int[] b = newint[i]; for (int j = 0; j < i; j++) { // Need to know j < i < a.length for static bce.
a[j] += 1; // Need to know just j < i for static bce.
b[j] += 1;
}
verifyTriangular(a, b, i, n);
}
}
/// CHECK-START: void Main.linearTriangularOnParameter(int) BCE (before) /// CHECK-DAG: BoundsCheck /// CHECK-DAG: BoundsCheck // /// CHECK-START: void Main.linearTriangularOnParameter(int) BCE (after) /// CHECK-NOT: BoundsCheck /// CHECK-NOT: Deoptimize privatestaticvoid linearTriangularOnParameter(int n) { int[] a = newint[n]; for (int i = 0; i < n; i++) { int[] b = newint[i]; for (int j = 0; j < i; j++) { // Need to know j < i < n for static bce.
a[j] += 1; // Need to know just j < i for static bce.
b[j] += 1;
}
verifyTriangular(a, b, i, n);
}
}
// Verifier for triangular loops. privatestaticvoid verifyTriangular(int[] a, int[] b, int m, int n) {
expectEquals(n, a.length); for (int i = 0, k = m; i < n; i++) {
expectEquals(a[i], k); if (k > 0) k--;
}
expectEquals(m, b.length); for (int i = 0; i < m; i++) {
expectEquals(b[i], 1);
}
}
// Verifier for triangular loops. privatestaticvoid verifyTriangular(int[] a) { int n = a.length; for (int i = 0; i < n; i++) {
expectEquals(a[i], n + n);
}
}
/// CHECK-START: int[] Main.linearTriangularOOB() BCE (before) /// CHECK-DAG: BoundsCheck // /// CHECK-START: int[] Main.linearTriangularOOB() BCE (after) /// CHECK-DAG: BoundsCheck // /// CHECK-START: int[] Main.linearTriangularOOB() BCE (after) /// CHECK-NOT: Deoptimize privatestaticint[] linearTriangularOOB() { int[] a = newint[200]; try { for (int i = 0; i < 200; i++) { // Lower bound must be recognized as lower precision induction with arithmetic // wrap-around to -128 when i exceeds 127. for (int j = (byte) i; j < 200; j++) {
a[j] += 1;
}
}
} catch (ArrayIndexOutOfBoundsException e) { return a;
} returnnull; // failure if this is reached
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.