tcg: movcond
v2 cover tcg: movcond
The patch set is available at
git://repo.or.cz/qemu/rth.git tcg-movcond
Changes v1->v2:
Patch 1 drops the spurrious return.
Patch 4 incorporates the fix Aurelian posted.
Patch 5 drops the non-movcond commutative optimization that is
present in Aurelian's tcg optimization patch set.
Patches 6 & 7 are new, incrementally improving 32-bit code generation.
Rebased vs c26032b2c91721245bfec542d94f37a0238e986e:
target-xtensa: don't emit extra tcg_gen_goto_tb
r~
Richard Henderson (7):
tcg: Introduce movcond
target-alpha: Use movcond
tcg-i386: Implement movcond
tcg: Optimize movcond for constant comparisons
tcg: Optimize two-address commutative operations
tcg: Streamline movcond_i64 using 32-bit arithmetic
tcg: Streamline movcond_i64 using movcond_i32
target-alpha/translate.c | 102 ++++++++++++++++++++++------------------------- tcg/README | 6 +++ tcg/arm/tcg-target.h | 1 + tcg/hppa/tcg-target.h | 1 + tcg/i386/tcg-target.c | 29 ++++++++++++++ tcg/i386/tcg-target.h | 7 ++++ tcg/ia64/tcg-target.h | 2 + tcg/mips/tcg-target.h | 1 + tcg/optimize.c | 53 ++++++++++++++++++++++++ tcg/ppc/tcg-target.h | 1 + tcg/ppc64/tcg-target.h | 2 + tcg/s390/tcg-target.h | 2 + tcg/sparc/tcg-target.h | 2 + tcg/tcg-op.h | 68 +++++++++++++++++++++++++++++++ tcg/tcg-opc.h | 2 + tcg/tcg.c | 11 +++-- tcg/tcg.h | 1 + tcg/tci/tcg-target.h | 2 + 18 files changed, 233 insertions(+), 60 deletions(-)
Signature · 1 line
1.7.11.4
PATCH v2 1/7 tcg: Introduce movcond
Implemented with setcond if the target does not provide the optional opcode.
--- tcg/README | 6 ++++++ tcg/arm/tcg-target.h | 1 + tcg/hppa/tcg-target.h | 1 + tcg/i386/tcg-target.h | 2 ++ tcg/ia64/tcg-target.h | 2 ++ tcg/mips/tcg-target.h | 1 + tcg/ppc/tcg-target.h | 1 + tcg/ppc64/tcg-target.h | 2 ++ tcg/s390/tcg-target.h | 2 ++ tcg/sparc/tcg-target.h | 2 ++ tcg/tcg-op.h | 40 ++++++++++++++++++++++++++++++++++++++++ tcg/tcg-opc.h | 2 ++ tcg/tcg.c | 11 +++++------ tcg/tcg.h | 1 + tcg/tci/tcg-target.h | 2 ++ 15 files changed, 70 insertions(+), 6 deletions(-)
tcg/README +6 -0
@@ -307,6 +307,12 @@ dest = (t1 cond t2) Set DEST to 1 if (T1 cond T2) is true, otherwise set to 0. +* movcond_i32/i64 cond, dest, c1, c2, v1, v2 + +dest = (c1 cond c2 ? v1 : v2) + +Set DEST to V1 if (C1 cond C2) is true, otherwise set to V2. + ********* Type conversions * ext_i32_i64 t0, t1
tcg/arm/tcg-target.h +1 -0
@@ -73,6 +73,7 @@ typedef enum {#define TCG_TARGET_HAS_nand_i32 0#define TCG_TARGET_HAS_nor_i32 0#define TCG_TARGET_HAS_deposit_i32 0 +#define TCG_TARGET_HAS_movcond_i32 0#define TCG_TARGET_HAS_GUEST_BASE
tcg/hppa/tcg-target.h +1 -0
@@ -96,6 +96,7 @@ typedef enum {#define TCG_TARGET_HAS_nand_i32 0#define TCG_TARGET_HAS_nor_i32 0#define TCG_TARGET_HAS_deposit_i32 1 +#define TCG_TARGET_HAS_movcond_i32 0/* optional instructions automatically implemented */ #define TCG_TARGET_HAS_neg_i32 0/* sub rd, 0, rs */
tcg/i386/tcg-target.h +2 -0
@@ -86,6 +86,7 @@ typedef enum {#define TCG_TARGET_HAS_nand_i32 0#define TCG_TARGET_HAS_nor_i32 0#define TCG_TARGET_HAS_deposit_i32 1 +#define TCG_TARGET_HAS_movcond_i32 0#if TCG_TARGET_REG_BITS ==64 #define TCG_TARGET_HAS_div2_i64 1
@@ -107,6 +108,7 @@ typedef enum {#define TCG_TARGET_HAS_nand_i64 0#define TCG_TARGET_HAS_nor_i64 0#define TCG_TARGET_HAS_deposit_i64 1 +#define TCG_TARGET_HAS_movcond_i64 0#endif #define TCG_TARGET_deposit_i32_valid (ofs, len) \
tcg/ia64/tcg-target.h +2 -0
@@ -133,6 +133,8 @@ typedef enum {#define TCG_TARGET_HAS_rot_i64 1#define TCG_TARGET_HAS_deposit_i32 0#define TCG_TARGET_HAS_deposit_i64 0 +#define TCG_TARGET_HAS_movcond_i32 0 +#define TCG_TARGET_HAS_movcond_i64 0/* optional instructions automatically implemented */ #define TCG_TARGET_HAS_neg_i32 0/* sub r1, r0, r3 */
tcg/mips/tcg-target.h +1 -0
@@ -90,6 +90,7 @@ typedef enum {#define TCG_TARGET_HAS_eqv_i32 0#define TCG_TARGET_HAS_nand_i32 0#define TCG_TARGET_HAS_deposit_i32 0 +#define TCG_TARGET_HAS_movcond_i32 0/* optional instructions automatically implemented */ #define TCG_TARGET_HAS_neg_i32 0/* sub rd, zero, rt */
tcg/ppc/tcg-target.h +1 -0
@@ -92,6 +92,7 @@ typedef enum {#define TCG_TARGET_HAS_nand_i32 1#define TCG_TARGET_HAS_nor_i32 1#define TCG_TARGET_HAS_deposit_i32 1 +#define TCG_TARGET_HAS_movcond_i32 0#define TCG_AREG0 TCG_REG_R27
tcg/ppc64/tcg-target.h +2 -0
@@ -83,6 +83,7 @@ typedef enum {#define TCG_TARGET_HAS_nand_i32 0#define TCG_TARGET_HAS_nor_i32 0#define TCG_TARGET_HAS_deposit_i32 0 +#define TCG_TARGET_HAS_movcond_i32 0#define TCG_TARGET_HAS_div_i64 1#define TCG_TARGET_HAS_rot_i64 0
@@ -103,6 +104,7 @@ typedef enum {#define TCG_TARGET_HAS_nand_i64 0#define TCG_TARGET_HAS_nor_i64 0#define TCG_TARGET_HAS_deposit_i64 0 +#define TCG_TARGET_HAS_movcond_i64 0#define TCG_AREG0 TCG_REG_R27
tcg/s390/tcg-target.h +2 -0
@@ -63,6 +63,7 @@ typedef enum TCGReg {#define TCG_TARGET_HAS_nand_i32 0#define TCG_TARGET_HAS_nor_i32 0#define TCG_TARGET_HAS_deposit_i32 0 +#define TCG_TARGET_HAS_movcond_i32 0#if TCG_TARGET_REG_BITS ==64 #define TCG_TARGET_HAS_div2_i64 1
@@ -84,6 +85,7 @@ typedef enum TCGReg {#define TCG_TARGET_HAS_nand_i64 0#define TCG_TARGET_HAS_nor_i64 0#define TCG_TARGET_HAS_deposit_i64 0 +#define TCG_TARGET_HAS_movcond_i64 0#endif #define TCG_TARGET_HAS_GUEST_BASE
tcg/sparc/tcg-target.h +2 -0
@@ -102,6 +102,7 @@ typedef enum {#define TCG_TARGET_HAS_nand_i32 0#define TCG_TARGET_HAS_nor_i32 0#define TCG_TARGET_HAS_deposit_i32 0 +#define TCG_TARGET_HAS_movcond_i32 0#if TCG_TARGET_REG_BITS ==64 #define TCG_TARGET_HAS_div_i64 1
@@ -123,6 +124,7 @@ typedef enum {#define TCG_TARGET_HAS_nand_i64 0#define TCG_TARGET_HAS_nor_i64 0#define TCG_TARGET_HAS_deposit_i64 0 +#define TCG_TARGET_HAS_movcond_i64 0#endif #ifdef CONFIG_SOLARIS
tcg/tcg-op.h +40 -0
@@ -2118,6 +2118,44 @@ static inline void tcg_gen_deposit_i64(TCGv_i64 ret, TCGv_i64 arg1,tcg_temp_free_i64 (t1); } +static inline void tcg_gen_movcond_i32 (TCGCond cond,TCGv_i32 ret, +TCGv_i32 c1,TCGv_i32 c2, +TCGv_i32 v1,TCGv_i32 v2) +{ +if (TCG_TARGET_HAS_movcond_i32) { +tcg_gen_op6i_i32 (INDEX_op_movcond_i32, ret, c1, c2, v1, v2, cond); + }else { +TCGv_i32 t0 =tcg_temp_new_i32 (); +TCGv_i32 t1 =tcg_temp_new_i32 (); +tcg_gen_setcond_i32 (cond, t0, c1, c2); +tcg_gen_neg_i32 (t0, t0); +tcg_gen_and_i32 (t1, v1, t0); +tcg_gen_andc_i32 (ret, v2, t0); +tcg_gen_or_i32 (ret, ret, t1); +tcg_temp_free_i32 (t0); +tcg_temp_free_i32 (t1); + } +} + +static inline void tcg_gen_movcond_i64 (TCGCond cond,TCGv_i64 ret, +TCGv_i64 c1,TCGv_i64 c2, +TCGv_i64 v1,TCGv_i64 v2) +{ +if (TCG_TARGET_HAS_movcond_i64) { +tcg_gen_op6i_i64 (INDEX_op_movcond_i64, ret, c1, c2, v1, v2, cond); + }else { +TCGv_i64 t0 =tcg_temp_new_i64 (); +TCGv_i64 t1 =tcg_temp_new_i64 (); +tcg_gen_setcond_i64 (cond, t0, c1, c2); +tcg_gen_neg_i64 (t0, t0); +tcg_gen_and_i64 (t1, v1, t0); +tcg_gen_andc_i64 (ret, v2, t0); +tcg_gen_or_i64 (ret, ret, t1); +tcg_temp_free_i64 (t0); +tcg_temp_free_i64 (t1); + } +} +/***************************************/ /* QEMU specific operations. Their type depend on the QEMU CPU type. */
@@ -2434,6 +2472,7 @@ static inline void tcg_gen_qemu_st64(TCGv_i64 arg, TCGv addr, int mem_index)#define tcg_gen_deposit_tl tcg_gen_deposit_i64#define tcg_const_tl tcg_const_i64#define tcg_const_local_tl tcg_const_local_i64 +#define tcg_gen_movcond_tl tcg_gen_movcond_i64#else #define tcg_gen_movi_tl tcg_gen_movi_i32#define tcg_gen_mov_tl tcg_gen_mov_i32
@@ -2505,6 +2544,7 @@ static inline void tcg_gen_qemu_st64(TCGv_i64 arg, TCGv addr, int mem_index)#define tcg_gen_deposit_tl tcg_gen_deposit_i32#define tcg_const_tl tcg_const_i32#define tcg_const_local_tl tcg_const_local_i32 +#define tcg_gen_movcond_tl tcg_gen_movcond_i32#endif #if TCG_TARGET_REG_BITS ==32
tcg/tcg-opc.h +2 -0
@@ -51,6 +51,7 @@ DEF(br, 0, 0, 1, TCG_OPF_BB_END | TCG_OPF_SIDE_EFFECTS) DEF(mov_i32, 1, 1, 0, 0) DEF(movi_i32, 1, 0, 1, 0) DEF(setcond_i32, 1, 2, 1, 0) +DEF (movcond_i32,1 ,4 ,1 ,IMPL (TCG_TARGET_HAS_movcond_i32)) /* load/store */ DEF(ld8u_i32, 1, 1, 1, 0)DEF (ld8s_i32,1 ,1 ,1 ,0 )
@@ -107,6 +108,7 @@ DEF(nor_i32, 1, 2, 0, IMPL(TCG_TARGET_HAS_nor_i32)) DEF(mov_i64, 1, 1, 0, IMPL64) DEF(movi_i64, 1, 0, 1, IMPL64) DEF(setcond_i64, 1, 2, 1, IMPL64) +DEF (movcond_i64,1 ,4 ,1 ,IMPL64 |IMPL (TCG_TARGET_HAS_movcond_i64)) /* load/store */ DEF(ld8u_i64, 1, 1, 1, IMPL64)DEF (ld8s_i64,1 ,1 ,1 ,IMPL64 )
tcg/tcg.c +5 -6
@@ -991,16 +991,15 @@ void tcg_dump_ops(TCGContext *s) }switch (c) {case INDEX_op_brcond_i32: -#if TCG_TARGET_REG_BITS ==32 -case INDEX_op_brcond2_i32: -#elif TCG_TARGET_REG_BITS ==64 -case INDEX_op_brcond_i64: -#endif case INDEX_op_setcond_i32: +case INDEX_op_movcond_i32:#if TCG_TARGET_REG_BITS ==32 +case INDEX_op_brcond2_i32:case INDEX_op_setcond2_i32: -#elif TCG_TARGET_REG_BITS ==64 +#else +case INDEX_op_brcond_i64:case INDEX_op_setcond_i64: +case INDEX_op_movcond_i64:#endif if (args[k] <ARRAY_SIZE (cond_name) && cond_name[args[k]]) {qemu_log (",%s" , cond_name[args[k++]]);
tcg/tcg.h +1 -0
@@ -79,6 +79,7 @@ typedef uint64_t TCGRegSet;#define TCG_TARGET_HAS_nand_i64 0#define TCG_TARGET_HAS_nor_i64 0#define TCG_TARGET_HAS_deposit_i64 0 +#define TCG_TARGET_HAS_movcond_i64 0#endif #ifndef TCG_TARGET_deposit_i32_valid
tcg/tci/tcg-target.h +2 -0
@@ -75,6 +75,7 @@#define TCG_TARGET_HAS_not_i32 1#define TCG_TARGET_HAS_orc_i32 0#define TCG_TARGET_HAS_rot_i32 1 +#define TCG_TARGET_HAS_movcond_i32 0#if TCG_TARGET_REG_BITS ==64 #define TCG_TARGET_HAS_bswap16_i64 1
@@ -98,6 +99,7 @@#define TCG_TARGET_HAS_not_i64 1#define TCG_TARGET_HAS_orc_i64 0#define TCG_TARGET_HAS_rot_i64 1 +#define TCG_TARGET_HAS_movcond_i64 0#endif /* TCG_TARGET_REG_BITS == 64 */ /* Offset to user memory in user mode. */
Signature · 1 line
1.7.11.4
PATCH v2 3/7 tcg-i386: Implement movcond
In reply to Richard Henderson--- tcg/i386/tcg-target.c | 29 +++++++++++++++++++++++++++++ tcg/i386/tcg-target.h | 7 ++++++- 2 files changed, 35 insertions(+), 1 deletion(-)
tcg/i386/tcg-target.c +29 -0
@@ -249,6 +249,7 @@ static inline int tcg_target_const_match(tcg_target_long val,#define OPC_ADD_GvEv (OPC_ARITH_GvEv | (ARITH_ADD << 3))#define OPC_BSWAP (0xc8 | P_EXT)#define OPC_CALL_Jz (0xe8) +#define OPC_CMOVCC (0x40 | P_EXT)/* ... plus condition code */ #define OPC_CMP_GvEv (OPC_ARITH_GvEv | (ARITH_CMP << 3))#define OPC_DEC_r32 (0x48)#define OPC_IMUL_GvEv (0xaf | P_EXT)
@@ -936,6 +937,24 @@ static void tcg_out_setcond2(TCGContext *s, const TCGArg *args, }#endif +static void tcg_out_movcond32 (TCGContext *s,TCGCond cond,TCGArg dest, +TCGArg c1,TCGArg c2,int const_c2, +TCGArg v1) +{ +tcg_out_cmp (s, c1, c2, const_c2,0 ); +tcg_out_modrm (s,OPC_CMOVCC | tcg_cond_to_jcc[cond], dest, v1); +} + +#if TCG_TARGET_REG_BITS ==64 +static void tcg_out_movcond64 (TCGContext *s,TCGCond cond,TCGArg dest, +TCGArg c1,TCGArg c2,int const_c2, +TCGArg v1) +{ +tcg_out_cmp (s, c1, c2, const_c2,P_REXW ); +tcg_out_modrm (s,OPC_CMOVCC | tcg_cond_to_jcc[cond] |P_REXW , dest, v1); +} +#endif +static void tcg_out_branch (TCGContext *s,int call,tcg_target_long dest) {tcg_target_long disp = dest - (tcg_target_long )s->code_ptr -5 ;
@@ -1668,6 +1687,10 @@ static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,tcg_out_setcond32 (s, args[3 ], args[0 ], args[1 ], args[2 ], const_args[2 ]);break ; +case INDEX_op_movcond_i32: +tcg_out_movcond32 (s, args[5 ], args[0 ], args[1 ], + args[2 ], const_args[2 ], args[3 ]); +break ; OP_32_64(bswap16):tcg_out_rolw_8 (s ,args [0 ]);
@@ -1796,6 +1819,10 @@ static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,tcg_out_setcond64 (s, args[3 ], args[0 ], args[1 ], args[2 ], const_args[2 ]);break ; +case INDEX_op_movcond_i64: +tcg_out_movcond64 (s, args[5 ], args[0 ], args[1 ], + args[2 ], const_args[2 ], args[3 ]); +break ;case INDEX_op_bswap64_i64:tcg_out_bswap64 (s, args[0 ]);
@@ -1880,6 +1907,7 @@ static const TCGTargetOpDef x86_op_defs[] = { { INDEX_op_setcond_i32, { "q", "r", "ri" } }, { INDEX_op_deposit_i32, { "Q", "0", "Q" } }, + { INDEX_op_movcond_i32, { "r", "r", "ri", "r", "0" } }, #if TCG_TARGET_REG_BITS == 32 { INDEX_op_mulu2_i32, { "a", "d", "a", "r" } },
@@ -1934,6 +1962,7 @@ static const TCGTargetOpDef x86_op_defs[] = { { INDEX_op_ext32u_i64, { "r", "r" } }, { INDEX_op_deposit_i64, { "Q", "0", "Q" } }, + { INDEX_op_movcond_i64, { "r", "r", "re", "r", "0" } }, #endif #if TCG_TARGET_REG_BITS == 64
tcg/i386/tcg-target.h +6 -1
@@ -86,7 +86,12 @@ typedef enum {#define TCG_TARGET_HAS_nand_i32 0#define TCG_TARGET_HAS_nor_i32 0#define TCG_TARGET_HAS_deposit_i32 1 +#if defined(__x86_64__) || defined(__i686__) +/* Use cmov only if the compiler is already doing so. */ +#define TCG_TARGET_HAS_movcond_i32 1 +#else #define TCG_TARGET_HAS_movcond_i32 0 +#endif #if TCG_TARGET_REG_BITS ==64 #define TCG_TARGET_HAS_div2_i64 1
@@ -108,7 +113,7 @@ typedef enum {#define TCG_TARGET_HAS_nand_i64 0#define TCG_TARGET_HAS_nor_i64 0#define TCG_TARGET_HAS_deposit_i64 1 -#define TCG_TARGET_HAS_movcond_i64 0 +#define TCG_TARGET_HAS_movcond_i64 1#endif #define TCG_TARGET_deposit_i32_valid (ofs, len) \
Signature · 1 line
1.7.11.4
PATCH v2 4/7 tcg: Optimize movcond for constant comparisons
In reply to Richard Henderson--- tcg/optimize.c | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+)
tcg/optimize.c +40 -0
@@ -394,6 +394,14 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr, args[3 ] =tcg_swap_cond (args[3 ]); }break ; + CASE_OP_32_64(movcond): +if (temps[args[1 ]].state ==TCG_TEMP_CONST + && temps[args[2 ]].state !=TCG_TEMP_CONST ) { + tmp = args[1 ]; + args[1 ] = args[2 ]; + args[2 ] = tmp; + args[5 ] =tcg_swap_cond (args[5 ]); + }default :break ; }
@@ -614,6 +622,38 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr, } args +=4 ;break ; + CASE_OP_32_64(movcond): +if (temps[args[1 ]].state ==TCG_TEMP_CONST + && temps[args[2 ]].state ==TCG_TEMP_CONST ) { + tmp =do_constant_folding_cond (op, temps[args[1 ]].val, + temps[args[2 ]].val, args[5 ]); +if (args[0 ] == args[4 -tmp] + || (temps[args[4 -tmp]].state ==TCG_TEMP_COPY + && temps[args[4 -tmp]].val == args[0 ])) { + gen_opc_buf[op_index] = INDEX_op_nop; + }else if (temps[args[4 -tmp]].state ==TCG_TEMP_CONST ) { + gen_opc_buf[op_index] =op_to_movi (op); +tcg_opt_gen_movi (gen_args, args[0 ], temps[args[4 -tmp]].val, + nb_temps, nb_globals); + gen_args +=2 ; + }else { + gen_opc_buf[op_index] =op_to_mov (op); +tcg_opt_gen_mov (gen_args, args[0 ], args[4 -tmp], + nb_temps, nb_globals); + gen_args +=2 ; + } + }else { +reset_temp (args[0 ], nb_temps, nb_globals); + gen_args[0 ] = args[0 ]; + gen_args[1 ] = args[1 ]; + gen_args[2 ] = args[2 ]; + gen_args[3 ] = args[3 ]; + gen_args[4 ] = args[4 ]; + gen_args[5 ] = args[5 ]; + gen_args +=6 ; + } + args +=6 ; +break ;case INDEX_op_call: nb_call_args = (args[0 ] >>16 ) + (args[0 ] &0xffff );if (!(args[nb_call_args +1 ] & (TCG_CALL_CONST |TCG_CALL_PURE ))) {
Signature · 1 line
1.7.11.4
PATCH v2 7/7 tcg: Streamline movcond_i64 using movcond_i32
In reply to Richard HendersonWhen movcond_i32 is available we can further reduce the generated op count from 12 to 6, and the generated code size on i686 from 88 to 74 bytes.
--- tcg/tcg-op.h | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-)
tcg/tcg-op.h +15 -7
@@ -2147,16 +2147,24 @@ static inline void tcg_gen_movcond_i64(TCGCond cond, TCGv_i64 ret,tcg_gen_op6i_i32 (INDEX_op_setcond2_i32, t0,TCGV_LOW (c1),TCGV_HIGH (c1),TCGV_LOW (c2),TCGV_HIGH (c2), cond); -tcg_gen_neg_i32 (t0, t0); -tcg_gen_and_i32 (t1,TCGV_LOW (v1), t0); -tcg_gen_andc_i32 (TCGV_LOW (ret),TCGV_LOW (v2), t0); -tcg_gen_or_i32 (TCGV_LOW (ret),TCGV_LOW (ret), t1); +if (TCG_TARGET_HAS_movcond_i32) { +tcg_gen_movi_i32 (t1,0 ); +tcg_gen_movcond_i32 (TCG_COND_NE ,TCGV_LOW (ret), t0, t1, +TCGV_LOW (v1),TCGV_LOW (v2)); +tcg_gen_movcond_i32 (TCG_COND_NE ,TCGV_HIGH (ret), t0, t1, +TCGV_HIGH (v1),TCGV_HIGH (v2)); + }else { +tcg_gen_neg_i32 (t0, t0); -tcg_gen_and_i32 (t1,TCGV_HIGH (v1), t0); -tcg_gen_andc_i32 (TCGV_HIGH (ret),TCGV_HIGH (v2), t0); -tcg_gen_or_i32 (TCGV_HIGH (ret),TCGV_HIGH (ret), t1); +tcg_gen_and_i32 (t1,TCGV_LOW (v1), t0); +tcg_gen_andc_i32 (TCGV_LOW (ret),TCGV_LOW (v2), t0); +tcg_gen_or_i32 (TCGV_LOW (ret),TCGV_LOW (ret), t1); +tcg_gen_and_i32 (t1,TCGV_HIGH (v1), t0); +tcg_gen_andc_i32 (TCGV_HIGH (ret),TCGV_HIGH (v2), t0); +tcg_gen_or_i32 (TCGV_HIGH (ret),TCGV_HIGH (ret), t1); + }tcg_temp_free_i32 (t0);tcg_temp_free_i32 (t1); }else {
Signature · 1 line
1.7.11.4
PATCH v2 2/7 target-alpha: Use movcond
In reply to Richard HendersonFor proper cmov insns, as well as the non-goto-tb case of conditional branch.
--- target-alpha/translate.c | 102 ++++++++++++++++++++++------------------------- 1 file changed, 48 insertions(+), 54 deletions(-)
target-alpha/translate.c +48 -54
@@ -426,27 +426,15 @@ static ExitStatus gen_bcond_internal(DisasContext *ctx, TCGCond cond,return EXIT_GOTO_TB ; }else { -int lab_over =gen_new_label (); - -/* ??? Consider using either -movi pc, next -addi tmp, pc, disp -movcond pc, cond, 0, tmp, pc -or -setcond tmp, cond, 0 -movi pc, next -neg tmp, tmp -andi tmp, tmp, disp -add pc, pc, tmp -The current diamond subgraph surely isn't efficient. */ +TCGv_i64 z =tcg_const_i64 (0 ); +TCGv_i64 d =tcg_const_i64 (dest); +TCGv_i64 p =tcg_const_i64 (ctx->pc); -tcg_gen_brcondi_i64 (cond, cmp,0 , lab_true); -tcg_gen_movi_i64 (cpu_pc, ctx->pc); -tcg_gen_br (lab_over); -gen_set_label (lab_true); -tcg_gen_movi_i64 (cpu_pc, dest); -gen_set_label (lab_over); +tcg_gen_movcond_i64 (cond, cpu_pc, cmp, z, d, p); +tcg_temp_free_i64 (z); +tcg_temp_free_i64 (d); +tcg_temp_free_i64 (p);return EXIT_PC_UPDATED ; } }
@@ -521,61 +509,67 @@ static ExitStatus gen_fbcond(DisasContext *ctx, TCGCond cond, int ra,static void gen_cmov (TCGCond cond,int ra,int rb,int rc,int islit,uint8_t lit,int mask) { -TCGCond inv_cond =tcg_invert_cond (cond); -int l1; +TCGv_i64 c1, z, v1; -if (unlikely (rc ==31 )) +if (unlikely (rc ==31 )) {return ; + } - l1 =gen_new_label (); - -if (ra !=31 ) { -if (mask) { -TCGv tmp =tcg_temp_new (); -tcg_gen_andi_i64 (tmp, cpu_ir[ra],1 ); -tcg_gen_brcondi_i64 (inv_cond, tmp,0 , l1); -tcg_temp_free (tmp); - }else -tcg_gen_brcondi_i64 (inv_cond, cpu_ir[ra],0 , l1); - }else { +if (ra ==31 ) {/* Very uncommon case - Do not bother to optimize. */ -TCGv tmp =tcg_const_i64 (0 ); -tcg_gen_brcondi_i64 (inv_cond, tmp,0 , l1); -tcg_temp_free (tmp); + c1 =tcg_const_i64 (0 ); + }else if (mask) { + c1 =tcg_const_i64 (1 ); +tcg_gen_and_i64 (c1, c1, cpu_ir[ra]); + }else { + c1 = cpu_ir[ra]; } +if (islit) { + v1 =tcg_const_i64 (lit); + }else { + v1 = cpu_ir[rb]; + } + z =tcg_const_i64 (0 ); -if (islit) -tcg_gen_movi_i64 (cpu_ir[rc], lit); -else -tcg_gen_mov_i64 (cpu_ir[rc], cpu_ir[rb]); -gen_set_label (l1); +tcg_gen_movcond_i64 (cond, cpu_ir[rc], c1, z, v1, cpu_ir[rc]); + +tcg_temp_free_i64 (z); +if (ra ==31 || mask) { +tcg_temp_free_i64 (c1); + } +if (islit) { +tcg_temp_free_i64 (v1); + } }static void gen_fcmov (TCGCond cond,int ra,int rb,int rc) { -TCGv cmp_tmp; -int l1; +TCGv_i64 c1, z, v1;if (unlikely (rc ==31 )) {return ; } - cmp_tmp =tcg_temp_new (); + c1 =tcg_temp_new_i64 ();if (unlikely (ra ==31 )) { -tcg_gen_movi_i64 (cmp_tmp,0 ); +tcg_gen_movi_i64 (c1,0 ); + }else { +gen_fold_mzero (cond, c1, cpu_fir[ra]); + } +if (rb ==31 ) { + v1 =tcg_const_i64 (0 ); }else { -gen_fold_mzero (cond, cmp_tmp, cpu_fir[ra]); + v1 = cpu_fir[rb]; } + z =tcg_const_i64 (0 ); - l1 =gen_new_label (); -tcg_gen_brcondi_i64 (tcg_invert_cond (cond), cmp_tmp,0 , l1); -tcg_temp_free (cmp_tmp); +tcg_gen_movcond_i64 (cond, cpu_fir[rc], c1, z, v1, cpu_fir[rc]); -if (rb !=31 ) -tcg_gen_mov_i64 (cpu_fir[rc], cpu_fir[rb]); -else -tcg_gen_movi_i64 (cpu_fir[rc],0 ); -gen_set_label (l1); +tcg_temp_free_i64 (z); +tcg_temp_free_i64 (c1); +if (rb ==31 ) { +tcg_temp_free_i64 (v1); + } }#define QUAL_RM_N 0x080/* Round mode nearest even */
Signature · 1 line
1.7.11.4
PATCH v2 5/7 tcg: Optimize two-address commutative operations
In reply to Richard HendersonWhile swapping constants to the second operand, swap sources matching destinations to the first operand.
--- tcg/optimize.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)
tcg/optimize.c +14 -1
@@ -334,6 +334,8 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr,const TCGOpDef *def;TCGArg *gen_args;TCGArg tmp; +TCGCond cond; +/* Array VALS has an element for each temp. If this temp holds a constant then its value is kept in VALS' element. If this temp is a copy of other ones then this equivalence class'
@@ -395,13 +397,24 @@ static TCGArg *tcg_constant_folding(TCGContext *s, uint16_t *tcg_opc_ptr, }break ; CASE_OP_32_64(movcond): + cond = args[5 ];if (temps[args[1 ]].state ==TCG_TEMP_CONST && temps[args[2 ]].state !=TCG_TEMP_CONST ) { tmp = args[1 ]; args[1 ] = args[2 ]; args[2 ] = tmp; - args[5 ] =tcg_swap_cond (args[5 ]); + cond =tcg_swap_cond (cond); + } +/* For movcond, we canonicalize the "false" input reg to match +the destination reg so that the tcg backend can implement +a "move if true" operation. */ +if (args[0 ] == args[3 ]) { + tmp = args[3 ]; + args[3 ] = args[4 ]; + args[4 ] = tmp; + cond =tcg_invert_cond (cond); } + args[5 ] = cond;default :break ; }
Signature · 1 line
1.7.11.4
PATCH v2 6/7 tcg: Streamline movcond_i64 using 32-bit arithmetic
In reply to Richard HendersonAvoiding 64-bit arithmetic (outside of the compare) reduces the generated op count from 15 to 12, and the generated code size on i686 from 105 to 88 bytes.
--- tcg/tcg-op.h | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-)
tcg/tcg-op.h +31 -11
@@ -2141,18 +2141,38 @@ static inline void tcg_gen_movcond_i64(TCGCond cond, TCGv_i64 ret, TCGv_i64 c1, TCGv_i64 c2, TCGv_i64 v1, TCGv_i64 v2) { - if (TCG_TARGET_HAS_movcond_i64) { -tcg_gen_op6i_i64 (INDEX_op_movcond_i64 ,ret ,c1 ,c2 ,v1 ,v2 ,cond ); + if (TCG_TARGET_REG_BITS == 32) { + TCGv_i32 t0 = tcg_temp_new_i32(); +TCGv_i32 t1 =tcg_temp_new_i32 (); +tcg_gen_op6i_i32 (INDEX_op_setcond2_i32, t0, +TCGV_LOW (c1),TCGV_HIGH (c1), +TCGV_LOW (c2),TCGV_HIGH (c2), cond); +tcg_gen_neg_i32 (t0, t0); + +tcg_gen_and_i32 (t1,TCGV_LOW (v1), t0); +tcg_gen_andc_i32 (TCGV_LOW (ret),TCGV_LOW (v2), t0); +tcg_gen_or_i32 (TCGV_LOW (ret),TCGV_LOW (ret), t1); + +tcg_gen_and_i32 (t1,TCGV_HIGH (v1), t0); +tcg_gen_andc_i32 (TCGV_HIGH (ret),TCGV_HIGH (v2), t0); +tcg_gen_or_i32 (TCGV_HIGH (ret),TCGV_HIGH (ret), t1); + +tcg_temp_free_i32 (t0); +tcg_temp_free_i32 (t1); } else { -TCGv_i64 t0 =tcg_temp_new_i64 (); -TCGv_i64 t1 =tcg_temp_new_i64 (); -tcg_gen_setcond_i64 (cond, t0, c1, c2); -tcg_gen_neg_i64 (t0, t0); -tcg_gen_and_i64 (t1, v1, t0); -tcg_gen_andc_i64 (ret, v2, t0); -tcg_gen_or_i64 (ret, ret, t1); -tcg_temp_free_i64 (t0); -tcg_temp_free_i64 (t1); +if (TCG_TARGET_HAS_movcond_i64) { +tcg_gen_op6i_i64 (INDEX_op_movcond_i64, ret, c1, c2, v1, v2, cond); + }else { +TCGv_i64 t0 =tcg_temp_new_i64 (); +TCGv_i64 t1 =tcg_temp_new_i64 (); +tcg_gen_setcond_i64 (cond, t0, c1, c2); +tcg_gen_neg_i64 (t0, t0); +tcg_gen_and_i64 (t1, v1, t0); +tcg_gen_andc_i64 (ret, v2, t0); +tcg_gen_or_i64 (ret, ret, t1); +tcg_temp_free_i64 (t0); +tcg_temp_free_i64 (t1); + } } }
Signature · 1 line
1.7.11.4
Quoted 9 lines · Richard Henderson
The patch set is available at
git://repo.or.cz/qemu/rth.gittcg-movcondChanges
v1->v2:
Patch 1 drops the spurrious return.Patch 4 incorporates the fix Aurelian posted.
Quoted 8 lines
Patch 5 drops the non-movcond commutative optimization that is
present in Aurelian's tcg optimization patch set.Patches 6 & 7 are new, incrementally improving 32-bit code generation.
Rebased vs
c26032b2c91721245bfec542d94f37a0238e986e:
target-xtensa: don't emit extratcg_gen_goto_tb
Thanks for the new version. I have applied patches 1 to 5. I leave some time for people to review patches 6 and 7.
Signature · 2 lines
Aurelien Jarno GPG: 1024D/F1BCDB73 [email protected] http://www.aurel32.net
Re: [Qemu-devel] [PATCH v2 0/7] tcg: movcond (ppc32 version)
In reply to Richard Hendersontcg/ppc/tcg-target.c +78 -0
@@ -390,6 +390,7 @@ static int tcg_target_const_match(tcg_target_long val,#define ORC XO31(412)#define EQV XO31(284)#define NAND XO31(476) +#define ISEL XO31( 15)#define LBZX XO31( 87)#define LHZX XO31(279)
@@ -1269,6 +1270,75 @@ static void tcg_out_setcond2 (TCGContext *s, const TCGArg *args, ); } +static void tcg_out_movcond (TCGContext *s,TCGCond cond, +TCGArg dest, +TCGArg c1,TCGArg c2, +TCGArg v1,TCGArg v2, +int const_c2) +{ +tcg_out_cmp (s, cond, c1, c2, const_c2,7 ); + +if (1 ) { +/* At least here on 7747A bit twiddling hacks are outperformed +by jumpy code (the testing was not scientific) */ +void *label_ptr; + +if (dest == v2) { + label_ptr = s->code_ptr; +tcg_out32 (s, tcg_to_bc[tcg_invert_cond (cond)]); +tcg_out_mov (s,TCG_TYPE_I32 , dest, v1); +reloc_pc14 (label_ptr, (tcg_target_long ) s->code_ptr); + } +else { +tcg_out_mov (s,TCG_TYPE_I32 , dest, v1); + label_ptr = s->code_ptr; +tcg_out32 (s, tcg_to_bc[cond]); +tcg_out_mov (s,TCG_TYPE_I32 , dest, v2); +reloc_pc14 (label_ptr, (tcg_target_long ) s->code_ptr); + } + } +else { +/* isel version, if (1) above should be replaced once a way to +figure out availability of isel on the underlying hardware +is found */ +int tab, bc; + +switch (cond) { +case TCG_COND_EQ : + tab =TAB (dest, v1, v2); + bc =CR_EQ ; +break ; +case TCG_COND_NE : + tab =TAB (dest, v2, v1); + bc =CR_EQ ; +break ; +case TCG_COND_LTU : +case TCG_COND_LT : + tab =TAB (dest, v1, v2); + bc =CR_LT ; +break ; +case TCG_COND_GEU : +case TCG_COND_GE : + tab =TAB (dest, v2, v1); + bc =CR_LT ; +break ; +case TCG_COND_LEU : +case TCG_COND_LE : + tab =TAB (dest, v2, v1); + bc =CR_GT ; +break ; +case TCG_COND_GTU : +case TCG_COND_GT : + tab =TAB (dest, v1, v2); + bc =CR_GT ; +break ; +default : +tcg_abort (); + } +tcg_out32 (s,ISEL | tab | ((bc +28 ) <<6 )); + } +} +static void tcg_out_brcond (TCGContext *s,TCGCond cond,TCGArg arg1,TCGArg arg2,int const_arg2,int label_index)
@@ -1826,6 +1896,13 @@ static void tcg_out_op(TCGContext *s, TCGOpcode opc, const TCGArg *args, );break ; +case INDEX_op_movcond_i32: +tcg_out_movcond (s, args[5 ], args[0 ], + args[1 ], args[2 ], + args[3 ], args[4 ], + const_args[2 ]); +break ; +default :tcg_dump_ops (s);tcg_abort ();
@@ -1922,6 +1999,7 @@ static const TCGTargetOpDef ppc_op_defs[] = { { INDEX_op_ext16u_i32, { "r", "r" } }, { INDEX_op_deposit_i32, { "r", "0", "r" } }, + { INDEX_op_movcond_i32, { "r", "r", "ri", "r", "r" } }, { -1 }, };
tcg/ppc/tcg-target.h +1 -1
@@ -92,7 +92,7 @@ typedef enum {#define TCG_TARGET_HAS_nand_i32 1#define TCG_TARGET_HAS_nor_i32 1#define TCG_TARGET_HAS_deposit_i32 1 -#define TCG_TARGET_HAS_movcond_i32 0 +#define TCG_TARGET_HAS_movcond_i32 1#define TCG_AREG0 TCG_REG_R27
Re: [Qemu-devel] [PATCH 6/7] tcg: Streamline movcond_i64 using 32-bit arithmetic
In reply to Richard HendersonQuoted 53 lines · Richard Henderson
Avoiding 64-bit arithmetic (outside of the compare) reduces the generated op count from 15 to 12, and the generated code size on i686 from 105 to 88 bytes.
Signed-off-by Richard Henderson <[email protected]>--- tcg/tcg-op.h | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-)tcg/tcg-op.h +24
-11@@ -2141,18 +2141,38 @@ static inline void tcg_gen_movcond_i64(TCGCond cond, TCGv_i64 ret, TCGv_i64 c1, TCGv_i64 c2, TCGv_i64 v1, TCGv_i64 v2) { - if (TCG_TARGET_HAS_movcond_i64) { -tcg_gen_op6i_i64 (INDEX_op_movcond_i64 ,ret ,c1 ,c2 ,v1 ,v2 ,cond ); + if (TCG_TARGET_REG_BITS == 32) { + TCGv_i32 t0 = tcg_temp_new_i32(); +TCGv_i32 t1 =tcg_temp_new_i32 (); +tcg_gen_op6i_i32 (INDEX_op_setcond2_i32, t0, +TCGV_LOW (c1),TCGV_HIGH (c1), +TCGV_LOW (c2),TCGV_HIGH (c2), cond); +tcg_gen_neg_i32 (t0, t0); + +tcg_gen_and_i32 (t1,TCGV_LOW (v1), t0); +tcg_gen_andc_i32 (TCGV_LOW (ret),TCGV_LOW (v2), t0); +tcg_gen_or_i32 (TCGV_LOW (ret),TCGV_LOW (ret), t1); + +tcg_gen_and_i32 (t1,TCGV_HIGH (v1), t0); +tcg_gen_andc_i32 (TCGV_HIGH (ret),TCGV_HIGH (v2), t0); +tcg_gen_or_i32 (TCGV_HIGH (ret),TCGV_HIGH (ret), t1); + +tcg_temp_free_i32 (t0); +tcg_temp_free_i32 (t1); } else { -TCGv_i64 t0 =tcg_temp_new_i64 (); -TCGv_i64 t1 =tcg_temp_new_i64 (); -tcg_gen_setcond_i64 (cond, t0, c1, c2); -tcg_gen_neg_i64 (t0, t0); -tcg_gen_and_i64 (t1, v1, t0); -tcg_gen_andc_i64 (ret, v2, t0); -tcg_gen_or_i64 (ret, ret, t1); -tcg_temp_free_i64 (t0); -tcg_temp_free_i64 (t1); +if (TCG_TARGET_HAS_movcond_i64) { +tcg_gen_op6i_i64 (INDEX_op_movcond_i64, ret, c1, c2, v1, v2, cond); + }else { +TCGv_i64 t0 =tcg_temp_new_i64 (); +TCGv_i64 t1 =tcg_temp_new_i64 (); +tcg_gen_setcond_i64 (cond, t0, c1, c2);
tcg/tcg-op.h static inline void tcg_gen_movcond_i64(TCGCond cond, TCGv_i64 ret, · line 2178
Quoted 10 lines
tcg/tcg-op.h +7
-0+tcg_gen_neg_i64 (t0, t0); +tcg_gen_and_i64 (t1, v1, t0); +tcg_gen_andc_i64 (ret, v2, t0); +tcg_gen_or_i64 (ret, ret, t1); +tcg_temp_free_i64 (t0); +tcg_temp_free_i64 (t1); + } } }
Signature · 2 lines
Aurelien Jarno GPG: 1024D/F1BCDB73 [email protected] http://www.aurel32.net
Re: [Qemu-devel] [PATCH 7/7] tcg: Streamline movcond_i64 using movcond_i32
In reply to Richard HendersonQuoted 35 lines · Richard Henderson
When
movcond_i32is available we can further reduce the generated op count from 12 to 6, and the generated code size on i686 from 88 to 74 bytes.Signed-off-by Richard Henderson <[email protected]>--- tcg/tcg-op.h | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-)tcg/tcg-op.h +9
-7@@ -2147,16 +2147,24 @@ static inline void tcg_gen_movcond_i64(TCGCond cond, TCGv_i64 ret,tcg_gen_op6i_i32 (INDEX_op_setcond2_i32, t0,TCGV_LOW (c1),TCGV_HIGH (c1),TCGV_LOW (c2),TCGV_HIGH (c2), cond); -tcg_gen_neg_i32 (t0, t0); -tcg_gen_and_i32 (t1,TCGV_LOW (v1), t0); -tcg_gen_andc_i32 (TCGV_LOW (ret),TCGV_LOW (v2), t0); -tcg_gen_or_i32 (TCGV_LOW (ret),TCGV_LOW (ret), t1); +if (TCG_TARGET_HAS_movcond_i32) { +tcg_gen_movi_i32 (t1,0 ); +tcg_gen_movcond_i32 (TCG_COND_NE ,TCGV_LOW (ret), t0, t1, +TCGV_LOW (v1),TCGV_LOW (v2)); +tcg_gen_movcond_i32 (TCG_COND_NE ,TCGV_HIGH (ret), t0, t1, +TCGV_HIGH (v1),TCGV_HIGH (v2)); + }else { +tcg_gen_neg_i32 (t0, t0); -tcg_gen_and_i32 (t1,TCGV_HIGH (v1), t0); -tcg_gen_andc_i32 (TCGV_HIGH (ret),TCGV_HIGH (v2), t0); -tcg_gen_or_i32 (TCGV_HIGH (ret),TCGV_HIGH (ret), t1); +tcg_gen_and_i32 (t1,TCGV_LOW (v1), t0);
tcg/tcg-op.h static inline void tcg_gen_movcond_i64(TCGCond cond, TCGv_i64 ret, · line 2170
Quoted 10 lines
tcg/tcg-op.h +6
-0+tcg_gen_andc_i32 (TCGV_LOW (ret),TCGV_LOW (v2), t0); +tcg_gen_or_i32 (TCGV_LOW (ret),TCGV_LOW (ret), t1); +tcg_gen_and_i32 (t1,TCGV_HIGH (v1), t0); +tcg_gen_andc_i32 (TCGV_HIGH (ret),TCGV_HIGH (v2), t0); +tcg_gen_or_i32 (TCGV_HIGH (ret),TCGV_HIGH (ret), t1); + }tcg_temp_free_i32 (t0);tcg_temp_free_i32 (t1); }else {
At some point I tried to think how to implement movcond_i64 for MIPS directly in the backend. I just tried your patch, and I got this kind of code:
| 0x2bb2ae58: sltu at,zero,s4 | 0x2bb2ae5c: sltu t0,zero,s3 | 0x2bb2ae60: or s3,at,t0 | 0x2bb2ae64: movz s1,s5,s3 | 0x2bb2ae68: movz s2,s6,s3 | | (in some cases some constants/globals loading appear in the middle, but | that's not due to movcond).
It's basically the kind of code I would have written. It's clearly better to implement it directly in TCG.
Now I wonder if it wouldn't be better to write brcond2 as setcond2 + brcond. And even setcond2 as a pair of setcond in TCG, which would allow some optimizations in case both high parts are zero.
Signature · 2 lines
Aurelien Jarno GPG: 1024D/F1BCDB73 [email protected] http://www.aurel32.net
Re: [Qemu-devel] [PATCH v2 0/7] tcg: movcond (ppc32 version)
In reply to malctcg/ppc/tcg-target.c static void tcg_out_setcond2 (TCGContext *s, const TCGArg *args, · line 1298
Quoted 13 lines · malc
tcg/ppc/tcg-target.c +13
-0+if (dest == v2) { + label_ptr = s->code_ptr; +tcg_out32 (s, tcg_to_bc[tcg_invert_cond (cond)]); +tcg_out_mov (s,TCG_TYPE_I32 , dest, v1); +reloc_pc14 (label_ptr, (tcg_target_long ) s->code_ptr); + } +else { +tcg_out_mov (s,TCG_TYPE_I32 , dest, v1); + label_ptr = s->code_ptr; +tcg_out32 (s, tcg_to_bc[cond]); +tcg_out_mov (s,TCG_TYPE_I32 , dest, v2); +reloc_pc14 (label_ptr, (tcg_target_long ) s->code_ptr); + }
How about
if (dest == v2) {
cond = tcg_invert_cond(cond);
v2 = v1;
} else if (dest != v1) {
tcg_out_mov(s, TCG_TYPE_I32, dest, v1);
}
/* Branch forward over one insn. */
tcg_out32 (s, tcg_to_bc[cond] | 4);
tcg_out_mov(s, TCG_TYPE_I32, dest, v2);which avoids an extra mov if dest == v1, and also minimizes the code.
r~
Re: [Qemu-devel] [PATCH 7/7] tcg: Streamline movcond_i64 using movcond_i32
In reply to Aurelien JarnoQuoted 3 lines · Aurelien Jarno
Now I wonder if it wouldn't be better to write brcond2 as setcond2 + brcond. And even setcond2 as a pair of setcond in TCG, which would allow some optimizations in case both high parts are zero.
I think brcond2 vs setcond2 is a choice that has to be made on a host-by-host basis. E.g. for i386 we implement setcond2 with branches. E.g. for hppa setcond2, while not using branches, is twice the size of brcond2. But there's nothing saying you couldn't have the mips version of brcond2 use setcond2 internals to do its job.
On the other hand, having tcg/optimize.c reduce both setcond2 and brcond2 to setcond and brcond with the appropriate values of zero would be a most welcome improvement.
Also, I've been thinking about having a tcg.h function that produces the cond-without-equality table that I introduced to fix hppa recently. Using that could reduce code size in some of the other backends as well.
r~
Re: [Qemu-devel] [PATCH v2 0/7] tcg: movcond (ppc32 version)
In reply to Richard HendersonQuoted 27 lines · Richard Henderson
tcg/ppc/tcg-target.c static void tcg_out_setcond2 (TCGContext *s, const TCGArg *args, · line 1298
Quoted 13 lines · malc
tcg/ppc/tcg-target.c +13
-0+if (dest == v2) { + label_ptr = s->code_ptr; +tcg_out32 (s, tcg_to_bc[tcg_invert_cond (cond)]); +tcg_out_mov (s,TCG_TYPE_I32 , dest, v1); +reloc_pc14 (label_ptr, (tcg_target_long ) s->code_ptr); + } +else { +tcg_out_mov (s,TCG_TYPE_I32 , dest, v1); + label_ptr = s->code_ptr; +tcg_out32 (s, tcg_to_bc[cond]); +tcg_out_mov (s,TCG_TYPE_I32 , dest, v2); +reloc_pc14 (label_ptr, (tcg_target_long ) s->code_ptr); + }How about
if (dest == v2) { cond = tcg_invert_cond(cond); v2 = v1; } else if (dest != v1) { tcg_out_mov(s, TCG_TYPE_I32, dest, v1); } /* Branch forward over one insn. */ tcg_out32 (s, tcg_to_bc[cond] | 4); tcg_out_mov(s, TCG_TYPE_I32, dest, v2);
Quoted 1 line
which avoids an extra mov if dest == v1, and also minimizes the code.
Yes, thanks, that's better (save for | 4 part which is 4 too little)
Signature · 1 line
mailto:[email protected]
Re: [Qemu-devel] [PATCH 7/7] tcg: Streamline movcond_i64 using movcond_i32
In reply to Richard HendersonQuoted 10 lines · Richard Henderson
Quoted 3 lines · Aurelien Jarno
Now I wonder if it wouldn't be better to write brcond2 as setcond2 + brcond. And even setcond2 as a pair of setcond in TCG, which would allow some optimizations in case both high parts are zero.
I think brcond2 vs setcond2 is a choice that has to be made on a host-by-host basis. E.g. for i386 we implement setcond2 with branches. E.g. for hppa setcond2, while not using branches, is twice the size of brcond2. But there's nothing saying you couldn't have the mips version of brcond2 use setcond2 internals to do its job.
Yes and now. Currently the MIPS version of setcond2, uses 2 temporary registers and the return argument. This is therefore not possible to make brcond2 to use setcond2.
I have just seen it's possible to reorder the instructions to use two temporaries only, but you see the problem there. Doing that as the TCG level means you can use more temporaries, at the backend level means you have to play with the registers you have.
Quoted 3 lines
On the other hand, having
tcg/optimize.creduce both setcond2 and brcond2 to setcond and brcond with the appropriate values of zero would be a most welcome improvement.
That's a good idea.
Quoted 4 lines
Also, I've been thinking about having a
tcg.hfunction that produces the cond-without-equality table that I introduced to fix hppa recently. Using that could reduce code size in some of the other backends as well.
I am using such a table in the MIPS backend, but done with a big switch.
Signature · 2 lines
Aurelien Jarno GPG: 1024D/F1BCDB73 [email protected] http://www.aurel32.net
Re: [Qemu-devel] [PATCH v2 0/7] tcg: movcond (ppc32 version)
In reply to malcQuoted 26 lines · malc
tcg/ppc/tcg-target.c +11
-0@@ -390,6 +390,7 @@ static int tcg_target_const_match(tcg_target_long val,#define ORC XO31(412)#define EQV XO31(284)#define NAND XO31(476) +#define ISEL XO31( 15)#define LBZX XO31( 87)#define LHZX XO31(279)@@ -1269,6 +1270,75 @@ static void tcg_out_setcond2 (TCGContext *s, const TCGArg *args, ); } +static void tcg_out_movcond (TCGContext *s,TCGCond cond, +TCGArg dest, +TCGArg c1,TCGArg c2, +TCGArg v1,TCGArg v2, +int const_c2) +{ +tcg_out_cmp (s, cond, c1, c2, const_c2,7 ); + +if (1 ) { +/* At least here on 7747A bit twiddling hacks are outperformed
tcg/ppc/tcg-target.c static void tcg_out_setcond2 (TCGContext *s, const TCGArg *args, · line 1292
Quoted 10 lines
tcg/ppc/tcg-target.c +10
-0+by jumpy code (the testing was not scientific) */ +void *label_ptr; + +if (dest == v2) { + label_ptr = s->code_ptr; +tcg_out32 (s, tcg_to_bc[tcg_invert_cond (cond)]); +tcg_out_mov (s,TCG_TYPE_I32 , dest, v1); +reloc_pc14 (label_ptr, (tcg_target_long ) s->code_ptr); + } +else {
} else {
tcg/ppc/tcg-target.c static void tcg_out_setcond2 (TCGContext *s, const TCGArg *args, · line 1300
Quoted 8 lines
tcg/ppc/tcg-target.c +8
-0+tcg_out_mov (s,TCG_TYPE_I32 , dest, v1); + label_ptr = s->code_ptr; +tcg_out32 (s, tcg_to_bc[cond]); +tcg_out_mov (s,TCG_TYPE_I32 , dest, v2); +reloc_pc14 (label_ptr, (tcg_target_long ) s->code_ptr); + } + } +else {
} else {
Quoted 80 lines
tcg/ppc/tcg-target.c +49
-0+/* isel version, if (1) above should be replaced once a way to +figure out availability of isel on the underlying hardware +is found */ +int tab, bc; + +switch (cond) { +case TCG_COND_EQ : + tab =TAB (dest, v1, v2); + bc =CR_EQ ; +break ; +case TCG_COND_NE : + tab =TAB (dest, v2, v1); + bc =CR_EQ ; +break ; +case TCG_COND_LTU : +case TCG_COND_LT : + tab =TAB (dest, v1, v2); + bc =CR_LT ; +break ; +case TCG_COND_GEU : +case TCG_COND_GE : + tab =TAB (dest, v2, v1); + bc =CR_LT ; +break ; +case TCG_COND_LEU : +case TCG_COND_LE : + tab =TAB (dest, v2, v1); + bc =CR_GT ; +break ; +case TCG_COND_GTU : +case TCG_COND_GT : + tab =TAB (dest, v1, v2); + bc =CR_GT ; +break ; +default : +tcg_abort (); + } +tcg_out32 (s,ISEL | tab | ((bc +28 ) <<6 )); + } +} +static void tcg_out_brcond (TCGContext *s,TCGCond cond,TCGArg arg1,TCGArg arg2,int const_arg2,int label_index)@@ -1826,6 +1896,13 @@ static void tcg_out_op(TCGContext *s, TCGOpcode opc, const TCGArg *args, );break ; +case INDEX_op_movcond_i32: +tcg_out_movcond (s, args[5 ], args[0 ], + args[1 ], args[2 ], + args[3 ], args[4 ], + const_args[2 ]); +break ; +default :tcg_dump_ops (s);tcg_abort ();@@ -1922,6 +1999,7 @@ static const TCGTargetOpDef ppc_op_defs[] = { { INDEX_op_ext16u_i32, { "r", "r" } }, { INDEX_op_deposit_i32, { "r", "0", "r" } }, + { INDEX_op_movcond_i32, { "r", "r", "ri", "r", "r" } }, { -1 }, };tcg/ppc/tcg-target.h +1
-1@@ -92,7 +92,7 @@ typedef enum {#define TCG_TARGET_HAS_nand_i32 1#define TCG_TARGET_HAS_nor_i32 1#define TCG_TARGET_HAS_deposit_i32 1 -#define TCG_TARGET_HAS_movcond_i32 0 +#define TCG_TARGET_HAS_movcond_i32 1#define TCG_AREG0 TCG_REG_R27
Re: [Qemu-devel] [PATCH 6/7] tcg: Streamline movcond_i64 using 32-bit arithmetic
In reply to Richard HendersonQuoted 14 lines · Richard Henderson
Avoiding 64-bit arithmetic (outside of the compare) reduces the generated op count from 15 to 12, and the generated code size on i686 from 105 to 88 bytes.
Signed-off-by Richard Henderson <[email protected]>--- tcg/tcg-op.h | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-)tcg/tcg-op.h +0
-0
tcg/tcg-op.h static inline void tcg_gen_movcond_i64(TCGCond cond, TCGv_i64 ret, · line 2144
Quoted 6 lines
tcg/tcg-op.h +1
-2@@ -2141,18 +2141,38 @@ static inline void tcg_gen_movcond_i64(TCGCond cond, TCGv_i64 ret, TCGv_i64 c1, TCGv_i64 c2, TCGv_i64 v1, TCGv_i64 v2) { - if (TCG_TARGET_HAS_movcond_i64) { -tcg_gen_op6i_i64 (INDEX_op_movcond_i64 ,ret ,c1 ,c2 ,v1 ,v2 ,cond ); + if (TCG_TARGET_REG_BITS == 32) {
Using such a construction doesn't compile on a 64-bit host.
tcg/tcg-op.h static inline void tcg_gen_movcond_i64(TCGCond cond, TCGv_i64 ret, · line 2149
Quoted 5 lines
tcg/tcg-op.h +5
-0+TCGv_i32 t0 =tcg_temp_new_i32 (); +TCGv_i32 t1 =tcg_temp_new_i32 (); +tcg_gen_op6i_i32 (INDEX_op_setcond2_i32, t0, +TCGV_LOW (c1),TCGV_HIGH (c1), +TCGV_LOW (c2),TCGV_HIGH (c2), cond);
Because there TCGV_LOW and TCGV_HIGH do not exist. You should use #ifdef instead.
Quoted 43 lines
tcg/tcg-op.h +25
-9+tcg_gen_neg_i32 (t0, t0); + +tcg_gen_and_i32 (t1,TCGV_LOW (v1), t0); +tcg_gen_andc_i32 (TCGV_LOW (ret),TCGV_LOW (v2), t0); +tcg_gen_or_i32 (TCGV_LOW (ret),TCGV_LOW (ret), t1); + +tcg_gen_and_i32 (t1,TCGV_HIGH (v1), t0); +tcg_gen_andc_i32 (TCGV_HIGH (ret),TCGV_HIGH (v2), t0); +tcg_gen_or_i32 (TCGV_HIGH (ret),TCGV_HIGH (ret), t1); + +tcg_temp_free_i32 (t0); +tcg_temp_free_i32 (t1); }else { -TCGv_i64 t0 =tcg_temp_new_i64 (); -TCGv_i64 t1 =tcg_temp_new_i64 (); -tcg_gen_setcond_i64 (cond, t0, c1, c2); -tcg_gen_neg_i64 (t0, t0); -tcg_gen_and_i64 (t1, v1, t0); -tcg_gen_andc_i64 (ret, v2, t0); -tcg_gen_or_i64 (ret, ret, t1); -tcg_temp_free_i64 (t0); -tcg_temp_free_i64 (t1); +if (TCG_TARGET_HAS_movcond_i64) { +tcg_gen_op6i_i64 (INDEX_op_movcond_i64, ret, c1, c2, v1, v2, cond); + }else { +TCGv_i64 t0 =tcg_temp_new_i64 (); +TCGv_i64 t1 =tcg_temp_new_i64 (); +tcg_gen_setcond_i64 (cond, t0, c1, c2); +tcg_gen_neg_i64 (t0, t0); +tcg_gen_and_i64 (t1, v1, t0); +tcg_gen_andc_i64 (ret, v2, t0); +tcg_gen_or_i64 (ret, ret, t1); +tcg_temp_free_i64 (t0); +tcg_temp_free_i64 (t1); + } } }Signature · 1 line
1.7.11.4
Signature · 2 lines
Aurelien Jarno GPG: 1024D/F1BCDB73 [email protected] http://www.aurel32.net
Re: [Qemu-devel] [PATCH 3/7] tcg-i386: Implement movcond
In reply to Richard Hendersonjust finished a git-bisect and I found this... and now I do not fully understand why I have the problem.
To replicate the error (in a i386 machine, at least):
$ make clean && ./configure --enable-debug && make -j && make install [Note: I tried both ppc and i386 targets, so doesn't seem machine-dependent] $ ./path/to/qemu/bin/qemu-i386 doesntmatter Invalid op definition for movcond_i32 /mnt/DATA/DARCO/qemu-git/tcg/tcg.c:1170: tcg fatal error Aborted
"doesntmatter" can be a non-existing file, or an existing file. It seems to fail before checking file existence.
Quoted 112 lines · Richard Henderson
Signed-off-by Richard Henderson <[email protected]>Reviewed-by Aurelien Jarno <[email protected]>--- tcg/i386/tcg-target.c | 29 +++++++++++++++++++++++++++++ tcg/i386/tcg-target.h | 7 ++++++- 2 files changed, 35 insertions(+), 1 deletion(-)tcg/i386/tcg-target.c +29
-0@@ -249,6 +249,7 @@ static inline int tcg_target_const_match(tcg_target_long val,#define OPC_ADD_GvEv (OPC_ARITH_GvEv | (ARITH_ADD << 3))#define OPC_BSWAP (0xc8 | P_EXT)#define OPC_CALL_Jz (0xe8) +#define OPC_CMOVCC (0x40 | P_EXT)/* ... plus condition code */ #define OPC_CMP_GvEv (OPC_ARITH_GvEv | (ARITH_CMP << 3))#define OPC_DEC_r32 (0x48)#define OPC_IMUL_GvEv (0xaf | P_EXT)@@ -936,6 +937,24 @@ static void tcg_out_setcond2(TCGContext *s, const TCGArg *args, }#endif +static void tcg_out_movcond32 (TCGContext *s,TCGCond cond,TCGArg dest, +TCGArg c1,TCGArg c2,int const_c2, +TCGArg v1) +{ +tcg_out_cmp (s, c1, c2, const_c2,0 ); +tcg_out_modrm (s,OPC_CMOVCC | tcg_cond_to_jcc[cond], dest, v1); +} + +#if TCG_TARGET_REG_BITS ==64 +static void tcg_out_movcond64 (TCGContext *s,TCGCond cond,TCGArg dest, +TCGArg c1,TCGArg c2,int const_c2, +TCGArg v1) +{ +tcg_out_cmp (s, c1, c2, const_c2,P_REXW ); +tcg_out_modrm (s,OPC_CMOVCC | tcg_cond_to_jcc[cond] |P_REXW , dest, v1); +} +#endif +static void tcg_out_branch (TCGContext *s,int call,tcg_target_long dest) {tcg_target_long disp = dest - (tcg_target_long )s->code_ptr -5 ;@@ -1668,6 +1687,10 @@ static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,tcg_out_setcond32 (s, args[3 ], args[0 ], args[1 ], args[2 ], const_args[2 ]);break ; +case INDEX_op_movcond_i32: +tcg_out_movcond32 (s, args[5 ], args[0 ], args[1 ], + args[2 ], const_args[2 ], args[3 ]); +break ; OP_32_64(bswap16):tcg_out_rolw_8 (s ,args [0 ]);@@ -1796,6 +1819,10 @@ static inline void tcg_out_op(TCGContext *s, TCGOpcode opc,tcg_out_setcond64 (s, args[3 ], args[0 ], args[1 ], args[2 ], const_args[2 ]);break ; +case INDEX_op_movcond_i64: +tcg_out_movcond64 (s, args[5 ], args[0 ], args[1 ], + args[2 ], const_args[2 ], args[3 ]); +break ;case INDEX_op_bswap64_i64:tcg_out_bswap64 (s, args[0 ]);@@ -1880,6 +1907,7 @@ static const TCGTargetOpDef x86_op_defs[] = { { INDEX_op_setcond_i32, { "q", "r", "ri" } }, { INDEX_op_deposit_i32, { "Q", "0", "Q" } }, + { INDEX_op_movcond_i32, { "r", "r", "ri", "r", "0" } }, #if TCG_TARGET_REG_BITS == 32 { INDEX_op_mulu2_i32, { "a", "d", "a", "r" } },@@ -1934,6 +1962,7 @@ static const TCGTargetOpDef x86_op_defs[] = { { INDEX_op_ext32u_i64, { "r", "r" } }, { INDEX_op_deposit_i64, { "Q", "0", "Q" } }, + { INDEX_op_movcond_i64, { "r", "r", "re", "r", "0" } }, #endif #if TCG_TARGET_REG_BITS == 64tcg/i386/tcg-target.h +6
-2@@ -86,7 +86,12 @@ typedef enum {#define TCG_TARGET_HAS_nand_i32 0#define TCG_TARGET_HAS_nor_i32 0#define TCG_TARGET_HAS_deposit_i32 1 +#if defined(__x86_64__) || defined(__i686__) +/* Use cmov only if the compiler is already doing so. */ +#define TCG_TARGET_HAS_movcond_i32 1 +#else #define TCG_TARGET_HAS_movcond_i32 0 +#endif #if TCG_TARGET_REG_BITS ==64 #define TCG_TARGET_HAS_div2_i64 1@@ -108,7 +113,7 @@ typedef enum {#define TCG_TARGET_HAS_nand_i64 0#define TCG_TARGET_HAS_nor_i64 0#define TCG_TARGET_HAS_deposit_i64 1 -#define TCG_TARGET_HAS_movcond_i64 0 +#define TCG_TARGET_HAS_movcond_i64 1#endif #define TCG_TARGET_deposit_i32_valid (ofs, len) \ --1.7.11.4
Re: [Qemu-devel] [PATCH 3/7] tcg-i386: Implement movcond
Quoted 10 lines · Alex Barcelo
just finished a git-bisect and I found this... and now I do not fully understand why I have the problem.
To replicate the error (in a i386 machine, at least):
$ make clean && ./configure --enable-debug && make -j && make install [Note: I tried both ppc and i386 targets, so doesn't seem machine-dependent] $ ./path/to/qemu/bin/qemu-i386 doesntmatter Invalid op definition for movcond_i32 /mnt/DATA/DARCO/qemu-git/tcg/tcg.c:1170: tcg fatal error Aborted
Ah, right. I only tried with -march=i686.
Quoted 1 line
Quoted 1 line
+ {
INDEX_op_movcond_i32, { "r", "r", "ri", "r", "0" } },
Should be protected by
#if TCG_TARGET_HAS_movcond_i32
If no one beats me to this, I'll submit a patch this evening.
r~
Aurelien Jarno
AppliedRe: [Qemu-devel] [PATCH 3/7] tcg-i386: Implement movcond
Quoted 14 lines · Richard Henderson
Quoted 10 lines · Alex Barcelo
just finished a git-bisect and I found this... and now I do not fully understand why I have the problem.
To replicate the error (in a i386 machine, at least):
$ make clean && ./configure --enable-debug && make -j && make install [Note: I tried both ppc and i386 targets, so doesn't seem machine-dependent] $ ./path/to/qemu/bin/qemu-i386 doesntmatter Invalid op definition for movcond_i32 /mnt/DATA/DARCO/qemu-git/tcg/tcg.c:1170: tcg fatal error AbortedAh, right. I only tried with -march=i686.
Quoted 9 lines
Quoted 1 line
Quoted 1 line
+ {
INDEX_op_movcond_i32, { "r", "r", "ri", "r", "0" } },Should be protected by
#if
TCG_TARGET_HAS_movcond_i32If no one beats me to this, I'll submit a patch this evening.
I have applied the following patch to fix the issue.
commit f813cb838f19ee8637d3c365659e6a6bb0c9c974
Author: Aurelien Jarno <[email protected]> Date: Wed Sep 26 00:30:12 2012 +0200
tcg/i386: fix build with -march < i686
The movcond_i32 op has to be protected with TCG_TARGET_HAS_movcond_i32 to fix the build with -march < i686.
Thanks to Richard Henderson for the hint.
tcg/i386/tcg-target.c +2 -0
@@ -1893,7 +1893,9 @@ static const TCGTargetOpDef x86_op_defs[] = { { INDEX_op_setcond_i32, { "q", "r", "ri" } }, { INDEX_op_deposit_i32, { "Q", "0", "Q" } }, +#if TCG_TARGET_HAS_movcond_i32 { INDEX_op_movcond_i32, { "r", "r", "ri", "r", "0" } }, +#endif #if TCG_TARGET_REG_BITS == 32 { INDEX_op_mulu2_i32, { "a", "d", "a", "r" } },
Signature · 2 lines
Aurelien Jarno GPG: 1024D/F1BCDB73 [email protected] http://www.aurel32.net