This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

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


On Mon, Jun 8, 2015 at 7:03 PM, Marc Glisse <marc.glisse@inria.fr> wrote:
> 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?

Yes, one space more.  I suppose using integer_zerop might in theory
allow for handling vector shifts at some point ...?

>> +       && 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.

Sounds like a good improvement.

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

Hm, yes.  I think signed overflow != shift amount overflow, so testing
the overflow
macros for this isn't valid.

Otherwise the patch looks ok to me as well - mind doing the improvement above?

Thanks,
Richard.

> FWIW, the patch looks good to me, thanks.
>
> --
> Marc Glisse


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]