Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8357726

C2 fails to recognize the counted loop when induction variable range is changed multiple times

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • 25
    • hotspot

      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.

            xgong Xiaohong Gong
            xgong Xiaohong Gong
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: