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

LoopPredication unnecessary lower bounds checks

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P4 P4
    • tbd
    • 23
    • hotspot
    • generic
    • generic

      Loop Predication creates an unnecessary lower bounds check in this example:

      ```
      public static void test(int iters, int []data) {
          for (int i = 0; i < iters; ++i) {
            blackhole(data[i]);
          }
      }
      ```

      The generated code looks like:
      ```
      public static void test(int iters, int []data) {
        if (iters <= 0) { return }
        if (data == null) { trap() }
        if (data.length <= 0) { trap() }
        if (data.length <= iters - 1) { trap() }
        for (int i = 0; i < iters; ++i) {
          blackhole(data[i]);
        }
      }
      ```

      I believe it is unnecessary to trap for `0 <= data.length`. The check for `iters - 1 >= data.length` is subsuming and should be sufficient to check for out of bounds. The only reason I can think of for the lower bounds check would be to check for overflow, but the upper bounds check conversion to long should be able to check this case.

      I played around with different scales and offsets, and it seems to me the lower bound is never actually needed. I do not have permissions to view the JBS issue in which the lower bound check was added.

      The code of interest is in https://github.com/openjdk/jdk/blob/6b09a79d64bcb1aa5382e60d1d690d4e4a9dc337/src/hotspot/share/opto/loopPredicate.cpp#L785

            jcao Joshua Cao
            jcao Joshua Cao
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: