From: Jakub Jelinek Date: Sun, 5 Jul 2026 14:47:47 +0000 (+0200) Subject: testsuite: Add C++ testcase for the recent PTA bug X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7da47d53d716b689784eaad20ebe26fdbb33982a;p=thirdparty%2Fgcc.git testsuite: Add C++ testcase for the recent PTA bug On Wed, Jul 01, 2026 at 09:47:46AM +0200, Eric Botcazou wrote: > this is a regression present on mainline, 16, 15 and 14 branches introduced by > the fix for PR tree-optimization/112653 (PTA and return). What happens is > that DSE incorrectly eliminates a call to __builtin_memcpy, whose destination > is obtained from (an equivalent of) malloc and is ultimately returned from the > function. But this happens only when the dynamic allocation is conditional. For us Ada illiterate, here is a C++ testcase which got fixed by this too. 2026-07-05 Jakub Jelinek * g++.dg/opt/20260703-1.C: New test. Reviewed-by: Richard Biener --- diff --git a/gcc/testsuite/g++.dg/opt/20260703-1.C b/gcc/testsuite/g++.dg/opt/20260703-1.C new file mode 100644 index 00000000000..007f3d2f112 --- /dev/null +++ b/gcc/testsuite/g++.dg/opt/20260703-1.C @@ -0,0 +1,46 @@ +// This started to be miscompiled with r15-579 or in a larger +// test with r15-3956 and got fixed with r17-2039. +// DSE would optimize away the store of 42. +// { dg-do run { target c++11 } } +// { dg-options "-O2" } + +struct B { }; + +void *volatile g; + +template +struct D { T foo (void) const { return (T) g; } }; + +struct G : public B +{ + virtual B *bar (int); + struct H : public B { H () : h (42) {} int h; }; + struct I : public B {}; + D g; +}; + +struct H : public G { virtual B *bar (int) { return nullptr; } }; + +B * +G::bar (int x) +{ + if (x == 0) + return new H (); + I *y = g.foo (); + return y; +} + +[[gnu::noipa]] B * +baz (G *x, int y) +{ + return x->bar (y); +} + +int +main () +{ + G g; + G::H *h = (G::H *) baz (&g, 0); + if (h->h != 42) + __builtin_abort (); +}