TCG new op: setcond
RFC 1/1 TCG new op: setcond
Hello,
this patch implements a new TCG op, setcond, that sets a temp to 1 if the condition is true, else to 0. The benefit is the potential removal of brcond instructions, and helpers size reduction which can lead to using TCG instead of helpers.
setcond(i)_i{32,64} have been implemented only for x86_64 and i386 TCG back-ends.
One ARM helper was converted to TCG using setcond. Alpha and MIPS are also patched to use setcond.
On my TODO list:
- implement it in all backends (arm, ppc, ppc64, sparc)
- use it at least once in every frontend
Comments starting with // in the patch are questions and/or TODO.
Please feel free to comment.
Laurent
translate.c +3 -13
@@ -504,30 +504,20 @@ int ra, int rb, int rc, int islit, uint8_t lit) { - int l1, l2; TCGv tmp;if (unlikely (rc ==31 )) -return ; +return ; - l1 =gen_new_label (); - l2 =gen_new_label (); -if (ra !=31 ) { tmp =tcg_temp_new (TCG_TYPE_I64 );tcg_gen_mov_i64 (tmp, cpu_ir[ra]); }else tmp =tcg_const_i64 (0 );if (islit) -tcg_gen_brcondi_i64 (cond, tmp, lit, l1); +tcg_gen_setcondi_i64 (cond, cpu_ir[rc], tmp, lit);else -tcg_gen_brcond_i64 (cond, tmp, cpu_ir[rb], l1); - -tcg_gen_movi_i64 (cpu_ir[rc],0 ); -tcg_gen_br (l2); -gen_set_label (l1); -tcg_gen_movi_i64 (cpu_ir[rc],1 ); -gen_set_label (l2); +tcg_gen_setcond_i64 (cond, cpu_ir[rc], tmp, cpu_ir[rb]); } static always_inline int translate_one (DisasContext *ctx, uint32_t insn)
translate.c +3 -27
@@ -771,15 +771,7 @@#define OP_COND (name, cond) \ static inline void glue(gen_op_, name) (TCGv t0, TCGv t1) \ { \ - int l1 = gen_new_label(); \ - int l2 = gen_new_label(); \ - \ - tcg_gen_brcond_tl(cond, t0, t1, l1); \ - tcg_gen_movi_tl(t0, 0); \ - tcg_gen_br(l2); \ - gen_set_label(l1); \ - tcg_gen_movi_tl(t0, 1); \ - gen_set_label(l2); \ + tcg_gen_setcond_tl(cond, t0, t0, t1); \ }OP_COND (eq,TCG_COND_EQ );OP_COND (ne,TCG_COND_NE );
@@ -792,15 +784,7 @@#define OP_CONDI (name, cond) \ static inline void glue(gen_op_, name) (TCGv t, target_ulong val) \ { \ - int l1 = gen_new_label(); \ - int l2 = gen_new_label(); \ - \ - tcg_gen_brcondi_tl(cond, t, val, l1); \ - tcg_gen_movi_tl(t, 0); \ - tcg_gen_br(l2); \ - gen_set_label(l1); \ - tcg_gen_movi_tl(t, 1); \ - gen_set_label(l2); \ + tcg_gen_setcondi_tl(cond, t, t, val); \ }OP_CONDI (lti,TCG_COND_LT );OP_CONDI (ltiu,TCG_COND_LTU );
@@ -809,15 +793,7 @@#define OP_CONDZ (name, cond) \ static inline void glue(gen_op_, name) (TCGv t) \ { \ - int l1 = gen_new_label(); \ - int l2 = gen_new_label(); \ - \ - tcg_gen_brcondi_tl(cond, t, 0, l1); \ - tcg_gen_movi_tl(t, 0); \ - tcg_gen_br(l2); \ - gen_set_label(l1); \ - tcg_gen_movi_tl(t, 1); \ - gen_set_label(l2); \ + tcg_gen_setcondi_tl(cond, t, t, 0); \ }OP_CONDZ (gez,TCG_COND_GE );OP_CONDZ (gtz,TCG_COND_GT );
tcg-op.h +56 -1
105 lines not on this page · raw message
tcg-opc.h +5 -0
@@ -76,10 +76,13 @@ DEF2(shr_i32, 1, 2, 0, 0) DEF2(sar_i32, 1, 2, 0, 0) +DEF2(setcond_i32, 1, 2, 1, 0) + DEF2(brcond_i32, 0, 2, 2, TCG_OPF_BB_END | TCG_OPF_SIDE_EFFECTS)#if TCG_TARGET_REG_BITS ==32 DEF2(add2_i32, 2, 4, 0, 0) DEF2(sub2_i32, 2, 4, 0, 0) +DEF2(setcond2_i32, 2, 4, 1, 0) DEF2(brcond2_i32, 0, 4, 2, TCG_OPF_BB_END | TCG_OPF_SIDE_EFFECTS) DEF2(mulu2_i32, 2, 2, 0, 0)#endif
@@ -129,6 +132,8 @@ DEF2(shr_i64, 1, 2, 0, 0) DEF2(sar_i64, 1, 2, 0, 0) +DEF2(setcond_i64, 1, 2, 1, 0) + DEF2(brcond_i64, 0, 2, 2, TCG_OPF_BB_END | TCG_OPF_SIDE_EFFECTS)#ifdef TCG_TARGET_HAS_ext8s_i64DEF2 (ext8s_i64,1 ,1 ,0 ,0 )
i386/tcg-target.c +120 -0
144 lines not on this page · raw message
README +4 -0
@@ -277,6 +277,10 @@ 64 bit byte swap +* setcond_i32/i64 cond, t0, t1, t2 + +Set t0 to 1 if t1 cond t2 is true, else t0 is set to 0. (cf brcond for cond.) + * discard_i32/i64 t0 Indicate that the value of t0 won't be used later. It is useful to
tcg.c +6 -0
@@ -877,6 +877,12 @@ #elif TCG_TARGET_REG_BITS == 64 || c == INDEX_op_brcond_i64#endif + || c == INDEX_op_setcond_i32 +#if TCG_TARGET_REG_BITS == 32 + || c == INDEX_op_setcond2_i32 +#elif TCG_TARGET_REG_BITS == 64 + || c == INDEX_op_setcond_i64 +#endif ) {if (args[k] <ARRAY_SIZE (cond_name) && cond_name[args[k]])fprintf (outfile,",%s" , cond_name[args[k++]]);
x86_64/tcg-target.c +35 -0
@@ -198,6 +198,7 @@#define SHIFT_SHR 5#define SHIFT_SAR 7 +/* The following defines apply to setcc instruction too. */ #define JCC_JMP (-1)#define JCC_JO 0x0#define JCC_JNO 0x1
@@ -464,6 +465,26 @@ } } +// TODO const_arg optimization +static void tcg_out_setcond (TCGContext *s,int cond, +TCGArg ret,TCGArg arg1,TCGArg arg2,int rexw) +{ +/* cmp */ +tcg_out_modrm (s,0x01 | (ARITH_CMP <<3 ) | rexw, arg2, arg1); +/* setcc */ +// TODO this should use tcg_out_modrm +// however currently tcg_out_modrm outputs an extra byte for [abcd]l +//tcg_out_modrm(s, (0x90 + tcg_cond_to_jcc[cond]) | P_EXT | P_REXB, ret, 0); +if (ret >3 ) +tcg_out8 (s,0x40 + (ret >>3 )); +tcg_out8 (s,0x0f ); +tcg_out8 (s,0x90 + tcg_cond_to_jcc[cond]); +tcg_out8 (s,0xc0 + (ret &7 )); +/* zero extend ret with movzbl since setcc only sets the lower 8 bits */ +// TODO that doesn't look pretty +tcg_out_modrm (s,0xb6 |P_EXT | (rexw ? rexw :P_REXB ), ret, ret); +} +static void tcg_out_brcond (TCGContext *s,int cond,TCGArg arg1,TCGArg arg2,int const_arg2,int label_index,int rexw)
@@ -1065,6 +1086,14 @@ c =SHIFT_SAR ; goto gen_shift64; +case INDEX_op_setcond_i32: +tcg_out_setcond (s, args[3 ], args[0 ], args[1 ], args[2 ],0 ); +break ; + +case INDEX_op_setcond_i64: +tcg_out_setcond (s, args[3 ], args[0 ], args[1 ], args[2 ],P_REXW ); +break ; +case INDEX_op_brcond_i32:tcg_out_brcond (s, args[2 ], args[0 ], args[1 ], const_args[1 ], args[3 ],0 );
@@ -1225,6 +1254,9 @@ { INDEX_op_shr_i32, { "r", "0", "ci" } }, { INDEX_op_sar_i32, { "r", "0", "ci" } }, +// TODO add 'i' to third "r" (second input argument) + { INDEX_op_setcond_i32, { "r", "r", "r" } }, + { INDEX_op_brcond_i32, { "r", "ri" } }, { INDEX_op_mov_i64, { "r", "r" } },
@@ -1254,6 +1286,9 @@ { INDEX_op_shr_i64, { "r", "0", "ci" } }, { INDEX_op_sar_i64, { "r", "0", "ci" } }, +// TODO add 'i' to third "r" (second input argument) + { INDEX_op_setcond_i64, { "r", "r", "r" } }, + { INDEX_op_brcond_i64, { "r", "re" } }, { INDEX_op_bswap_i32, { "r", "0" } },
translate.c +30 -1
@@ -201,7 +201,6 @@#define gen_op_addl_T0_T1_cc () gen_helper_add_cc(cpu_T[0], cpu_T[0], cpu_T[1])#define gen_op_adcl_T0_T1_cc () gen_helper_adc_cc(cpu_T[0], cpu_T[0], cpu_T[1]) -#define gen_op_subl_T0_T1_cc () gen_helper_sub_cc(cpu_T[0], cpu_T[0], cpu_T[1])#define gen_op_sbcl_T0_T1_cc () gen_helper_sbc_cc(cpu_T[0], cpu_T[0], cpu_T[1])#define gen_op_rsbl_T0_T1_cc () gen_helper_sub_cc(cpu_T[0], cpu_T[1], cpu_T[0])#define gen_op_rscl_T0_T1_cc () gen_helper_sbc_cc(cpu_T[0], cpu_T[1], cpu_T[0])
@@ -243,6 +242,36 @@dead_tmp (tmp); } +//#define gen_op_subl_T0_T1_cc() gen_helper_sub_cc(cpu_T[0], cpu_T[0], cpu_T[1]) +static inline void gen_op_subl_T0_T1_cc (void ) +{ +TCGv tmp32_res; +TCGv tmp1; +TCGv tmp2; + + tmp32_res =new_tmp (); +tcg_gen_sub_i32 (tmp32_res, cpu_T[0 ], cpu_T[1 ]); +tcg_gen_st_i32 (tmp32_res, cpu_env, offsetof(CPUState , NF)); +tcg_gen_st_i32 (tmp32_res, cpu_env, offsetof(CPUState , ZF)); + + tmp1 =new_tmp (); + +tcg_gen_setcond_i32 (TCG_COND_GEU , tmp1, cpu_T[0 ], cpu_T[1 ]); +tcg_gen_st_i32 (tmp1, cpu_env, offsetof(CPUState , CF)); + + tmp2 =new_tmp (); +tcg_gen_xor_i32 (tmp1, cpu_T[0 ], cpu_T[1 ]); +tcg_gen_xor_i32 (tmp2, cpu_T[0 ], tmp32_res); +tcg_gen_and_i32 (tmp1, tmp1, tmp2); +dead_tmp (tmp2); +tcg_gen_st_i32 (tmp1, cpu_env, offsetof(CPUState , VF)); +dead_tmp (tmp1); + +tcg_gen_mov_i32 (cpu_T[0 ], tmp32_res); + +dead_tmp (tmp32_res); +} +static void gen_smul_dual (TCGv a,TCGv b) {TCGv tmp1 =new_tmp ();
Same patch with a fix shown by DEBUG_TCGV. Thanks to Aurelien.
Laurent
Quoted 20 lines · Laurent Desnogues
this patch implements a new TCG op, setcond, that sets a temp to 1 if the condition is true, else to 0. The benefit is the potential removal of brcond instructions, and helpers size reduction which can lead to using TCG instead of helpers.
setcond(i)_i{32,64} have been implemented only for
x86_64and i386 TCG back-ends.One ARM helper was converted to TCG using setcond. Alpha and MIPS are also patched to use setcond.
On my TODO list:
- implement it in all backends (arm, ppc, ppc64, sparc)
- use it at least once in every frontendComments starting with // in the patch are questions and/or TODO.
Quoted 7 lines
Please feel free to comment.
Laurent
Signed-off-by Laurent Desnogues <[email protected]>
translate.c +3 -13
@@ -504,30 +504,20 @@ int ra, int rb, int rc, int islit, uint8_t lit) { - int l1, l2; TCGv tmp;if (unlikely (rc ==31 )) -return ; +return ; - l1 =gen_new_label (); - l2 =gen_new_label (); -if (ra !=31 ) { tmp =tcg_temp_new (TCG_TYPE_I64 );tcg_gen_mov_i64 (tmp, cpu_ir[ra]); }else tmp =tcg_const_i64 (0 );if (islit) -tcg_gen_brcondi_i64 (cond, tmp, lit, l1); +tcg_gen_setcondi_i64 (cond, cpu_ir[rc], tmp, lit);else -tcg_gen_brcond_i64 (cond, tmp, cpu_ir[rb], l1); - -tcg_gen_movi_i64 (cpu_ir[rc],0 ); -tcg_gen_br (l2); -gen_set_label (l1); -tcg_gen_movi_i64 (cpu_ir[rc],1 ); -gen_set_label (l2); +tcg_gen_setcond_i64 (cond, cpu_ir[rc], tmp, cpu_ir[rb]); } static always_inline int translate_one (DisasContext *ctx, uint32_t insn)
translate.c +3 -27
@@ -771,15 +771,7 @@#define OP_COND (name, cond) \ static inline void glue(gen_op_, name) (TCGv t0, TCGv t1) \ { \ - int l1 = gen_new_label(); \ - int l2 = gen_new_label(); \ - \ - tcg_gen_brcond_tl(cond, t0, t1, l1); \ - tcg_gen_movi_tl(t0, 0); \ - tcg_gen_br(l2); \ - gen_set_label(l1); \ - tcg_gen_movi_tl(t0, 1); \ - gen_set_label(l2); \ + tcg_gen_setcond_tl(cond, t0, t0, t1); \ }OP_COND (eq,TCG_COND_EQ );OP_COND (ne,TCG_COND_NE );
@@ -792,15 +784,7 @@#define OP_CONDI (name, cond) \ static inline void glue(gen_op_, name) (TCGv t, target_ulong val) \ { \ - int l1 = gen_new_label(); \ - int l2 = gen_new_label(); \ - \ - tcg_gen_brcondi_tl(cond, t, val, l1); \ - tcg_gen_movi_tl(t, 0); \ - tcg_gen_br(l2); \ - gen_set_label(l1); \ - tcg_gen_movi_tl(t, 1); \ - gen_set_label(l2); \ + tcg_gen_setcondi_tl(cond, t, t, val); \ }OP_CONDI (lti,TCG_COND_LT );OP_CONDI (ltiu,TCG_COND_LTU );
@@ -809,15 +793,7 @@#define OP_CONDZ (name, cond) \ static inline void glue(gen_op_, name) (TCGv t) \ { \ - int l1 = gen_new_label(); \ - int l2 = gen_new_label(); \ - \ - tcg_gen_brcondi_tl(cond, t, 0, l1); \ - tcg_gen_movi_tl(t, 0); \ - tcg_gen_br(l2); \ - gen_set_label(l1); \ - tcg_gen_movi_tl(t, 1); \ - gen_set_label(l2); \ + tcg_gen_setcondi_tl(cond, t, t, 0); \ }OP_CONDZ (gez,TCG_COND_GE );OP_CONDZ (gtz,TCG_COND_GT );
tcg-op.h +56 -1
105 lines not on this page · raw message
tcg-opc.h +5 -0
@@ -76,10 +76,13 @@ DEF2(shr_i32, 1, 2, 0, 0) DEF2(sar_i32, 1, 2, 0, 0) +DEF2(setcond_i32, 1, 2, 1, 0) + DEF2(brcond_i32, 0, 2, 2, TCG_OPF_BB_END | TCG_OPF_SIDE_EFFECTS)#if TCG_TARGET_REG_BITS ==32 DEF2(add2_i32, 2, 4, 0, 0) DEF2(sub2_i32, 2, 4, 0, 0) +DEF2(setcond2_i32, 2, 4, 1, 0) DEF2(brcond2_i32, 0, 4, 2, TCG_OPF_BB_END | TCG_OPF_SIDE_EFFECTS) DEF2(mulu2_i32, 2, 2, 0, 0)#endif
@@ -129,6 +132,8 @@ DEF2(shr_i64, 1, 2, 0, 0) DEF2(sar_i64, 1, 2, 0, 0) +DEF2(setcond_i64, 1, 2, 1, 0) + DEF2(brcond_i64, 0, 2, 2, TCG_OPF_BB_END | TCG_OPF_SIDE_EFFECTS)#ifdef TCG_TARGET_HAS_ext8s_i64DEF2 (ext8s_i64,1 ,1 ,0 ,0 )
i386/tcg-target.c +120 -0
144 lines not on this page · raw message
README +4 -0
@@ -277,6 +277,10 @@ 64 bit byte swap +* setcond_i32/i64 cond, t0, t1, t2 + +Set t0 to 1 if t1 cond t2 is true, else t0 is set to 0. (cf brcond for cond.) + * discard_i32/i64 t0 Indicate that the value of t0 won't be used later. It is useful to
tcg.c +6 -0
@@ -877,6 +877,12 @@ #elif TCG_TARGET_REG_BITS == 64 || c == INDEX_op_brcond_i64#endif + || c == INDEX_op_setcond_i32 +#if TCG_TARGET_REG_BITS == 32 + || c == INDEX_op_setcond2_i32 +#elif TCG_TARGET_REG_BITS == 64 + || c == INDEX_op_setcond_i64 +#endif ) {if (args[k] <ARRAY_SIZE (cond_name) && cond_name[args[k]])fprintf (outfile,",%s" , cond_name[args[k++]]);
x86_64/tcg-target.c +35 -0
@@ -198,6 +198,7 @@#define SHIFT_SHR 5#define SHIFT_SAR 7 +/* The following defines apply to setcc instruction too. */ #define JCC_JMP (-1)#define JCC_JO 0x0#define JCC_JNO 0x1
@@ -464,6 +465,26 @@ } } +// TODO const_arg optimization +static void tcg_out_setcond (TCGContext *s,int cond, +TCGArg ret,TCGArg arg1,TCGArg arg2,int rexw) +{ +/* cmp */ +tcg_out_modrm (s,0x01 | (ARITH_CMP <<3 ) | rexw, arg2, arg1); +/* setcc */ +// TODO this should use tcg_out_modrm +// however currently tcg_out_modrm outputs an extra byte for [abcd]l +//tcg_out_modrm(s, (0x90 + tcg_cond_to_jcc[cond]) | P_EXT | P_REXB, ret, 0); +if (ret >3 ) +tcg_out8 (s,0x40 + (ret >>3 )); +tcg_out8 (s,0x0f ); +tcg_out8 (s,0x90 + tcg_cond_to_jcc[cond]); +tcg_out8 (s,0xc0 + (ret &7 )); +/* zero extend ret with movzbl since setcc only sets the lower 8 bits */ +// TODO that doesn't look pretty +tcg_out_modrm (s,0xb6 |P_EXT | (rexw ? rexw :P_REXB ), ret, ret); +} +static void tcg_out_brcond (TCGContext *s,int cond,TCGArg arg1,TCGArg arg2,int const_arg2,int label_index,int rexw)
@@ -1065,6 +1086,14 @@ c =SHIFT_SAR ; goto gen_shift64; +case INDEX_op_setcond_i32: +tcg_out_setcond (s, args[3 ], args[0 ], args[1 ], args[2 ],0 ); +break ; + +case INDEX_op_setcond_i64: +tcg_out_setcond (s, args[3 ], args[0 ], args[1 ], args[2 ],P_REXW ); +break ; +case INDEX_op_brcond_i32:tcg_out_brcond (s, args[2 ], args[0 ], args[1 ], const_args[1 ], args[3 ],0 );
@@ -1225,6 +1254,9 @@ { INDEX_op_shr_i32, { "r", "0", "ci" } }, { INDEX_op_sar_i32, { "r", "0", "ci" } }, +// TODO add 'i' to third "r" (second input argument) + { INDEX_op_setcond_i32, { "r", "r", "r" } }, + { INDEX_op_brcond_i32, { "r", "ri" } }, { INDEX_op_mov_i64, { "r", "r" } },
@@ -1254,6 +1286,9 @@ { INDEX_op_shr_i64, { "r", "0", "ci" } }, { INDEX_op_sar_i64, { "r", "0", "ci" } }, +// TODO add 'i' to third "r" (second input argument) + { INDEX_op_setcond_i64, { "r", "r", "r" } }, + { INDEX_op_brcond_i64, { "r", "re" } }, { INDEX_op_bswap_i32, { "r", "0" } },
translate.c +30 -1
@@ -201,7 +201,6 @@#define gen_op_addl_T0_T1_cc () gen_helper_add_cc(cpu_T[0], cpu_T[0], cpu_T[1])#define gen_op_adcl_T0_T1_cc () gen_helper_adc_cc(cpu_T[0], cpu_T[0], cpu_T[1]) -#define gen_op_subl_T0_T1_cc () gen_helper_sub_cc(cpu_T[0], cpu_T[0], cpu_T[1])#define gen_op_sbcl_T0_T1_cc () gen_helper_sbc_cc(cpu_T[0], cpu_T[0], cpu_T[1])#define gen_op_rsbl_T0_T1_cc () gen_helper_sub_cc(cpu_T[0], cpu_T[1], cpu_T[0])#define gen_op_rscl_T0_T1_cc () gen_helper_sbc_cc(cpu_T[0], cpu_T[1], cpu_T[0])
@@ -243,6 +242,36 @@dead_tmp (tmp); } +//#define gen_op_subl_T0_T1_cc() gen_helper_sub_cc(cpu_T[0], cpu_T[0], cpu_T[1]) +static inline void gen_op_subl_T0_T1_cc (void ) +{ +TCGv tmp32_res; +TCGv tmp1; +TCGv tmp2; + + tmp32_res =new_tmp (); +tcg_gen_sub_i32 (tmp32_res, cpu_T[0 ], cpu_T[1 ]); +tcg_gen_st_i32 (tmp32_res, cpu_env, offsetof(CPUState , NF)); +tcg_gen_st_i32 (tmp32_res, cpu_env, offsetof(CPUState , ZF)); + + tmp1 =new_tmp (); + +tcg_gen_setcond_i32 (TCG_COND_GEU , tmp1, cpu_T[0 ], cpu_T[1 ]); +tcg_gen_st_i32 (tmp1, cpu_env, offsetof(CPUState , CF)); + + tmp2 =new_tmp (); +tcg_gen_xor_i32 (tmp1, cpu_T[0 ], cpu_T[1 ]); +tcg_gen_xor_i32 (tmp2, cpu_T[0 ], tmp32_res); +tcg_gen_and_i32 (tmp1, tmp1, tmp2); +dead_tmp (tmp2); +tcg_gen_st_i32 (tmp1, cpu_env, offsetof(CPUState , VF)); +dead_tmp (tmp1); + +tcg_gen_mov_i32 (cpu_T[0 ], tmp32_res); + +dead_tmp (tmp32_res); +} +static void gen_smul_dual (TCGv a,TCGv b) {TCGv tmp1 =new_tmp ();
And again another copy/paste error spotted by Stuart. Sorry for all that.
Laurent
Quoted 37 lines · Laurent Desnogues
Same patch with a fix shown by
DEBUG_TCGV. Thanks to Aurelien.Laurent
Signed-off-by Laurent Desnogues <[email protected]>Quoted 27 lines · Laurent Desnogues
this patch implements a new TCG op, setcond, that sets a temp to 1 if the condition is true, else to 0. The benefit is the potential removal of brcond instructions, and helpers size reduction which can lead to using TCG instead of helpers.
setcond(i)_i{32,64} have been implemented only for
x86_64and i386 TCG back-ends.One ARM helper was converted to TCG using setcond. Alpha and MIPS are also patched to use setcond.
On my TODO list:
- implement it in all backends (arm, ppc, ppc64, sparc)
- use it at least once in every frontendComments starting with // in the patch are questions and/or TODO.
Please feel free to comment.
Laurent
Signed-off-by Laurent Desnogues <[email protected]>
translate.c +3 -13
@@ -504,30 +504,20 @@ int ra, int rb, int rc, int islit, uint8_t lit) { - int l1, l2; TCGv tmp;if (unlikely (rc ==31 )) -return ; +return ; - l1 =gen_new_label (); - l2 =gen_new_label (); -if (ra !=31 ) { tmp =tcg_temp_new (TCG_TYPE_I64 );tcg_gen_mov_i64 (tmp, cpu_ir[ra]); }else tmp =tcg_const_i64 (0 );if (islit) -tcg_gen_brcondi_i64 (cond, tmp, lit, l1); +tcg_gen_setcondi_i64 (cond, cpu_ir[rc], tmp, lit);else -tcg_gen_brcond_i64 (cond, tmp, cpu_ir[rb], l1); - -tcg_gen_movi_i64 (cpu_ir[rc],0 ); -tcg_gen_br (l2); -gen_set_label (l1); -tcg_gen_movi_i64 (cpu_ir[rc],1 ); -gen_set_label (l2); +tcg_gen_setcond_i64 (cond, cpu_ir[rc], tmp, cpu_ir[rb]); } static always_inline int translate_one (DisasContext *ctx, uint32_t insn)
translate.c +3 -27
@@ -771,15 +771,7 @@#define OP_COND (name, cond) \ static inline void glue(gen_op_, name) (TCGv t0, TCGv t1) \ { \ - int l1 = gen_new_label(); \ - int l2 = gen_new_label(); \ - \ - tcg_gen_brcond_tl(cond, t0, t1, l1); \ - tcg_gen_movi_tl(t0, 0); \ - tcg_gen_br(l2); \ - gen_set_label(l1); \ - tcg_gen_movi_tl(t0, 1); \ - gen_set_label(l2); \ + tcg_gen_setcond_tl(cond, t0, t0, t1); \ }OP_COND (eq,TCG_COND_EQ );OP_COND (ne,TCG_COND_NE );
@@ -792,15 +784,7 @@#define OP_CONDI (name, cond) \ static inline void glue(gen_op_, name) (TCGv t, target_ulong val) \ { \ - int l1 = gen_new_label(); \ - int l2 = gen_new_label(); \ - \ - tcg_gen_brcondi_tl(cond, t, val, l1); \ - tcg_gen_movi_tl(t, 0); \ - tcg_gen_br(l2); \ - gen_set_label(l1); \ - tcg_gen_movi_tl(t, 1); \ - gen_set_label(l2); \ + tcg_gen_setcondi_tl(cond, t, t, val); \ }OP_CONDI (lti,TCG_COND_LT );OP_CONDI (ltiu,TCG_COND_LTU );
@@ -809,15 +793,7 @@#define OP_CONDZ (name, cond) \ static inline void glue(gen_op_, name) (TCGv t) \ { \ - int l1 = gen_new_label(); \ - int l2 = gen_new_label(); \ - \ - tcg_gen_brcondi_tl(cond, t, 0, l1); \ - tcg_gen_movi_tl(t, 0); \ - tcg_gen_br(l2); \ - gen_set_label(l1); \ - tcg_gen_movi_tl(t, 1); \ - gen_set_label(l2); \ + tcg_gen_setcondi_tl(cond, t, t, 0); \ }OP_CONDZ (gez,TCG_COND_GE );OP_CONDZ (gtz,TCG_COND_GT );
tcg-op.h +56 -1
105 lines not on this page · raw message
tcg-opc.h +5 -0
@@ -76,10 +76,13 @@ DEF2(shr_i32, 1, 2, 0, 0) DEF2(sar_i32, 1, 2, 0, 0) +DEF2(setcond_i32, 1, 2, 1, 0) + DEF2(brcond_i32, 0, 2, 2, TCG_OPF_BB_END | TCG_OPF_SIDE_EFFECTS)#if TCG_TARGET_REG_BITS ==32 DEF2(add2_i32, 2, 4, 0, 0) DEF2(sub2_i32, 2, 4, 0, 0) +DEF2(setcond2_i32, 2, 4, 1, 0) DEF2(brcond2_i32, 0, 4, 2, TCG_OPF_BB_END | TCG_OPF_SIDE_EFFECTS) DEF2(mulu2_i32, 2, 2, 0, 0)#endif
@@ -129,6 +132,8 @@ DEF2(shr_i64, 1, 2, 0, 0) DEF2(sar_i64, 1, 2, 0, 0) +DEF2(setcond_i64, 1, 2, 1, 0) + DEF2(brcond_i64, 0, 2, 2, TCG_OPF_BB_END | TCG_OPF_SIDE_EFFECTS)#ifdef TCG_TARGET_HAS_ext8s_i64DEF2 (ext8s_i64,1 ,1 ,0 ,0 )
i386/tcg-target.c +120 -0
144 lines not on this page · raw message
README +4 -0
@@ -277,6 +277,10 @@ 64 bit byte swap +* setcond_i32/i64 cond, t0, t1, t2 + +Set t0 to 1 if t1 cond t2 is true, else t0 is set to 0. (cf brcond for cond.) + * discard_i32/i64 t0 Indicate that the value of t0 won't be used later. It is useful to
tcg.c +6 -0
@@ -877,6 +877,12 @@ #elif TCG_TARGET_REG_BITS == 64 || c == INDEX_op_brcond_i64#endif + || c == INDEX_op_setcond_i32 +#if TCG_TARGET_REG_BITS == 32 + || c == INDEX_op_setcond2_i32 +#elif TCG_TARGET_REG_BITS == 64 + || c == INDEX_op_setcond_i64 +#endif ) {if (args[k] <ARRAY_SIZE (cond_name) && cond_name[args[k]])fprintf (outfile,",%s" , cond_name[args[k++]]);
x86_64/tcg-target.c +35 -0
@@ -198,6 +198,7 @@#define SHIFT_SHR 5#define SHIFT_SAR 7 +/* The following defines apply to setcc instruction too. */ #define JCC_JMP (-1)#define JCC_JO 0x0#define JCC_JNO 0x1
@@ -464,6 +465,26 @@ } } +// TODO const_arg optimization +static void tcg_out_setcond (TCGContext *s,int cond, +TCGArg ret,TCGArg arg1,TCGArg arg2,int rexw) +{ +/* cmp */ +tcg_out_modrm (s,0x01 | (ARITH_CMP <<3 ) | rexw, arg2, arg1); +/* setcc */ +// TODO this should use tcg_out_modrm +// however currently tcg_out_modrm outputs an extra byte for [abcd]l +//tcg_out_modrm(s, (0x90 + tcg_cond_to_jcc[cond]) | P_EXT | P_REXB, ret, 0); +if (ret >3 ) +tcg_out8 (s,0x40 + (ret >>3 )); +tcg_out8 (s,0x0f ); +tcg_out8 (s,0x90 + tcg_cond_to_jcc[cond]); +tcg_out8 (s,0xc0 + (ret &7 )); +/* zero extend ret with movzbl since setcc only sets the lower 8 bits */ +// TODO that doesn't look pretty +tcg_out_modrm (s,0xb6 |P_EXT | (rexw ? rexw :P_REXB ), ret, ret); +} +static void tcg_out_brcond (TCGContext *s,int cond,TCGArg arg1,TCGArg arg2,int const_arg2,int label_index,int rexw)
@@ -1065,6 +1086,14 @@ c =SHIFT_SAR ; goto gen_shift64; +case INDEX_op_setcond_i32: +tcg_out_setcond (s, args[3 ], args[0 ], args[1 ], args[2 ],0 ); +break ; + +case INDEX_op_setcond_i64: +tcg_out_setcond (s, args[3 ], args[0 ], args[1 ], args[2 ],P_REXW ); +break ; +case INDEX_op_brcond_i32:tcg_out_brcond (s, args[2 ], args[0 ], args[1 ], const_args[1 ], args[3 ],0 );
@@ -1225,6 +1254,9 @@ { INDEX_op_shr_i32, { "r", "0", "ci" } }, { INDEX_op_sar_i32, { "r", "0", "ci" } }, +// TODO add 'i' to third "r" (second input argument) + { INDEX_op_setcond_i32, { "r", "r", "r" } }, + { INDEX_op_brcond_i32, { "r", "ri" } }, { INDEX_op_mov_i64, { "r", "r" } },
@@ -1254,6 +1286,9 @@ { INDEX_op_shr_i64, { "r", "0", "ci" } }, { INDEX_op_sar_i64, { "r", "0", "ci" } }, +// TODO add 'i' to third "r" (second input argument) + { INDEX_op_setcond_i64, { "r", "r", "r" } }, + { INDEX_op_brcond_i64, { "r", "re" } }, { INDEX_op_bswap_i32, { "r", "0" } },
translate.c +30 -1
@@ -201,7 +201,6 @@#define gen_op_addl_T0_T1_cc () gen_helper_add_cc(cpu_T[0], cpu_T[0], cpu_T[1])#define gen_op_adcl_T0_T1_cc () gen_helper_adc_cc(cpu_T[0], cpu_T[0], cpu_T[1]) -#define gen_op_subl_T0_T1_cc () gen_helper_sub_cc(cpu_T[0], cpu_T[0], cpu_T[1])#define gen_op_sbcl_T0_T1_cc () gen_helper_sbc_cc(cpu_T[0], cpu_T[0], cpu_T[1])#define gen_op_rsbl_T0_T1_cc () gen_helper_sub_cc(cpu_T[0], cpu_T[1], cpu_T[0])#define gen_op_rscl_T0_T1_cc () gen_helper_sbc_cc(cpu_T[0], cpu_T[1], cpu_T[0])
@@ -243,6 +242,36 @@dead_tmp (tmp); } +//#define gen_op_subl_T0_T1_cc() gen_helper_sub_cc(cpu_T[0], cpu_T[0], cpu_T[1]) +static inline void gen_op_subl_T0_T1_cc (void ) +{ +TCGv tmp32_res; +TCGv tmp1; +TCGv tmp2; + + tmp32_res =new_tmp (); +tcg_gen_sub_i32 (tmp32_res, cpu_T[0 ], cpu_T[1 ]); +tcg_gen_st_i32 (tmp32_res, cpu_env, offsetof(CPUState , NF)); +tcg_gen_st_i32 (tmp32_res, cpu_env, offsetof(CPUState , ZF)); + + tmp1 =new_tmp (); + +tcg_gen_setcond_i32 (TCG_COND_GEU , tmp1, cpu_T[0 ], cpu_T[1 ]); +tcg_gen_st_i32 (tmp1, cpu_env, offsetof(CPUState , CF)); + + tmp2 =new_tmp (); +tcg_gen_xor_i32 (tmp1, cpu_T[0 ], cpu_T[1 ]); +tcg_gen_xor_i32 (tmp2, cpu_T[0 ], tmp32_res); +tcg_gen_and_i32 (tmp1, tmp1, tmp2); +dead_tmp (tmp2); +tcg_gen_st_i32 (tmp1, cpu_env, offsetof(CPUState , VF)); +dead_tmp (tmp1); + +tcg_gen_mov_i32 (cpu_T[0 ], tmp32_res); + +dead_tmp (tmp32_res); +} +static void gen_smul_dual (TCGv a,TCGv b) {TCGv tmp1 =new_tmp ();