fold-const.c (fold_comparison): Fold ~X op ~Y as Y op X.
authorRoger Sayle <roger@eyesopen.com>
Sun, 29 Oct 2006 21:41:48 +0000 (21:41 +0000)
committerRoger Sayle <sayle@gcc.gnu.org>
Sun, 29 Oct 2006 21:41:48 +0000 (21:41 +0000)
* fold-const.c (fold_comparison): Fold ~X op ~Y as Y op X.
Fold ~X op C as X op' ~C, where op' is the swapped comparison.
(fold_binary): ~X eq/ne C is now handled in fold_comparison.
Fold -X eq/ne -Y as X eq/ne Y.

* gcc.dg/fold-compare-1.c: New test case.

From-SVN: r118158

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

index 920d31da62b29319cddadfdb94ee3a94e3316a34..1a5c2cf91a0450cd7d09b93c348b0fb915d37b54 100644 (file)
@@ -1,3 +1,10 @@
+2006-10-29  Roger Sayle  <roger@eyesopen.com>
+
+       * fold-const.c (fold_comparison): Fold ~X op ~Y as Y op X.
+       Fold ~X op C as X op' ~C, where op' is the swapped comparison.
+       (fold_binary): ~X eq/ne C is now handled in fold_comparison.
+       Fold -X eq/ne -Y as X eq/ne Y.
+
 2006-10-29  Richard Sandiford  <richard@codesourcery.com>
 
        * config/mips/mips.md (mul<mode>3): Check ISA_HAS_MUL3 rather than
index 1c3c752ceb4fa5d3ff00e64442758a8026978a2b..a822abdce90520532705c7cda76d817d7ea99f04 100644 (file)
@@ -8360,6 +8360,20 @@ fold_comparison (enum tree_code code, tree type, tree op0, tree op1)
        return tem;
     }
 
+  /* Fold ~X op ~Y as Y op X.  */
+  if (TREE_CODE (arg0) == BIT_NOT_EXPR
+      && TREE_CODE (arg1) == BIT_NOT_EXPR)
+    return fold_build2 (code, type,
+                       TREE_OPERAND (arg1, 0),
+                       TREE_OPERAND (arg0, 0));
+
+  /* Fold ~X op C as X op' ~C, where op' is the swapped comparison.  */
+  if (TREE_CODE (arg0) == BIT_NOT_EXPR
+      && TREE_CODE (arg1) == INTEGER_CST)
+    return fold_build2 (swap_tree_comparison (code), type,
+                       TREE_OPERAND (arg0, 0),
+                       fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1), arg1));
+
   return NULL_TREE;
 }
 
@@ -10426,13 +10440,6 @@ fold_binary (enum tree_code code, tree type, tree op0, tree op1)
           && code == EQ_EXPR)
         return fold_build1 (TRUTH_NOT_EXPR, type, arg0);
 
-      /*  ~a != C becomes a != ~C where C is a constant.  Likewise for ==.  */
-      if (TREE_CODE (arg0) == BIT_NOT_EXPR
-         && TREE_CODE (arg1) == INTEGER_CST)
-       return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
-                           fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1), 
-                                        arg1));
-
       /* If this is an equality comparison of the address of a non-weak
         object against zero, then we know the result.  */
       if (TREE_CODE (arg0) == ADDR_EXPR
@@ -10798,6 +10805,14 @@ fold_binary (enum tree_code code, tree type, tree op0, tree op1)
          tree res = constant_boolean_node (code==NE_EXPR, type);
          return omit_one_operand (type, res, arg0);
        }
+
+      /* Fold -X op -Y as X op Y, where op is eq/ne.  */
+      if (TREE_CODE (arg0) == NEGATE_EXPR
+          && TREE_CODE (arg1) == NEGATE_EXPR)
+       return fold_build2 (code, type,
+                           TREE_OPERAND (arg0, 0),
+                           TREE_OPERAND (arg1, 0));
+
       return NULL_TREE;
 
     case LT_EXPR:
index f81e5837714b2a74c12c871c35124fb28c2b1224..bec0930032749e3a60275562a80ba3e273968d30 100644 (file)
@@ -1,3 +1,7 @@
+2006-10-29  Roger Sayle  <roger@eyesopen.com>
+
+       * gcc.dg/fold-compare-1.c: New test case.
+
 2006-10-29  Dirk Mueller  <dmueller@suse.de>
 
        PR c++/16307
diff --git a/gcc/testsuite/gcc.dg/fold-compare-1.c b/gcc/testsuite/gcc.dg/fold-compare-1.c
new file mode 100644 (file)
index 0000000..485d5f5
--- /dev/null
@@ -0,0 +1,53 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-original" } */
+
+int test1(int a, int b)
+{
+  return ~a == ~b;
+}
+
+int test2(int c, int d)
+{
+  return -c == -d;
+}
+
+int test3(int e)
+{
+  return -e == 5;
+}
+
+int test4(int f)
+{
+  return ~f == 5;
+}
+
+int test5(int g, int h)
+{
+  return ~g < ~h;
+}
+
+int test6(int i, int j)
+{
+  return ~i >= ~j;
+}
+
+int test7(int k)
+{
+  return ~k < 3;
+}
+
+int test8(int l)
+{
+  return ~l >= 2;
+}
+
+/* { dg-final { scan-tree-dump-times "b == a" 1 "original" } } */
+/* { dg-final { scan-tree-dump-times "c == d" 1 "original" } } */
+/* { dg-final { scan-tree-dump-times "e == -5" 1 "original" } } */
+/* { dg-final { scan-tree-dump-times "f == -6" 1 "original" } } */
+/* { dg-final { scan-tree-dump-times "h < g" 1 "original" } } */
+/* { dg-final { scan-tree-dump-times "j >= i" 1 "original" } } */
+/* { dg-final { scan-tree-dump-times "k > -4" 1 "original" } } */
+/* { dg-final { scan-tree-dump-times "l <= -3" 1 "original" } } */
+/* { dg-final { cleanup-tree-dump "original" } } */
+