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.
This patch is only posted here to get comments before I dig further into that and propose a proper update.
One of the ARM helpers (sub with flag settings) has been converted to TCG and uses setcond to compute carry.
setcond has been implemented only for x86_64 TCG back-end.
On my TODO list:
- document setcond in tcg/README
- implement it in all backends
* arm
* hppa (not applicable according to Stuart Brady)
* i386
* ppc
* ppc64
* sparc
* x86_64 (done and partly tested)
- use it at least once in every frontend
- setcondi
- 64 bit setcond's
- a variant that sets -1 instead of 1 for masking
- provide a test
Please feel free to comment :-)
Laurent
tcg-op.h +6 -0
@@ -499,6 +499,12 @@ } } +static inline void tcg_gen_setcond_i32 (int cond,TCGv ret, +TCGv arg1,int32_t arg2) +{ +tcg_gen_op4i (INDEX_op_setcond_i32, ret, arg1, arg2, cond); +} +static inline void tcg_gen_brcond_i32 (int cond,TCGv arg1,TCGv arg2,int label_index) {
tcg-opc.h +4 -0
@@ -76,6 +76,8 @@ 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 )
@@ -129,6 +131,8 @@ DEF2(shr_i64, 1, 2, 0, 0) DEF2(sar_i64, 1, 2, 0, 0) +DEF2(setcond_i64, 1, 2, 0, 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 )
x86_64/tcg-target.c +34 -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,27 @@ } } +// TODO should apply to setcond_i64 but not tested +static void tcg_out_setcond (TCGContext *s,int cond, +TCGArg ret,TCGArg arg1,TCGArg arg2,int rexw) +{ +/* clear ret since setcc only sets the lower 8 bits */ +tcg_out_modrm (s,0x01 | (ARITH_XOR <<3 ) | rexw, ret, ret); +/* 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 ); +else if (ret >7 ) +tcg_out8 (s,0x41 ); +tcg_out8 (s,0x0f ); +tcg_out8 (s,0x90 + tcg_cond_to_jcc[cond]); +tcg_out8 (s,0xc0 + (ret &7 )); +} +static void tcg_out_brcond (TCGContext *s,int cond,TCGArg arg1,TCGArg arg2,int const_arg2,int label_index,int rexw)
@@ -1065,6 +1087,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 +1255,8 @@ { INDEX_op_shr_i32, { "r", "0", "ci" } }, { INDEX_op_sar_i32, { "r", "0", "ci" } }, + { INDEX_op_setcond_i32, { "r", "r", "r" } }, + { INDEX_op_brcond_i32, { "r", "ri" } }, { INDEX_op_mov_i64, { "r", "r" } },
@@ -1254,6 +1286,8 @@ { INDEX_op_shr_i64, { "r", "0", "ci" } }, { INDEX_op_sar_i64, { "r", "0", "ci" } }, + { INDEX_op_setcond_i64, { "r", "r", "r" } }, + { INDEX_op_brcond_i64, { "r", "re" } }, { INDEX_op_bswap_i32, { "r", "0" } },
tcg.c +2 -0
@@ -877,6 +877,8 @@ #elif TCG_TARGET_REG_BITS == 64 || c == INDEX_op_brcond_i64#endif + || c == INDEX_op_setcond_i32 + || c == INDEX_op_setcond_i64 ) {if (args[k] <ARRAY_SIZE (cond_name) && cond_name[args[k]])fprintf (outfile,",%s" , cond_name[args[k++]]);
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 ();
Quoted 6 lines
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.
- a variant that sets -1 instead of 1 for masking
I'm worried about this. If we're not careful we'll end up with an explosion of different patterns, many of which aren't optimal of different hosts.
Quoted 1 line
- 64 bit setcond's
You should do this sooner rather than later, and on a 32-bit host.
x86_64/tcg-target.c line 473
Quoted 2 lines
x86_64/tcg-target.c +2
-0+/* clear ret since setcc only sets the lower 8 bits */ +tcg_out_modrm (s,0x01 | (ARITH_XOR <<3 ) | rexw, ret, ret);
This is broken. Inputs and outputs may overlap.
Quoted 4 lines
(quoted diff) +2
-0+ // 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)
This is the wrong way to fix this. If you really care about the extra code byte (which is harmless) you should fix tcg_out_modrm.
Also, please use C comments, not c++ style //.
Paul
Quoted 9 lines · Paul Brook
Quoted 6 lines
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.
- a variant that sets -1 instead of 1 for masking
I'm worried about this. If we're not careful we'll end up with an explosion of different patterns, many of which aren't optimal of different hosts.
I am worried too. I added that because you mentioned you would prefer -1 over 1. I am still unsure which one of these variants is the most useful and/or host runtime critical (one can be derived from the other by generating one extra TCG op).
Quoted 3 lines
Quoted 1 line
- 64 bit setcond's
You should do this sooner rather than later, and on a 32-bit host.
OK.
Quoted 4 lines
x86_64/tcg-target.c line 473
Quoted 2 lines
x86_64/tcg-target.c +2
-0+/* clear ret since setcc only sets the lower 8 bits */ +tcg_out_modrm (s,0x01 | (ARITH_XOR <<3 ) | rexw, ret, ret);This is broken. Inputs and outputs may overlap.
Oh right. I will use some bit extending instruction.
Quoted 7 lines
Quoted 4 lines
(quoted diff) +2
-0+ // 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)This is the wrong way to fix this. If you really care about the extra code byte (which is harmless) you should fix
tcg_out_modrm.
I asked about that directly to Fabrice yesterday. I am waiting for his answer. I certainly am not very good at x86 and its baroque encoding :)
Quoted 1 line
Also, please use C comments, not c++ style //.
These were done so that it is explicit they are here temporarily for reviewers. They will disappear in the final patch.
Thanks for the comments.
Laurent
Quoted 21 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.
This patch is only posted here to get comments before I dig further into that and propose a proper update.
One of the ARM helpers (sub with flag settings) has been converted to TCG and uses setcond to compute carry.
setcond has been implemented only for
x86_64TCG back-end.x86_64/tcg-target.c +6
-0+// TODO should apply to setcond_i64 but not tested +static void tcg_out_setcond (TCGContext *s,int cond, +TCGArg ret,TCGArg arg1,TCGArg arg2,int rexw) +{ +/* clear ret since setcc only sets the lower 8 bits */ +tcg_out_modrm (s,0x01 | (ARITH_XOR <<3 ) | rexw, ret, ret);
x86_64/tcg-target.c line 483
Quoted 10 lines
x86_64/tcg-target.c +10
-0+/* 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 ); +else if (ret >7 ) +tcg_out8 (s,0x41 );
if ret == 8 this fails. You need to reverse the order of the tests.
Quoted 7 lines
x86_64/tcg-target.c +5
-0+tcg_out8 (s,0x0f ); +tcg_out8 (s,0x90 + tcg_cond_to_jcc[cond]); +tcg_out8 (s,0xc0 + (ret &7 )); +} +
Signature · 1 line
error compiling committee.c: too many arguments to function
Hello,
Here is an updated patch for setcond.
Changes since the previous patch:
- support TCG i386 for 32 and 64 bit variants
- support TCG x86_64 support for 32 and 64 bit variants
- fix for x86_64 prefix output
- fix for input/output overlap
- use setcond for Alpha cmp instructions
- add setcondi
Digging into x86 is certainly not a pleasant experience for me, especially as I have no experience on it. So this patch may contain bugs and overlooks; note however my changes were tested on x86_64 and i386 platforms with test programs for Alpha and ARM.
What remains to be done:
- document setcond in tcg/README
- implement it in all backends (ARM, PPC, PPC64, SPARC)
- use it at least once in every frontend (cris, i386, m68k, MIPS, PPC, sh4, SPARC)
- provide tests.
Also there's a choice to be done between return 1 if the test succeeds or -1.
I am concerned by 64 bit setcond on the register-started i386. Isn't there a high risk the register allocator will run out of registers? If it's so, I guess it should be rewritten with a helper.
Thanks to Paul and Avi for their comments and uncovering silly bugs and overlooks.
Comments and criticisms welcome as usual :-)
Laurent
Quoted 19 lines · Laurent Desnogues
Hello,
Here is an updated patch for setcond.
Changes since the previous patch:
- support TCG i386 for 32 and 64 bit variants
- support TCGx86_64support for 32 and 64 bit variants
- fix forx86_64prefix output
- fix for input/output overlap
- use setcond for Alpha cmp instructions
- add setcondiDigging into x86 is certainly not a pleasant experience for me, especially as I have no experience on it. So this patch may contain bugs and overlooks; note however my changes were tested on
x86_64and i386 platforms with test programs for Alpha and ARM.
Quoted 6 lines
What remains to be done:
- document setcond in tcg/README
- implement it in all backends (ARM, PPC, PPC64, SPARC)
There is no equivalent instruction for Sparc32 (V8), it should be implemented otherwise. Though currently we assume that V9 is always available.
Sparc64 (V9) has a conditional move instruction (MOVcc). The manual suggests a three-instruction sequence (cmp, mov, movcc) to implement a setcond-type operation. For setcondi where the immediate value is zero, MOVR may be used to skip the comparison (mov, movrcc).
Quoted 2 lines
- use it at least once in every frontend (cris, i386, m68k, MIPS, PPC, sh4, SPARC)
Candidate brconds to be replaced by setcond in target-sparc/translate
are: all of gen_cc_*, gen_op_mulscc, gen_op_div_cc and gen_cond_reg. Especially gen_cond_reg should benefit.
Quoted 2 lines
Also there's a choice to be done between return 1 if the test succeeds or -1.
'1' would be much more useful than '-1' for Sparc target, because it can be shifted to form PSR_xxx values that can be or'ed to the condition codes. All of the above candidates except gen_cond_reg would need additional masking to remove the other bits than the zeroth.