RFR: 8336759: C2: int counted loop with long limit not recognized as counted loop

Kangcheng Xu kxu at openjdk.org
Tue Dec 3 02:37:50 UTC 2024


This patch implements [JDK-8336759](https://bugs.openjdk.org/browse/JDK-8336759) that recognizes int counted loops with long limits.

Currently, patterns like `for ( int i =...; i < long_limit; ...)` where int `i` is implicitly promoted to long (i.e., `(long) i < long_limit`) is not recognized as (int) counted loop. This patch speculatively and optimistically converts long limits to ints and deoptimize if the limit is outside int range, allowing more optimization opportunities. 

In other words, it transforms 


for (int i = 0; (long) i < long_limit; i++) {...}


to 


if (int_min <= long_limit && long_limit <= int_max ) {
    for (int i = 0;  i < (int) long_limit; i++) {...}
} else {
    trap: loop_limit_check
}


This could benefit calls to APIs like `long MemorySegment#byteSize()` when iterating over a long limit.

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

Commit messages:
 - wrap a line at 80 chars
 - register new node with optimizer
 - update testLimitNotInvariant
 - fix typo in comments
 - update parse predicate check, over/underflow detection, tests
 - uncomment tests
 - update tests
 - update comments
 - add TestIntLoopLongLimit.java
 - Merge branch 'openjdk:master' into 8336759-int-loop-long-limit
 - ... and 9 more: https://git.openjdk.org/jdk/compare/df2d4c15...4a7d03fe

Changes: https://git.openjdk.org/jdk/pull/22449/files
  Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=22449&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8336759
  Stats: 293 lines in 4 files changed: 283 ins; 1 del; 9 mod
  Patch: https://git.openjdk.org/jdk/pull/22449.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/22449/head:pull/22449

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


More information about the hotspot-compiler-dev mailing list