public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Richard Biener <richard.guenther@gmail.com>
To: Andrew Pinski <pinskia@gmail.com>
Cc: Daniil Frolov <exactlywb@ispras.ru>, gcc@gcc.gnu.org
Subject: Re: Question on GIMPLE shifts
Date: Thu, 2 Nov 2023 09:00:32 +0100	[thread overview]
Message-ID: <CAFiYyc2T_j2umaFOsVK=sFh=uaOk_3C8FWM2Q-ZwutUpRbzDHg@mail.gmail.com> (raw)
In-Reply-To: <CA+=Sn1mi7ecRGMZsqTR9SRuDwtvBJ-zGNouAT78BQL_Eb7X+ww@mail.gmail.com>

On Wed, Nov 1, 2023 at 5:01 PM Andrew Pinski via Gcc <gcc@gcc.gnu.org> wrote:
>
> On Wed, Nov 1, 2023 at 3:56 AM Daniil Frolov <exactlywb@ispras.ru> wrote:
> >
> > Hi!
> >
> > When investigating bit shifts I got an incomprehensible moment with
> > the following example:
> >
> > int f(int x, int k)
> > {
> >      int tmp = x >> k;
> >      return (tmp & 1) << 10;
> > }
> >
> > If we would like to take a look into GIMPLE then we'll get:
> >
> > int f (int x, int k)
> > {
> >    int tmp;
> >    int D.2746;
> >    int _1;
> >    int _5;
> >
> >    <bb 2> :
> >    tmp_4 = x_2(D) >> k_3(D);
> >    _1 = tmp_4 << 10;
> >    _5 = _1 & 1024;
> >
> >    <bb 3> :
> > <L0>:
> >    return _5;
> >
> > }
> >
> > Is the expression '_1 = tmp_4 << 10' considered legal in GIMPLE?  Given
> > the
> > semantics of C bit shifts, this statement could modify the sign bit,
> > potentially leading to overflow.
>
> Except it was not undefined in C90.

Also in GIMPLE/GENERIC left-shifts are always logical and the result is
modulo-reduced to the target type.  There's no (undefined) arithmetic overflow
involved for any shift operation (but there is for multiply).  Only the shift
argument magnitude is constrained.

Richard.

> Thanks,
> Andrew
>
> >
> > ---
> > With best regards,
> > Daniil

      reply	other threads:[~2023-11-02  8:03 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-01  9:52 Daniil Frolov
2023-11-01 16:00 ` Andrew Pinski
2023-11-02  8:00   ` 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='CAFiYyc2T_j2umaFOsVK=sFh=uaOk_3C8FWM2Q-ZwutUpRbzDHg@mail.gmail.com' \
    --to=richard.guenther@gmail.com \
    --cc=exactlywb@ispras.ru \
    --cc=gcc@gcc.gnu.org \
    --cc=pinskia@gmail.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).