[patch] tree-ssa-copy.c: Fold conditionals.

Kazu Hirata kazu@cs.umass.edu
Wed Apr 13 14:55:00 GMT 2005


Hi,

Attached is a patch to fix a use of find_taken_edge.

A while I go, I removed folding capability from find_taken_edge.  This
means that we are no longer folding COND_EXPRs like

  if (a_1 == a_1)

during copy propagation, blocking some copy propagation opportunities.

This patch restores the ability to fold these COND_EXPRs.  Note that
with the patch, we call fold and find_taken_edge only when we have
conditionals of the form "SSA_NAME op SSA_NAME" where the two SSA_NAME
are identical, which is the only form of conditionals that we can fold
among those involving two SSA_NAMEs.

Tested on i686-pc-linux-gnu.  OK to apply?

Kazu Hirata

2005-04-13  Kazu Hirata  <kazu@cs.umass.edu>

	PR tree-optimization/20913
	* tree-ssa-copy.c (copy_prop_visit_cond_stmt): Fold COND_EXPR.

2005-04-13  Kazu Hirata  <kazu@cs.umass.edu>

	PR tree-optimization/20913
	* gcc.dg/tree-ssa/pr20913.c: New.

Index: tree-ssa-copy.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/tree-ssa-copy.c,v
retrieving revision 2.24
diff -u -d -p -r2.24 tree-ssa-copy.c
--- tree-ssa-copy.c	9 Apr 2005 01:37:23 -0000	2.24
+++ tree-ssa-copy.c	9 Apr 2005 02:24:58 -0000
@@ -626,9 +626,16 @@ copy_prop_visit_cond_stmt (tree stmt, ed
 	  print_generic_stmt (dump_file, cond, 0);
 	}
 
-      *taken_edge_p = find_taken_edge (bb_for_stmt (stmt), cond);
-      if (*taken_edge_p)
-	retval = SSA_PROP_INTERESTING;
+      /* We can fold COND only and get a useful result only when we
+	 have the same SSA_NAME on both sides of a comparison
+	 operator.  */
+      if (TREE_CODE (TREE_OPERAND (cond, 0)) == SSA_NAME
+	  && TREE_OPERAND (cond, 0) == TREE_OPERAND (cond, 1))
+	{
+	  *taken_edge_p = find_taken_edge (bb_for_stmt (stmt), fold (cond));
+	  if (*taken_edge_p)
+	    retval = SSA_PROP_INTERESTING;
+	}
 
       /* Restore the original operands.  */
       for (i = 0; i < NUM_USES (uses); i++)
--- /dev/null	2005-04-11 14:38:08.227034184 -0400
+++ testsuite/gcc.dg/tree-ssa/pr20913.c	2005-04-09 13:30:31.000000000 -0400
@@ -0,0 +1,25 @@
+/* PR tree-optimization/20913
+   COPY-PROP did not fold COND_EXPR, blocking some copy propagation
+   opportunities.  */
+
+/* { dg-do compile } */
+/* { dg-options "-O2 -fno-tree-dominator-opts -fdump-tree-copyprop1-details" } */
+
+int
+foo (int a, int b, int c, int d)
+{
+  int x, y;
+
+  b = a;
+  if (a == b)
+    x = c;
+  else
+    x = d;
+
+  if (x == c)
+    return a;
+  else
+    return b;
+}
+
+/* { dg-final { scan-tree-dump-times "with if \\(1\\)" 2 "copyprop1"} } */



More information about the Gcc-patches mailing list