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>
Richard Henderson committed
May 20, 2026 at 14:51 UTC
c9349965ce4a1548a0b19dc3cf859a461d525f6b
1 file changed
+28
tcg/optimize.c
+28
@@ -2212,6 +2212,34 @@ static bool fold_multiply2(OptContext *ctx, TCGOp *op)
2212
tcg_opt_gen_movi(ctx, op2, rh, h);
2213
return true;
2214
}
2215
+
2216
+ if (b == 0) {
2217
+ op2 = opt_insert_before(ctx, op, 0, 2);
2218
+ tcg_opt_gen_movi(ctx, op2, rl, 0);
2219
+ tcg_opt_gen_movi(ctx, op, rh, 0);
2220
+ return true;
2221
+ }
2222
+ if (b == 1) {
2223
+ op2 = opt_insert_before(ctx, op, 0, 2);
2224
+ tcg_opt_gen_mov(ctx, op2, rl, op->args[2]);
2225
+
2226
+ switch (op->opc) {
2227
+ case INDEX_op_mulu2:
2228
+ tcg_opt_gen_movi(ctx, op, rh, 0);
2229
+ break;
2230
+ case INDEX_op_muls2:
2231
+ op->opc = INDEX_op_sar;
2232
+ op->args[0] = rh;
2233
+ op->args[1] = rl;
2234
+ op->args[2] =
2235
+ arg_new_constant(ctx, tcg_type_size(ctx->type) * 8 - 1);
2236
+ break;
2237
+ default:
2238
+ g_assert_not_reached();
2239
+ }
2240
+
2241
+ return true;
2242
+ }
2243
}
2244
return finish_folding(ctx, op);
2245
}