This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH] Fix problematic folding
- From: "Richard Guenther" <richard dot guenther at gmail dot com>
- To: "Eric Botcazou" <ebotcazou at adacore dot com>
- Cc: gcc-patches at gcc dot gnu dot org
- Date: Thu, 31 Jul 2008 23:28:49 +0200
- Subject: Re: [PATCH] Fix problematic folding
- References: <200807311946.49872.ebotcazou@adacore.com>
On Thu, Jul 31, 2008 at 7:46 PM, Eric Botcazou <ebotcazou@adacore.com> wrote:
> Hi,
>
> the recent conversion of GNAT to boolean_type_node for Standard.Boolean has
> uncovered a problem in the constant folder, namely fold_binary:
>
> /* A - B -> A + (-B) if B is easily negatable. */
> if (negate_expr_p (arg1)
> && ((FLOAT_TYPE_P (type)
> /* Avoid this transformation if B is a positive REAL_CST. */
> && (TREE_CODE (arg1) != REAL_CST
> || REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
> || INTEGRAL_TYPE_P (type)))
> return fold_build2 (PLUS_EXPR, type,
> fold_convert (type, arg0),
> fold_convert (type, negate_expr (arg1)));
>
> Suppose that the type is unsigned integral with precision 1 (and QImode), then
> A-1 is turned into A+1 by this transformation, which changes the final code
> since A+1 doesn't wrap around in QImode (255 would have been needed).
It should wrap around as we properly sign-/zero-extend based on TYPE_PRECISION
nowadays. I suspect you lose some conversion to bool somewhere?
Note that the other frontends specialize boolean arithmetic completely and
never run into the above folding for bools.
Richard.
>
> Hence the proposed fix, tested on i586-suse-linux, OK for mainline?
>
>
> 2008-07-31 Eric Botcazou <ebotcazou@adacore.com>
>
> * fold-const.c (fold_binary) <MINUS_EXPR>: Turn it into a PLUS_EXPR
> for integral types only if the sign bit of the mode is toggled.
>
>
> 2008-07-31 Eric Botcazou <ebotcazou@adacore.com>
>
> * gnat.dg/boolean_expr2.adb: New test.
>
>
> --
> Eric Botcazou
>