public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r11-8495] c++: no clobber for C++20 destroying delete [PR91859]
@ 2021-06-01 15:53 Jason Merrill
  0 siblings, 0 replies; only message in thread
From: Jason Merrill @ 2021-06-01 15:53 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:ee3edeb01eca1cc8d7ebe777b4adb333f0c1118a

commit r11-8495-gee3edeb01eca1cc8d7ebe777b4adb333f0c1118a
Author: Jason Merrill <jason@redhat.com>
Date:   Fri May 28 17:05:23 2021 -0400

    c++: no clobber for C++20 destroying delete [PR91859]
    
    Before C++20 added destroying operator delete, by the time we called
    operator delete for a pointer, the object would already be gone.  But that
    isn't true for destroying delete.  Since the optimizers' assumptions about
    operator delete are based on either DECL_IS_REPLACEABLE_OPERATOR (which
    already is not set) or CALL_FROM_NEW_OR_DELETE_P, let's avoid setting the
    latter flag in this case.
    
            PR c++/91859
    
    gcc/ChangeLog:
    
            * tree.h (CALL_FROM_NEW_OR_DELETE_P): Adjust comment.
    
    gcc/cp/ChangeLog:
    
            * call.c (build_op_delete_call): Don't set CALL_FROM_NEW_OR_DELETE_P
            for destroying delete.
            * init.c (build_delete): Don't clobber before destroying delete.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp2a/destroying-delete5.C: New test.

Diff:
---
 gcc/tree.h                                      |  2 +-
 gcc/cp/call.c                                   |  4 ++-
 gcc/cp/init.c                                   |  5 +++-
 gcc/testsuite/g++.dg/cpp2a/destroying-delete5.C | 36 +++++++++++++++++++++++++
 4 files changed, 44 insertions(+), 3 deletions(-)

diff --git a/gcc/tree.h b/gcc/tree.h
index f00ea2ef0af..5d85792b305 100644
--- a/gcc/tree.h
+++ b/gcc/tree.h
@@ -937,7 +937,7 @@ extern void omp_clause_range_check_failed (const_tree, const char *, int,
 
 /* In a CALL_EXPR, if the function being called is DECL_IS_OPERATOR_NEW_P or
    DECL_IS_OPERATOR_DELETE_P, true for allocator calls from C++ new or delete
-   expressions.  */
+   expressions.  Not set for C++20 destroying delete operators.  */
 #define CALL_FROM_NEW_OR_DELETE_P(NODE) \
   (CALL_EXPR_CHECK (NODE)->base.protected_flag)
 
diff --git a/gcc/cp/call.c b/gcc/cp/call.c
index cae87eae234..4c4cb5d0491 100644
--- a/gcc/cp/call.c
+++ b/gcc/cp/call.c
@@ -7211,8 +7211,10 @@ build_op_delete_call (enum tree_code code, tree addr, tree size,
 	 treat that as an implicit delete-expression.  This is also called for
 	 the delete if the constructor throws in a new-expression, and for a
 	 deleting destructor (which implements a delete-expression).  */
+      /* But leave this flag off for destroying delete to avoid wrong
+	 assumptions in the optimizers.  */
       tree call = extract_call_expr (ret);
-      if (TREE_CODE (call) == CALL_EXPR)
+      if (TREE_CODE (call) == CALL_EXPR && !destroying_delete_p (fn))
 	CALL_FROM_NEW_OR_DELETE_P (call) = 1;
 
       return ret;
diff --git a/gcc/cp/init.c b/gcc/cp/init.c
index a85f4d50750..04d495807ef 100644
--- a/gcc/cp/init.c
+++ b/gcc/cp/init.c
@@ -4881,7 +4881,10 @@ build_delete (location_t loc, tree otype, tree addr,
 			    complain);
     }
 
-  if (!destroying_delete && type_build_dtor_call (type))
+  if (destroying_delete)
+    /* The operator delete will call the destructor.  */
+    expr = addr;
+  else if (type_build_dtor_call (type))
     expr = build_dtor_call (cp_build_fold_indirect_ref (addr),
 			    auto_delete, flags, complain);
   else
diff --git a/gcc/testsuite/g++.dg/cpp2a/destroying-delete5.C b/gcc/testsuite/g++.dg/cpp2a/destroying-delete5.C
new file mode 100644
index 00000000000..553c964b9e9
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/destroying-delete5.C
@@ -0,0 +1,36 @@
+// PR c++/91859
+// { dg-do run { target c++20 } }
+// { dg-additional-options -O2 }
+
+#include <cstdlib>
+#include <new>
+
+struct Expression {
+  int i = 0;
+  void *operator new(std::size_t);
+  void operator delete(Expression *, std::destroying_delete_t);
+};
+
+void * Expression::operator new(std::size_t sz)
+{
+  return std::malloc(sz);
+}
+
+int i;
+
+void Expression::operator delete(Expression *p, std::destroying_delete_t)
+{
+  Expression * e = p;
+  ::i = e->i;
+  p->~Expression();
+  std::free(p);
+}
+
+int main()
+{
+  auto p = new Expression();
+  p->i = 1;
+  delete p;
+  if (i != 1)
+    __builtin_abort();
+}


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-06-01 15:53 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-01 15:53 [gcc r11-8495] c++: no clobber for C++20 destroying delete [PR91859] Jason Merrill

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).