On Mon, Jul 27, 2015 at 09:11:12AM +0100, Kyrill Tkachov wrote: > On 25/07/15 03:19, Segher Boessenkool wrote: > >On Fri, Jul 24, 2015 at 09:09:39AM +0100, Kyrill Tkachov wrote: > >>This transformation folds (X % C) == N into > >>X & ((1 << (size - 1)) | (C - 1))) == N > >>for constants C and N where N is positive and C is a power of 2. > >For N = 0 you can transform it to > > > > ((unsigned)X % C) == N > > > >and for 0 < N < C you can transform it to > > > > X > 0 && ((unsigned)X % C) == N (or X >= 0) > > > >and for -C < N < 0 it is > > > > X < 0 && ((unsigned)X % C) == N + C (or X <= 0) > > > >and for other N it is > > > > 0. > > > >For N not a constant, well, do you really care? :-) > > > >(That second case might eventually fold to your original expression). > > Yeah, these avoid the potentially expensive mask,
Fun fact: the current code ends up using the exact same mask, for some targets. > but introduce more operations, > which I believe may not be desirable at this stage. It is getting rid of the (expensive) division/modulo. In many cases it could get rid of the sign test, or hoist it to some outer structure, hard to test here though (at least, I have no idea how to do that). > Unless these transformations are ok for match.pd I'll try to implement this > transformation > at RTL expansion time. If you have to do conditional jumps, the RTL optimisers will not be able to do very much :-( Segher