[PATCH] Optimize (CST1 << A) == CST2 (PR tree-optimization/66299)

Marc Glisse marc.glisse@inria.fr
Mon Jun 8 17:14:00 GMT 2015


On Mon, 8 Jun 2015, Marek Polacek wrote:

>       PR tree-optimization/66299
>       * match.pd ((CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
>       ((CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)): New

You are braver than I am, I would have abbreviated ctz (CST2) - ctz (CST1)
to CST3 in the ChangeLog ;-)

> +/* (CST1 << A) == CST2 -> A == ctz (CST2) - ctz (CST1)
> +   (CST1 << A) != CST2 -> A != ctz (CST2) - ctz (CST1)
> +   if CST2 != 0.  */
> +(for cmp (ne eq)
> + (simplify
> +  (cmp (lshift INTEGER_CST@0 @1) INTEGER_CST@2)
> +  (with {
> +   unsigned int cand = wi::ctz (@2) - wi::ctz (@0); }
> +  (if (!integer_zerop (@2)

You can probably use directly wi::ne_p (@2, 0) here. Shouldn't this be
indented one space more?

> +       && wi::eq_p (wi::lshift (@0, cand), @2))
> +   (cmp @1 { build_int_cst (TREE_TYPE (@1), cand); })))))

Making 'cand' signed, you could return 0 when cand<0, like (2<<x)==1. You
could also return 0 when the candidate turns out not to work: (3<<x)==4.

Tweaking it so that (6<<X)==0 becomes X>=31 for TYPE_OVERFLOW_WRAPS and
false for TYPE_OVERFLOW_UNDEFINED is probably more controversial.

FWIW, the patch looks good to me, thanks.

-- 
Marc Glisse



More information about the Gcc-patches mailing list