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

--- Comment #6 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:997d05b82407adf2a26b9fc15959f2a2b2c3c855

commit r16-5828-g997d05b82407adf2a26b9fc15959f2a2b2c3c855
Author: Tamar Christina <[email protected]>
Date:   Tue Dec 2 10:55:51 2025 +0000

    vect: don't hoist conditional loads above their condition [PR122868]

    The example in the PR

    #include <vector>

    std::vector<bool> x, y;
    int main() { return x == y; }

    now vectorizes but the attributes on std::vector indicate that the vector
is
    aligned to the natural vector alignment.  In C this is equivalent to the
    testcase

    int f (int a[12], int b[12], int n)
    {
        a = __builtin_assume_aligned (a, 16);
        b = __builtin_assume_aligned (b, 16);
        for (int i = 0; i < n; i++)
          {
            if (b[i] == 0)
              return 0;
            if (a[0] > b[i])
              return 1;
          }
        return 2;
    }

    Here the load a[0] is loop invariant, and the vectorizer hoists this out of
the
    loop into the pre-header.  For early break this isn't safe to do as a[0] is
    conditionally valid based on the conditions in the block preceding it.  As
such
    we need some guarantee that the load is valid before we can hoist it or the
load
    needs to be unconditional (e.g. in the loop header block).

    Conceptually alignment peeling can provide this guarantee since making it
    through the prologue means the invariant value was loaded at least once and
so
    we know the address is valid.  At the moment however there's no real
defined
    order between how GCC inserts conditions in the pre-header, so having tried
to
    change the order a few times the load always ends up before the prologue. 
So
    for now I marked it as a missed optimization.

    Since we still can hoist invariant loads if in the header, I didn't change
    LOOP_VINFO_NO_DATA_DEPENDENCIES since that would be global and instead I
    modified the usage site of LOOP_VINFO_NO_DATA_DEPENDENCIES.

    gcc/ChangeLog:

            PR tree-optimization/122868
            * tree-vect-stmts.cc (vectorizable_load): Don't hoist loop
invariant
            conditional loads unless in header.

    gcc/testsuite/ChangeLog:

            PR tree-optimization/122868
            * gcc.dg/vect/vect-early-break_140-pr122868_1.c: New test.
            * gcc.dg/vect/vect-early-break_140-pr122868_2.c: New test.
            * gcc.dg/vect/vect-early-break_140-pr122868_3.c: New test.
            * gcc.dg/vect/vect-early-break_140-pr122868_4.c: New test.

Reply via email to