public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH 1/2] Factor out copy_phi_args from gimple_duplicate_sese_tail and remove_forwarder_block.
@ 2023-05-03  2:30 Andrew Pinski
  2023-05-03  2:30 ` [PATCH 2/2] PHIOPT: Improve replace_phi_edge_with_variable for diamond shapped bb Andrew Pinski
  0 siblings, 1 reply; 2+ messages in thread
From: Andrew Pinski @ 2023-05-03  2:30 UTC (permalink / raw)
  To: gcc-patches; +Cc: Andrew Pinski

While improving replace_phi_edge_with_variable for the diamond formed bb
case, I needed a new function, copy_phi_args and then I went to search for
similar code and noticed both gimple_duplicate_sese_tail and
remove_forwarder_block have the same code I need. So I decided it would
be best if I factor it out into a new function into a common area
and call it from those two places (and will use it for phiopt too).

OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.

gcc/ChangeLog:

	* tree-cfg.cc (copy_phi_args): New function
	(gimple_duplicate_sese_tail): Use it instead of
	doing it inline.
	* tree-cfg.h (copy_phi_args): New declaration.
	* tree-cfgcleanup.cc (remove_forwarder_block): Use
	copy_phi_args instead of implementing it inline.
---
 gcc/tree-cfg.cc        | 29 ++++++++++++++++++-----------
 gcc/tree-cfg.h         |  1 +
 gcc/tree-cfgcleanup.cc | 10 +---------
 3 files changed, 20 insertions(+), 20 deletions(-)

diff --git a/gcc/tree-cfg.cc b/gcc/tree-cfg.cc
index 4927fc0a8d9..3f24d9c5b1c 100644
--- a/gcc/tree-cfg.cc
+++ b/gcc/tree-cfg.cc
@@ -6802,6 +6802,23 @@ bb_part_of_region_p (basic_block bb, basic_block* bbs, unsigned n_region)
   return false;
 }
 
+/* For each PHI in BB, copy the PHI argument associated with SRC_E to TGT_E.  */
+
+void
+copy_phi_args (basic_block bb, edge src_e, edge tgt_e)
+{
+  gphi_iterator gsi;
+  int src_indx = src_e->dest_idx;
+
+  for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
+    {
+      gphi *phi = gsi.phi ();
+      tree def = gimple_phi_arg_def (phi, src_indx);
+      location_t locus = gimple_phi_arg_location (phi, src_indx);
+      add_phi_arg (phi, unshare_expr (def), tgt_e, locus);
+    }
+}
+
 /* Duplicates REGION consisting of N_REGION blocks.  The new blocks
    are stored to REGION_COPY in the same order in that they appear
    in REGION, if REGION_COPY is not NULL.  ENTRY is the entry to
@@ -6847,9 +6864,6 @@ gimple_duplicate_sese_tail (edge entry, edge exit,
   gimple_stmt_iterator gsi;
   edge sorig, snew;
   basic_block exit_bb;
-  gphi_iterator psi;
-  gphi *phi;
-  tree def;
   class loop *target, *aloop, *cloop;
 
   gcc_assert (EDGE_COUNT (exit->src->succs) == 2);
@@ -6947,14 +6961,7 @@ gimple_duplicate_sese_tail (edge entry, edge exit,
 	gcc_assert (single_succ_edge (region_copy[i]));
 	e = redirect_edge_and_branch (single_succ_edge (region_copy[i]), exit_bb);
 	PENDING_STMT (e) = NULL;
-	for (psi = gsi_start_phis (exit_bb);
-	     !gsi_end_p (psi);
-	     gsi_next (&psi))
-	  {
-	    phi = psi.phi ();
-	    def = PHI_ARG_DEF (phi, nexits[0]->dest_idx);
-	    add_phi_arg (phi, def, e, gimple_phi_arg_location_from_edge (phi, e));
-	  }
+	copy_phi_args (exit_bb, nexits[0], e);
       }
   e = redirect_edge_and_branch (nexits[1], nexits[0]->dest);
   PENDING_STMT (e) = NULL;
diff --git a/gcc/tree-cfg.h b/gcc/tree-cfg.h
index 9b56a68fe9d..7ec3f812a76 100644
--- a/gcc/tree-cfg.h
+++ b/gcc/tree-cfg.h
@@ -113,6 +113,7 @@ extern basic_block gimple_switch_default_bb (function *, gswitch *);
 extern edge gimple_switch_edge (function *, gswitch *, unsigned);
 extern edge gimple_switch_default_edge (function *, gswitch *);
 extern bool cond_only_block_p (basic_block);
+extern void copy_phi_args (basic_block, edge, edge);
 
 /* Return true if the LHS of a call should be removed.  */
 
diff --git a/gcc/tree-cfgcleanup.cc b/gcc/tree-cfgcleanup.cc
index 42b25312122..f3582e5ce52 100644
--- a/gcc/tree-cfgcleanup.cc
+++ b/gcc/tree-cfgcleanup.cc
@@ -612,15 +612,7 @@ remove_forwarder_block (basic_block bb)
 	{
 	  /* Create arguments for the phi nodes, since the edge was not
 	     here before.  */
-	  for (gphi_iterator psi = gsi_start_phis (dest);
-	       !gsi_end_p (psi);
-	       gsi_next (&psi))
-	    {
-	      gphi *phi = psi.phi ();
-	      location_t l = gimple_phi_arg_location_from_edge (phi, succ);
-	      tree def = gimple_phi_arg_def (phi, succ->dest_idx);
-	      add_phi_arg (phi, unshare_expr (def), s, l);
-	    }
+	  copy_phi_args (dest, succ, s);
 	}
     }
 
-- 
2.39.1


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

* [PATCH 2/2] PHIOPT: Improve replace_phi_edge_with_variable for diamond shapped bb
  2023-05-03  2:30 [PATCH 1/2] Factor out copy_phi_args from gimple_duplicate_sese_tail and remove_forwarder_block Andrew Pinski
@ 2023-05-03  2:30 ` Andrew Pinski
  0 siblings, 0 replies; 2+ messages in thread
