RFR: 8325674: Constant fold across compares [v2]
Emanuel Peter
epeter at openjdk.org
Mon Feb 26 11:52:58 UTC 2024
On Fri, 23 Feb 2024 23:23:20 GMT, Joshua Cao <duke at openjdk.org> wrote:
>> For example, `x + 1 < 2` -> `x < 2 - 1` iff we can prove that `x + 1` does not overflow and `2 - 1` does not overflow. We can always fold if it is an `==` or `!=` since overflow will not affect the result of the comparison.
>>
>> 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 never enter the loop. I believe the simplification can also help with stronger integer range analysis in https://bugs.openjdk.org/browse/JDK-8275202.
>>
>> Some additional notes:
>> * there is various overflow checking code across `src/hotspot/share/opto`. I separated about the functions from convertnode.cpp into `type.hpp`. Maybe the functions belong somewhere else?
>> * there is a change in Parse::do_if() to repeatedly apply GVN until the test is canonical. We need multiple iterations in the case of `C1 > C2 - X` -> `C2 - X < C1` -> `C2 < X` -> `X > C2`. This fails the assertion if `BoolTest(btest).is_canonical()`. We can avoid this by applying GVN one more time to get `C2 < X`.
>> * we should not transform loop backedge conditions. For example, if we have `for (i = 0; i < 10; ++i) {}`, the backedge condition is `i + 1 < 10`. If we transform it into `i < 9`, it messes with CountedLoop's recognition of induction variables and strides.r
>> * this change optimizes some of the equality checks in `TestUnsignedComparison.java` and breaks the IR checks. I removed those tests.
>
> Joshua Cao has updated the pull request with a new target base due to a merge or a rebase. The incremental webrev excludes the unrelated changes brought in by the merge/rebase. The pull request contains three additional commits since the last revision:
>
> - Modify tests to work with -XX:-TieredCompilation
> - Merge branch 'master' into cmpconstantfold
> - 8325674: Constant fold across compares
Since this is dealing with guards and RangeChecks, we need to review this very carefully.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/17853#issuecomment-1963962345
More information about the hotspot-compiler-dev
mailing list