public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Richard Biener <richard.guenther@gmail.com>
To: Andrew Pinski <apinski@marvell.com>
Cc: gcc-patches@gcc.gnu.org
Subject: Re: [PATCH 1/2] Fix PR 110487: invalid signed boolean value
Date: Mon, 3 Jul 2023 09:50:12 +0200	[thread overview]
Message-ID: <CAFiYyc3h83s-jdwz1NmWa_oaUiVbQcQ26WqYt9w6Wy_8g1gazw@mail.gmail.com> (raw)
In-Reply-To: <20230701082216.299104-1-apinski@marvell.com>

On Sat, Jul 1, 2023 at 10:23 AM Andrew Pinski via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> This fixes the first part of this bug where `a ? -1 : 0`
> would cause a value of 1 into the signed boolean value.
> It fixes the problem by casting to an integer type of
> the same size/signedness before doing the negative and
> then casting to the type of expression.
>
> OK? Bootstrapped and tested on x86_64.

OK.

Richard.

> gcc/ChangeLog:
>
>         * match.pd (a?-1:0): Cast type an integer type
>         rather the type before the negative.
>         (a?0:-1): Likewise.
> ---
>  gcc/match.pd | 22 ++++++++++++++++++++--
>  1 file changed, 20 insertions(+), 2 deletions(-)
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 45c72e733a5..a0d114f6a16 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -4703,7 +4703,12 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>      /* a ? -1 : 0 -> -a.  No need to check the TYPE_PRECISION not being 1
>         here as the powerof2cst case above will handle that case correctly.  */
>      (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@1))
> -     (negate (convert (convert:boolean_type_node @0))))))
> +     (with {
> +       auto prec = TYPE_PRECISION (type);
> +       auto unsign = TYPE_UNSIGNED (type);
> +       tree inttype = build_nonstandard_integer_type (prec, unsign);
> +      }
> +      (convert (negate (convert:inttype (convert:boolean_type_node @0))))))))
>    (if (integer_zerop (@1))
>     (with {
>        tree booltrue = constant_boolean_node (true, boolean_type_node);
> @@ -4722,7 +4727,20 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>       /* a ? -1 : 0 -> -(!a).  No need to check the TYPE_PRECISION not being 1
>         here as the powerof2cst case above will handle that case correctly.  */
>       (if (INTEGRAL_TYPE_P (type) && integer_all_onesp (@2))
> -      (negate (convert (bit_xor (convert:boolean_type_node @0) { booltrue; } ))))
> +      (with {
> +       auto prec = TYPE_PRECISION (type);
> +       auto unsign = TYPE_UNSIGNED (type);
> +       tree inttype = build_nonstandard_integer_type (prec, unsign);
> +       }
> +       (convert
> +       (negate
> +         (convert:inttype
> +         (bit_xor (convert:boolean_type_node @0) { booltrue; } )
> +        )
> +       )
> +       )
> +      )
> +     )
>      )
>     )
>    )
> --
> 2.31.1
>

      parent reply	other threads:[~2023-07-03  7:53 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-01  8:22 Andrew Pinski
2023-07-01  8:22 ` [PATCH 2/2] PR 110487: `(a !=/== CST1 ? CST2 : CST3)` pattern for type safety Andrew Pinski
2023-07-04  8:55   ` Richard Biener
2023-07-03  7:50 ` Richard Biener [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAFiYyc3h83s-jdwz1NmWa_oaUiVbQcQ26WqYt9w6Wy_8g1gazw@mail.gmail.com \
    --to=richard.guenther@gmail.com \
    --cc=apinski@marvell.com \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).