]> git.ipfire.org Git - thirdparty/gcc.git/commit
vect: don't hoist conditional loads above their condition [PR122868]
authorTamar Christina <tamar.christina@arm.com>
Tue, 2 Dec 2025 10:55:51 +0000 (10:55 +0000)
committerTamar Christina <tamar.christina@arm.com>
Tue, 2 Dec 2025 10:55:51 +0000 (10:55 +0000)
commit997d05b82407adf2a26b9fc15959f2a2b2c3c855
treec87cca37a86d19e58a95b6ac3083eecb2b661e69
parentf76e54a7cff14bfbbb1d83c45e042b18c325ce67
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.
gcc/testsuite/gcc.dg/vect/vect-early-break_140-pr122868_1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/vect/vect-early-break_140-pr122868_2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/vect/vect-early-break_140-pr122868_3.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/vect/vect-early-break_140-pr122868_4.c [new file with mode: 0644]
gcc/tree-vect-stmts.cc