From 437f1df1afacb10132608f3cf27f4289c1bdbe1c Mon Sep 17 00:00:00 2001 From: Roger Sayle Date: Fri, 7 Jun 2002 23:42:53 +0000 Subject: fold-const.c (fold): Place both integer and real constants last in comparisons. * fold-const.c (fold) [EQ_EXPR]: Place both integer and real constants last in comparisons. Optimize (x+1.0)>0.0 into the equivalent x > -1.0 when -ffast-math. * gcc.dg/20020607-2.c: New test case. From-SVN: r54356 --- gcc/fold-const.c | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) (limited to 'gcc/fold-const.c') diff --git a/gcc/fold-const.c b/gcc/fold-const.c index 6febe58..db41564 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -5814,6 +5814,20 @@ fold (expr) case GT_EXPR: case LE_EXPR: case GE_EXPR: + /* If one arg is a real or integer constant, put it last. */ + if ((TREE_CODE (arg0) == INTEGER_CST + && TREE_CODE (arg1) != INTEGER_CST) + || (TREE_CODE (arg0) == REAL_CST + && TREE_CODE (arg0) != REAL_CST)) + { + TREE_OPERAND (t, 0) = arg1; + TREE_OPERAND (t, 1) = arg0; + arg0 = TREE_OPERAND (t, 0); + arg1 = TREE_OPERAND (t, 1); + code = swap_tree_comparison (code); + TREE_SET_CODE (t, code); + } + if (FLOAT_TYPE_P (TREE_TYPE (arg0))) { /* (-a) CMP (-b) -> b CMP a */ @@ -5835,18 +5849,21 @@ fold (expr) && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (arg1))) return fold (build (code, type, arg0, build_real (TREE_TYPE (arg1), dconst0))); - } - /* If one arg is a constant integer, put it last. */ - if (TREE_CODE (arg0) == INTEGER_CST - && TREE_CODE (arg1) != INTEGER_CST) - { - TREE_OPERAND (t, 0) = arg1; - TREE_OPERAND (t, 1) = arg0; - arg0 = TREE_OPERAND (t, 0); - arg1 = TREE_OPERAND (t, 1); - code = swap_tree_comparison (code); - TREE_SET_CODE (t, code); + /* If this is a comparison of a real constant with a PLUS_EXPR + or a MINUS_EXPR of a real constant, we can convert it into a + comparison with a revised real constant as long as no overflow + occurs when unsafe_math_optimizations are enabled. */ + if (flag_unsafe_math_optimizations + && TREE_CODE (arg1) == REAL_CST + && (TREE_CODE (arg0) == PLUS_EXPR + || TREE_CODE (arg0) == MINUS_EXPR) + && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST + && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR + ? MINUS_EXPR : PLUS_EXPR, + arg1, TREE_OPERAND (arg0, 1), 0)) + && ! TREE_CONSTANT_OVERFLOW (tem)) + return fold (build (code, type, TREE_OPERAND (arg0, 0), tem)); } /* Convert foo++ == CONST into ++foo == CONST + INCR. -- cgit v1.1