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

            Bug ID: 128384
           Summary: Unnecessary alignment versioning for early-exit
                    comparison of fixed-size arrays
           Product: gcc
           Version: 17.0
               URL: https://godbolt.org/z/a6dd9ro8v
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: adamant.pwn at gmail dot com
  Target Milestone: ---
              Host: x86_64-linux-gnu
            Target: x86_64-linux-gnu
             Build: x86_64-linux-gnu

Created attachment 66072
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=66072&action=edit
GCC 17 snapshot: compiler version and configuration

GCC 17.0.0 20261102 (7eb405c3d119), x86_64-linux-gnu:

    extern "C" bool equal(const char (&a)[64], const char (&b)[64]) {
        for (int i = 0; i < 64; ++i)
            if (a[i] != b[i]) return false;
        return true;
    }

    g++ -O3 -march=x86-64-v3 -S repro.cpp

GCC checks whether the pointers have matching alignment modulo 32, peels to
alignment, and uses vmovdqa. Different alignments select a fully scalar loop.

Replacing the body with `return __builtin_memcmp(a, b, 64) == 0;` gives
unaligned vector loads without these guards. libstdc++'s std::equal and
std::array::operator== use this memcmp path. Could GCC also use the array
extent to establish that the loop's vector loads are safe?

Two relevant checks:

- ref_within_array_bound rejects these accesses as potentially flexible-sized
(confirmed under GDB with GCC 16.2.1):
https://github.com/gcc-mirror/gcc/blob/7eb405c3d119d4fc84b31f21d6499069fbfedad8/gcc/tree-if-conv.cc#L790-L797

- Even with known bounds, vect_enhance_data_refs_alignment only relaxes the
speculative-read alignment requirement for nonconstant vector widths, excluding
AVX2:
https://github.com/gcc-mirror/gcc/blob/7eb405c3d119d4fc84b31f21d6499069fbfedad8/gcc/tree-vect-data-refs.cc#L2331-L2352

Original std::array example: https://godbolt.org/z/a6dd9ro8v

Related: PR115130 and PR126366. PR120352 fixed the scalar recheck after a
vector mismatch; alignment handling remains.

Reduced and checked with AI assistance.

Reply via email to