re PR tree-optimization/15784 (fold misses binary optimization)
authorJames A. Morrison <phython@gcc.gnu.org>
Fri, 4 Mar 2005 02:48:30 +0000 (02:48 +0000)
committerJames A. Morrison <phython@gcc.gnu.org>
Fri, 4 Mar 2005 02:48:30 +0000 (02:48 +0000)
2005-03-03  James A. Morrison  <phython@gcc.gnu.org>

        PR tree-optimization/15784
        * fold-const.c (fold): Fold ~A + 1 to -1.  Fold -A - 1
        and -1 - A to ~A.

From-SVN: r95870

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

index 67c26a21d4cbd26d1544edd8b2f02ca9be2085df..19b45f231d913ba7c7ecfbb75e8b1a7dbdcdd960 100644 (file)
@@ -1,3 +1,9 @@
+2005-03-03  James A. Morrison  <phython@gcc.gnu.org>
+
+       PR tree-optimization/15784
+       * fold-const.c (fold): Fold ~A + 1 to -1.  Fold -A - 1
+       and -1 - A to ~A.
+
 2005-03-03  David Edelsohn  <edelsohn@gnu.org>
 
        * config/rs6000/predicates.md (branch_comparison_operator):
index 72e557eeb7179758ed6a619fed91118b3d3f5932..2c6d71ec030880e5a12e9230370206b0135b9720 100644 (file)
@@ -7223,6 +7223,11 @@ fold (tree expr)
       if (TREE_CODE (arg0) == NEGATE_EXPR
          && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
        return fold (build2 (MINUS_EXPR, type, arg1, TREE_OPERAND (arg0, 0)));
+      /* Convert ~A + 1 to -A.  */
+      if (INTEGRAL_TYPE_P (type)
+         && TREE_CODE (arg0) == BIT_NOT_EXPR
+         && integer_onep (arg1))
+       return fold (build1 (NEGATE_EXPR, type, TREE_OPERAND (arg0, 0)));
 
       if (TREE_CODE (type) == COMPLEX_TYPE)
        {
@@ -7661,6 +7666,16 @@ fold (tree expr)
          && reorder_operands_p (arg0, arg1))
        return fold (build2 (MINUS_EXPR, type, negate_expr (arg1),
                             TREE_OPERAND (arg0, 0)));
+      /* Convert -A - 1 to ~A.  */
+      if (INTEGRAL_TYPE_P (type)
+         && TREE_CODE (arg0) == NEGATE_EXPR
+         && integer_onep (arg1))
+       return fold (build1 (BIT_NOT_EXPR, type, TREE_OPERAND (arg0, 0)));
+
+      /* Convert -1 - A to ~A.  */
+      if (INTEGRAL_TYPE_P (type)
+         && integer_all_onesp (arg0))
+       return fold (build1 (BIT_NOT_EXPR, type, arg1));
 
       if (TREE_CODE (type) == COMPLEX_TYPE)
        {
index e589f7bff525ed9d65efc00df311bc6a7baeb84a..b7a3692e6a25f469ef4dc4275a223c1cd4e9a4ab 100644 (file)
@@ -1,3 +1,7 @@
+2005-03-03  James A. Morrison  <phython@gcc.gnu.org>
+
+       * gcc.dg/pr15784-4.c: New test.
+
 2005-03-03  Geoffrey Keating  <geoffk@apple.com>
 
        * gcc.c-torture/execute/pr17133.c: New.
diff --git a/gcc/testsuite/gcc.dg/pr15784-4.c b/gcc/testsuite/gcc.dg/pr15784-4.c
new file mode 100644 (file)
index 0000000..021b637
--- /dev/null
@@ -0,0 +1,12 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+int a (int x) {
+       return ~x + 1; /* -x */
+}
+
+int b (int x) {
+       return -x -1; /* ~x */
+}
+
+/* { dg-final { scan-tree-dump "~x;" "optimized" } } */
+/* { dg-final { scan-tree-dump "-x;" "optimized" } } */