re PR tree-optimization/20922 (missed always false conditional)

2005-04-18  James A. Morrison  <phython@gcc.gnu.org>

        PR tree-optimization/20922
        * fold-const.c (fold_binary): Fold X - c > X and X + c < X to false.
        Fold X + c >= X and fold X - c <= X to true.

From-SVN: r98321
This commit is contained in:
James A. Morrison 2005-04-18 15:18:21 +00:00
parent 8f6c588902
commit 0eeb03e6fb
9 changed files with 270 additions and 0 deletions

View File

@ -1,3 +1,9 @@
2005-04-18 James A. Morrison <phython@gcc.gnu.org>
PR tree-optimization/20922
* fold-const.c (fold_binary): Fold X - c > X and X + c < X to false.
Fold X + c >= X and fold X - c <= X to true.
2005-04-18 James A. Morrison <phython@gcc.gnu.org>
* config/ia64/unwind-ia64.c (emergency_reg_state_free): Make an

View File

@ -8848,6 +8848,77 @@ fold_binary (enum tree_code code, tree type, tree op0, tree op1)
}
}
/* Transform comparisons of the form X +- C CMP X. */
if ((code != EQ_EXPR && code != NE_EXPR)
&& (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
&& operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
&& ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
&& !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
|| (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
&& !TYPE_UNSIGNED (TREE_TYPE (arg1))
&& !(flag_wrapv || flag_trapv))))
{
tree arg01 = TREE_OPERAND (arg0, 1);
enum tree_code code0 = TREE_CODE (arg0);
int is_positive;
if (TREE_CODE (arg01) == REAL_CST)
is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
else
is_positive = tree_int_cst_sgn (arg01);
/* (X - c) > X becomes false. */
if (code == GT_EXPR
&& ((code0 == MINUS_EXPR && is_positive >= 0)
|| (code0 == PLUS_EXPR && is_positive <= 0)))
return constant_boolean_node (0, type);
/* Likewise (X + c) < X becomes false. */
if (code == LT_EXPR
&& ((code0 == PLUS_EXPR && is_positive >= 0)
|| (code0 == MINUS_EXPR && is_positive <= 0)))
return constant_boolean_node (0, type);
/* Convert (X - c) <= X to true. */
if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
&& code == LE_EXPR
&& ((code0 == MINUS_EXPR && is_positive >= 0)
|| (code0 == PLUS_EXPR && is_positive <= 0)))
return constant_boolean_node (1, type);
/* Convert (X + c) >= X to true. */
if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
&& code == GE_EXPR
&& ((code0 == PLUS_EXPR && is_positive >= 0)
|| (code0 == MINUS_EXPR && is_positive <= 0)))
return constant_boolean_node (1, type);
if (TREE_CODE (arg01) == INTEGER_CST)
{
/* Convert X + c > X and X - c < X to true for integers. */
if (code == GT_EXPR
&& ((code0 == PLUS_EXPR && is_positive > 0)
|| (code0 == MINUS_EXPR && is_positive < 0)))
return constant_boolean_node (1, type);
if (code == LT_EXPR
&& ((code0 == MINUS_EXPR && is_positive > 0)
|| (code0 == PLUS_EXPR && is_positive < 0)))
return constant_boolean_node (1, type);
/* Convert X + c <= X and X - c >= X to false for integers. */
if (code == LE_EXPR
&& ((code0 == PLUS_EXPR && is_positive > 0)
|| (code0 == MINUS_EXPR && is_positive < 0)))
return constant_boolean_node (0, type);
if (code == GE_EXPR
&& ((code0 == MINUS_EXPR && is_positive > 0)
|| (code0 == PLUS_EXPR && is_positive < 0)))
return constant_boolean_node (0, type);
}
}
if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
{
tree targ0 = strip_float_extensions (arg0);

View File

@ -1,3 +1,13 @@
2005-04-18 James A. Morrison <phython@gcc.gnu.org>
PR tree-optimization/20922
* gcc.dg/pr20922-1.c: New test.
* gcc.dg/pr20922-2.c: New test.
* gcc.dg/pr20922-3.c: New test.
* gcc.dg/pr20922-4.c: New test.
* gcc.dg/pr20922-5.c: New test.
* gcc.dg/pr20922-6.c: New test.
2005-04-11 Francois-Xavier Coudert <coudert@clipper.ens.fr>
PR libfortran/20950

View File

@ -0,0 +1,38 @@
/* { dg-do compile } */
/* { dg-options "-fno-wrapv -fdump-tree-generic" } */
int f(int i)
{
return (i - 2) > i;
}
int g(int i)
{
return (i + 2) < i;
}
int h(int i)
{
return (i + (-2)) > i;
}
int j(int i)
{
return (i - (-2)) < i;
}
int x(double i)
{
return (i - 2.0) > i;
}
int y(double i)
{
return (i + 2.0) < i;
}
int z(double i)
{
return (i + (-2.0)) > i;
}
/* { dg-final { scan-tree-dump-times " = 0" 7 "generic" } } */
/* { dg-final { cleanup-tree-dump "generic" } } */

View File

@ -0,0 +1,18 @@
/* { dg-do compile } */
/* { dg-options "-fwrapv -fdump-tree-generic" } */
int f (int i)
{
return (i - 2) > i;
}
int g (int i)
{
return (i + 2) < i;
}
int h (double i)
{
return (i + 2.0) <= i;
}
/* { dg-final { scan-tree-dump-times " = 0" 0 "generic" } } */
/* { dg-final { cleanup-tree-dump "generic" } } */

View File

@ -0,0 +1,33 @@
/* { dg-do compile } */
/* { dg-options "-ffast-math -fno-wrapv -fdump-tree-generic" } */
int f(int i)
{
return (i - 2) <= i;
}
int g(int i)
{
return (i + 2) >= i;
}
int h(int i)
{
return (i + (-2)) <= i;
}
int x(double i)
{
return (i - 2.0) <= i;
}
int y(double i)
{
return (i + 2.0) >= i;
}
int z(double i)
{
return (i + (-2.0)) <= i;
}
/* { dg-final { scan-tree-dump-times " = 1" 6 "generic" } } */
/* { dg-final { cleanup-tree-dump "generic" } } */

View File

@ -0,0 +1,38 @@
/* { dg-do compile } */
/* { dg-options "-fno-wrapv -fdump-tree-generic" } */
int f(int i)
{
return i < (i - 2);
}
int g(int i)
{
return i > (i + 2);
}
int h(int i)
{
return i < (i + (-2));
}
int j(int i)
{
return i > (i - (-2));
}
int x(double i)
{
return i < (i - 2.0);
}
int y(double i)
{
return i > (i + 2.0);
}
int z(double i)
{
return i < (i + (-2.0));
}
/* { dg-final { scan-tree-dump-times " = 0" 7 "generic" } } */
/* { dg-final { cleanup-tree-dump "generic" } } */

View File

@ -0,0 +1,23 @@
/* { dg-do compile } */
/* { dg-options "-fsignaling-nans -fwrapv -fdump-tree-generic" } */
int f(int i)
{
return i < (i - 2);
}
int g(int i)
{
return i > (i + 2);
}
int h (double i)
{
return i >= i + 2.0;
}
int j (double i)
{
return i > i + 2.0;
}
/* { dg-final { scan-tree-dump-times " = 0" 0 "generic" } } */
/* { dg-final { cleanup-tree-dump "generic" } } */

View File

@ -0,0 +1,33 @@
/* { dg-do compile } */
/* { dg-options "-ffast-math -fno-wrapv -fdump-tree-generic" } */
int f(int i)
{
return i >= (i - 2);
}
int g(int i)
{
return i <= (i + 2);
}
int h(int i)
{
return i >= (i + (-2));
}
int x(double i)
{
return i >= (i - 2.0);
}
int y(double i)
{
return i <= (i + 2.0);
}
int z(double i)
{
return i >= (i + (-2.0));
}
/* { dg-final { scan-tree-dump-times " = 1" 6 "generic" } } */
/* { dg-final { cleanup-tree-dump "generic" } } */