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

Andrew Haley aph at openjdk.org
Wed Dec 7 18:58:03 UTC 2022


On Wed, 7 Dec 2022 18:42:52 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 inroduced 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 is 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 shows the changed code with `-prof perfasm`.
> 
> Typical nanobenchmark with a loop with a Blackhole over array shows no difference in performance as the constant is anyway moved out of the loop and usualy there are enought 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.
> 
> 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.

src/hotspot/cpu/aarch64/aarch64.ad line 15816:

> 15814:   %}
> 15815: %}
> 15816: 

Please put all this repetitive stuff into aarch64_ad.m4 and we'll review that.

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

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


More information about the hotspot-compiler-dev mailing list