https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113105
Jakub Jelinek <jakub at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |jakub at gcc dot gnu.org --- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> --- I think with int v, a, b; ... v / a * b + v % a can be simplified into (int) (v / a * ((unsigned) b - a) + v), i.e. perform just the division in signed and everything else in corresponding unsigned type. Also, a question is if this is a useful optimization on targets where one instruction can compute both v / a and v % a together, because then the original has roughly one divmod insn, one multiplication and one addition, compared to the divmod insn from which only division is used, subtraction, multiplication and addition. Of course, if b - a can fold into a constant, it is different (but multiplication by constant is often done using shifts and additions and multiplication by b might be cheaper than by b - a. When v % a needs to be computed separately and especially when it is expensive, it can be obviously a win. >From the usual GIMPLE IL rules, both forms are 4 statements so equally good, but for the case where casts are needed, the replacement is more expensive. So, perhaps this shouldn't be done in match.pd, but during expansion or immediately before expansion, expanding to RTL both forms and comparing the costs.