tcg/optimize: fixes and improvements
v3 cover tcg/optimize: fixes and improvements
This patchset first fixes known-zero bits optimization so that it works in more than a few cases, and does some further optimizations for 32-bit ops and unsigned loads.
v2 -> v3:
- added support for the new INDEX_op_qemu_ld_{i32,i64} opcodes in patch 4
v1 -> v2:
- swapped patches 1 & 2
- Cc:ed qemu-stable for patch 1
- improved description of patch 2
Aurelien Jarno (4):
tcg/optimize: fix known-zero bits for right shift ops tcg/optimize: fix known-zero bits optimization tcg/optimize: improve known-zero bits for 32-bit ops tcg/optimize: add known-zero bits compute for load ops
tcg/optimize.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 63 insertions(+), 5 deletions(-)
Signature · 1 line
1.7.10.4
PATCH v3 1/4 tcg/optimize: fix known-zero bits for right shift ops
32-bit versions of sar and shr ops should not propagate known-zero bits from the unused 32 high bits. For sar it could even lead to wrong code being generated.
--- tcg/optimize.c | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-)
tcg/optimize.c +17 -4
@@ -726,16 +726,29 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr, mask = temps[args[1 ]].mask & mask;break ; - CASE_OP_32_64(sar): +case INDEX_op_sar_i32: +if (temps[args[2 ]].state ==TCG_TEMP_CONST ) { + mask = ((int32_t )temps[args[1 ]].mask + >> temps[args[2 ]].val); + } +break ; +case INDEX_op_sar_i64:if (temps[args[2 ]].state ==TCG_TEMP_CONST ) { - mask = ((tcg_target_long )temps[args[1 ]].mask + mask = ((int64_t )temps[args[1 ]].mask >> temps[args[2 ]].val); }break ; - CASE_OP_32_64(shr): +case INDEX_op_shr_i32:if (temps[args[2 ]].state ==TCG_TEMP_CONST ) { - mask = temps[args[1 ]].mask >> temps[args[2 ]].val; + mask = ((uint32_t )temps[args[1 ]].mask + >> temps[args[2 ]].val); + } +break ; +case INDEX_op_shr_i64: +if (temps[args[2 ]].state ==TCG_TEMP_CONST ) { + mask = ((uint64_t )temps[args[1 ]].mask + >> temps[args[2 ]].val); }break ;
Signature · 1 line
1.7.10.4
PATCH v3 2/4 tcg/optimize: fix known-zero bits optimization
In reply to Aurelien JarnoKnown-zero bits optimization is a great idea that helps to generate more optimized code. However the current implementation only works in very few cases as the computed mask is not saved.
Fix this to make it really working.
--- tcg/optimize.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
tcg/optimize.c +7 -1
@@ -691,7 +691,8 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,break ; } -/* Simplify using known-zero bits */ +/* Simplify using known-zero bits. Currently only ops with a single +output argument is supported. */ mask =-1 ; affected =-1 ;switch (op) {
@@ -1153,6 +1154,11 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr, }else {for (i =0 ; i < def->nb_oargs; i++) {reset_temp (args[i]); +/* Save the corresponding known-zero bits mask for the +first output argument (only one supported so far). */ +if (i ==0 ) { + temps[args[i]].mask = mask; + } } }for (i =0 ; i < def->nb_args; i++) {
Signature · 1 line
1.7.10.4
PATCH v3 3/4 tcg/optimize: improve known-zero bits for 32-bit ops
In reply to Aurelien JarnoThe shl_i32 op might set some bits of the unused 32 high bits of the mask. Fix that by clearing the unused 32 high bits for all 32-bit ops except load/store which operate on tl values.
--- tcg/optimize.c | 6 ++++++ 1 file changed, 6 insertions(+)
tcg/optimize.c +6 -0
@@ -787,6 +787,12 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,break ; } +/* 32-bit ops (non 64-bit ops and non load/store ops) generate 32-bit +results */ +if (!(tcg_op_defs[op].flags & (TCG_OPF_CALL_CLOBBER |TCG_OPF_64BIT ))) { + mask &=0xffffffffu ; + } +if (mask ==0 ) {assert (def->nb_oargs ==1 ); s->gen_opc_buf[op_index] =op_to_movi (op);
Signature · 1 line
1.7.10.4
PATCH v3 4/4 tcg/optimize: add known-zero bits compute for load ops
In reply to Aurelien Jarno--- tcg/optimize.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+)
tcg/optimize.c +33 -0
@@ -783,6 +783,39 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr, mask = temps[args[3 ]].mask | temps[args[4 ]].mask;break ; + CASE_OP_32_64(ld8u): +case INDEX_op_qemu_ld8u: + mask =0xff ; +break ; + CASE_OP_32_64(ld16u): +case INDEX_op_qemu_ld16u: + mask =0xffff ; +break ; +case INDEX_op_ld32u_i64: +case INDEX_op_qemu_ld32u: + mask =0xffffffffu ; +break ; + +case INDEX_op_qemu_ld_i32: +case INDEX_op_qemu_ld_i64: + { +const TCGMemOp opc = args[def->nb_oargs + def->nb_iargs]; +if (!(opc &MO_SIGN )) { +switch (opc &MO_SIZE ) { +case MO_8 : + mask =0xff ; +break ; +case MO_16 : + mask =0xffff ; +break ; +case MO_32 : + mask =0xffffffffu ; +break ; + } + } + } +break ; +default :break ; }
Signature · 1 line
1.7.10.4
Re: [Qemu-devel] [PATCH v3 4/4] tcg/optimize: add known-zero bits compute for load ops
Quoted 6 lines · Aurelien Jarno
Cc Richard Henderson <[email protected]>Cc Paolo Bonzini <[email protected]>Signed-off-by Aurelien Jarno <[email protected]>--- tcg/optimize.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+)
r~