tcg/optimize: Optimize cmp_vec and cmpsel_vec

Place immediate values second in the comparison.
Place destination matches first in the true/false values.
All of this mirrors what we do for integer setcond and movcond.

Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
This commit is contained in:
Richard Henderson 2024-09-06 12:22:41 -07:00
parent 141125e08c
commit 1f106544fd

View File

@ -2422,6 +2422,36 @@ static bool fold_setcond2(OptContext *ctx, TCGOp *op)
return tcg_opt_gen_movi(ctx, op, op->args[0], i); return tcg_opt_gen_movi(ctx, op, op->args[0], i);
} }
static bool fold_cmp_vec(OptContext *ctx, TCGOp *op)
{
/* Canonicalize the comparison to put immediate second. */
if (swap_commutative(NO_DEST, &op->args[1], &op->args[2])) {
op->args[3] = tcg_swap_cond(op->args[3]);
}
return false;
}
static bool fold_cmpsel_vec(OptContext *ctx, TCGOp *op)
{
/* If true and false values are the same, eliminate the cmp. */
if (args_are_copies(op->args[3], op->args[4])) {
return tcg_opt_gen_mov(ctx, op, op->args[0], op->args[3]);
}
/* Canonicalize the comparison to put immediate second. */
if (swap_commutative(NO_DEST, &op->args[1], &op->args[2])) {
op->args[5] = tcg_swap_cond(op->args[5]);
}
/*
* Canonicalize the "false" input reg to match the destination,
* so that the tcg backend can implement "move if true".
*/
if (swap_commutative(op->args[0], &op->args[4], &op->args[3])) {
op->args[5] = tcg_invert_cond(op->args[5]);
}
return false;
}
static bool fold_sextract(OptContext *ctx, TCGOp *op) static bool fold_sextract(OptContext *ctx, TCGOp *op)
{ {
uint64_t z_mask, s_mask, s_mask_old; uint64_t z_mask, s_mask, s_mask_old;
@ -2928,6 +2958,12 @@ void tcg_optimize(TCGContext *s)
case INDEX_op_setcond2_i32: case INDEX_op_setcond2_i32:
done = fold_setcond2(&ctx, op); done = fold_setcond2(&ctx, op);
break; break;
case INDEX_op_cmp_vec:
done = fold_cmp_vec(&ctx, op);
break;
case INDEX_op_cmpsel_vec:
done = fold_cmpsel_vec(&ctx, op);
break;
CASE_OP_32_64(sextract): CASE_OP_32_64(sextract):
done = fold_sextract(&ctx, op); done = fold_sextract(&ctx, op);
break; break;