OSDN Git Service

2007-02-24 Richard Guenther <rguenther@suse.de>
authorrguenth <rguenth@138bc75d-0d04-0410-961f-82ee72b054a4>
Sat, 24 Feb 2007 19:55:47 +0000 (19:55 +0000)
committerrguenth <rguenth@138bc75d-0d04-0410-961f-82ee72b054a4>
Sat, 24 Feb 2007 19:55:47 +0000 (19:55 +0000)
PR middle-end/30951
* fold-const.c (fold_binary): Fold x +- CST op x for
EQ_EXPR and NE_EXPR.

* gcc.dg/pr30951.c: New testcase.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@122295 138bc75d-0d04-0410-961f-82ee72b054a4

gcc/ChangeLog
gcc/fold-const.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/pr30951.c [new file with mode: 0644]

index 58980dc..2368d6a 100644 (file)
@@ -1,3 +1,9 @@
+2007-02-24  Richard Guenther  <rguenther@suse.de>
+
+       PR middle-end/30951
+       * fold-const.c (fold_binary): Fold x +- CST op x for
+       EQ_EXPR and NE_EXPR.
+
 2007-02-24  John David Anglin  <dave.anglin@nrc-cnrc.gc.ca>
 
        * pa.md (muldi3): Force subregs to registers in 64-bit expander.
index ffd63cf..80c3c41 100644 (file)
@@ -11203,6 +11203,24 @@ fold_binary (enum tree_code code, tree type, tree op0, tree op1)
                                         fold_convert (TREE_TYPE (arg0), arg1),
                                         TREE_OPERAND (arg0, 1)));
 
+      /* Transform comparisons of the form X +- C CMP X.  */
+      if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
+         && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
+         && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
+         && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
+             || POINTER_TYPE_P (TREE_TYPE (arg0))))
+       {
+         tree cst = TREE_OPERAND (arg0, 1);
+
+         if (code == EQ_EXPR
+             && !integer_zerop (cst))
+           return omit_two_operands (type, boolean_false_node,
+                                     TREE_OPERAND (arg0, 0), arg1);
+         else
+           return omit_two_operands (type, boolean_true_node,
+                                     TREE_OPERAND (arg0, 0), arg1);
+       }
+
       /* If we have X - Y == 0, we can convert that to X == Y and similarly
         for !=.  Don't do this for ordered comparisons due to overflow.  */
       if (TREE_CODE (arg0) == MINUS_EXPR
index a4b1af9..bd67ab6 100644 (file)
@@ -1,3 +1,8 @@
+2007-02-24  Richard Guenther  <rguenther@suse.de>
+
+       PR middle-end/30951
+       * gcc.dg/pr30951.c: New testcase.
+
 2007-02-24  Kaveh R. Ghazi  <ghazi@caip.rutgers.edu>
 
        * gcc.dg/torture/builtin-modf-1.c: New test.
diff --git a/gcc/testsuite/gcc.dg/pr30951.c b/gcc/testsuite/gcc.dg/pr30951.c
new file mode 100644 (file)
index 0000000..149b8ec
--- /dev/null
@@ -0,0 +1,36 @@
+/* { dg-do link } */
+
+extern void link_error (void);
+
+void test (int x, unsigned int y)
+{
+  if (x + 5 == x)
+    link_error ();
+  if (x == x + 10)
+    link_error ();
+  if (y + 5 == y)
+    link_error ();
+  if (y == y + 10)
+    link_error ();
+  if (x + 5 != x)
+    ;
+  else
+    link_error ();
+  if (x != x + 10)
+    ;
+  else
+    link_error ();
+  if (y + 5 != y)
+    ;
+  else
+    link_error ();
+  if (y != y + 10)
+    ;
+  else
+    link_error ();
+}
+
+int main()
+{
+  return 0;
+}