public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Question on GIMPLE shifts
@ 2023-11-01  9:52 Daniil Frolov
  2023-11-01 16:00 ` Andrew Pinski
  0 siblings, 1 reply; 3+ messages in thread
From: Daniil Frolov @ 2023-11-01  9:52 UTC (permalink / raw)
  To: gcc

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.

---
With best regards,
Daniil

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Question on GIMPLE shifts
  2023-11-01  9:52 Question on GIMPLE shifts Daniil Frolov
@ 2023-11-01 16:00 ` Andrew Pinski
  2023-11-02  8:00   ` Richard Biener
  0 siblings, 1 reply; 3+ messages in thread
From: Andrew Pinski @ 2023-11-01 16:00 UTC (permalink / raw)
  To: Daniil Frolov; +Cc: gcc

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.

Thanks,
Andrew

>
> ---
> With best regards,
> Daniil

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Question on GIMPLE shifts
  2023-11-01 16:00 ` Andrew Pinski
@ 2023-11-02  8:00   ` Richard Biener
  0 siblings, 0 replies; 3+ messages in thread
From: Richard Biener @ 2023-11-02  8:00 UTC (permalink / raw)
  To: Andrew Pinski; +Cc: Daniil Frolov, gcc

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-11-02  8:03 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-01  9:52 Question on GIMPLE shifts Daniil Frolov
2023-11-01 16:00 ` Andrew Pinski
2023-11-02  8:00   ` Richard Biener

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).