JDK-8356647 : C2: Excessively strict assert in PhaseIdealLoop::do_unroll
  • Type: Bug
  • Component: hotspot
  • Sub-Component: compiler
  • Affected Version: 11,17,21,25
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2025-05-09
  • Updated: 2025-05-28
  • Resolved: 2025-05-28
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 25
25 masterFixed
Related Reports
Relates :  
Relates :  
Description
In PhaseIdealLoop::do_unroll the assert
```
assert(trip_count > 0 && (julong)trip_count < (julong)max_juint/2, "sanity");
```
is too strict and should be `<=` instead of `<` (at least).

Reasoning:
with:
```
    int new_stride_con = stride_con * 2;
    int stride_m    = new_stride_con - (stride_con > 0 ? 1 : -1);
    jlong trip_count = (limit_con - init_con + stride_m)/new_stride_con;
```

if
limit_con = max_int = 2^31 - 1
init_con = min_int + 1 = -2^31 + 1
stride_con = 1
then
stride_m = 2
and trip_count = (2^31 - 1 - (-2^31 + 1) + 1)/2 = (2^32 - 1) / 2 = 2^31 - 1/2 = max_juint/2 + 1/2 (so = max_juint/2 in integer arithmetic)

This was observed on compiler/c2/Test6850611.java with -XX:+StressLoopPeeling

It is not clear to me if the case `init_con = min_int = -2^31` is possible, but I don't see why not. That would lead to trip_count = 2^31 = max_juint/2 + 1. In this case, a correct assert would be
```
assert(trip_count > 0 && (julong)trip_count <= (julong)max_juint/2 + 1, "sanity");
```
Comments
Changeset: 4b9290af Branch: master Author: Marc Chevalier <mchevalier@openjdk.org> Date: 2025-05-28 13:26:15 +0000 URL: https://git.openjdk.org/jdk/commit/4b9290af0a46bdf662735c24d00732a4c1601102
28-05-2025

A pull request was submitted for review. Branch: master URL: https://git.openjdk.org/jdk/pull/25295 Date: 2025-05-19 06:43:38 +0000
19-05-2025

ILW = Assert during C2 compilation (harmless in product), with loop peeling stress flag, no workaround but disable compilation of affected method = MLM = P4
12-05-2025