Constant fold across compares

XMLWordPrintable

    • Type: Enhancement
    • Resolution: Unresolved
    • Priority: P5
    • tbd
    • Affects Version/s: 23
    • Component/s: hotspot
    • generic
    • generic

      For example:

      `x + 1 < 2` -> `x < 2 - 1`

      If we can prove that `x + 1` does not overflow and `2 - 1` does not overflow.

      Consider this more practical example:

      ```
      public void foo(int[] arr) {
        for (i = arr.length - 1; i >= 0; --i) {
          blackhole(arr[i]);
        }
      }
      ```

      C2 emits a loop guard that looks `arr.length - 1 < 0`. We know `arr.length - 1` does not overflow because `arr.length` is positive. We can fold the comparison into `arr.length < 1`. We have to compute `arr.length - 1` computation if we enter the loop anyway, but we can avoid the subtraction computation if we exit the loop. I believe the simplification can also help with stronger integer range analysis in https://bugs.openjdk.org/browse/JDK-8275202.

            Assignee:
            Joshua Cao
            Reporter:
            Joshua Cao
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: