public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Martin Liska <marxin@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc(refs/users/marxin/heads/PR94314-fix-new-delete-pair-deletion-part-3)] List valid pairs for new and delete operators.
Date: Thu,  9 Apr 2020 13:53:56 +0000 (GMT)	[thread overview]
Message-ID: <20200409135356.0420C385BF81@sourceware.org> (raw)

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

commit dc1f52d856c7d4fcda4a632963158dce2af24b43
Author: Martin Liska <mliska@suse.cz>
Date:   Thu Apr 9 15:50:58 2020 +0200

    List valid pairs for new and delete operators.
    
    gcc/ChangeLog:
    
    2020-04-09  Martin Liska  <mliska@suse.cz>
    
            PR c++/94314
            * cgraphclones.c (set_new_clone_decl_and_node_flags): Drop
            DECL_IS_REPLACEABLE_OPERATOR during cloning.
            * tree-ssa-dce.c (valid_new_delete_pair_p): New function.
            (propagate_necessity): Check operator names.
    
    gcc/testsuite/ChangeLog:
    
    2020-04-09  Martin Liska  <mliska@suse.cz>
    
            PR c++/94314
            * g++.dg/pr94314-4.C: New test.

Diff:
---
 gcc/cgraphclones.c               |  2 +
 gcc/testsuite/g++.dg/pr94314-4.C | 33 ++++++++++++++++
 gcc/tree-ssa-dce.c               | 83 +++++++++++++++++++++++++++++++++++-----
 3 files changed, 108 insertions(+), 10 deletions(-)

diff --git a/gcc/cgraphclones.c b/gcc/cgraphclones.c
index c73b8f810f0..8f541a28b6e 100644
--- a/gcc/cgraphclones.c
+++ b/gcc/cgraphclones.c
@@ -165,6 +165,7 @@ set_new_clone_decl_and_node_flags (cgraph_node *new_node)
   DECL_STATIC_DESTRUCTOR (new_node->decl) = 0;
   DECL_SET_IS_OPERATOR_NEW (new_node->decl, 0);
   DECL_SET_IS_OPERATOR_DELETE (new_node->decl, 0);
+  DECL_IS_REPLACEABLE_OPERATOR (new_node->decl) = 0;
 
   new_node->externally_visible = 0;
   new_node->local = 1;
@@ -1030,6 +1031,7 @@ cgraph_node::create_version_clone_with_body
   DECL_STATIC_DESTRUCTOR (new_decl) = 0;
   DECL_SET_IS_OPERATOR_NEW (new_decl, 0);
   DECL_SET_IS_OPERATOR_DELETE (new_decl, 0);
+  DECL_IS_REPLACEABLE_OPERATOR (new_decl) = 0;
 
   /* Create the new version's call-graph node.
      and update the edges of the new node. */
diff --git a/gcc/testsuite/g++.dg/pr94314-4.C b/gcc/testsuite/g++.dg/pr94314-4.C
new file mode 100644
index 00000000000..afa2a443dc4
--- /dev/null
+++ b/gcc/testsuite/g++.dg/pr94314-4.C
@@ -0,0 +1,33 @@
+/* PR c++/94314.  */
+/* { dg-do run } */
+/* { dg-options "-O2 -fdump-tree-cddce-details -std=c++14" } */
+/* { dg-additional-options "-fdelete-null-pointer-checks" } */
+
+#include <stdio.h>
+
+int count = 0;
+
+__attribute__((malloc, noinline)) void* operator new[](__SIZE_TYPE__ sz) {
+  ++count;
+  return ::operator new(sz);
+}
+
+void operator delete[](void* ptr) noexcept {
+  --count;
+  ::operator delete(ptr);
+}
+
+void operator delete[](void* ptr, __SIZE_TYPE__ sz) noexcept {
+  --count;
+  ::operator delete(ptr, sz);
+}
+
+int main() {
+  delete[] new int[1];
+  if (count != 0)
+    __builtin_abort ();
+
+  return 0;
+}
+
+/* { dg-final { scan-tree-dump-not "Deleting : operator delete" "cddce1"} } */
diff --git a/gcc/tree-ssa-dce.c b/gcc/tree-ssa-dce.c
index fd5f24c746c..ef8c32b9f99 100644
--- a/gcc/tree-ssa-dce.c
+++ b/gcc/tree-ssa-dce.c
@@ -646,6 +646,62 @@ degenerate_phi_p (gimple *phi)
   return true;
 }
 
