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

Kangcheng Xu kxu at openjdk.org
Tue Dec 3 21:40:41 UTC 2024


On Tue, 3 Dec 2024 09:04:08 GMT, Christian Hagedorn <chagedorn at openjdk.org> wrote:

>> 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.
>
> src/hotspot/share/opto/loopnode.hpp line 1229:
> 
>> 1227:   bool is_counted_loop_with_speculative_long_limit(Node* x, IdealLoopTree*& loop, BasicType iv_bt);
>> 1228:  private:
>> 1229:   bool do_is_counted_loop(Node* x, IdealLoopTree*& loop, BasicType iv_bt);
> 
> Was like that before but I think `is_counted_loop()` is a bit misleading, suggesting it's a query but it's actually doing the conversion work. Since you now change these methods anyway, what do you think about the following naming suggestions?
> - public `try_convert_to_counted_loop()`
>   - calls: private `convert_to_counted_loop()`
>   - call: private `convert_to_counted_loop_with_speculative_long_limit()`

Yes I found the naming counter-intuitive, too, upon first reading it. I like your suggestions. Thanks!

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

PR Review Comment: https://git.openjdk.org/jdk/pull/22449#discussion_r1868399930


More information about the hotspot-compiler-dev mailing list