re PR middle-end/52578 (Fails to fold another size difference)
authorRichard Guenther <rguenther@suse.de>
Wed, 14 Mar 2012 10:51:34 +0000 (10:51 +0000)
committerRichard Biener <rguenth@gcc.gnu.org>
Wed, 14 Mar 2012 10:51:34 +0000 (10:51 +0000)
2012-03-14  Richard Guenther  <rguenther@suse.de>

PR middle-end/52578
* fold-const.c (fold_unary_loc): Fold (T1)(T2)x to (T1)x if
the outermost conversion is a sign-change only.
(fold_binary_loc): Disregard widening and sign-changing
conversions when we determine if two variables are equal
for reassociation.
* tree-ssa-forwprop.c (combine_conversions): Fold (T1)(T2)x to
(T1)x if the outermost conversion is a sign-change only.

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

From-SVN: r185378

gcc/ChangeLog
gcc/fold-const.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/pr52578.c [new file with mode: 0644]
gcc/tree-ssa-forwprop.c

index 88fc542..4a3d0d1 100644 (file)
@@ -1,3 +1,14 @@
+2012-03-14  Richard Guenther  <rguenther@suse.de>
+
+       PR middle-end/52578
+       * fold-const.c (fold_unary_loc): Fold (T1)(T2)x to (T1)x if
+       the outermost conversion is a sign-change only.
+       (fold_binary_loc): Disregard widening and sign-changing
+       conversions when we determine if two variables are equal
+       for reassociation.
+       * tree-ssa-forwprop.c (combine_conversions): Fold (T1)(T2)x to
+       (T1)x if the outermost conversion is a sign-change only.
+
 2012-03-14  Uros Bizjak  <ubizjak@gmail.com>
 
        Revert:
index ab653ea..9f5c097 100644 (file)
@@ -7843,10 +7843,13 @@ fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
            return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
 
          /* If we have a sign-extension of a zero-extended value, we can
-            replace that by a single zero-extension.  */
+            replace that by a single zero-extension.  Likewise if the
+            final conversion does not change precision we can drop the
+            intermediate conversion.  */
          if (inside_int && inter_int && final_int
-             && inside_prec < inter_prec && inter_prec < final_prec
-             && inside_unsignedp && !inter_unsignedp)
+             && ((inside_prec < inter_prec && inter_prec < final_prec
+                  && inside_unsignedp && !inter_unsignedp)
+                 || final_prec == inter_prec))
            return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
 
          /* Two conversions in a row are not needed unless:
@@ -10335,10 +10338,21 @@ fold_binary_loc (location_t loc,
 
                  if (TREE_CODE (tmp0) == NEGATE_EXPR)
                    tmp0 = TREE_OPERAND (tmp0, 0);
+                 if (CONVERT_EXPR_P (tmp0)
+                     && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
+                     && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp0, 0)))
+                         <= TYPE_PRECISION (type)))
+                   tmp0 = TREE_OPERAND (tmp0, 0);
                  if (TREE_CODE (tmp1) == NEGATE_EXPR)
                    tmp1 = TREE_OPERAND (tmp1, 0);
+                 if (CONVERT_EXPR_P (tmp1)
+                     && INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
+                     && (TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (tmp1, 0)))
+                         <= TYPE_PRECISION (type)))
+                   tmp1 = TREE_OPERAND (tmp1, 0);
                  /* The only case we can still associate with two variables
-                    is if they are the same, modulo negation.  */
+                    is if they are the same, modulo negation and bit-pattern
+                    preserving conversions.  */
                  if (!operand_equal_p (tmp0, tmp1, 0))
                    ok = false;
                }
index 11c8e8f..b4626f3 100644 (file)
@@ -1,3 +1,8 @@
+2012-03-14  Richard Guenther  <rguenther@suse.de>
+
+       PR middle-end/52578
+       * gcc.dg/pr52578.c: New testcase.
+
 2012-03-14  Jakub Jelinek  <jakub@redhat.com>
 
        PR c++/52521
diff --git a/gcc/testsuite/gcc.dg/pr52578.c b/gcc/testsuite/gcc.dg/pr52578.c
new file mode 100644 (file)
index 0000000..0e66830
--- /dev/null
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-fdump-tree-original" } */
+
+long bar (long i)
+{
+  return (long)((unsigned long)i + 2) - (long)i;
+}
+long foo (int i)
+{
+  return (long)((unsigned long)i + 2) - (long)i;
+}
+
+/* { dg-final { scan-tree-dump-times "return 2;" 2 "original" } } */
+/* { dg-final { cleanup-tree-dump "original" } } */
index 20821ef..9197795 100644 (file)
@@ -2285,10 +2285,13 @@ combine_conversions (gimple_stmt_iterator *gsi)
        }
 
       /* If we have a sign-extension of a zero-extended value, we can
-        replace that by a single zero-extension.  */
+        replace that by a single zero-extension.  Likewise if the
+        final conversion does not change precision we can drop the
+        intermediate conversion.  */
       if (inside_int && inter_int && final_int
-         && inside_prec < inter_prec && inter_prec < final_prec
-         && inside_unsignedp && !inter_unsignedp)
+         && ((inside_prec < inter_prec && inter_prec < final_prec
+              && inside_unsignedp && !inter_unsignedp)
+             || final_prec == inter_prec))
        {
          gimple_assign_set_rhs1 (stmt, defop0);
          update_stmt (stmt);