On Wed, Sep 05, 2018 at 03:14:40PM +0200, Jakub Jelinek wrote: > It isn't really a GIMPLE transform, just needs to provide the callers > with a tree. Specifically, if it passes several initial checks, it > expands the X expression unconditionally and then just uses the result of > that. What I could and probably should do is to just emit the cheaper > of the two sequences and make_tree the result thereof, so the caller > would emit only the actual comparison, rather than the modulo or its > replacement again too as the patch does.
In particular I meant this incremental patch, where the effect is really that the first argument of the modulo is expanded unconditionally once, the two possible variants are then each expanded into a sequence, then based on cost we decide which one to use, emit it (the other one is GCed) and let the callers emit the comparison. --- gcc/expr.c.jj 2018-09-05 15:34:10.395601918 +0200 +++ gcc/expr.c 2018-09-05 15:47:00.609834755 +0200 @@ -11716,12 +11716,13 @@ maybe_optimize_mod_cmp (enum tree_code c if (mocost <= mucost) { - *arg0 = build2_loc (loc, TRUNC_MOD_EXPR, TREE_TYPE (treeop0), - treeop0, treeop1); + emit_insn (moinsns); + *arg0 = make_tree (TREE_TYPE (*arg0), mor); return code; } - *arg0 = t; + emit_insn (muinsns); + *arg0 = make_tree (type, mur); *arg1 = c4; return code == EQ_EXPR ? LE_EXPR : GT_EXPR; } Jakub