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

            Bug ID: 128671
           Summary: [14 Regression] wrong code at -O2: PRE phi translation
                    folds ((m >> 4) & 3) != 0 to 1 using an unavailable
                    SSA name; fixed on trunk by r15-7589 (PR118895)
           Product: gcc
           Version: 14.4.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dori.hoevel at gmail dot com
  Target Milestone: ---
              Host: x86_64-pc-linux-gnu
            Target: x86_64-pc-linux-gnu
             Build: x86_64-pc-linux-gnu

The program below aborts when built with gcc 14 at -O2 or -O3; f(0, 1, 3, 1)
must return 3, but it returns 0.

__attribute__((noipa)) int g(void) { return 1; }

static int hw(int m, int op, int family) {
    if ((m & 3) || op != 2) return 0;
    return family;
}

__attribute__((noipa)) int f(int m, int has, int op, int family) {
    int flush = 3;
    if (!op) {
        flush = 0;
        if (!(m & 16)) return 0;
        if ((m & 48) == 16) return 0;
    }
    int d = has ? (m >> 4) & 3 : 0;
    if (op == 1 && d) return 0;
    if (!g()) return 0;
    if (d) return 0;
    if (flush && hw(m, op, family)) return 0;
    return flush;
}

int main(void) {
    if (f(0, 1, 3, 1) != 3) __builtin_abort();
    return 0;
}

$ gcc -O2 t.c && ./a.out
Aborted

Correct (exit 0): -O0, -O1, -O2 -fno-tree-pre, -O2 -fno-tree-vrp, -O2
-fno-expensive-optimizations, gcc 13.3.0 (Debian 13.3.0-16) at -O2, clang 19 at
-O2. Wrong: gcc and g++ at -O2 and -O3. Of the 45 flags that -O2 adds over -O1,
those three are the only ones that fix it when disabled alone. No warnings with
-Wall -Wextra. No sanitizer report with -fsanitize=undefined,address (gcc; at
-O2 it still aborts, at -O0 it exits 0) or with -fsanitize=memory (clang, exits
0). -O2 -fwrapv also exits 0, but the program has no signed overflow: -fwrapv
only changes the range recorded for _2 (see below) from [16, 16][32, 48] to [0,
48].

Also wrong at -O2 and -O3 with GCC 14.4.0 built from the release tarball and
with the gcc-14 branch at r14-13007-g8671806d8b4719 (14.4.1 20261109). Correct
with GCC 15.3.0 and 16.2.0 built the same way. What fixes it is
r15-7589-gdfd0ced98fcf62 (PR118895, "ICE during PRE"), which is not on the
gcc-14 branch: its tree-ssa-sccvn.cc change applies cleanly to r14-13007, and
with it the testcase exits 0 at -O2 and -O3 and PRE creates "PHI <_5(16),
0(14)>". So this is a wrong-code case for that fix on gcc-14, and a request to
backport it. PR118895's ICE was attributed to r15-7472 (PR118817), which is
only on gcc-15 and later, but the line r15-7589 guards is older: on the gcc-14
branch vn_nary_build_or_lookup_1 still sets mprts_hook =
vn_lookup_simplify_result unconditionally before res_op->resimplify, and this
testcase reaches it there without r15-7472.

gcc -v (Debian's gcc-14, which its changelog says is the gcc-14 branch at git
20250315 and includes the PR115494 fix):
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 14.2.0-19'
--with-bugurl=file:///usr/share/doc/gcc-14/README.Bugs
--enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++,m2,rust --prefix=/usr
--with-gcc-major-version-only --program-suffix=-14
--program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id
--libexecdir=/usr/libexec --without-included-gettext --enable-threads=posix
--libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes
--with-default-libstdcxx-abi=new --enable-libstdcxx-backtrace
--enable-gnu-unique-object --disable-vtable-verify --enable-plugin
--enable-default-pie --with-system-zlib --enable-libphobos-checking=release
--with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch
--disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64
--with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic
--enable-offload-targets=nvptx-none=/build/reproducible-path/gcc-14-14.2.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/reproducible-path/gcc-14-14.2.0/debian/tmp-gcn/usr
--enable-offload-defaulted --without-cuda-driver --enable-checking=release
--build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
--with-build-config=bootstrap-lto-lean --enable-link-serialization=3
Thread model: posix
gcc version 14.2.0 (Debian 14.2.0-19)

In the generated code, the op != 0 && has && ((m >> 4) & 3) == 0 path jumps
into the tail "call g; xor %eax,%eax; ret" that is only right when d != 0:
.L4:                      # op != 0 (flush = 3)
        testl   %esi, %esi    # has ?
        je      .L9
        andl    $48, %edi     # d == 0 ...
        je      .L27          # ... -> "call g; return 0"

The PRE dump (-fdump-tree-pre-details) has "Found partial redundancy for
expression {ne_expr,iftmp.0_33,0}" and "Created phi prephitmp_24 = PHI <1(16),
0(14)>", where BB16 is the split false edge of "if ((op == 1) & (iftmp.0_17 !=
0))", on which "iftmp.0_17 != 0" is not known. This matches what the r15-7589
commit message describes. When phi translation simplifies iftmp.0_17 != 0 on
that edge, ((m >> 4) & 3) != 0 becomes (m & 48) != 0, and because
vn_nary_build_or_lookup_1 sets mprts_hook = vn_lookup_simplify_result also when
it is called from PRE, m & 48 is replaced by the existing _2 = m_14(D) & 48. _2
is defined in a block that is reached only when op == 0 and (m & 16) != 0,
which does not dominate BB16, and it carries the range [16, 16][32, 48], so _2
!= 0 folds to 1. Changing (m & 48) == 16 to (m & 112) == 16 makes the result
correct, as does -fno-tree-vrp. GCC 13.3.0 has the same code in
vn_nary_build_or_lookup_1 but records [0, 48] for _2, and PRE creates "PHI
<_5(17), 0(15)>" there.

Found in a real project (a shader recompiler,
https://github.com/boykopovar/AnyPS5), where the same pattern drops a flush and
fails four tests. The reduction, the gcc-14 branch build and the backport check
were done with the help of AI agents on my machine; I reviewed every result
above, and the release-compiler results were re-run independently.
  • [Bug tree-optimization/128671] N... dori.hoevel at gmail dot com via Gcc-bugs

Reply via email to