public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Remove variables only used with .DEFERRED_INIT
@ 2023-03-14 10:12 Richard Biener
  0 siblings, 0 replies; 2+ messages in thread
From: Richard Biener @ 2023-03-14 10:12 UTC (permalink / raw)
  To: gcc-patches; +Cc: Jakub Jelinek

In PR109087 it was noticed that we rely on DSE to remove .DEFERRED_INIT
when it is the only remaining use of a variable.  Since DSE is imperfect
and even if it were not would be limited by the amount of statements to
walk the following enhances the unused var removal pass to handle
.DEFERRED_INIT like CLOBBERs, thus we do not keep local variables just
because they are deferred initialized.

Bootstrapped and tested on x86_64-unknown-linux-gnu, for now queued
for stage1 unless somebody thinks its applicable for GCC 13.

	* tree-ssa-live.cc (remove_unused_locals): Do not treat
	the .DEFERRED_INIT of a variable as use, instead remove
	that if it is the only use.

	* gcc.dg/auto-init-unused-1.c: New testcase.
---
 gcc/testsuite/gcc.dg/auto-init-unused-1.c | 16 ++++++++++++++++
 gcc/tree-ssa-live.cc                      | 21 ++++++++++++++++++++-
 2 files changed, 36 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.dg/auto-init-unused-1.c

diff --git a/gcc/testsuite/gcc.dg/auto-init-unused-1.c b/gcc/testsuite/gcc.dg/auto-init-unused-1.c
new file mode 100644
index 00000000000..b7d44e6b4f2
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/auto-init-unused-1.c
@@ -0,0 +1,16 @@
+/* { dg-do compile } */
+/* { dg-options "-O -ftrivial-auto-var-init=zero -fdump-tree-ssa" } */
+
+int a;
+int foo (void);
+int bar (void);
+
+void
+baz (void)
+{
+  int *b[6];
+  if (foo ())
+    a |= bar ();
+}
+
+/* { dg-final { scan-tree-dump-not "DEFERRED_INIT" "ssa" } } */
diff --git a/gcc/tree-ssa-live.cc b/gcc/tree-ssa-live.cc
index c179444e8e1..9118e82b4f1 100644
--- a/gcc/tree-ssa-live.cc
+++ b/gcc/tree-ssa-live.cc
@@ -813,6 +813,12 @@ remove_unused_locals (void)
 	      continue;
 	    }
 
+	  if (gimple_call_internal_p (stmt, IFN_DEFERRED_INIT))
+	    {
+	      have_local_clobbers = true;
+	      continue;
+	    }
+
 	  if (b)
 	    TREE_USED (b) = true;
 
@@ -856,7 +862,7 @@ remove_unused_locals (void)
      to remove them if they are the only references to a local variable,
      but we want to retain them when there's any other.  So the first pass
      ignores them, and the second pass (if there were any) tries to remove
-     them.  */
+     them.  We do the same for .DEFERRED_INIT.  */
   if (have_local_clobbers)
     FOR_EACH_BB_FN (bb, cfun)
       {
@@ -888,6 +894,19 @@ remove_unused_locals (void)
 		if (b)
 		  TREE_USED (b) = true;
 	      }
+	    else if (gimple_call_internal_p (stmt, IFN_DEFERRED_INIT))
+	      {
+		tree lhs = gimple_call_lhs (stmt);
+		if (DECL_P (lhs) && !is_used_p (lhs))
+		  {
+		    unlink_stmt_vdef (stmt);
+		    gsi_remove (&gsi, true);
+		    release_defs (stmt);
+		    continue;
+		  }
+		if (b)
+		  TREE_USED (b) = true;
+	      }
 	    else if (gimple_debug_bind_p (stmt))
 	      {
 		tree var = gimple_debug_bind_get_var (stmt);
-- 
2.35.3

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] Remove variables only used with .DEFERRED_INIT
       [not found] <20230314101303.498F73858412@sourceware.org>
