]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
re PR middle-end/30322 (((-i-1) + i) +1) is turned into ~i + (i+1) and never into...
authorRoger Sayle <roger@eyesopen.com>
Sun, 31 Dec 2006 16:17:49 +0000 (16:17 +0000)
committerRoger Sayle <sayle@gcc.gnu.org>
Sun, 31 Dec 2006 16:17:49 +0000 (16:17 +0000)
PR middle-end/30322
* fold-const.c (fold_binary): Fold X + ~X and ~X + X into -1 when
we don't care about trapping overflow.  Only convert -A - 1 into ~A
when we don't care about trapping overflow.

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

From-SVN: r120300

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

index 1e2601bac4913f66e378b9fc72cad8090fbc10c1..be1f4fba56284136b9b9fba4e09c50a76ca12f08 100644 (file)
@@ -1,3 +1,10 @@
+2006-12-31  Roger Sayle  <roger@eyesopen.com>
+
+       PR middle-end/30322
+       * fold-const.c (fold_binary): Fold X + ~X and ~X + X into -1 when
+       we don't care about trapping overflow.  Only convert -A - 1 into ~A
+       when we don't care about trapping overflow.
+
 2006-12-31  Richard Guenther  <rguenther@suse.de>
 
        PR middle-end/30338
index dbdc9c26367efee27f0a4315bc727ea265d257ba..da9579e6b4e5e558f3af3d01e8f11ead74c86cfe 100644 (file)
@@ -8662,6 +8662,26 @@ fold_binary (enum tree_code code, tree type, tree op0, tree op1)
          if (integer_zerop (arg1))
            return non_lvalue (fold_convert (type, arg0));
 
+         /* ~X + X is -1.  */
+         if (TREE_CODE (arg0) == BIT_NOT_EXPR
+             && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
+             && !TYPE_TRAP_SIGNED (type))
+           {
+             t1 = build_int_cst (type, -1);
+             t1 = force_fit_type (t1, 0, false, false);
+             return omit_one_operand (type, t1, arg1);
+           }
+
+         /* X + ~X is -1.  */
+         if (TREE_CODE (arg1) == BIT_NOT_EXPR
+             && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
+             && !TYPE_TRAP_SIGNED (type))
+           {
+             t1 = build_int_cst (type, -1);
+             t1 = force_fit_type (t1, 0, false, false);
+             return omit_one_operand (type, t1, arg0);
+         }
+
          /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
             with a constant, and the two constants have no bits in common,
             we should treat this as a BIT_IOR_EXPR since this may produce more
@@ -9002,7 +9022,8 @@ fold_binary (enum tree_code code, tree type, tree op0, tree op1)
       /* Convert -A - 1 to ~A.  */
       if (INTEGRAL_TYPE_P (type)
          && TREE_CODE (arg0) == NEGATE_EXPR
-         && integer_onep (arg1))
+         && integer_onep (arg1)
+         && !TYPE_TRAP_SIGNED (type))
        return fold_build1 (BIT_NOT_EXPR, type,
                            fold_convert (type, TREE_OPERAND (arg0, 0)));
 
index df63017b96c836f36631fbca9f39ff93ec0a1aa6..30d25ea11dd4c78da2cab0a5f51d9ccd1b974114 100644 (file)
@@ -1,3 +1,8 @@
+2006-12-31  Roger Sayle  <roger@eyesopen.com>
+
+       PR middle-end/30322
+       * gcc.dg/fold-plusnot-1.c: New test case.
+
 2006-12-31  Simon Martin  <simartin@users.sourceforge.net>
 
        PR c++/29731
diff --git a/gcc/testsuite/gcc.dg/fold-plusnot-1.c b/gcc/testsuite/gcc.dg/fold-plusnot-1.c
new file mode 100644 (file)
index 0000000..81ddf46
--- /dev/null
@@ -0,0 +1,30 @@
+/* PR middle-end/30322 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-original" } */
+
+int test1(int a)
+{
+  return a + ~a;
+}
+
+int test2(int b)
+{
+  return ~b + b;
+}
+
+unsigned int test3(unsigned int c)
+{
+  return c + ~c;
+}
+
+unsigned int test4(unsigned int d)
+{
+  return ~d + d;
+}
+
+/* { dg-final { scan-tree-dump-times "\\+ a" 0 "original" } } */
+/* { dg-final { scan-tree-dump-times "\\+ b" 0 "original" } } */
+/* { dg-final { scan-tree-dump-times "\\+ c" 0 "original" } } */
+/* { dg-final { scan-tree-dump-times "\\+ d" 0 "original" } } */
+/* { dg-final { cleanup-tree-dump "original" } } */
+