public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Improve simple_dce for phis that only used in itself
@ 2023-05-11 15:17 Andrew Pinski
  2023-05-11 16:57 ` Richard Biener
  0 siblings, 1 reply; 2+ messages in thread
From: Andrew Pinski @ 2023-05-11 15:17 UTC (permalink / raw)
  To: gcc-patches; +Cc: Andrew Pinski

While I was looking at differences before and after
r14-569-g21e2ef2dc25de3, I noticed that one phi node was
not being removed.
For an example, while compiling combine.cc, in expand_field_assignment,
we would remove `# pos_51 = PHI <pos_221(31), pos_51(30)>`
but we don't any more since pos_51 has more than zero users
but in this case it is only itself.
This patch improves simple_dce_from_worklist to detect that
case and now we able to remove this phi statement again.

OK? Bootstrapped and tested on x86_64-linux-gnu.

gcc/ChangeLog:

	* tree-ssa-dce.cc (simple_dce_from_worklist): For ssa names
	defined by a phi node with more than one uses, allow for the
	only uses are in that same defining statement.
---
 gcc/tree-ssa-dce.cc | 31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)

diff --git a/gcc/tree-ssa-dce.cc b/gcc/tree-ssa-dce.cc
index 6554b5db03e..045c64a9c02 100644
--- a/gcc/tree-ssa-dce.cc
+++ b/gcc/tree-ssa-dce.cc
@@ -2107,9 +2107,36 @@ simple_dce_from_worklist (bitmap worklist, bitmap need_eh_cleanup)
       unsigned i = bitmap_clear_first_set_bit (worklist);
 
       tree def = ssa_name (i);
-      /* Removed by somebody else or still in use.  */
+      /* Removed by somebody else or still in use.
+	 Note use in itself for a phi node is not counted as still in use.  */
       if (! def || ! has_zero_uses (def))
-	continue;
+	{
+
+	  if (!def)
+	    continue;
+
+	  gimple *def_stmt = SSA_NAME_DEF_STMT (def);
+	  if (gimple_code (def_stmt) != GIMPLE_PHI)
+	    continue;
+
+	  gimple *use_stmt;
+	  imm_use_iterator use_iter;
+	  bool canremove = true;
+
+	  FOR_EACH_IMM_USE_STMT (use_stmt, use_iter, def)
+	    {
+	      /* Ignore debug statements. */
+	      if (is_gimple_debug (use_stmt))
+		continue;
+	      if (use_stmt != def_stmt)
+		{
+		  canremove = false;
+		  break;
+		}
+	    }
+	  if (!canremove)
+	    continue;
+	}
 
       gimple *t = SSA_NAME_DEF_STMT (def);
       if (gimple_has_side_effects (t))
-- 
2.31.1


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

end of thread, other threads:[~2023-05-11 16:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-11 15:17 [PATCH] Improve simple_dce for phis that only used in itself Andrew Pinski
2023-05-11 16:57 ` Richard Biener

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