https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128028
--- Comment #9 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Tamar Christina <[email protected]>: https://gcc.gnu.org/g:1b7a552c8dbb712244968ac528481d0cf12d3461 commit r17-5740-g1b7a552c8dbb712244968ac528481d0cf12d3461 Author: Tamar Christina <[email protected]> Date: Thu Oct 29 13:14:34 2026 +0000 vect: Fix peeled early-break loop counts [PR128028] This patch moves the counting-exit handling for peeled early-break loops into vect_set_loop_condition. The loop-control helpers now build the condition and leave the caller to place it. This shares the code for full and partial vectors and removes vect_reuse_loop_latch. It also fixes the iteration counts for loops where the counting exit comes before the body. For example: int bump (const long *restrict a, const long *restrict b, long *restrict c, unsigned long n) { unsigned long i = 0; while (i != n) { unsigned long j = i++; c[j] += 1; if (a[j] != b[j]) return 1; } return 0; } Previously the code would adjust the IV and the bounds during loop peeling and would set it up in anticipating of the latch of the loop bein in an unnatural position. After the patch of mine to peel the header the latch is now in a normal position. Which means that by updating the IV to be that of the previous iteration we would do the final iteration twice. The tests now tests for this as well. Conceptually by moving the header adjustment code into the caller, i.e. vect_gen_vector_loop_niters, we can remove almost all the special treatment of LOOP_VINFO_EARLY_BREAKS_VECT_PEELED from the loop manip code, since we know loop control would always adjust it. By doing so we can also remove all the additional code added to the individual loop control methods that conditional adjust where the latch is inserted. The caller now decides and so the code is much simpler. Only uncounted loops are treated special still, and we might be able to remove that as well. But I'm delaying that until I see Victor's patches to update uncounted loops. Co-Authored-By: Alfie Richards <[email protected]> gcc/ChangeLog: PR tree-optimization/128028 * tree-vect-loop-manip.cc (vect_set_loop_controls_directly): Move the trip count adjustment to vect_gen_vector_loop_niters. (vect_reuse_loop_latch): Remove. (vect_set_loop_condition_partial_vectors): Leave condition insertion and final IV updates to the caller. (vect_set_loop_condition_partial_vectors_avx512): Likewise. (vect_set_loop_condition_normal): Leave condition insertion to the caller and use the completed iteration count. (vect_set_loop_condition): Share exit relocation and partial-vector final IV updates. (vect_gen_vector_loop_niters): Exclude the header-only iteration. (vect_gen_vector_loop_niters_mult_vf): Remove the VF subtraction. (vect_do_peeling): Replay the main exit only for uncounted loops. * tree-vect-loop.cc (vect_analyze_loop_2): Account for the header-only iteration in the versioning threshold. gcc/testsuite/ChangeLog: PR tree-optimization/128028 * gcc.dg/vect/vect-early-break-pr128028-unlimited.c: New test. * gcc.dg/vect/vect-early-break-pr128028.c: New test.
