tcg: store constants without using registers when possible
RFC 1/1 tcg: store constants without using registers when possible
Currently only implemented for x86/x86_64. It may also be implemented for targets that keep one register for TCG internal use.
--- tcg/arm/tcg-target.c | 6 ++++++ tcg/hppa/tcg-target.c | 6 ++++++ tcg/i386/tcg-target.c | 9 +++++++++ tcg/ppc/tcg-target.c | 9 +++++++++ tcg/ppc64/tcg-target.c | 9 +++++++++ tcg/sparc/tcg-target.c | 9 +++++++++ tcg/tcg.c | 10 ++++++---- tcg/x86_64/tcg-target.c | 16 ++++++++++++++++ 8 files changed, 70 insertions(+), 4 deletions(-)
tcg/arm/tcg-target.c +6 -0
@@ -1669,6 +1669,12 @@ static inline void tcg_out_st(TCGContext *s, TCGType type, int arg,tcg_out_st32 (s,COND_AL , arg, arg1, arg2); } +static inline int tcg_out_sti (TCGContext *s,TCGType type,tcg_target_long val, +int arg1,tcg_target_long arg2) +{ +return 0 ; +} +static void tcg_out_addi (TCGContext *s,int reg,tcg_target_long val) {if (val >0 )
tcg/hppa/tcg-target.c +6 -0
@@ -285,6 +285,12 @@ static inline void tcg_out_st(TCGContext *s, TCGType type, int ret,tcg_abort (); } +static inline int tcg_out_sti (TCGContext *s,TCGType type,tcg_target_long val, +int arg1,tcg_target_long arg2) +{ +return 0 ; +} +static inline void tcg_out_arith (TCGContext *s,int t,int r1,int r2,int op) {tcg_out32 (s, op |INSN_T (t) |INSN_R1 (r1) |INSN_R2 (r2));
tcg/i386/tcg-target.c +9 -0
@@ -276,6 +276,15 @@ static inline void tcg_out_st(TCGContext *s, TCGType type, int arg,tcg_out_modrm_offset (s,0x89 , arg, arg1, arg2); } +static inline int tcg_out_sti (TCGContext *s,TCGType type,tcg_target_long val, +int arg1,tcg_target_long arg2) +{ +/* movl */ +tcg_out_modrm_offset (s,0xc7 ,0 , arg1, arg2); +tcg_out32 (s, val); +return 1 ; +} +static inline void tgen_arithi (TCGContext *s,int c,int r0,int32_t val,int cf) {if (!cf && ((c ==ARITH_ADD && val ==1 ) || (c ==ARITH_SUB && val ==-1 ))) {
tcg/ppc/tcg-target.c +9 -0
@@ -958,6 +958,15 @@ static void tcg_out_st (TCGContext *s, TCGType type, int arg, int arg1,tcg_out_ldst (s, arg, arg1, arg2,STW ,STWX ); } +static inline int tcg_out_sti (TCGContext *s,TCGType type,tcg_target_long val, +int arg1,tcg_target_long arg2) +{ +/* movl */ +tcg_out_modrm_offset (s,0xc7 ,0 , arg1, arg2); +tcg_out32 (s, val); +return 1 ; +} +static void ppc_addi (TCGContext *s,int rt,int ra,tcg_target_long si) {if (!si && rt == ra)
tcg/ppc64/tcg-target.c +9 -0
@@ -917,6 +917,15 @@ static void tcg_out_st (TCGContext *s, TCGType type, int arg, int arg1,tcg_out_ldsta (s, arg, arg1, arg2,STD ,STDX ); } +static inline int tcg_out_sti (TCGContext *s,TCGType type,tcg_target_long val, +int arg1,tcg_target_long arg2) +{ +/* movl */ +tcg_out_modrm_offset (s,0xc7 ,0 , arg1, arg2); +tcg_out32 (s, val); +return 1 ; +} +static void ppc_addi32 (TCGContext *s,int rt,int ra,tcg_target_long si) {if (!si && rt == ra)
tcg/sparc/tcg-target.c +9 -0
@@ -392,6 +392,15 @@ static inline void tcg_out_st(TCGContext *s, TCGType type, int arg,tcg_out_ldst (s, arg, arg1, arg2,STX ); } +static inline int tcg_out_sti (TCGContext *s,TCGType type,tcg_target_long val, +int arg1,tcg_target_long arg2) +{ +/* movl */ +tcg_out_modrm_offset (s,0xc7 ,0 , arg1, arg2); +tcg_out32 (s, val); +return 1 ; +} +static inline void tcg_out_sety (TCGContext *s,tcg_target_long val) {if (val ==0 || val ==-1 )
tcg/tcg.c +6 -4
@@ -1385,12 +1385,14 @@ static void temp_save(TCGContext *s, int temp, TCGRegSet allocated_regs) ts->val_type =TEMP_VAL_MEM ;break ;case TEMP_VAL_CONST : - reg =tcg_reg_alloc (s, tcg_target_available_regs[ts->type], - allocated_regs);if (!ts->mem_allocated)temp_allocate_frame (s, temp); -tcg_out_movi (s, ts->type, reg, ts->val); -tcg_out_st (s, ts->type, reg, ts->mem_reg, ts->mem_offset); +if (!tcg_out_sti (s, ts->type, ts->val, ts->mem_reg, ts->mem_offset)) { + reg =tcg_reg_alloc (s, tcg_target_available_regs[ts->type], + allocated_regs); +tcg_out_movi (s, ts->type, reg, ts->val); +tcg_out_st (s, ts->type, reg, ts->mem_reg, ts->mem_offset); + } ts->val_type =TEMP_VAL_MEM ;break ;case TEMP_VAL_MEM :
tcg/x86_64/tcg-target.c +16 -0
@@ -395,6 +395,22 @@ static inline void tcg_out_st(TCGContext *s, TCGType type, int arg,tcg_out_modrm_offset (s,0x89 |P_REXW , arg, arg1, arg2);/* movq */ } +static inline int tcg_out_sti (TCGContext *s,TCGType type,tcg_target_long val, +int arg1,tcg_target_long arg2) +{ +if (type ==TCG_TYPE_I32 ) { +tcg_out_modrm_offset (s,0xc7 ,0 , arg1, arg2);/* movl */ +tcg_out32 (s, val); + }else { +if ((int32_t ) val != val) { +return 0 ; + } +tcg_out_modrm_offset (s,0xc7 |P_REXW ,0 , arg1, arg2);/* movq */ +tcg_out32 (s, val); + } +return 1 ; +} +static inline void tgen_arithi32 (TCGContext *s,int c,int r0,int32_t val) {if ((c ==ARITH_ADD && val ==1 ) || (c ==ARITH_SUB && val ==-1 )) {
Signature · 6 lines
1.6.4.3 -- Aurelien Jarno GPG: 1024D/F1BCDB73 [email protected] http://www.aurel32.net
Quoted 2 lines · Aurelien Jarno
Currently only implemented for x86/x86_64. It may also be implemented for targets that keep one register for TCG internal use.
Nice, but do you think the op will be used often?
Quoted 5 lines
tcg/sparc/tcg-target.c +0
-0@@ -392,6 +392,15 @@ static inline void tcg_out_st(TCGContext *s, TCGType type, int arg, tcg_out_ldst(s, arg, arg1, arg2, STX); }
tcg/sparc/tcg-target.c static inline void tcg_out_st(TCGContext *s, TCGType type, int arg, · line 403
Quoted 10 lines
tcg/sparc/tcg-target.c +9
-0+static inline int tcg_out_sti (TCGContext *s,TCGType type,tcg_target_long val, + int arg1, tcg_target_long arg2) +{ + /* movl */ + tcg_out_modrm_offset(s, 0xc7, 0, arg1, arg2); + tcg_out32(s, val); + return 1; +} +
Sparc doesn't have store immediate ops, except for val == 0 case:
static inline int tcg_out_sti(TCGContext *s, TCGType type, tcg_target_long val,
int arg1, tcg_target_long arg2)
{
if (val != 0) {
return 0; }
/* clr/clrx */
tcg_out_st(s, type, TCG_REG_G0, arg1, arg2);
return 1;
}
Quoted 5 lines · Blue Swirl
Quoted 2 lines · Aurelien Jarno
Currently only implemented for x86/x86_64. It may also be implemented for targets that keep one register for TCG internal use.
Nice, but do you think the op will be used often?
It is used each time a constant is written to a global TCG variable and written back to memory. A constant propagation patch like the ones Filip proposed a few months ago increase the number of those cases.
Quoted 29 lines
Quoted 5 lines
tcg/sparc/tcg-target.c +0
-0@@ -392,6 +392,15 @@ static inline void tcg_out_st(TCGContext *s, TCGType type, int arg, tcg_out_ldst(s, arg, arg1, arg2, STX); }tcg/sparc/tcg-target.c static inline void tcg_out_st(TCGContext *s, TCGType type, int arg, · line 403
Quoted 10 lines
tcg/sparc/tcg-target.c +9
-0+static inline int tcg_out_sti (TCGContext *s,TCGType type,tcg_target_long val, + int arg1, tcg_target_long arg2) +{ + /* movl */ + tcg_out_modrm_offset(s, 0xc7, 0, arg1, arg2); + tcg_out32(s, val); + return 1; +} +Sparc doesn't have store immediate ops, except for val == 0 case:
static inline int tcg_out_sti(TCGContext *s, TCGType type,
tcg_target_longval,
int arg1,tcg_target_longarg2)
{if (val != 0) { return 0;}
/* clr/clrx */
tcg_out_st(s, type, TCG_REG_G0, arg1, arg2);
return 1;
}
Signature · 2 lines
Aurelien Jarno GPG: 1024D/F1BCDB73 [email protected] http://www.aurel32.net