From c9349965ce4a1548a0b19dc3cf859a461d525f6b Mon Sep 17 00:00:00 2001 From: Richard Henderson Date: Wed, 20 May 2026 14:51:39 +0200 Subject: [PATCH] tcg: Optimize INDEX_op_mul[us]2 for 0 and 1 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Zero operands produce a zero high and low product. One operands produce a copy of the other operand and a zero or sign extension in the high half. Fold those cases during TCG optimization so wide-multiply idioms used by target translators can collapse before code generation. Signed-off-by: James Hilliard Signed-off-by: Philippe Mathieu-Daudé Signed-off-by: Richard Henderson Message-ID: <20260520125139.13352-3-philmd@linaro.org> --- tcg/optimize.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/tcg/optimize.c b/tcg/optimize.c index ef5eb2cf17..fcdef25bee 100644 --- a/tcg/optimize.c +++ b/tcg/optimize.c @@ -2212,6 +2212,34 @@ static bool fold_multiply2(OptContext *ctx, TCGOp *op) tcg_opt_gen_movi(ctx, op2, rh, h); return true; } + + if (b == 0) { + op2 = opt_insert_before(ctx, op, 0, 2); + tcg_opt_gen_movi(ctx, op2, rl, 0); + tcg_opt_gen_movi(ctx, op, rh, 0); + return true; + } + if (b == 1) { + op2 = opt_insert_before(ctx, op, 0, 2); + tcg_opt_gen_mov(ctx, op2, rl, op->args[2]); + + switch (op->opc) { + case INDEX_op_mulu2: + tcg_opt_gen_movi(ctx, op, rh, 0); + break; + case INDEX_op_muls2: + op->opc = INDEX_op_sar; + op->args[0] = rh; + op->args[1] = rl; + op->args[2] = + arg_new_constant(ctx, tcg_type_size(ctx->type) * 8 - 1); + break; + default: + g_assert_not_reached(); + } + + return true; + } } return finish_folding(ctx, op); }