]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
testsuite: Add C++ testcase for the recent PTA bug
authorJakub Jelinek <jakub@redhat.com>
Sun, 5 Jul 2026 14:47:47 +0000 (16:47 +0200)
committerJakub Jelinek <jakub@gcc.gnu.org>
Sun, 5 Jul 2026 14:47:47 +0000 (16:47 +0200)
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  <jakub@redhat.com>

* g++.dg/opt/20260703-1.C: New test.

Reviewed-by: Richard Biener <rguenth@suse.de>
gcc/testsuite/g++.dg/opt/20260703-1.C [new file with mode: 0644]

diff --git a/gcc/testsuite/g++.dg/opt/20260703-1.C b/gcc/testsuite/g++.dg/opt/20260703-1.C
new file mode 100644 (file)
index 0000000..007f3d2
--- /dev/null
@@ -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 <typename T>
+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 <I *> 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 ();
+}