RFR: JDK-8153837: AArch64: Handle special cases for MaxINode & MinINode [v6]

Andrew Haley aph at openjdk.org
Wed Jan 18 10:25:30 UTC 2023


On Wed, 18 Jan 2023 10:11:39 GMT, Dmitry Chuyko <dchuyko at openjdk.org> wrote:

>> This is a return to an older optimization suggestion for integer Math.min() and Math.max(). See original thread at https://mail.openjdk.org/pipermail/hotspot-compiler-dev/2016-April/022367.html
>> 
>> In a case when one of the arguments is a constant 0, 1 or -1 this constant is currently materialized (like zero is moved to a dedicated register). Instead of that we can produce denoted values from ZR zero register using CSEL, CSINC or CSINV. Thus the register usage and the load instruction are removed.
>> 
>> The implementation adds 3 additional matching rules for min and 3 for max in aarch64.ad file. As constants currently can be in any MinI/MaxI node input, ideal transformation for that nodes is changed to put a constant into the first input. It allows to have a single rule for each value instead of two. First input is not very natural, it is selected because of https://github.com/openjdk/jdk/pull/3513 optimization that added right-spline transformation for MinI/MaxI. I think it can be symmetrically changed to left-spline but it was not a subject of this PR. Each match rule generates 2 'instruct' intructions following the pattern introduced in https://github.com/openjdk/jdk/commit/fde854e03779e6809279dbf85b0645eb49d8736a. They are newly added ones with one peculiarity. They try to follow regular naming scheme but lack one of operands that corresponds to a constant that is not actually needed. E.g. 'instruct cmovI_reg_immM1_ge(iRegINoSp dst, iRegI src1, rFlagsReg cr)' that don't have 
 an 'immI_M1' input.
>> 
>> New TestMinMaxIntrinsics jtreg test compares results produced by intrinsics for generic and specialized versions with Java implementation of min and max. Intrinsics are used in lambdas that are compiled with the Whitebox API. The cases include -1, 0, 1 and a couple of regular values. This test can be used to check the generated assembly by adding `-XX:+PrintCompilation -XX:+PrintOptoAssembly`. Nano-benchmarks where a specialized version is called from a not inlined method also show the changed code with `-prof perfasm`.
>> 
>> Typical nano-benchmark with a loop and a Blackhole over array shows no difference in performance as the constant is anyway moved out of the loop and usually there are enough registers. However special nano-benchmarks can be considered, e.g.
>> 
>> 
>>     @Benchmark
>>     @OperationsPerInvocation(TESTSIZE)
>>     public int max0_use8_i() {
>>         int sum = 0;
>>         for(int i = 0; i < TESTSIZE; i++) {
>>             use8(0, 1, 2, 3, 4, 5, 6, 7);
>>             sum += Math.max(i, 0);
>>         }
>>         return sum;
>>     }
>>     
>>     @CompilerControl(CompilerControl.Mode.DONT_INLINE)
>>     public void use8(int p0, int p1, int p2, int p3, int p4, int p5, int p6, int p7) {
>>     }
>> 
>> 
>> Saving ~1 L1 icache load makes it ~9% faster, and more than a half of the cost is use8() helper.
>> 
>> New version passes new TestMinMaxIntrinsics test on x86 and aarch64 and tier1,2 tests on that platforms (release build).
>> 
>> Zero case is especially interesting. In general, I wonder how it could be possible to allocate ZR as a register for zero constants so the load there could be a no-op and we might get rid of special rules with immI0.
>
> Dmitry Chuyko 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 six additional commits since the last revision:
> 
>  - Merge branch 'openjdk:master' into JDK-8153837
>  - Corrected instruction costs
>  - 392806Register, iRegIorL2I matched, m4 cleanup
>  - Merge branch 'openjdk:master' into JDK-8153837
>  - Reverted Ideal change, moved definitions to m4
>  - JDK-8153837: AArch64: Handle special cases for MaxINode & MinINode

Yes, that's very thorough.

-------------

Marked as reviewed by aph (Reviewer).

PR: https://git.openjdk.org/jdk/pull/11570


More information about the hotspot-compiler-dev mailing list