On Thu, 10 Sep 2026 17:35:48 GMT, Kangcheng Xu <[email protected]> wrote:

>> Hi,
>> 
>> This PR adds intrinsics to `Preconditions.checkFromToIndex()` and 
>> `Preconditions.checkFromIndexSize()` to produced optimized IR that uses 
>> `RangeCheck` node instead of implicit comparisons. 
>> `Preconditions.checkIndex()` is also refactored (without additional 
>> optimizations) to use the same helper function.
>> 
>> Some common patterns where calling `checkFromToIndex` or 
>> `checkFromIndexSize` in a loop can have range checks in main loop eliminated 
>> completely and, therefore, enables empty loop removal.
>> 
>> IR and correctness tests are included and passing. Additional tests on 
>> `Preconditions.checkIndex()` were also added.
>> 
>> Thanks!
>> 
>> ---------
>> - [x] I confirm that I make this contribution in accordance with the 
>> [OpenJDK Interim AI Policy](https://openjdk.org/legal/ai).
>
> Kangcheng Xu has updated the pull request incrementally with two additional 
> commits since the last revision:
> 
>  - simplify logic by allowing deopt with size == 0
>  - remove left over code

The latest commit simplifies bound check logic by check the first and last 
indices of a subrange. This avoids the `u<= length` to `u< length + 1` 
workaround. The old code had a corner case causing deopt with `size == 
jint_max` or `size == jlong_max` . The new corner cases are deopt-ing with 
`size == 0` or `to - from == 0`, which are equally unlikely to happen in 
practice.


the latest benchmark is the following:

<img width="2681" height="889" alt="image" 
src="https://github.com/user-attachments/assets/d05ab241-f702-4939-8b75-5192373509fc";
 />

It is slightly more noisy on array copies, but no overall improvement or 
regression compared to before the simplification.

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

PR Comment: https://git.openjdk.org/jdk/pull/31138#issuecomment-5624110331

Reply via email to