https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82170
--- Comment #8 from Jakub Jelinek <jakub at gcc dot gnu.org> --- To summarize IRC discussions about this, the first step should be to introduce SEXT_EXPR (split from Prathamesh's patch, improve), then add match.pd canonicalization of these range testing to SEXT_EXPR + EQ_EXPR/NE_EXPR or BIT_AND_EXPR + EQ/NE, recognize those in range discovery for reassoc. Combine probably isn't able to handle transformation of one seq to the other, and in any case, combiner would be only one way (it simplifies/canonicalizes the IL to something, but doesn't have easy way to try two completely different but equivalent sequences; also, often it is more than 4 instructions). So we should try to do something in the expansion to try both sequences and pick the less costly.