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

C1: RangeCheckElimination for % operator if divisor is IntConstant

    XMLWordPrintable

Details

    • Enhancement
    • Resolution: Fixed
    • P4
    • 17
    • 17
    • hotspot
    • b23

    Description

      % operator follows from this rule that the result of the remainder operation can be negative only if the dividend is negative, and can be positive only if the dividend is positive. Moreover, the magnitude of the result is always less than the magnitude of the divisor(See JLS 15.17.3).
            
      So if y is a constant integer and not equal to 0, then we can deduce the bound of remainder operation:
        x % -y ==> [0, y - 1] RCE
        x % y ==> [0, y - 1] RCE
        -x % y ==> [-y + 1, 0]
        -x % -y ==> [-y + 1, 0]

      Based on above rationale, we can apply RCE for the remainder operations whose dividend is constant integer and >= 0, e.g.:

      for(int i=0;i<1000;i++){
        int top5 = arr[i%5]; // Apply RCE if arr is a loop invariant
        ....
      }

      Attachments

        Issue Links

          Activity

            People

              yyang Yi Yang
              yyang Yi Yang
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: