Skip to content

Commit

Permalink
tcg: Fold deposit with zero to and
Browse files Browse the repository at this point in the history
Inserting a zero into a value, or inserting a value
into zero at offset 0 may be implemented with AND.

Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
  • Loading branch information
rth7680 committed Aug 24, 2023
1 parent 36df88c commit 8f7a840
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions tcg/optimize.c
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,8 @@ static bool fold_ctpop(OptContext *ctx, TCGOp *op)

static bool fold_deposit(OptContext *ctx, TCGOp *op)
{
TCGOpcode and_opc;

if (arg_is_const(op->args[1]) && arg_is_const(op->args[2])) {
uint64_t t1 = arg_info(op->args[1])->val;
uint64_t t2 = arg_info(op->args[2])->val;
Expand All @@ -1287,6 +1289,41 @@ static bool fold_deposit(OptContext *ctx, TCGOp *op)
return tcg_opt_gen_movi(ctx, op, op->args[0], t1);
}

switch (ctx->type) {
case TCG_TYPE_I32:
and_opc = INDEX_op_and_i32;
break;
case TCG_TYPE_I64:
and_opc = INDEX_op_and_i64;
break;
default:
g_assert_not_reached();
}

/* Inserting a value into zero at offset 0. */
if (arg_is_const(op->args[1])
&& arg_info(op->args[1])->val == 0
&& op->args[3] == 0) {
uint64_t mask = MAKE_64BIT_MASK(0, op->args[4]);

op->opc = and_opc;
op->args[1] = op->args[2];
op->args[2] = temp_arg(tcg_constant_internal(ctx->type, mask));
ctx->z_mask = mask & arg_info(op->args[1])->z_mask;
return false;
}

/* Inserting zero into a value. */
if (arg_is_const(op->args[2])
&& arg_info(op->args[2])->val == 0) {
uint64_t mask = deposit64(-1, op->args[3], op->args[4], 0);

op->opc = and_opc;
op->args[2] = temp_arg(tcg_constant_internal(ctx->type, mask));
ctx->z_mask = mask & arg_info(op->args[1])->z_mask;
return false;
}

ctx->z_mask = deposit64(arg_info(op->args[1])->z_mask,
op->args[3], op->args[4],
arg_info(op->args[2])->z_mask);
Expand Down

0 comments on commit 8f7a840

Please sign in to comment.