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

Roland Westrelin roland at openjdk.org
Tue Dec 3 02:37:52 UTC 2024


On Fri, 29 Nov 2024 01:08:04 GMT, Kangcheng Xu <kxu 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.cpp line 1660:

> 1658:   Node* new_cmp = _igvn.register_new_node_with_optimizer(
> 1659:       cmp->in(1) == incr ? new CmpINode(new_incr, new_limit) : new CmpINode(new_limit, new_incr),
> 1660: //      new CmpINode(new_incr, new_limit),

That shouldn't be here.

src/hotspot/share/opto/loopnode.cpp line 1668:

> 1666: 
> 1667:   _igvn.rehash_node_delayed(bol);
> 1668:   bol->replace_edge(cmp, new_cmp, &_igvn);

You should use: `PhaseIdealLoop::insert_loop_limit_check_predicate()`. `do_is_counted_loop()` may need to add more loop limit checks and the way you've implemented it, I think you're replacing the place holder predicate that we use to create other ones.

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

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


More information about the hotspot-compiler-dev mailing list