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 <exactl...@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