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:42:21 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.

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

> 179:   // of the underlying storage rather than the logical type width.  So we
> 180:   // instead compute the number of 1s in the maximum value.
> 181:   static_assert(!std::is_signed<T>::value, "type_width requires an unsigned type");

`std::is_signed` is `false` for `intn_t`, you need to manually check that `T(-1) > T(0)`.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/29898#discussion_r2870459963


More information about the hotspot-dev mailing list