tcg: improve instruction selection for extract and deposit_z
cover tcg: improve instruction selection for extract and deposit_z
extract and deposit_z are similar operations, only differing in that extract shifts the operand right and deposit_z shifts it left. However, their code generation is currently different.
extract is implemented as either SHL+SHR or SHR+AND, with the latter chosen for "simple" cases where we expect the immediate to be available or a zero extension instruction to be usable. deposit instead uses only AND+SHL, though SHL+SHR would be just as usable.
To get the best of both worlds, introduce tcg_op_imm_match to check whether the processor supports the immediate that is needed for the mask, and if not fall back to two shifts.
Paolo
Paolo Bonzini (2):
tcg: target-dependent lowering of extract to shr/and
tcg: possibly convert deposit_z to shl+shr
include/tcg/tcg.h | 2 ++ tcg/tcg-op.c | 58 +++++++++++++++++++++++++++++++---------------- tcg/tcg.c | 23 +++++++++++++++---- 3 files changed, 59 insertions(+), 24 deletions(-)
Signature · 1 line
2.52.0
PATCH 1/2 tcg: target-dependent lowering of extract to shr/and
Instead of assuming only small immediates are available for AND, consult the backend in order to decide between SHL/SHR and SHR/AND.
--- include/tcg/tcg.h | 2 ++ tcg/tcg-op.c | 30 ++++++++++++++---------------- tcg/tcg.c | 23 +++++++++++++++++++---- 3 files changed, 35 insertions(+), 20 deletions(-)
include/tcg/tcg.h +2 -0
@@ -795,6 +795,8 @@ bool tcg_op_supported(TCGOpcode op, TCGType type, unsigned flags);*/ bool tcg_op_deposit_valid (TCGType type,unsigned ofs,unsigned len); +bool tcg_op_imm_match (TCGOpcode op,TCGType type,tcg_target_ulong imm); +void tcg_gen_call0 (void *func,TCGHelperInfo *,TCGTemp *ret);void tcg_gen_call1 (void *func,TCGHelperInfo *,TCGTemp *ret,TCGTemp *); void tcg_gen_call2(void *func, TCGHelperInfo *, TCGTemp *ret,
tcg/tcg-op.c +14 -16
@@ -995,18 +995,17 @@ void tcg_gen_extract_i32(TCGv_i32 ret, TCGv_i32 arg,return ; } -/* ??? Ideally we'd know what values are available for immediate AND. -Assume that 8 bits are available, plus the special case of 16, -so that we get ext8u, ext16u. */ -switch (len) { - case 1 ... 8: case 16: +/* +* Use TCG_TARGET_extract_valid to check for 8- and 16-bit extension +* opcodes, which tcg_gen_andi_i32 can produce. +*/ +if (TCG_TARGET_extract_valid (TCG_TYPE_I32 ,0 , len) || +tcg_op_imm_match (INDEX_op_and,TCG_TYPE_I32 , (1u << len) -1 )) {tcg_gen_shri_i32 (ret, arg, ofs);tcg_gen_andi_i32 (ret, ret, (1u << len) -1 ); -break ; -default : + }else {tcg_gen_shli_i32 (ret, arg,32 - len - ofs);tcg_gen_shri_i32 (ret, ret,32 - len); -break ; } }
@@ -2690,18 +2689,17 @@ void tcg_gen_extract_i64(TCGv_i64 ret, TCGv_i64 arg,return ; } -/* ??? Ideally we'd know what values are available for immediate AND. -Assume that 8 bits are available, plus the special cases of 16 and 32, -so that we get ext8u, ext16u, and ext32u. */ -switch (len) { - case 1 ... 8: case 16: case 32: +/* +* Use TCG_TARGET_extract_valid to check for 8-, 16- and 32-bit extension +* opcodes, which tcg_gen_andi_i64 can produce. +*/ +if (TCG_TARGET_extract_valid (TCG_TYPE_I64 ,0 , len) || +tcg_op_imm_match (INDEX_op_and,TCG_TYPE_I64 , (1ull << len) -1 )) {tcg_gen_shri_i64 (ret, arg, ofs);tcg_gen_andi_i64 (ret, ret, (1ull << len) -1 ); -break ; -default : + }else {tcg_gen_shli_i64 (ret, arg,64 - len - ofs);tcg_gen_shri_i64 (ret, ret,64 - len); -break ; } }
tcg/tcg.c +19 -4
@@ -3509,11 +3509,8 @@ static void process_constraint_sets(void) } } -static const TCGArgConstraint *opcode_args_ct (const TCGOp *op) +static const TCGArgConstraint *op_args_ct (TCGOpcode opc,TCGType type,unsigned flags) { -TCGOpcode opc = op->opc; -TCGType type =TCGOP_TYPE (op); -unsigned flags =TCGOP_FLAGS (op);const TCGOpDef *def = &tcg_op_defs[opc];const TCGOutOp *outop = all_outop[opc];TCGConstraintSetIndex con_set;
@@ -3540,6 +3537,24 @@ static const TCGArgConstraint *opcode_args_ct(const TCGOp *op)return all_cts[con_set]; } +static const TCGArgConstraint *opcode_args_ct (const TCGOp *op) +{ +return op_args_ct (op->opc,TCGOP_TYPE (op),TCGOP_FLAGS (op)); +} + +/* Return true if the backend can efficiently handle IMM as an immediate +for OP. */ +bool tcg_op_imm_match (TCGOpcode opc,TCGType type,tcg_target_ulong imm) +{ +const TCGOpDef *const def = &tcg_op_defs[opc]; +const TCGArgConstraint *args_ct =op_args_ct (opc, type,0 ); + +tcg_debug_assert (def->nb_iargs ==2 ); +return tcg_target_const_match ( + imm, args_ct[def->nb_oargs +2 ].ct, + type,0 ,0 ); +} +static void remove_label_use (TCGOp *op,int idx) {TCGLabel *label =arg_label (op->args[idx]);
Signature · 1 line
2.52.0
Re: [PATCH 1/2] tcg: target-dependent lowering of extract to shr/and
Quoted 32 lines · Paolo Bonzini
tcg/tcg.c +12
-4@@ -3509,11 +3509,8 @@ static void process_constraint_sets(void) } } -static const TCGArgConstraint *opcode_args_ct (const TCGOp *op) +static const TCGArgConstraint *op_args_ct (TCGOpcode opc,TCGType type,unsigned flags) { -TCGOpcode opc = op->opc; -TCGType type =TCGOP_TYPE (op); -unsigned flags =TCGOP_FLAGS (op);const TCGOpDef *def = &tcg_op_defs[opc];const TCGOutOp *outop = all_outop[opc];TCGConstraintSetIndex con_set;@@ -3540,6 +3537,24 @@ static const TCGArgConstraint *opcode_args_ct(const TCGOp *op)return all_cts[con_set]; } +static const TCGArgConstraint *opcode_args_ct (const TCGOp *op) +{ +return op_args_ct (op->opc,TCGOP_TYPE (op),TCGOP_FLAGS (op)); +} + +/* Return true if the backend can efficiently handle IMM as an immediate +for OP. */ +bool tcg_op_imm_match (TCGOpcode opc,TCGType type,tcg_target_ulong imm) +{ +const TCGOpDef *const def = &tcg_op_defs[opc]; +const TCGArgConstraint *args_ct =op_args_ct (opc, type,0 );
tcg/tcg.c static const TCGArgConstraint *opcode_args_ct(const TCGOp *op) · line 3560
Quoted 10 lines
tcg/tcg.c +7
-0+ +tcg_debug_assert (def->nb_iargs ==2 ); +return tcg_target_const_match ( + imm, args_ct[def->nb_oargs +2 ].ct, + type,0 ,0 ); +} +static void remove_label_use (TCGOp *op,int idx) {TCGLabel *label =arg_label (op->args[idx]);
Functionally this looks good, but there are a couple of coding standards issues raised by scripts/checkpatch.pl: The definition of op_args_ct is over 80 characters, and your block comment for tcg_op_imm_match should use /* and */ on separate lines. If those are fixed I'm happy to approve it.
Jim
Re: [PATCH 1/2] tcg: target-dependent lowering of extract to shr/and
Quoted 46 lines · Jim MacArthur
On 1/15/26 13:54, Paolo Bonzini wrote:
tcg/tcg.c +19
-4@@ -3509,11 +3509,8 @@ static void process_constraint_sets(void) } } -static const TCGArgConstraint *opcode_args_ct (const TCGOp *op) +static const TCGArgConstraint *op_args_ct (TCGOpcode opc,TCGType type,unsigned flags) { -TCGOpcode opc = op->opc; -TCGType type =TCGOP_TYPE (op); -unsigned flags =TCGOP_FLAGS (op);const TCGOpDef *def = &tcg_op_defs[opc];const TCGOutOp *outop = all_outop[opc];TCGConstraintSetIndex con_set;@@ -3540,6 +3537,24 @@ static const TCGArgConstraint *opcode_args_ct(const TCGOp *op)return all_cts[con_set]; } +static const TCGArgConstraint *opcode_args_ct (const TCGOp *op) +{ +return op_args_ct (op->opc,TCGOP_TYPE (op),TCGOP_FLAGS (op)); +} + +/* Return true if the backend can efficiently handle IMM as an immediate +for OP. */ +bool tcg_op_imm_match (TCGOpcode opc,TCGType type,tcg_target_ulong imm) +{ +const TCGOpDef *const def = &tcg_op_defs[opc]; +const TCGArgConstraint *args_ct =op_args_ct (opc, type,0 ); + +tcg_debug_assert (def->nb_iargs ==2 ); +return tcg_target_const_match ( + imm, args_ct[def->nb_oargs +2 ].ct, + type,0 ,0 ); +} +static void remove_label_use (TCGOp *op,int idx) {TCGLabel *label =arg_label (op->args[idx]);
Quoted 1 line
Functionally this looks good, but there are a couple of coding standards issues raised by
scripts/checkpatch.pl: The definition ofop_args_ctis over 80 characters, and your block comment fortcg_op_imm_matchshould use /* and */ on separate lines. If those are fixed I'm happy to approve it.
tcg/tcg.c has several comments with that style, especially at the head of functions (see temp_sync, temp_free_or_dead). I'll fix the 80 characters.
Paolo
PATCH 2/2 tcg: possibly convert deposit_z to shl+shr
In reply to Paolo Bonziniextract and deposit_z are similar operations, only differing in that extract shifts the operand right and deposit_z shifts it left. Like extract, it is possible to implement deposit_z as either AND+SHL or SHL+SHR. Use tcg_op_imm_match to check whether the processor supports the immediate that is needed for the mask, and if not fall back to two shifts.
--- tcg/tcg-op.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-)
tcg/tcg-op.c +24 -4
@@ -960,8 +960,18 @@ void tcg_gen_deposit_z_i32(TCGv_i32 ret, TCGv_i32 arg,tcg_gen_extract_i32 (ret, ret,0 , ofs + len);return ; } -tcg_gen_andi_i32 (ret, arg, (1u << len) -1 ); -tcg_gen_shli_i32 (ret, ret, ofs); +/* +* Use TCG_TARGET_extract_valid to check for 8- and 16-bit extension +* opcodes, which tcg_gen_andi_i32 can produce. +*/ +if (TCG_TARGET_extract_valid (TCG_TYPE_I32 ,0 , len) || +tcg_op_imm_match (INDEX_op_and,TCG_TYPE_I32 , (1u << len) -1 )) { +tcg_gen_andi_i32 (ret, arg, (1u << len) -1 ); +tcg_gen_shli_i32 (ret, ret, ofs); + }else { +tcg_gen_shli_i32 (ret, arg,32 - len); +tcg_gen_shri_i32 (ret, ret,32 - len - ofs); + } } }
@@ -2628,8 +2638,18 @@ void tcg_gen_deposit_z_i64(TCGv_i64 ret, TCGv_i64 arg,tcg_gen_extract_i64 (ret, ret,0 , ofs + len);return ; } -tcg_gen_andi_i64 (ret, arg, (1ull << len) -1 ); -tcg_gen_shli_i64 (ret, ret, ofs); +/* +* Use TCG_TARGET_extract_valid to check for 8-, 16- and 32-bit extension +* opcodes, which tcg_gen_andi_i64 can produce. +*/ +if (TCG_TARGET_extract_valid (TCG_TYPE_I64 ,0 , len) || +tcg_op_imm_match (INDEX_op_and,TCG_TYPE_I64 , (1ull << len) -1 )) { +tcg_gen_andi_i64 (ret, arg, (1ull << len) -1 ); +tcg_gen_shli_i64 (ret, ret, ofs); + }else { +tcg_gen_shli_i64 (ret, arg,64 - len); +tcg_gen_shri_i64 (ret, ret,64 - len - ofs); + } } }
Signature · 1 line
2.52.0
Re: [PATCH 2/2] tcg: possibly convert deposit_z to shl+shr
Quoted 12 lines · Paolo Bonzini
tcg/tcg-op.c +5
-2@@ -2628,8 +2638,18 @@ void tcg_gen_deposit_z_i64(TCGv_i64 ret, TCGv_i64 arg,tcg_gen_extract_i64 (ret, ret,0 , ofs + len);return ; } -tcg_gen_andi_i64 (ret, arg, (1ull << len) -1 ); -tcg_gen_shli_i64 (ret, ret, ofs); +/* +* Use TCG_TARGET_extract_valid to check for 8-, 16- and 32-bit extension +* opcodes, which tcg_gen_andi_i64 can produce. +*/ + if (TCG_TARGET_extract_valid(TCG_TYPE_I64, 0, len) ||
tcg/tcg-op.c void tcg_gen_deposit_z_i64(TCGv_i64 ret, TCGv_i64 arg, · line 2655
Quoted 10 lines
tcg/tcg-op.c +7
-0+ tcg_op_imm_match(INDEX_op_and, TCG_TYPE_I64, (1ull << len) - 1)) { + tcg_gen_andi_i64(ret, arg, (1ull << len) - 1); + tcg_gen_shli_i64(ret, ret, ofs); + } else { +tcg_gen_shli_i64 (ret, arg,64 - len); +tcg_gen_shri_i64 (ret, ret,64 - len - ofs); + } } }
Also just a couple of coding standards issues - "Use TCG_TARGET_extract_valid" comment is over 80 characters and you have a tab at the start of the call to tcp_op_imm_match. Otherwise, looks good.
Jim
Quoted 4 lines · Paolo Bonzini
extract and
deposit_zare similar operations, only differing in that extract shifts the operand right anddeposit_zshifts it left. However, their code generation is currently different.
Quoted 8 lines
extract is implemented as either SHL+SHR or SHR+AND, with the latter chosen for "simple" cases where we expect the immediate to be available or a zero extension instruction to be usable. deposit instead uses only AND+SHL, though SHL+SHR would be just as usable.
To get the best of both worlds, introduce
tcg_op_imm_matchto check whether the processor supports the immediate that is needed for the mask, and if not fall back to two shifts.
Hmm.
I have a patch set that's been stagnant for a while, now currently waiting on the removal of 32-bit hosts, which delays expansion of extract and deposit until optimize, when we can see then input constants.
There's probably some overlap here.
r~