]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
tree-optimization/111773 - avoid CD-DCE of noreturn special calls
authorRichard Biener <rguenther@suse.de>
Thu, 12 Oct 2023 08:13:58 +0000 (10:13 +0200)
committerRichard Biener <rguenther@suse.de>
Fri, 13 Oct 2023 06:34:05 +0000 (08:34 +0200)
The support to elide calls to allocation functions in DCE runs into
the issue that when implementations are discovered noreturn we end
up DCEing the calls anyway, leaving blocks without termination and
without outgoing edges which is both invalid IL and wrong-code when
as in the example the noreturn call would throw.  The following
avoids taking advantage of both noreturn and the ability to elide
allocation at the same time.

For the testcase it's valid to throw or return 10 by eliding the
allocation.  But we have to do either where currently we'd run
off the function.

PR tree-optimization/111773
* tree-ssa-dce.cc (mark_stmt_if_obviously_necessary): Do
not elide noreturn calls that are reflected to the IL.

* g++.dg/torture/pr111773.C: New testcase.

gcc/testsuite/g++.dg/torture/pr111773.C [new file with mode: 0644]
gcc/tree-ssa-dce.cc

diff --git a/gcc/testsuite/g++.dg/torture/pr111773.C b/gcc/testsuite/g++.dg/torture/pr111773.C
new file mode 100644 (file)
index 0000000..af8c687
--- /dev/null
@@ -0,0 +1,31 @@
+// { dg-do run }
+
+#include <new>
+
+void* operator new(std::size_t sz)
+{
+  throw std::bad_alloc{};
+}
+
+int __attribute__((noipa)) foo ()
+{
+  int* p1 = static_cast<int*>(::operator new(sizeof(int)));
+  return 10;
+}
+
+int main()
+{
+  int res;
+  try
+    {
+      res = foo ();
+    }
+  catch (...)
+    {
+      return 0;
+    }
+
+  if (res != 10)
+    __builtin_abort ();
+  return 0;
+}
index f0b02456132f74b75ed71f41c3b899a63267122c..bbdf9312c9f4ee3f17a88b63567b4ef7e63e145e 100644 (file)
@@ -221,6 +221,14 @@ mark_stmt_if_obviously_necessary (gimple *stmt, bool aggressive)
 
     case GIMPLE_CALL:
       {
+       /* Never elide a noreturn call we pruned control-flow for.  */
+       if ((gimple_call_flags (stmt) & ECF_NORETURN)
+           && gimple_call_ctrl_altering_p (stmt))
+         {
+           mark_stmt_necessary (stmt, true);
+           return;
+         }
+
        tree callee = gimple_call_fndecl (stmt);
        if (callee != NULL_TREE
            && fndecl_built_in_p (callee, BUILT_IN_NORMAL))