From: Richard Biener <richard.guenther@gmail.com>
To: Marc Glisse <marc.glisse@inria.fr>
Cc: Roger Sayle <roger@nextmovesoftware.com>,
GCC Patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] Optimize (X<<C)+(Y<<C) as (X+Y)<<C for signed addition.
Date: Thu, 15 Sep 2022 08:39:16 +0200 [thread overview]
Message-ID: <CAFiYyc1dAZSZLk06S6830j+N4NabOSpvnfqMB8_tC9FAztFPLg@mail.gmail.com> (raw)
In-Reply-To: <4855eb8c-22fd-17f5-fa97-eacd33d015dc@inria.fr>
On Wed, Sep 14, 2022 at 11:42 PM Marc Glisse via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> On Tue, 13 Sep 2022, Roger Sayle wrote:
>
> > This patch tweaks the match.pd transformation previously added to fold
> > (X<<C)+(Y<<C) as (X+Y)<<C that was previously restricted to unsigned
> > (wrapping) types, to also allow signed integer types provided that they
> > don't trap and the overflow needn't be preserved for sanitization.
> > i.e. this should now apply (by default) for "int x,y;", but is disabled
> > with -ftrapv.
>
> In https://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html , I
> read:
>
> "Bitwise operators act on the representation of the value including both
> the sign and value bits, where the sign bit is considered immediately
> above the highest-value value bit. Signed ‘>>’ acts on negative numbers by
> sign extension.
>
> As an extension to the C language, GCC does not use the latitude given in
> C99 and C11 only to treat certain aspects of signed ‘<<’ as undefined."
Oh, I wasn't aware of that.
> To me, this means that for instance INT_MIN<<1 is well defined and
> evaluates to 0. But with this patch we turn (INT_MIN<<1)+(INT_MIN<<1) into
> (INT_MIN+INT_MIN)<<1, which is UB.
>
> If we decide not to support this extension anymore, I think we need to
> change the documentation first.
Or perform the (X+Y) operation in an unsigned type.
Richard.
> --
> Marc Glisse
prev parent reply other threads:[~2022-09-15 6:39 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-09-13 18:37 Roger Sayle
2022-09-14 8:28 ` Richard Biener
2022-09-14 21:42 ` Marc Glisse
2022-09-15 6:39 ` 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=CAFiYyc1dAZSZLk06S6830j+N4NabOSpvnfqMB8_tC9FAztFPLg@mail.gmail.com \
--to=richard.guenther@gmail.com \
--cc=gcc-patches@gcc.gnu.org \
--cc=marc.glisse@inria.fr \
--cc=roger@nextmovesoftware.com \
/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).