]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
cselim: Don't create a phi node if the rhs side are the same [PR122155]
authorAndrew Pinski <andrew.pinski@oss.qualcomm.com>
Sat, 4 Oct 2025 17:16:20 +0000 (10:16 -0700)
committerAndrew Pinski <andrew.pinski@oss.qualcomm.com>
Mon, 6 Oct 2025 21:45:46 +0000 (14:45 -0700)
This is a small compile time optimization where if commonalizing stores
that have the same rhs, a phi node does not need to be created.
This uses the same code as what was added for the `= {};` case.
The reason why it is a compile time optimization is that Copy prop
later on will do the same thing so not creating a new phi and a new
ssa name will have a small compile time improvement.

Bootstrapped and tested on x86_64-linux-gnu.

PR tree-optimization/122155

gcc/ChangeLog:

* tree-ssa-phiopt.cc (cond_if_else_store_replacement_1): Don't
create a phi if the 2 rhs are the same.

gcc/testsuite/ChangeLog:

* gcc.dg/tree-ssa/cselim-3.c: New test.

Signed-off-by: Andrew Pinski <andrew.pinski@oss.qualcomm.com>
gcc/testsuite/gcc.dg/tree-ssa/cselim-3.c [new file with mode: 0644]
gcc/tree-ssa-phiopt.cc

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/cselim-3.c b/gcc/testsuite/gcc.dg/tree-ssa/cselim-3.c
new file mode 100644 (file)
index 0000000..d0c8e6a
--- /dev/null
@@ -0,0 +1,16 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-phiopt1-details-stats" } */
+/* PR tree-optimization/122155 */
+
+void f(int *a, int b, int c)
+{
+  if (c)
+    *a = b;
+  else
+    *a = b;
+}
+
+/* When commonalizing a store with the same rhs, a PHI does not need to be created. */
+/* { dg-final { scan-tree-dump "if-then-else store replacement: 1" "phiopt1" } } */
+/* { dg-final { scan-tree-dump-not "to use phi:" "phiopt1" } } */
+/* { dg-final { scan-tree-dump-not "PHI <" "phiopt1" } } */
index bdd1f7396da22476c7d1f21381240aca5ed306eb..f1e56c9383fe08ac5cdaad8cb7816e97124b5df1 100644 (file)
@@ -3643,9 +3643,8 @@ cond_if_else_store_replacement_1 (basic_block then_bb, basic_block else_bb,
   tree lhs_base, lhs, then_rhs, else_rhs, name;
   location_t then_locus, else_locus;
   gimple_stmt_iterator gsi;
-  gphi *newphi;
+  gphi *newphi = nullptr;
   gassign *new_stmt;
-  bool empty_constructor = false;
 
   if (then_assign == NULL
       || !gimple_assign_single_p (then_assign)
@@ -3680,7 +3679,6 @@ cond_if_else_store_replacement_1 (basic_block then_bb, basic_block else_bb,
       /* Currently only handle commoning of `= {}`.   */
       if (TREE_CODE (then_rhs) != CONSTRUCTOR)
        return false;
-      empty_constructor = true;
     }
 
   if (dump_file && (dump_flags & TDF_DETAILS))
@@ -3709,8 +3707,8 @@ cond_if_else_store_replacement_1 (basic_block then_bb, basic_block else_bb,
   /* 2) Create a PHI node at the join block, with one argument
        holding the old RHS, and the other holding the temporary
        where we stored the old memory contents.  */
-  if (empty_constructor)
-    name = unshare_expr (then_rhs);
+  if (operand_equal_p (then_rhs, else_rhs))
+    name = then_rhs;
   else
     {
       name = make_temp_ssa_name (TREE_TYPE (lhs), NULL, "cstore");
@@ -3730,7 +3728,7 @@ cond_if_else_store_replacement_1 (basic_block then_bb, basic_block else_bb,
   update_stmt (vphi);
   if (dump_file && (dump_flags & TDF_DETAILS))
     {
-      if (!empty_constructor)
+      if (newphi)
        {
         fprintf(dump_file, "to use phi:\n");
          print_gimple_stmt (dump_file, newphi, 0,