+/* Return that NEW_CALL and DELETE_CALL are a valid pair of new
+   and delete  operators.  */
+
+static bool
+valid_new_delete_pair_p (gimple *new_call, gimple *delete_call)
+{
+  const char *new_name
+    = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (gimple_call_fndecl (new_call)));
+  const char *delete_name
+    = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (gimple_call_fndecl (delete_call)));
+
+  /* Invalid pairs:
+     non-[] and []
+     aligned and non-aligned
+  */
+
+  const char *valid_pairs[][2] = {
+	/* non-[] operators.  */
+	{"_Znwm", "_ZdlPv" },
+	{"_Znwm", "_ZdlPvm" },
+	{"_Znwm", "_ZdlPvRKSt9nothrow_t" },
+	{"_ZnwmRKSt9nothrow_t", "_ZdlPv" },
+	{"_ZnwmRKSt9nothrow_t", "_ZdlPvm" },
+	{"_ZnwmRKSt9nothrow_t", "_ZdlPvRKSt9nothrow_t" },
+	/* non-[] operators with alignment.  */
+	{"_ZnwmSt11align_val_t", "_ZdlPvmSt11align_val_t" },
+	{"_ZnwmSt11align_val_t", "_ZdlPvSt11align_val_t" },
+	{"_ZnwmSt11align_val_t", "_ZdlPvSt11align_val_tRKSt9nothrow_t" },
+	{"_ZnwmSt11align_val_tRKSt9nothrow_t", "_ZdlPvmSt11align_val_t" },
+	{"_ZnwmSt11align_val_tRKSt9nothrow_t", "_ZdlPvSt11align_val_t" },
+	{"_ZnwmSt11align_val_tRKSt9nothrow_t", "_ZdlPvSt11align_val_tRKSt9nothrow_t" },
+	/* [] operators.  */
+	{ "_Znam", "_ZdaPv" },
+	{ "_Znam", "_ZdaPvm" },
+	{ "_Znam", "_ZdaPvRKSt9nothrow_t" },
+	{ "_ZnamRKSt9nothrow_t", "_ZdaPv" },
+	{ "_ZnamRKSt9nothrow_t", "_ZdaPvm" },
+	{ "_ZnamRKSt9nothrow_t", "_ZdaPvRKSt9nothrow_t" },
+	/* [] operators with alignment.  */
+	{ "_ZnamSt11align_val_t", "_ZdaPvmSt11align_val_t" },
+	{ "_ZnamSt11align_val_t", "_ZdaPvSt11align_val_t" },
+	{ "_ZnamSt11align_val_t", "_ZdaPvSt11align_val_tRKSt9nothrow_t" },
+	{ "_ZnamSt11align_val_tRKSt9nothrow_t", "_ZdaPvmSt11align_val_t"},
+	{ "_ZnamSt11align_val_tRKSt9nothrow_t", "_ZdaPvSt11align_val_t"},
+	{ "_ZnamSt11align_val_tRKSt9nothrow_t", "_ZdaPvSt11align_val_tRKSt9nothrow_t"},
+	{ NULL, NULL }
+  };
+
+  for (unsigned i = 0; valid_pairs[i][0] != NULL; i++)
+    if (strcmp (new_name, valid_pairs[i][0]) == 0
+	&& strcmp (delete_name, valid_pairs[i][1]) == 0)
+      return true;
+
+  return false;
+}
+
 /* Propagate necessity using the operands of necessary statements.
    Process the uses on each statement in the worklist, and add all
    feeding statements which contribute to the calculation of this
@@ -824,16 +880,23 @@ propagate_necessity (bool aggressive)
 			   || DECL_FUNCTION_CODE (def_callee) == BUILT_IN_CALLOC))
 		      || DECL_IS_REPLACEABLE_OPERATOR_NEW_P (def_callee)))
 		{
-		  /* Delete operators can have alignment and (or) size as next
-		     arguments.  When being a SSA_NAME, they must be marked
-		     as necessary.  */
-		  if (is_delete_operator && gimple_call_num_args (stmt) >= 2)
-		    for (unsigned i = 1; i < gimple_call_num_args (stmt); i++)
-		      {
-			tree arg = gimple_call_arg (stmt, i);
-			if (TREE_CODE (arg) == SSA_NAME)
-			  mark_operand_necessary (arg);
-		      }
+		  if (is_delete_operator)
+		    {
+		      if (!valid_new_delete_pair_p (def_stmt, stmt))
+			mark_operand_necessary (gimple_call_arg (stmt, 0));
+
+		      /* Delete operators can have alignment and (or) size
+			 as next arguments.  When being a SSA_NAME, they
+			 must be marked as necessary.  */
+		      if (gimple_call_num_args (stmt) >= 2)
+			for (unsigned i = 1; i < gimple_call_num_args (stmt);
+			     i++)
+			  {
+			    tree arg = gimple_call_arg (stmt, i);
+			    if (TREE_CODE (arg) == SSA_NAME)
+			      mark_operand_necessary (arg);
+			  }
+		    }
 
 		  continue;
 		}


             reply	other threads:[~2020-04-09 13:53 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-09 13:53 Martin Liska [this message]
2020-04-09 14:12 Martin Liska
2020-04-10  8:04 Martin Liska
2020-04-10  8:27 Martin Liska
2020-04-14  6:40 Martin Liska
2020-04-14  7:11 Martin Liska
2020-04-14 10:53 Martin Liska

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200409135356.0420C385BF81@sourceware.org \
    --to=marxin@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).