[PATCH] Reassociate X == CST1 || X == CST2 if popcount (CST2 - CST1) == 1 into ((X - CST1) & ~(CST2 - CST1)) == 0

Jakub Jelinek jakub@redhat.com
Thu Oct 10 10:38:00 GMT 2013

On Thu, Oct 10, 2013 at 05:25:01PM +0800, Zhenqiang Chen wrote:
> > Note I've been suggesting the bits I'm referring to in fold-const.c move
> out
> > into the tree-ssa optimizers.  If they fit well into tree-ssa-reassoc.c
> I'd look
> > favorably upon a patch which moved them.
> The code is similar with the code (in tree-ssa-reassoc.c) for 
>      Optimize X == CST1 || X == CST2
>      if popcount (CST1 ^ CST2) == 1 into
>      (X & ~(CST1 ^ CST2)) == (CST1 & ~(CST1 ^ CST2))

Yeah.  Though, that is one operation cheaper than this, the above
is replacing two comparisons with constants plus one || with
one arithmetic operation (with constant) plus one comparison of constant,
I think that should be always a win (ok, it is one arithmetic operation
more expensive if X == CST1 all the time, but otherwise it is likely
cheaper).  While in your case it is two arithmetic operations plus
comparison with constant, so perhaps for very cheap BRANCH_COST it might
not be beneficial.


More information about the Gcc-patches mailing list