https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71474
Bug ID: 71474 Summary: PRED_LOOP_IV_COMPARE wrongly calculates number of iterations of a loop Product: gcc Version: 7.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: middle-end Assignee: unassigned at gcc dot gnu.org Reporter: marxin at gcc dot gnu.org Target Milestone: --- Hello. During investigation of poor predictor 'loop iv compare' heuristics, I've come to a problematic test-case: subroutine rubber(arg1, arg2) integer k,l real*8 arg1(3,3,3), arg2(3,3,3) do l=1,3 do k=1,l arg1(k,l,1) = arg2(k,l,1) enddo enddo end 042t.profile_estimate gimple looks as follows: <bb 3>: # l_13 = PHI <1(2), l_28(8)> if (l_13 > 0) goto <bb 4>; else goto <bb 6>; <bb 4>: # k_6 = PHI <1(3)> <bb 5>: # k_12 = PHI <k_6(4), k_26(9)> _1 = (integer(kind=8)) l_13; _2 = _1 * 3; _3 = (integer(kind=8)) k_12; _4 = _2 + _3; _5 = _4 + -4; _11 = *arg2_22(D)[_5]; *arg1_23(D)[_5] = _11; k_26 = k_12 + 1; if (k_12 == l_13) goto <bb 6>; else goto <bb 9>; <bb 9>: goto <bb 5>; <bb 6>: l_28 = l_13 + 1; if (l_13 == 3) goto <bb 7>; else goto <bb 8>; <bb 8>: goto <bb 3>; <bb 7>: return; Problem is that is_comparison_with_loop_invariant_p is called for the gimple compare in BB 3 (if (l_13 > 0)), which does not determine a number of iterations of the loop. As a result we get confusing loop_bound_step, loop_iv_base that are eventually used by predict_iv_comparison to calculate the misleading probability: loop iv compare heuristics of edge 3->4: 0.0% While the proper value should be 100% as l_13 is always a positive number.