Li Jia He <heli...@linux.ibm.com> writes:

> Hi,
> @@ -546,6 +546,12 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
>   (simplify
>    (mod (mod@2 @0 @1) @1)
>    @2)
> + /* Optimize (x shift (n mod C)) to (x shift (n bit_and (C - 1))) when C is a
> +    constant and power of two.  */
> + (for shift (lshift rshift)
> +  (simplify
> +   (shift @0 (mod @1 integer_pow2p@2))
> +   (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2), 1); 
> })))))
>   /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2.  
> */
>   (simplify
>    (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
> +unsigned a(unsigned x, int n)
> +{
> +  return x >> (n % 32);
> +}
> +

With this patch, the behavior will change on negtive @2. While this
would not be an issue because it is UB.

In below foo, r and r1 are not same after patch.
---------
int __attribute__ ((noinline))
foo (unsigned x, int n, int t)
{
  int r = x >> (n % 4);
  int r1 = x >> t;
  __builtin_printf ("%d : %d\n", r, r1);  
  return r == r1;
}

int main()
{
  unsigned x = 0x12c;
  int n = -3;
  return foo (x, n, n %4);
}
---------
For: "x=0x12c, n=-3; x >> (n % 4)"  without patch, it is 0; with patch
it is 0x96.

Reply via email to