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.

Ok for trunk and perhaps release branches too?

2026-07-03  Jakub Jelinek  <[email protected]>

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

--- gcc/testsuite/g++.dg/opt/20260703-1.C.jj    2026-07-03 17:01:03.845156091 
+0200
+++ gcc/testsuite/g++.dg/opt/20260703-1.C       2026-07-03 17:00:48.744347853 
+0200
@@ -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 ();
+}


        Jakub

Reply via email to