From: Andrew Pinski @ 2023-05-03  2:30 UTC (permalink / raw)
  To: gcc-patches; +Cc: Andrew Pinski

While looking at differences between what minmax_replacement
and match_simplify_replacement does. I noticed that they sometimes
chose different edges to remove. I decided we should be able to do
better and be able to remove both empty basic blocks in the
case of match_simplify_replacement as that moves the statements.

This also updates the testcases as now match_simplify_replacement
will remove the unused MIN/MAX_EXPR and they were checking for
those.

OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.

gcc/ChangeLog:

	* tree-ssa-phiopt.cc (replace_phi_edge_with_variable): Handle
	diamond form bb with forwarder only empty blocks better.

gcc/testsuite/ChangeLog:

	* gcc.dg/tree-ssa/minmax-15.c: Update test.
	* gcc.dg/tree-ssa/minmax-16.c: Update test.
	* gcc.dg/tree-ssa/minmax-3.c: Update test.
	* gcc.dg/tree-ssa/minmax-4.c: Update test.
	* gcc.dg/tree-ssa/minmax-5.c: Update test.
	* gcc.dg/tree-ssa/minmax-8.c: Update test.
---
 gcc/testsuite/gcc.dg/tree-ssa/minmax-15.c |  3 ++-
 gcc/testsuite/gcc.dg/tree-ssa/minmax-16.c |  9 +++----
 gcc/testsuite/gcc.dg/tree-ssa/minmax-3.c  |  2 +-
 gcc/testsuite/gcc.dg/tree-ssa/minmax-4.c  |  2 +-
 gcc/testsuite/gcc.dg/tree-ssa/minmax-5.c  |  2 +-
 gcc/testsuite/gcc.dg/tree-ssa/minmax-8.c  |  2 +-
 gcc/tree-ssa-phiopt.cc                    | 32 ++++++++++++++++++++++-
 7 files changed, 40 insertions(+), 12 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/minmax-15.c b/gcc/testsuite/gcc.dg/tree-ssa/minmax-15.c
