RFR: 8325495: C2: implement optimization for series of Add of unique value [v9]

Christian Hagedorn chagedorn at openjdk.org
Tue Oct 8 07:13:01 UTC 2024


On Mon, 7 Oct 2024 18:50:57 GMT, Kangcheng Xu <kxu at openjdk.org> wrote:

>> This pull request resolves [JDK-8325495](https://bugs.openjdk.org/browse/JDK-8325495) by converting series of additions of the same operand into multiplications. I.e., `a + a + ... + a + a + a => n*a`.
>> 
>> As an added benefit, it also converts `C * a + a` into `(C+1) * a` and `a << C + a` into `(2^C + 1) * a` (with respect to constant `C`). This is actually a side effect of IGVN being iterative: at converting the `i`-th addition, the previous `i-1` additions would have already been optimized to multiplication (and thus, further into bit shifts and additions/subtractions if possible).  
>> 
>> Some notable examples of this transformation include:
>> - `a + a + a` => `a*3` => `(a<<1) + a`
>> - `a + a + a + a` => `a*4` => `a<<2`
>> - `a*3 + a` => `a*4` => `a<<2`
>> - `(a << 1) + a + a` => `a*2 + a + a` => `a*3 + a` => `a*4 => a<<2`
>> 
>> See included IR unit tests for more.
>
> Kangcheng Xu has updated the pull request incrementally with three additional commits since the last revision:
> 
>  - remove matching power-of-2 subtractions since it's already handled by Identity()
>  - verify results with custom test methods
>  - update comments to be more descriptive, remove unused can_reshape argument

Thanks for the updates! Good conversion of the tests. 

ll give this another spinning in our testing.

src/hotspot/share/opto/addnode.cpp line 439:

> 437: 
> 438: // Try to match `a + a`. On success, return `a` and set `2` as `multiplier`.
> 439: // The method matches `n` to for pattern: AddNode(a, a).

Suggestion:

// The method matches `n` for pattern: AddNode(a, a).

test/hotspot/jtreg/compiler/c2/TestSerialAdditions.java line 65:

> 63:             "mulAndAddToZero", //
> 64:             "mulAndAddToMinus1", //
> 65:             "mulAndAddToMinus42" //

Why did you add the trailing `//`?

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

Marked as reviewed by chagedorn (Reviewer).

PR Review: https://git.openjdk.org/jdk/pull/20754#pullrequestreview-2353577415
PR Review Comment: https://git.openjdk.org/jdk/pull/20754#discussion_r1791311803
PR Review Comment: https://git.openjdk.org/jdk/pull/20754#discussion_r1791313522


More information about the hotspot-compiler-dev mailing list