-
Enhancement
-
Resolution: Unresolved
-
P4
-
25
Described as the title, a counted loop is failed to be recognized by C2 if the range of loop iv is changed more than one times with `CastII`. This will influence the followed loop optimizations like range check elimination or loop predication for a if inside a loop.
Please see more discussions from: https://github.com/openjdk/jdk/pull/25138#issuecomment-2892720654 .
Following is a simple case to reproduce the issue:
```
private static final int LEN = 1024;
static int[] a;
static byte[] b;
static {
a = new int[LEN];
b = new byte[LEN];
for (int i = 0; i < LEN; ++i) {
a[i] = i;
}
}
static void func() {
for (int i = 0; i < LEN; i += 16) {
Objects.checkIndex(i, LEN - 3);
int a0 = a[i];
Objects.checkIndex(i, LEN - 15);
br[i] = (byte) a0;
}
}
```
The ideal graph is almost similar with what in the discussions. The range of the loop iv is changed two times, making the `PhiNode` used by two continues `CastIINode`. Skipping all cast for such cases make sense when recognizing the counted loop.
Please see more discussions from: https://github.com/openjdk/jdk/pull/25138#issuecomment-2892720654 .
Following is a simple case to reproduce the issue:
```
private static final int LEN = 1024;
static int[] a;
static byte[] b;
static {
a = new int[LEN];
b = new byte[LEN];
for (int i = 0; i < LEN; ++i) {
a[i] = i;
}
}
static void func() {
for (int i = 0; i < LEN; i += 16) {
Objects.checkIndex(i, LEN - 3);
int a0 = a[i];
Objects.checkIndex(i, LEN - 15);
br[i] = (byte) a0;
}
}
```
The ideal graph is almost similar with what in the discussions. The range of the loop iv is changed two times, making the `PhiNode` used by two continues `CastIINode`. Skipping all cast for such cases make sense when recognizing the counted loop.