index 8a39871c938..6731f91e6c3 100644
--- a/gcc/testsuite/gcc.dg/tree-ssa/minmax-15.c
+++ b/gcc/testsuite/gcc.dg/tree-ssa/minmax-15.c
@@ -30,5 +30,6 @@ main (void)
   return 0;
 }
 
-/* { dg-final { scan-tree-dump-times "MIN_EXPR" 3 "phiopt1" } } */
+/* There should only be two MIN_EXPR left, the 3rd one was removed. */
+/* { dg-final { scan-tree-dump-times "MIN_EXPR" 2 "phiopt1" } } */
 /* { dg-final { scan-tree-dump-times "MAX_EXPR" 0 "phiopt1" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/minmax-16.c b/gcc/testsuite/gcc.dg/tree-ssa/minmax-16.c
index 623b12b3f74..094364e6424 100644
--- a/gcc/testsuite/gcc.dg/tree-ssa/minmax-16.c
+++ b/gcc/testsuite/gcc.dg/tree-ssa/minmax-16.c
@@ -25,11 +25,8 @@ main (void)
   return 0;
 }
 
-/* After phiopt1, there really should be only 3 MIN_EXPR in the IR (including debug statements).
-   But the way phiopt does not cleanup the CFG all the time, the PHI might still reference the
-   alternative bb's moved statement.
-   Note in the end, we do dce the statement and other debug statements to end up with only 2 MIN_EXPR.
-   So check that too. */
-/* { dg-final { scan-tree-dump-times "MIN_EXPR" 4 "phiopt1" } } */
+/* After phiopt1, will be only 2 MIN_EXPR in the IR (including debug statements). */
+/* xk will only have the final result so the extra debug info does not change anything. */
+/* { dg-final { scan-tree-dump-times "MIN_EXPR" 2 "phiopt1" } } */
 /* { dg-final { scan-tree-dump-times "MIN_EXPR" 2 "optimized" } } */
 /* { dg-final { scan-tree-dump-times "MAX_EXPR" 0 "phiopt1" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/minmax-3.c b/gcc/testsuite/gcc.dg/tree-ssa/minmax-3.c
index 2af10776346..521afe3e4d9 100644
--- a/gcc/testsuite/gcc.dg/tree-ssa/minmax-3.c
+++ b/gcc/testsuite/gcc.dg/tree-ssa/minmax-3.c
@@ -25,5 +25,5 @@ main (void)
   return 0;
 }
 
-/* { dg-final { scan-tree-dump-times "MIN_EXPR" 3 "phiopt1" } } */
+/* { dg-final { scan-tree-dump-times "MIN_EXPR" 2 "phiopt1" } } */
 /* { dg-final { scan-tree-dump-times "MAX_EXPR" 0 "phiopt1" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/minmax-4.c b/gcc/testsuite/gcc.dg/tree-ssa/minmax-4.c
index 973f39bfed3..49e27185b5e 100644
--- a/gcc/testsuite/gcc.dg/tree-ssa/minmax-4.c
+++ b/gcc/testsuite/gcc.dg/tree-ssa/minmax-4.c
@@ -26,4 +26,4 @@ main (void)
 }
 
 /* { dg-final { scan-tree-dump-times "MIN_EXPR" 0 "phiopt1" } } */
-/* { dg-final { scan-tree-dump-times "MAX_EXPR" 3 "phiopt1" } } */
+/* { dg-final { scan-tree-dump-times "MAX_EXPR" 2 "phiopt1" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/minmax-5.c b/gcc/testsuite/gcc.dg/tree-ssa/minmax-5.c
index 34e4e720511..194c881cc98 100644
--- a/gcc/testsuite/gcc.dg/tree-ssa/minmax-5.c
+++ b/gcc/testsuite/gcc.dg/tree-ssa/minmax-5.c
@@ -25,5 +25,5 @@ main (void)
   return 0;
 }
 
-/* { dg-final { scan-tree-dump-times "MIN_EXPR" 2 "phiopt1" } } */
+/* { dg-final { scan-tree-dump-times "MIN_EXPR" 1 "phiopt1" } } */
 /* { dg-final { scan-tree-dump-times "MAX_EXPR" 1 "phiopt1" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/minmax-8.c b/gcc/testsuite/gcc.dg/tree-ssa/minmax-8.c
index 0160e573fef..d5cb53145ea 100644
--- a/gcc/testsuite/gcc.dg/tree-ssa/minmax-8.c
+++ b/gcc/testsuite/gcc.dg/tree-ssa/minmax-8.c
@@ -26,4 +26,4 @@ main (void)
 }
 
 /* { dg-final { scan-tree-dump-times "MIN_EXPR" 1 "phiopt1" } } */
