OSDN Git Service

2005-07-02 Andrew Pinski <pinskia@physics.uc.edu>
authorpinskia <pinskia@138bc75d-0d04-0410-961f-82ee72b054a4>
Sat, 2 Jul 2005 16:24:31 +0000 (16:24 +0000)
committerpinskia <pinskia@138bc75d-0d04-0410-961f-82ee72b054a4>
Sat, 2 Jul 2005 16:24:31 +0000 (16:24 +0000)
        PR middle-end/14490
        * fold-const.c (fold_binary): Handle the return value of
        fold_to_nonsharp_ineq_using_bound if we get back the same operand back.
        Implement "X +- C1 CMP C2" folding to "X CMP C2 -+ C1".

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

gcc/ChangeLog
gcc/fold-const.c
gcc/testsuite/gcc.dg/20050702-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/pr14490-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/pr14490-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/pr14490-3.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/pr14490-4.c [new file with mode: 0644]

index 6e5396a..4b3d554 100644 (file)
@@ -1,3 +1,10 @@
+2005-07-02  Andrew Pinski  <pinskia@physics.uc.edu>
+
+       PR middle-end/14490
+       * fold-const.c (fold_binary): Handle the return value of
+       fold_to_nonsharp_ineq_using_bound if we get back the same operand back.
+       Implement "X +- C1 CMP C2" folding to "X CMP C2 -+ C1".
+
 2005-07-02  Jeff Law  <law@redhat.com>
 
        * tree-ssa-dom.c (find_equivalent_equality_comparison): Do not
index 564cec3..682ae00 100644 (file)
@@ -8575,11 +8575,11 @@ fold_binary (enum tree_code code, tree type, tree op0, tree op1)
          && !TREE_SIDE_EFFECTS (arg1))
        {
          tem = fold_to_nonsharp_ineq_using_bound (arg0, arg1);
-         if (tem)
+         if (tem && !operand_equal_p (tem, arg0, 0))
            return fold_build2 (code, type, tem, arg1);
 
          tem = fold_to_nonsharp_ineq_using_bound (arg1, arg0);
-         if (tem)
+         if (tem && !operand_equal_p (tem, arg1, 0))
            return fold_build2 (code, type, arg0, tem);
        }
 
@@ -8865,6 +8865,30 @@ fold_binary (enum tree_code code, tree type, tree op0, tree op1)
            }
        }
 
+      /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1.  */
+      if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
+         && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
+             && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
+             && !TYPE_UNSIGNED (TREE_TYPE (arg1))
+             && !(flag_wrapv || flag_trapv))
+         && (TREE_CODE (arg1) == INTEGER_CST
+             && !TREE_OVERFLOW (arg1)))
+       {
+         tree const1 = TREE_OPERAND (arg0, 1);
+         tree const2 = arg1;
+         tree variable = TREE_OPERAND (arg0, 0);
+         tree lhs;
+         int lhs_add;
+         lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
+         
+         lhs = fold_build2 (lhs_add ? PLUS_EXPR : MINUS_EXPR,
+                            TREE_TYPE (arg1), const2, const1);
+         if (TREE_CODE (lhs) == TREE_CODE (arg1)
+             && (TREE_CODE (lhs) != INTEGER_CST
+                 || !TREE_OVERFLOW (lhs)))
+           return fold_build2 (code, type, variable, lhs);
+       }
+
       if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
        {
          tree targ0 = strip_float_extensions (arg0);
diff --git a/gcc/testsuite/gcc.dg/20050702-1.c b/gcc/testsuite/gcc.dg/20050702-1.c
new file mode 100644 (file)
index 0000000..ce151d3
--- /dev/null
@@ -0,0 +1,24 @@
+/* { dg-do compile } */
+/* { dg-options "-O2" } */
+/* We going into an infinite loop in fold because we
+    were mishandling the return value of 
+    fold_to_nonsharp_ineq_using_bound. */
+_Bool f();
+void g(int);
+void h (int old_size)
+{
+   int new_size = old_size, i;
+   g(old_size - 1);
+   i = 0;
+   while (i < old_size - 1)
+   {
+     if (f())
+     {
+       i++;
+       continue;
+     }
+     while (i < old_size - 1)
+       i++;
+   }
+   g(new_size);
+}
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr14490-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr14490-1.c
new file mode 100644 (file)
index 0000000..733367b
--- /dev/null
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-options "-fdump-tree-gimple" } */
+int g(int x)
+{
+   return (x - 10) < 0;
+}
+/* There should be only x >= 9 and no x - 10. */
+/* { dg-final { scan-tree-dump-times ">= 9" 1 "gimple"} } */
+/* { dg-final { scan-tree-dump-times "- 10" 0 "gimple"} } */
+/* { dg-final { cleanup-tree-dump "gimple" } } */
+
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr14490-2.c b/gcc/testsuite/gcc.dg/tree-ssa/pr14490-2.c
new file mode 100644 (file)
index 0000000..eaa0f13
--- /dev/null
@@ -0,0 +1,10 @@
+/* { dg-do compile } */
+/* { dg-options "-fdump-tree-gimple -fwrapv" } */
+int g(int x)
+{
+   return (x - 10) < 0;
+}
+/* There should be no x >= 9 and one x - 10. */
+/* { dg-final { scan-tree-dump-times ">= 9" 0 "gimple"} } */
+/* { dg-final { scan-tree-dump-times "- 10" 1 "gimple"} } */
+/* { dg-final { cleanup-tree-dump "gimple" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr14490-3.c b/gcc/testsuite/gcc.dg/tree-ssa/pr14490-3.c
new file mode 100644 (file)
index 0000000..0e6873b
--- /dev/null
@@ -0,0 +1,10 @@
+/* { dg-do compile } */
+/* { dg-options "-fdump-tree-gimple" } */
+int g(int x)
+{
+   return (x + 10) < 0;
+}
+/* There should be only x >= -9 and no x + 10. */
+/* { dg-final { scan-tree-dump-times ">= 9" 1 "gimple"} } */
+/* { dg-final { scan-tree-dump-times "- 10" 0 "gimple"} } */
+/* { dg-final { cleanup-tree-dump "gimple" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr14490-4.c b/gcc/testsuite/gcc.dg/tree-ssa/pr14490-4.c
new file mode 100644 (file)
index 0000000..365ab77
--- /dev/null
@@ -0,0 +1,10 @@
+/* { dg-do compile } */
+/* { dg-options "-fdump-tree-gimple -fwrapv" } */
+int g(int x)
+{
+   return (x + 10) < 0;
+}
+/* There should be no x >= -9 and one x + 10. */
+/* { dg-final { scan-tree-dump-times ">= 9" 0 "gimple"} } */
+/* { dg-final { scan-tree-dump-times "- 10" 1 "gimple"} } */
+/* { dg-final { cleanup-tree-dump "gimple" } } */