@ 2023-03-14 10:19 ` Jakub Jelinek
  0 siblings, 0 replies; 2+ messages in thread
From: Jakub Jelinek @ 2023-03-14 10:19 UTC (permalink / raw)
  To: Richard Biener; +Cc: gcc-patches

On Tue, Mar 14, 2023 at 10:12:58AM +0000, Richard Biener via Gcc-patches wrote:
> In PR109087 it was noticed that we rely on DSE to remove .DEFERRED_INIT
> when it is the only remaining use of a variable.  Since DSE is imperfect
> and even if it were not would be limited by the amount of statements to
> walk the following enhances the unused var removal pass to handle
> .DEFERRED_INIT like CLOBBERs, thus we do not keep local variables just
> because they are deferred initialized.
> 
> Bootstrapped and tested on x86_64-unknown-linux-gnu, for now queued
> for stage1 unless somebody thinks its applicable for GCC 13.
> 
> 	* tree-ssa-live.cc (remove_unused_locals): Do not treat
> 	the .DEFERRED_INIT of a variable as use, instead remove
> 	that if it is the only use.
> 
> 	* gcc.dg/auto-init-unused-1.c: New testcase.

Given how simple the patch is and that -ftrivial-auto-var-init= is still
a fairly new feature, I think it would be nice to commit this to GCC 13.
That option is not on by default, but when people do use it, better not
make it uselessly expensive.

LGTM.

> diff --git a/gcc/testsuite/gcc.dg/auto-init-unused-1.c b/gcc/testsuite/gcc.dg/auto-init-unused-1.c
> new file mode 100644
> index 00000000000..b7d44e6b4f2
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/auto-init-unused-1.c
> @@ -0,0 +1,16 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O -ftrivial-auto-var-init=zero -fdump-tree-ssa" } */
> +
> +int a;
> +int foo (void);
> +int bar (void);
> +
> +void
> +baz (void)
> +{
> +  int *b[6];
> +  if (foo ())
> +    a |= bar ();
> +}
> +
> +/* { dg-final { scan-tree-dump-not "DEFERRED_INIT" "ssa" } } */
> diff --git a/gcc/tree-ssa-live.cc b/gcc/tree-ssa-live.cc
> index c179444e8e1..9118e82b4f1 100644
> --- a/gcc/tree-ssa-live.cc
> +++ b/gcc/tree-ssa-live.cc
> @@ -813,6 +813,12 @@ remove_unused_locals (void)
>  	      continue;
>  	    }
>  
> +	  if (gimple_call_internal_p (stmt, IFN_DEFERRED_INIT))
> +	    {
> +	      have_local_clobbers = true;
> +	      continue;
> +	    }
> +
>  	  if (b)
>  	    TREE_USED (b) = true;
>  
> @@ -856,7 +862,7 @@ remove_unused_locals (void)
>       to remove them if they are the only references to a local variable,
>       but we want to retain them when there's any other.  So the first pass
>       ignores them, and the second pass (if there were any) tries to remove
> -     them.  */
> +     them.  We do the same for .DEFERRED_INIT.  */
>    if (have_local_clobbers)
>      FOR_EACH_BB_FN (bb, cfun)
>        {
> @@ -888,6 +894,19 @@ remove_unused_locals (void)
>  		if (b)
>  		  TREE_USED (b) = true;
>  	      }
> +	    else if (gimple_call_internal_p (stmt, IFN_DEFERRED_INIT))
> +	      {
> +		tree lhs = gimple_call_lhs (stmt);
> +		if (DECL_P (lhs) && !is_used_p (lhs))
> +		  {
> +		    unlink_stmt_vdef (stmt);
> +		    gsi_remove (&gsi, true);
> +		    release_defs (stmt);
> +		    continue;
> +		  }
> +		if (b)
> +		  TREE_USED (b) = true;
> +	      }
>  	    else if (gimple_debug_bind_p (stmt))
>  	      {
>  		tree var = gimple_debug_bind_get_var (stmt);
> -- 
> 2.35.3

	Jakub


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-03-14 10:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-14 10:12 [PATCH] Remove variables only used with .DEFERRED_INIT Richard Biener
     [not found] <20230314101303.498F73858412@sourceware.org>
2023-03-14 10:19 ` Jakub Jelinek

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).