RFR: 8356184: C2 MemorySegment: long RangeCheck with ConvI2L(iv + invar) prevents RCE [v2]

Kerem Kat krk at openjdk.org
Mon Jan 26 17:31:38 UTC 2026


> `MemorySegment` bounds checks use long arithmetic, but when accessing with an int loop variable plus an int invariant offset, the pattern `ConvI2L(iv + invar)` was not recognized by Range Check Elimination. This prevented RCE and consequently blocked vectorization for common `MemorySegment` access patterns.
> 
> The fix teaches `is_scaled_iv_plus_offset` to recognize linear int expressions inside `ConvI2L`. A new `short_offset` flag signals that the offset is part of int arithmetic (not added separately in long), requiring the range to be clamped at `max_jint + 1` to correctly handle potential int overflow. This also removes pre-existing dead code where an `exp_bt != bt` check was intended to bail out on such patterns but never actually executed.
> 
> With this change, `MemorySegment` loops using int invariant offsets now benefit from RCE and vectorization, matching the behavior already supported for long invariant offsets.
> 
> 
> void process(MemorySegment segment, int offset, int size) {
>   for (int i = 0; i < size; i++) {
>       long addr = i + offset;  // ConvI2L(AddI(iv, offset)) was not recognized
>       segment.set(JAVA_BYTE, addr, (byte) 0);
>   }
> }

Kerem Kat has updated the pull request incrementally with one additional commit since the last revision:

  Remove IR rules from TestMemorySegment methods where vectorization depends on backing store type

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

Changes:
  - all: https://git.openjdk.org/jdk/pull/29392/files
  - new: https://git.openjdk.org/jdk/pull/29392/files/2cb35a3b..23eff0db

Webrevs:
 - full: https://webrevs.openjdk.org/?repo=jdk&pr=29392&range=01
 - incr: https://webrevs.openjdk.org/?repo=jdk&pr=29392&range=00-01

  Stats: 14 lines in 1 file changed: 0 ins; 6 del; 8 mod
  Patch: https://git.openjdk.org/jdk/pull/29392.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/29392/head:pull/29392

PR: https://git.openjdk.org/jdk/pull/29392


More information about the hotspot-compiler-dev mailing list