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

            Bug ID: 128755
           Summary: wrong code at -O1: tree-sink sinks a store in an EH
                    cleanup into a noreturn branch, off the resx path,
                    since r16-2443
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jeffchen006 at gmail dot com
  Target Milestone: ---
            Target: x86_64-pc-linux-gnu

#include <cstdio>
static int d;
bool b;
struct G {
  bool t;
  ~G() noexcept(false) { ++d; if (t) throw 1; }
};
void f() { G g{b}; throw 2; }
int main() {
  try { f(); } catch (...) {}
  try { f(); } catch (...) {}
  printf("%d\n", d);
}

$ g++ -O0 t.cpp && ./a.out
2
$ g++ -O1 t.cpp && ./a.out
0

-O1, -O2, -O3 fail. -O1 -fno-tree-sink gives 2.

Expected 2: each call of f throws 2, and ~G runs once during unwinding (b is
false, so ~G does not throw).

sink1 (-fdump-tree-sink1-details):

  Sinking # .MEM_14 = VDEF <.MEM_8>
  d = _11;
   from bb 3 to bb 4

  <bb 3>: <L0>:
    if (b.0_1 != 0) goto <bb 4>; else goto <bb 5>;
  <bb 4>:
    d.1_10 = d; _11 = d.1_10 + 1; d = _11;
    _13 = __cxa_allocate_exception (4); ... __cxa_throw (_13, &_ZTIi, 0B);
  <bb 5>:
    resx 2

The store d = d + 1 of the cleanup is moved into the path that throws again;
the path that leaves f through resx 2 loses it. resx has no virtual use, so
that path looks like one where the store is dead, but the exception goes on to
main, which reads d.

Suspected cause: statement_sink_location (tree-ssa-sink.cc), the store sinking
added by r16-2443-g44b24b62a44 (PR121220: "sink towards a single virtual
definition ... restricts this to sinking to noreturn paths"). A path that ends
in resx (or any exit by EH) must count as a use of memory. 15.2 is correct and
16.2 fails, which agrees with this commit (not bisected).

PR91355 is another tree-sink problem with resx (sinking onto the EH edge), not
this one.

The same fault also shows with C++17 DR 2176 cleanup of a return object (new in
GCC 16), for example g++.dg/eh/return1.C aborts with -O3 -fwhole-program (and
passes with -fno-tree-sink added), and this test (ICF merges f and g, so f is
not inlined):

#include <cstdio>
static int d;
struct X {
  bool t;
  ~X() noexcept(false) { ++d; if (t) throw 1; }
};
static X f() { X x{true}; return X{false}; }
static X g() { X x{true}; return X{false}; }
int main() {
  try { f(); } catch (...) {}
  try { g(); } catch (...) {}
  printf("%d\n", d);
}

$ g++ -O0 t.cpp && ./a.out
4
$ g++ -O2 t.cpp && ./a.out
2

(-O2 -fno-tree-sink and -O2 -fno-ipa-icf give 4.)

-O0 -fsanitize=undefined,address is clean. Clang gives 2 for the first test.

gcc -Wall -Wextra gives no warnings. -fno-strict-aliasing -fwrapv does not
change the output.

Possible fix: One possible fix is to treat a path that leaves the function
through resx (an exit by EH) as a use of memory in statement_sink_location
(tree-ssa-sink.cc), so that a store is not sunk into a noreturn successor while
another successor can still reach the caller. A minimal guard would be to not
sink a store out of a block that has a successor ending in resx.

gcc version 17.0.0 20261114 (experimental), r17-6275-gfceddda853f,
x86_64-pc-linux-gnu
Configured with: --enable-languages=c,c++ --disable-multilib
--disable-bootstrap --enable-checking=release

Reply via email to