tcg: Optimize INDEX_op_mul[us]2 for 0 and 1

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 <james.hilliard1@gmail.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
Message-ID: <20260520125139.13352-3-philmd@linaro.org>
This commit is contained in:
Richard Henderson
2026-05-20 14:51:39 +02:00
parent f7c62771e6
commit c9349965ce

View File

@@ -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);
}