-/* { dg-final { scan-tree-dump-times "MAX_EXPR" 2 "phiopt1" } } */
+/* { dg-final { scan-tree-dump-times "MAX_EXPR" 1 "phiopt1" } } */
diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc
index 37b98ef3c52..8905e6c4d21 100644
--- a/gcc/tree-ssa-phiopt.cc
+++ b/gcc/tree-ssa-phiopt.cc
@@ -94,6 +94,7 @@ replace_phi_edge_with_variable (basic_block cond_block,
   basic_block bb = gimple_bb (phi);
   gimple_stmt_iterator gsi;
   tree phi_result = PHI_RESULT (phi);
+  bool deleteboth = false;
 
   /* Duplicate range info if they are the only things setting the target PHI.
      This is needed as later on, the new_tree will be replacing
@@ -137,7 +138,14 @@ replace_phi_edge_with_variable (basic_block cond_block,
       keep_edge = EDGE_SUCC (cond_block, 1);
     }
   else if ((keep_edge = find_edge (cond_block, e->src)))
-    ;
+    {
+      basic_block bb1 = EDGE_SUCC (cond_block, 0)->dest;
+      basic_block bb2 = EDGE_SUCC (cond_block, 1)->dest;
+      if (single_pred_p (bb1) && single_pred_p (bb2)
+	  && single_succ_p (bb1) && single_succ_p (bb2)
+	  && empty_block_p (bb1) && empty_block_p (bb2))
+	deleteboth = true;
+    }
   else
     gcc_unreachable ();
 
@@ -148,6 +156,28 @@ replace_phi_edge_with_variable (basic_block cond_block,
       e->probability = profile_probability::always ();
       delete_basic_block (edge_to_remove->dest);
 
+      /* Eliminate the COND_EXPR at the end of COND_BLOCK.  */
+      gsi = gsi_last_bb (cond_block);
+      gsi_remove (&gsi, true);
+    }
+  else if (deleteboth)
+    {
+      basic_block bb1 = EDGE_SUCC (cond_block, 0)->dest;
+      basic_block bb2 = EDGE_SUCC (cond_block, 1)->dest;
+
+      edge newedge = redirect_edge_and_branch (keep_edge, bb);
+      /* no new edge should be the same. */
+      gcc_assert (newedge == keep_edge);
+      keep_edge->flags |= EDGE_FALLTHRU;
+      keep_edge->flags &= ~(EDGE_TRUE_VALUE | EDGE_FALSE_VALUE);
+      keep_edge->probability = profile_probability::always ();
+      /* Copy the edge's phi entry from the old one */
+      copy_phi_args(bb, e, keep_edge);
+
+      /* Delete the old 2 empty basic blocks */
+      delete_basic_block (bb1);
+      delete_basic_block (bb2);
+
       /* Eliminate the COND_EXPR at the end of COND_BLOCK.  */
       gsi = gsi_last_bb (cond_block);
       gsi_remove (&gsi, true);
-- 
2.39.1


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

end of thread, other threads:[~2023-05-03  2:31 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-03  2:30 [PATCH 1/2] Factor out copy_phi_args from gimple_duplicate_sese_tail and remove_forwarder_block Andrew Pinski
2023-05-03  2:30 ` [PATCH 2/2] PHIOPT: Improve replace_phi_edge_with_variable for diamond shapped bb Andrew Pinski

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