This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [patch] for PR 23361
- From: "Richard Guenther" <richard dot guenther at gmail dot com>
- To: "Zdenek Dvorak" <rakdver at atrey dot karlin dot mff dot cuni dot cz>
- Cc: gcc-patches at gcc dot gnu dot org
- Date: Thu, 8 Feb 2007 16:32:50 +0100
- Subject: Re: [patch] for PR 23361
- References: <20070208143714.GA32631@atrey.karlin.mff.cuni.cz>
On 2/8/07, Zdenek Dvorak <rakdver@atrey.karlin.mff.cuni.cz> wrote:
Hello,
one of the reasons for this PR is that we fail to fold expressions like
a + 10 > MIN_INT + 2, that are always true (assuming that the
arithmetics in the considered type does not overflow). Such expressions
are fairly often produced by number of iterations analysis.
This patch implements such simplifications.
Bootstrapped & regtested on i686 and x86_64.
Hmm, this looks overly complicated. But maybe I'm missing something
obvious - the
attached also works for your testcase, but I dind't yet bootstrap & test it.
Richard.
Index: fold-const.c
===================================================================
*** fold-const.c (revision 121711)
--- fold-const.c (working copy)
*************** fold_comparison (enum tree_code code, tr
*** 8043,8048 ****
--- 8043,8071 ----
lhs = fold_build2 (lhs_add ? PLUS_EXPR : MINUS_EXPR,
TREE_TYPE (arg1), const2, const1);
+
+ /* If the constant operation overflowed this can be
+ simplified as a comparison against INT_MAX/INT_MIN. */
+ if (TREE_CODE (lhs) == INTEGER_CST
+ && TREE_OVERFLOW (lhs))
+ {
+ int const1_sgn = tree_int_cst_sgn (const1);
+ enum tree_code code2 = code;
+
+ if (TREE_CODE (arg0) == MINUS_EXPR)
+ const1_sgn = -const1_sgn;
+ if (const1_sgn == -1)
+ code2 = swap_tree_comparison (code);
+
+ if (code2 == LT_EXPR
+ || code2 == LE_EXPR
+ || code2 == EQ_EXPR)
+ return boolean_false_node;
+ else if (code2 == NE_EXPR
+ || code2 == GT_EXPR)
+ return boolean_true_node;
+ }
+
if (TREE_CODE (lhs) == TREE_CODE (arg1)
&& (TREE_CODE (lhs) != INTEGER_CST
|| !TREE_OVERFLOW (lhs)))