On 4/15/25 12:24, Richard Henderson wrote: > For most binary operands, two const operands fold. > However, the add/sub carry opcodes have a third input. > Prefer "reg, zero, const" since many risc hosts have a > zero register that can fit a "reg, reg, const" insn format. > > Signed-off-by: Richard Henderson <richard.henderson@linaro.org> > --- > tcg/optimize.c | 18 ++++++++++++------ > 1 file changed, 12 insertions(+), 6 deletions(-) > > diff --git a/tcg/optimize.c b/tcg/optimize.c > index 1b3d0b5b5d..442f5b75e6 100644 > --- a/tcg/optimize.c > +++ b/tcg/optimize.c > @@ -725,12 +725,18 @@ static int do_constant_folding_cond(TCGType type, TCGArg x, > > #define NO_DEST temp_arg(NULL) > > +static int pref_commutative(TempOptInfo *ti) > +{ > + /* Slight preference for non-zero constants second. */ > + return !ti_is_const(ti) ? 0 : ti_const_val(ti) ? 3 : 2; > +} > + > static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2) > { > TCGArg a1 = *p1, a2 = *p2; > int sum = 0; > - sum += arg_is_const(a1); > - sum -= arg_is_const(a2); > + sum += pref_commutative(arg_info(a1)); > + sum -= pref_commutative(arg_info(a2)); > > /* Prefer the constant in second argument, and then the form > op a, a, b, which is better handled on non-RISC hosts. */ > @@ -745,10 +751,10 @@ static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2) > static bool swap_commutative2(TCGArg *p1, TCGArg *p2) > { > int sum = 0; > - sum += arg_is_const(p1[0]); > - sum += arg_is_const(p1[1]); > - sum -= arg_is_const(p2[0]); > - sum -= arg_is_const(p2[1]); > + sum += pref_commutative(arg_info(p1[0])); > + sum += pref_commutative(arg_info(p1[1])); > + sum -= pref_commutative(arg_info(p2[0])); > + sum -= pref_commutative(arg_info(p2[1])); > if (sum > 0) { > TCGArg t; > t = p1[0], p1[0] = p2[0], p2[0] = t; Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
diff --git a/tcg/optimize.c b/tcg/optimize.c index 1b3d0b5b5d..442f5b75e6 100644 --- a/tcg/optimize.c +++ b/tcg/optimize.c @@ -725,12 +725,18 @@ static int do_constant_folding_cond(TCGType type, TCGArg x, #define NO_DEST temp_arg(NULL) +static int pref_commutative(TempOptInfo *ti) +{ + /* Slight preference for non-zero constants second. */ + return !ti_is_const(ti) ? 0 : ti_const_val(ti) ? 3 : 2; +} + static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2) { TCGArg a1 = *p1, a2 = *p2; int sum = 0; - sum += arg_is_const(a1); - sum -= arg_is_const(a2); + sum += pref_commutative(arg_info(a1)); + sum -= pref_commutative(arg_info(a2)); /* Prefer the constant in second argument, and then the form op a, a, b, which is better handled on non-RISC hosts. */ @@ -745,10 +751,10 @@ static bool swap_commutative(TCGArg dest, TCGArg *p1, TCGArg *p2) static bool swap_commutative2(TCGArg *p1, TCGArg *p2) { int sum = 0; - sum += arg_is_const(p1[0]); - sum += arg_is_const(p1[1]); - sum -= arg_is_const(p2[0]); - sum -= arg_is_const(p2[1]); + sum += pref_commutative(arg_info(p1[0])); + sum += pref_commutative(arg_info(p1[1])); + sum -= pref_commutative(arg_info(p2[0])); + sum -= pref_commutative(arg_info(p2[1])); if (sum > 0) { TCGArg t; t = p1[0], p1[0] = p2[0], p2[0] = t;
For most binary operands, two const operands fold. However, the add/sub carry opcodes have a third input. Prefer "reg, zero, const" since many risc hosts have a zero register that can fit a "reg, reg, const" insn format. Signed-off-by: Richard Henderson <richard.henderson@linaro.org> --- tcg/optimize.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-)