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

Constant fold across compares

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P5 P5
    • tbd
    • 23
    • 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.

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

              Created:
              Updated: