This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[patch] fold-const.c: Fix tree-optimization/16632.


Hi,

Attached is a patch to fix tree-optimization/16632.

Consider

int
bar (int i)
{
  if ((i & 4) == 2)
    return 1;
  return 0;
}

Note that the condition inside the "if" statement is always false, but
unfortunately fold() fails to see that, and the "if" statement
survives through all the tree optimizations.

Let C be the constant on the left in the condition, which is 4 in the
example above.  Let D be the constant on the right.  In general, we
can fold (i & C) != D into 0 if D & ~C != 0.  Without this patch,
fold() does not fold ~C, failing to see that D & ~C != 0.

The patch fixes the problem by telling fold() to fold ~C.

It also fixes a similar problem where | is used instead of &.

I've attached a testcase, but I am wondering if it's OK to add a
fold()-related testcase into the tree-ssa area of the testsuite.
Could somebody clarify this for me?

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

Kazu Hirata

2004-10-03  Kazu Hirata  <kazu@cs.umass.edu>

	* fold-const.c (fold) [EQ_EXPR]: When seeing if D & ~C != 0 to
	fold (A & C) == D into 0, fold ~C.  Similarly, for the case
	where | is used instead of &.

2004-10-03  Kazu Hirata  <kazu@cs.umass.edu>

	* gcc.dg/tree-ssa/20041002-1.c: New.

Index: fold-const.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/fold-const.c,v
retrieving revision 1.465
diff -u -r1.465 fold-const.c
--- fold-const.c	29 Sep 2004 19:44:53 -0000	1.465
+++ fold-const.c	2 Oct 2004 12:36:08 -0000
@@ -8400,11 +8400,11 @@
 	  && TREE_CODE (arg1) == INTEGER_CST
 	  && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
 	{
-	  tree dandnotc
-	    = fold (build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
-			    arg1, build1 (BIT_NOT_EXPR,
-					  TREE_TYPE (TREE_OPERAND (arg0, 1)),
-					  TREE_OPERAND (arg0, 1))));
+	  tree notc = fold (build1 (BIT_NOT_EXPR,
+				    TREE_TYPE (TREE_OPERAND (arg0, 1)),
+				    TREE_OPERAND (arg0, 1)));
+	  tree dandnotc = fold (build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
+					arg1, notc));
 	  tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
 	  if (integer_nonzerop (dandnotc))
 	    return omit_one_operand (type, rslt, arg0);
@@ -8417,10 +8417,9 @@
 	  && TREE_CODE (arg1) == INTEGER_CST
 	  && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
 	{
-	  tree candnotd
-	    = fold (build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
-			    TREE_OPERAND (arg0, 1),
-			    build1 (BIT_NOT_EXPR, TREE_TYPE (arg1), arg1)));
+	  tree notd = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (arg1), arg1));
+	  tree candnotd = fold (build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
+					TREE_OPERAND (arg0, 1), notd));
 	  tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
 	  if (integer_nonzerop (candnotd))
 	    return omit_one_operand (type, rslt, arg0);
--- /dev/null	2004-02-23 16:02:56.000000000 -0500
+++ testsuite/gcc.dg/tree-ssa/20041002-1.c	2004-10-02 08:35:16.969652951 -0400
@@ -0,0 +1,23 @@
+/* PR tree-optimization/16632
+   fold() failed to see the following "if" statements never trigger.  */
+
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-ssa" } */
+
+int
+foo (int i)
+{
+  if ((i | 3) == 1)
+    return 1;
+  return 0;
+}
+
+int
+bar (int i)
+{
+  if ((i & 4) == 2)
+    return 1;
+  return 0;
+}
+
+/* { dg-final { scan-tree-dump-times "if" 0 "ssa" } } */


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]