This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Mark DECL_SET_IS_OPERATOR_DELETE for user-provided delete operators.


Hi.

As discussed with Marc, I'm suggesting to mark user-provided
delete operators with DECL_SET_IS_OPERATOR_DELETE in order to allow
possible DCE for it. I've checked that clang can also optimize that
situation out.

Patch can bootstrap on x86_64-linux-gnu and survives regression tests.

Ready to be installed?
Thanks,
Martin

gcc/cp/ChangeLog:

2019-07-30  Martin Liska  <mliska@suse.cz>

	* decl.c (grok_op_properties):
	Mark DECL_SET_IS_OPERATOR_DELETE for user-provided delete operators.

gcc/testsuite/ChangeLog:

2019-07-30  Martin Liska  <mliska@suse.cz>

	* g++.dg/cpp1y/new2.C: New test.
---
 gcc/cp/decl.c                     |  5 +++-
 gcc/testsuite/g++.dg/cpp1y/new2.C | 39 +++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp1y/new2.C


diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 9fa090c8767..bc438d23db3 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -13670,7 +13670,10 @@ grok_op_properties (tree decl, bool complain)
 	}
 
       if (op_flags & OVL_OP_FLAG_DELETE)
-	coerce_delete_type (decl, loc);
+	{
+	  DECL_SET_IS_OPERATOR_DELETE (decl, true);
+	  coerce_delete_type (decl, loc);
+	}
       else
 	{
 	  DECL_SET_IS_OPERATOR_NEW (decl, true);
diff --git a/gcc/testsuite/g++.dg/cpp1y/new2.C b/gcc/testsuite/g++.dg/cpp1y/new2.C
new file mode 100644
index 00000000000..926e7960528
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/new2.C
@@ -0,0 +1,39 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -std=c++17 -fdump-tree-cddce-details" } */
+
+#include <cstdio>
+#include <cstdlib>
+#include <new>
+
+void* operator new(std::size_t sz)
+{
+  std::printf("global op new called, size = %zu\n", sz);
+  void *ptr = std::malloc(sz);
+  if (ptr)
+    return ptr;
+  else
+    throw std::bad_alloc{};
+}
+
+void operator delete(void* ptr) noexcept
+{
+  std::puts("global op delete called");
+  std::free(ptr);
+}
+
+void
+new_primitive_load() {
+  int *x = new int;
+  int tmp = *x;
+  delete x;
+}
+
+void
+new_array_load() {
+  int *x = new int[10];
+  int tmp = x[4];
+  delete [] x;
+}
+
+/* { dg-final { scan-tree-dump-times "Deleting : _\\d+ = operator new" 2 "cddce1"} } */
+/* { dg-final { scan-tree-dump-times "Deleting : operator delete" 2 "cddce1"} } */


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]