https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128580

--- Comment #4 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Kyrylo Tkachov <[email protected]>:

https://gcc.gnu.org/g:d438c4df9c804a865bc3353146d96b44417177a7

commit r17-6349-gd438c4df9c804a865bc3353146d96b44417177a7
Author: Kyrylo Tkachov <[email protected]>
Date:   Thu Nov 12 01:48:32 2026 -0800

    tree-optimization/128580 - Fix chrec multiplication across nested loops

    chrec_fold_multiply_poly_poly multiplies two chrecs of different, nested
    loops.  The chrec of the outer loop is invariant in the inner loop, so
    both the base and the step of the inner chrec need to be scaled by it:

      {a, +, b}_1 * {c, +, d}_2  ->  {c*{a, +, b}_1, +, d*{a, +, b}_1}_2

    The step was left unscaled, giving {c*{a, +, b}_1, +, d}_2.  For i * k
    in a nest over i, k, j with i = {0, +, 1}_1 and k = {0, +, 1}_2 this
    produced {0, +, 1}_2 instead of {0, +, {0, +, 1}_1}_2.  The access
    function of a[i * k + j] then appeared independent of i and loop
    interchange swapped the loops over i and k, giving wrong code.

    Scale the step in both nested-loop cases.

    Bootstrapped and tested on aarch64-none-linux-gnu.

    gcc/ChangeLog:

            PR tree-optimization/128580
            * tree-chrec.cc (chrec_fold_multiply_poly_poly): Multiply
            CHREC_RIGHT by the invariant chrec in the nested-loop cases.

    gcc/testsuite/ChangeLog:

            PR tree-optimization/128580
            * gcc.dg/torture/pr128580.c: New test.

    Signed-off-by: Kyrylo Tkachov <[email protected]>

Reply via email to