]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
PHIOPT: Improve replace_phi_edge_with_variable for diamond shapped bb
authorAndrew Pinski <apinski@marvell.com>
Fri, 28 Apr 2023 23:21:50 +0000 (16:21 -0700)
committerAndrew Pinski <apinski@marvell.com>
Thu, 4 May 2023 11:25:38 +0000 (04:25 -0700)
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
gcc/testsuite/gcc.dg/tree-ssa/minmax-16.c
gcc/testsuite/gcc.dg/tree-ssa/minmax-3.c
gcc/testsuite/gcc.dg/tree-ssa/minmax-4.c
gcc/testsuite/gcc.dg/tree-ssa/minmax-5.c
gcc/testsuite/gcc.dg/tree-ssa/minmax-8.c
gcc/tree-ssa-phiopt.cc

index 8a39871c93890b553dc8d4aed494c2f14f8508fe..6731f91e6c3ab504153e59ed104c0321eebac538 100644 (file)
@@ -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" } } */
index 623b12b3f74f5b13a993a3871f2b59ba3e9fc038..094364e642439724e6ef895a6b3f82b3359f3748 100644 (file)
@@ -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" } } */
index 2af107763469cd16401b618f0e6b556e5ac2b04d..521afe3e4d987b929d72751f156c2fb7458b71b0 100644 (file)
@@ -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" } } */
index 973f39bfed305d636cd7290ab5dbb726d6d75f12..49e27185b5efd60d943c161dd7e9a3400836804c 100644 (file)
@@ -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" } } */
index 34e4e7205111b6c3e28fbe7c8cde85740ac52630..194c881cc9816ba9a06102ba37383b3e6cc163c5 100644 (file)
@@ -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" } } */
index 0160e573fef8aba106d4f8ef86b5c958ab52cb30..d5cb53145eab84a07c2fb424a1ca788f84a3159b 100644 (file)
@@ -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" } } */
index 154a5abf86bb157d11255ba06a8e63c22cd572ea..2f70463b3db49e2c0fa3e5ff1a8baaef2f422299 100644 (file)
@@ -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,31 @@ 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);
+
+      /* The 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_arg_into_existing_phi (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);