JDK-8231246 : C2 computes wrong trip count for loops
  • Type: Bug
  • Component: hotspot
  • Sub-Component: compiler
  • Affected Version: 11,12,13,14
  • Priority: P3
  • Status: Closed
  • Resolution: Won't Fix
  • Submitted: 2019-09-19
  • Updated: 2019-09-24
  • Resolved: 2019-09-24
Related Reports
Relates :  
Description
The trip count computed by IdealLoopTree::compute_profile_trip_cnt() is wrong. Both of the following loops have 9 iterations:

        for (int i = 1; i < 10; i++) {

        }

        int i = 1;
        do {

        } while (++i < 10);

But the trip count of the first one is computed as ~10 (9,9..):

Loop: N0/N0  has_sfpt
  Loop: N180/N179  limit_check
    Loop: N181/N91  limit_check counted [1,10),+1 (-1 iters)  has_sfpt strip_mined
  Loop: N191/N190  limit_check
    Loop: N192/N164  limit_check counted [2,10),+1 (-1 iters)  has_sfpt strip_mined
compute_profile_trip_cnt  lp: 181 cnt: 9,986592

compute_profile_trip_cnt  lp: 192 cnt: 9,000001
Comments
Okay, assuming that there is no simple check for detecting this, closing as WNF seems reasonable to me.
24-09-2019

The trip count computed from the profile data by adding the count of every exit from the loop and the backedge count and using the following formula: trip_cnt = (loop_back_cnt + loop_exit_cnt) / loop_exit_cnt; That assumes exit of the loop happens after the loop body is run which is not the case for a for loop for which javac compiles the exit test first and an unconditional jump at the end of the body. The trip count computed from the profile data doesn't need to be accurate so an off by 1 error seems acceptable to me.
23-09-2019

ILW = Wrong computation of loop trip count (might lead to over-unrolling), with loops that have a small trip count, no workaround = MMH = P3
20-09-2019