public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Martin Liška" <mliska@suse.cz>
To: Jakub Jelinek <jakub@redhat.com>
Cc: Jonathan Wakely <jwakely.gcc@gmail.com>,
	Marc Glisse <marc.glisse@inria.fr>,
	Richard Biener <richard.guenther@gmail.com>,
	Jason Merrill <jason@redhat.com>, Jan Hubicka <hubicka@ucw.cz>,
	GCC Patches <gcc-patches@gcc.gnu.org>,
	Nathan Sidwell <nathan@acm.org>
Subject: Re: [PATCH] Allow new/delete operator deletion only for replaceable.
Date: Tue, 14 Apr 2020 09:09:43 +0200	[thread overview]
Message-ID: <069c1c26-d74a-0eea-554b-1aeb6881dca7@suse.cz> (raw)
In-Reply-To: <20200410091754.GQ2212@tucnak>

[-- Attachment #1: Type: text/plain, Size: 4513 bytes --]

On 4/10/20 11:17 AM, Jakub Jelinek wrote:
> On Fri, Apr 10, 2020 at 10:29:29AM +0200, Martin Liška wrote:
>> +/* Valid pairs of new and delete operators for DCE.  */
>> +static hash_set<nofree_string_hash> *valid_pairs = NULL;
>> +
>> +/* 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)));
>> +
>> +  if (new_name[0] == '_' and new_name[1] == '_')
>> +    ++new_name;
>> +  if (delete_name[0] == '_' and delete_name[1] == '_')
>> +    ++delete_name;
>> +
>> +  char *needle = concat (new_name, ":", delete_name, NULL);
>> +
>> +  if (valid_pairs == NULL)
>> +    {
>> +      valid_pairs = new hash_set<nofree_string_hash> ();
>> +      /* Invalid pairs:
>> +	 non-[] and []
>> +	 aligned and non-aligned
>> +      */
>> +
>> +      const char *pairs[] = {
>> +	  /* non-[] operators.  */
>> +	  "_Znwm:_ZdlPv" ,
>> +	  "_Znwm:_ZdlPvm" ,
> 
> Formatting, I think the /* and "_Z should be two columns from const char,
> and no space before ,
> Not sure I like using the : and using a hash table looks like an overkill to
> me, don't we have only 8 options, and most of them with different identifier
> lengths?  The concat itself will take a while...
> And, what's worse, the m in there is really different on different targets.
> It can be j, m or y depending on what fundamental type size_t is.
> 
> So why not (completely untested):

Hi.

I've updated a bit the Jakub's patch.

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

Ready to be installed?
Thanks,
Martin

> 
> static bool
> valid_new_delete_pair_p (gimple *new_call, gimple *delete_call)
> {
>    tree new_asm = DECL_ASSEMBLER_NAME (gimple_call_fndecl (new_call));
>    tree delete_asm = DECL_ASSEMBLER_NAME (gimple_call_fndecl (delete_call));
>    const char *new_name = IDENTIFIER_POINTER (new_asm);
>    const char *delete_name = IDENTIFIER_POINTER (delete_asm);
>    unsigned int new_len = IDENTIFIER_LENGTH (mew_asm);
>    unsigned int delete_len = IDENTIFIER_LENGTH (delete_asm);
>    if (new_name < 5 || delete_len < 6)
>      return false;
>    if (new_name[0] == '_')
>      ++new_name, --new_len;
>    if (new_name[0] == '_')
>      ++new_name, --new_len;
>    if (delete_name[0] == '_')
>      ++delete_name, --delete_len;
>    if (delete_name[0] == '_')
>      ++delete_name, --delete_len;
>    if (new_len < 4 || delete_len < 5)
>      return false;
>    /* *_len is now just the length after initial underscores.  */
>    if (new_name[0] != 'Z' || new_name[1] != 'n')
>      return false;
>    if (delete_name[0] != 'Z' || delete_name[1] != 'd')
>      return false;
>    /* _Znw must match _Zdl, _Zna must match _Zda.  */
>    if ((new_name[2] != 'w' || delete_name[2] != 'l')
>        && (new_name[2] != 'a' || delete_name[2] != 'a'))
>      return false;
>    if (new_name[3] != 'j' && new_name[3] != 'm' && new_name[3] != 'y')
>      return false;
>    if (delete_name[3] != 'P' || delete_name[4] != 'v')
>      return false;
>    if (new_len == 4
>        || (new_len == 18 && !memcmp (new_name + 4, "RKSt9nothrow_t", 14)))
>      {
>        /* _ZnXY or _ZnXYRKSt9nothrow_t matches
> 	 _ZdXPv, _ZdXPvY and _ZdXPvRKSt9nothrow_t.  */
>        if (delete_len == 5)
> 	return true;
>        if (delete_len == 6 && delete_name[5] == new_name[3])
> 	return true;
>        if (delete_len == 19 && !memcmp (delete_name + 5, "RKSt9nothrow_t", 14))
> 	return true;
>      }
>    else if ((new_len == 19 && !memcmp (new_name + 4, "St11align_val_t", 15))
> 	   || (new_len == 33
> 	       && !memcmp (new_name + 4, "St11align_val_tRKSt9nothrow_t", 29)))
>      {
>        /* _ZnXYSt11align_val_t or _ZnXYSt11align_val_tRKSt9nothrow_t matches
> 	 _ZdXPvSt11align_val_t or _ZdXPvYSt11align_val_t or  or
> 	 _ZdXPvSt11align_val_tRKSt9nothrow_t.  */
>        if (delete_len == 20 && !memcmp (delete_name + 5, "St11align_val_t", 15))
> 	return true;
>        if (delete_len == 21
> 	  && delete_name[5] == new_name[3]
> 	  && !memcmp (delete_name + 6, "St11align_val_t", 15))
> 	return true;
>        if (delete_len == 34
> 	  && !memcmp (delete_name + 5, "St11align_val_tRKSt9nothrow_t", 29))
> 	return true;
>      }
>    return false;
> }
> 
> 	Jakub
> 


[-- Attachment #2: 0001-List-valid-pairs-for-new-and-delete-operators.patch --]
[-- Type: text/x-patch, Size: 7492 bytes --]

From 60626d4b040bc502c563b7c014b62751a517705a Mon Sep 17 00:00:00 2001
From: Martin Liska <mliska@suse.cz>
Date: Thu, 9 Apr 2020 15:50:58 +0200
Subject: [PATCH] List valid pairs for new and delete operators.

gcc/ChangeLog:

2020-04-09  Martin Liska  <mliska@suse.cz>
	    Jakub Jelinek  <jakub@redhat.com>

	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.
	(perform_tree_ssa_dce): Delete valid_pairs.

gcc/testsuite/ChangeLog:

2020-04-09  Martin Liska  <mliska@suse.cz>
	    Jakub Jelinek  <jakub@redhat.com>

	PR c++/94314
	* g++.dg/pr94314-4.C: New test.
---
 gcc/cgraphclones.c               |   2 +
 gcc/testsuite/g++.dg/pr94314-4.C |  33 ++++++++++
 gcc/tree-ssa-dce.c               | 103 ++++++++++++++++++++++++++++---
 3 files changed, 128 insertions(+), 10 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/pr94314-4.C

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..69566cd50e2 100644
--- a/gcc/tree-ssa-dce.c
+++ b/gcc/tree-ssa-dce.c
@@ -646,6 +646,80 @@ degenerate_phi_p (gimple *phi)
   return true;
 }
 
+/* Valid pairs of new and delete operators for DCE.  */
+static hash_set<nofree_string_hash> *valid_pairs = NULL;
+
+/* 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)
+{
+  tree new_asm = DECL_ASSEMBLER_NAME (gimple_call_fndecl (new_call));
+  tree delete_asm = DECL_ASSEMBLER_NAME (gimple_call_fndecl (delete_call));
+  const char *new_name = IDENTIFIER_POINTER (new_asm);
+  const char *delete_name = IDENTIFIER_POINTER (delete_asm);
+  unsigned int new_len = IDENTIFIER_LENGTH (new_asm);
+  unsigned int delete_len = IDENTIFIER_LENGTH (delete_asm);
+
+  if (new_len < 5 || delete_len < 6)
+    return false;
+  if (new_name[0] == '_')
+    ++new_name, --new_len;
+  if (new_name[0] == '_')
+    ++new_name, --new_len;
+  if (delete_name[0] == '_')
+    ++delete_name, --delete_len;
+  if (delete_name[0] == '_')
+    ++delete_name, --delete_len;
+  if (new_len < 4 || delete_len < 5)
+    return false;
+  /* *_len is now just the length after initial underscores.  */
+  if (new_name[0] != 'Z' || new_name[1] != 'n')
+    return false;
+  if (delete_name[0] != 'Z' || delete_name[1] != 'd')
+    return false;
+  /* _Znw must match _Zdl, _Zna must match _Zda.  */
+  if ((new_name[2] != 'w' || delete_name[2] != 'l')
+      && (new_name[2] != 'a' || delete_name[2] != 'a'))
+    return false;
+  /* 'j', 'm' and 'y' correspond to size_t.  */
+  if (new_name[3] != 'j' && new_name[3] != 'm' && new_name[3] != 'y')
+    return false;
+  if (delete_name[3] != 'P' || delete_name[4] != 'v')
+    return false;
+  if (new_len == 4
+      || (new_len == 18 && !memcmp (new_name + 4, "RKSt9nothrow_t", 14)))
+    {
+      /* _ZnXY or _ZnXYRKSt9nothrow_t matches
+	 _ZdXPv, _ZdXPvY and _ZdXPvRKSt9nothrow_t.  */
+      if (delete_len == 5)
+	return true;
+      if (delete_len == 6 && delete_name[5] == new_name[3])
+	return true;
+      if (delete_len == 19 && !memcmp (delete_name + 5, "RKSt9nothrow_t", 14))
+	return true;
+    }
+  else if ((new_len == 19 && !memcmp (new_name + 4, "St11align_val_t", 15))
+	   || (new_len == 33
+	       && !memcmp (new_name + 4, "St11align_val_tRKSt9nothrow_t", 29)))
+    {
+      /* _ZnXYSt11align_val_t or _ZnXYSt11align_val_tRKSt9nothrow_t matches
+	 _ZdXPvSt11align_val_t or _ZdXPvYSt11align_val_t or  or
+	 _ZdXPvSt11align_val_tRKSt9nothrow_t.  */
+      if (delete_len == 20 && !memcmp (delete_name + 5, "St11align_val_t", 15))
+	return true;
+      if (delete_len == 21
+	  && delete_name[5] == new_name[3]
+	  && !memcmp (delete_name + 6, "St11align_val_t", 15))
+	return true;
+      if (delete_len == 34
+	  && !memcmp (delete_name + 5, "St11align_val_tRKSt9nothrow_t", 29))
+	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 +898,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;
 		}
@@ -1662,6 +1743,8 @@ perform_tree_ssa_dce (bool aggressive)
   visited = BITMAP_ALLOC (NULL);
   propagate_necessity (aggressive);
   BITMAP_FREE (visited);
+  delete valid_pairs;
+  valid_pairs = NULL;
 
   something_changed |= eliminate_unnecessary_stmts ();
   something_changed |= cfg_altered;
-- 
2.26.0


  reply	other threads:[~2020-04-14  7:09 UTC|newest]

Thread overview: 55+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-30  8:40 [PATCH] Check DECL_CONTEXT of new/delete operators Martin Liška
2020-03-30  8:53 ` Richard Biener
2020-03-31 12:29   ` Jan Hubicka
2020-03-31 12:38     ` Martin Liška
2020-04-03 15:26       ` Jan Hubicka
2020-04-03 15:42         ` Jan Hubicka
2020-04-04 11:53           ` Jan Hubicka
2020-04-06  9:27             ` Richard Biener
2020-04-06 15:10               ` Jason Merrill
2020-04-06  8:34         ` Martin Liška
2020-04-06 12:45           ` Nathan Sidwell
2020-04-07  8:26             ` Jonathan Wakely
2020-04-07  9:29               ` Richard Biener
2020-04-07  9:49                 ` Jan Hubicka
2020-04-07 10:22                   ` Richard Biener
2020-04-07 10:42                     ` Martin Liška
2020-04-07 11:41                 ` Jonathan Wakely
2020-04-07 10:46             ` Martin Liška
2020-04-07 11:29             ` Jonathan Wakely
2020-04-07 11:40               ` Richard Biener
2020-04-07 11:46                 ` Jonathan Wakely
2020-04-07 11:57                   ` Richard Biener
2020-04-07 15:00                     ` [PATCH] Allow new/delete operator deletion only for replaceable Martin Liška
2020-04-08  8:47                       ` Richard Biener
2020-04-08 13:20                         ` Jason Merrill
2020-04-08 13:32                           ` Jakub Jelinek
2020-04-08 13:34                             ` Jason Merrill
2020-04-08 15:16                               ` Martin Liška
2020-04-08 15:46                                 ` Jan Hubicka
2020-04-08 16:06                                   ` Jakub Jelinek
2020-04-09  5:05                                 ` Martin Liška
2020-04-09  6:45                                   ` Richard Biener
2020-04-09  6:59                                     ` Martin Liška
2020-04-09  7:21                                       ` Richard Biener
2020-04-09  7:55                                       ` Jakub Jelinek
2020-04-09  8:04                                     ` Marc Glisse
2020-04-09  8:13                                       ` Jonathan Wakely
2020-04-10  8:08                                         ` Martin Liška
2020-04-10  8:18                                           ` Jonathan Wakely
2020-04-10  8:29                                             ` Martin Liška
2020-04-10  9:17                                               ` Jakub Jelinek
2020-04-14  7:09                                                 ` Martin Liška [this message]
2020-04-14  7:11                                                   ` Martin Liška
2020-04-14  8:37                                                     ` Jakub Jelinek
2020-04-14 10:54                                                       ` Martin Liška
2020-04-17  7:05                                                         ` Jakub Jelinek
2020-04-17  8:12                                                           ` Jonathan Wakely
2020-04-10  8:37                                           ` Marc Glisse
2020-04-10  9:11                                             ` Iain Sandoe
2020-04-09 16:55                                   ` Jason Merrill
2020-04-07 15:16                     ` [PATCH] Check DECL_CONTEXT of new/delete operators Jonathan Wakely
2020-04-08  7:34                       ` Richard Biener
2020-04-08  8:11                         ` Jonathan Wakely
2020-04-07 14:11               ` Nathan Sidwell
2020-03-30  9:29 ` Marc Glisse

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=069c1c26-d74a-0eea-554b-1aeb6881dca7@suse.cz \
    --to=mliska@suse.cz \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=hubicka@ucw.cz \
    --cc=jakub@redhat.com \
    --cc=jason@redhat.com \
    --cc=jwakely.gcc@gmail.com \
    --cc=marc.glisse@inria.fr \
    --cc=nathan@acm.org \
    --cc=richard.guenther@gmail.com \
    /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).