RFR: 8347459: C2: missing transformation for chain of shifts/multiplications by constants [v5]
Damon Fenacci
dfenacci at openjdk.org
Fri Mar 7 09:26:56 UTC 2025
On Thu, 27 Feb 2025 07:54:34 GMT, Marc Chevalier <duke at openjdk.org> wrote:
>> This collapses double shift lefts by constants in a single constant: (x << con1) << con2 => x << (con1 + con2). Care must be taken in the case con1 + con2 is bigger than the number of bits in the integer type. In this case, we must simplify to 0.
>>
>> Moreover, the simplification logic of the sign extension trick had to be improved. For instance, we use `(x << 16) >> 16` to convert a 32 bits into a 16 bits integer, with sign extension. When storing this into a 16-bit field, this can be simplified into simple `x`. But in the case where `x` is itself a left-shift expression, say `y << 3`, this PR makes the IR looks like `(y << 19) >> 16` instead of the old `((y << 3) << 16) >> 16`. The former logic didn't handle the case where the left and the right shift have different magnitude. In this PR, I generalize this simplification to cases where the left shift has a larger magnitude than the right shift. This improvement was needed not to miss vectorization opportunities: without the simplification, we have a left shift and a right shift instead of a single left shift, which confuses the type inference.
>>
>> This also works for multiplications by powers of 2 since they are already translated into shifts.
>>
>> Thanks,
>> Marc
>
> Marc Chevalier has updated the pull request incrementally with one additional commit since the last revision:
>
> + comment on why not zerocon
I just left one more small comment. Otherwise it looks good to me. Thanks @marc-chevalier!
test/hotspot/jtreg/compiler/c2/irTests/LShiftINodeIdealizationTests.java line 178:
> 176:
> 177: short[] arr = new short[1];
> 178: arr[0] = (short)1;
What do you think about using random short value here? (I was just thinking it might slightly increase the chances of spotting if something is wrong with the shifts...)
-------------
Marked as reviewed by dfenacci (Committer).
PR Review: https://git.openjdk.org/jdk/pull/23728#pullrequestreview-2666681057
PR Review Comment: https://git.openjdk.org/jdk/pull/23728#discussion_r1984716376
More information about the hotspot-compiler-dev
mailing list