RFR: 8278857: C2: optimize (x << 2) & -4 to x (and similar patterns) -> KnownBits for LShift [v2]

Quan Anh Mai qamai at openjdk.org
Mon Mar 2 04:45:22 UTC 2026


On Fri, 27 Feb 2026 19:43:03 GMT, Ashay Rane <duke at openjdk.org> wrote:

>> This PR contains the first of the two changes necessary to resolve
>> JDK-8278857.
>> 
>> The commit message, pasted below, has additional details.
>> 
>>> Augment `RangeInference` with information about left shifts
>>> 
>>> Add `RangeInference::infer_lshift()` to compute type information for
>>> left shift operations.  This method tracks known zero bits in the low
>>> positions (the bottom `shift` bits are always zero after a left shift)
>>> and computes signed and unsigned range bounds when the shift does not
>>> cause overflow.
>>> 
>>> This is the first of the two changes necessary to resolve JDK-8278857.
>>> The second change will build on top of these changes to enable AND nodes
>>> to be optimized away when the bits that would have been cleared by the
>>> AND operation were already known to be zero.
>
> Ashay Rane has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Addressed review comments
>   
>   1. Elided redundant check when type is known to not be `Type::BOTTOM`.
>   
>   2. Used `RangerInference` to AND the shift amount with 31 or 63 (based
>      on the basic type) to match the Java shift semantics so that we have
>      a higher chance of triggering the optimization.
>   
>   3. Added a `type_width()` function that can work with `uintn_t<N>`
>      types, in addition to other unsigned types like `juint` and `julong`.
>   
>   4. Updated `RangeInference::infer_lshift()` to now accept masked shift,
>      which works correctly because the caller (`LShiftINode::Ideal()`)
>      ANDs the shift amount with 31 or 63.

LGTM otherwise, I submitted testing.

src/hotspot/share/utilities/intn_t.hpp line 167:

> 165: }
> 166: 
> 167: template <typename T>

Can you move this into a class, maybe `HotspotNumerics` is a good name, it helps hide this private implementation, too.

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

PR Review: https://git.openjdk.org/jdk/pull/29898#pullrequestreview-3874027327
PR Review Comment: https://git.openjdk.org/jdk/pull/29898#discussion_r2870467903


More information about the hotspot-dev mailing list