tcg/arm: fix division
cover tcg/arm: fix division
The division on TCG ARM is currently broken due to to wrong constraints:
"1" and "2" are interpreted as arguments aliasing instead of constraints.
This could obviously be fixed, but the division on TCG ARM becomes quite
complex: for no obvious reason div2/divu2 are implemented instead of div/divu/rem/remu for no obvious reason and the code actually calls the 64-bit helpers for a 32-bit division.
This patch series fixes that by allowing TCG targets to use helpers for the division in addition to the two current implementations.
PATCH 1/3 tcg: add div/rem 32-bit helpers
Some targets like ARM would benefit to use 32-bit helpers for div/rem/divu/remu.
Create a #define for div2 so that targets can select between div, div2 and helpers implementation. Use the helpers version if none of the #define are present.
--- tcg-runtime.c | 24 +++++++++++++++++++ tcg/arm/tcg-target.h | 1 + tcg/hppa/tcg-target.h | 1 + tcg/i386/tcg-target.h | 1 + tcg/tcg-op.h | 57 +++++++++++++++++++++++++++++++++++++++++++++- tcg/tcg-runtime.h | 5 ++++ tcg/x86_64/tcg-target.h | 2 + 7 files changed, 89 insertions(+), 2 deletions(-)
tcg-runtime.c +24 -0
@@ -25,6 +25,30 @@#include "tcg/tcg-runtime.h" +/* 32-bit helpers */ + +int32_t tcg_helper_div_i32 (int32_t arg1,int32_t arg2) +{ +return arg1 / arg2; +} + +int32_t tcg_helper_rem_i32 (int32_t arg1,int32_t arg2) +{ +return arg1 % arg2; +} + +uint32_t tcg_helper_divu_i32 (uint32_t arg1,uint32_t arg2) +{ +return arg1 / arg2; +} + +uint32_t tcg_helper_remu_i32 (uint32_t arg1,uint32_t arg2) +{ +return arg1 % arg2; +} + +/* 64-bit helpers */ +int64_t tcg_helper_shl_i64 (int64_t arg1,int64_t arg2) {return arg1 << arg2;
tcg/arm/tcg-target.h +1 -0
@@ -56,6 +56,7 @@ enum {#define TCG_TARGET_CALL_STACK_OFFSET 0/* optional instructions */ +#define TCG_TARGET_HAS_div2_i32#define TCG_TARGET_HAS_ext8s_i32#define TCG_TARGET_HAS_ext16s_i32// #define TCG_TARGET_HAS_ext8u_i32
tcg/hppa/tcg-target.h +1 -0
@@ -75,6 +75,7 @@ enum {#define TCG_TARGET_STACK_GROWSUP /* optional instructions */ +#define TCG_TARGET_HAS_div2_i32//#define TCG_TARGET_HAS_ext8s_i32 //#define TCG_TARGET_HAS_ext16s_i32 //#define TCG_TARGET_HAS_bswap16_i32
tcg/i386/tcg-target.h +1 -0
@@ -45,6 +45,7 @@ enum {#define TCG_TARGET_CALL_STACK_OFFSET 0/* optional instructions */ +#define TCG_TARGET_HAS_div2_i32#define TCG_TARGET_HAS_rot_i32#define TCG_TARGET_HAS_ext8s_i32#define TCG_TARGET_HAS_ext16s_i32
tcg/tcg-op.h +55 -2
87 lines not on this page · raw message
tcg/tcg-runtime.h +5 -0
@@ -2,6 +2,11 @@#define TCG_RUNTIME_H /* tcg-runtime.c */ +int32_t tcg_helper_div_i32 (int32_t arg1,int32_t arg2); +int32_t tcg_helper_rem_i32 (int32_t arg1,int32_t arg2); +uint32_t tcg_helper_divu_i32 (uint32_t arg1,uint32_t arg2); +uint32_t tcg_helper_remu_i32 (uint32_t arg1,uint32_t arg2); +int64_t tcg_helper_shl_i64 (int64_t arg1,int64_t arg2);int64_t tcg_helper_shr_i64 (int64_t arg1,int64_t arg2);int64_t tcg_helper_sar_i64 (int64_t arg1,int64_t arg2);
tcg/x86_64/tcg-target.h +2 -0
@@ -56,6 +56,8 @@ enum {#define TCG_TARGET_CALL_STACK_OFFSET 0/* optional instructions */ +#define TCG_TARGET_HAS_div2_i32 +#define TCG_TARGET_HAS_div2_i64#define TCG_TARGET_HAS_bswap16_i32#define TCG_TARGET_HAS_bswap16_i64#define TCG_TARGET_HAS_bswap32_i32
Signature · 1 line
1.7.0
PATCH 2/3 tcg/arm: use helpers for divu/remu
In reply to Aurelien Jarno--- tcg/arm/tcg-target.c | 94 -------------------------------------------------- tcg/arm/tcg-target.h | 1 - 2 files changed, 0 insertions(+), 95 deletions(-)
tcg/arm/tcg-target.c +0 -94
@@ -157,19 +157,6 @@ static int target_parse_constraint(TCGArgConstraint *ct, const char **pct_str)# endif #endif -case '1' : - ct->ct |=TCG_CT_REG ; -tcg_regset_set32 (ct->u.regs,0 , (1 <<TCG_TARGET_NB_REGS ) -1 ); -tcg_regset_reset_reg (ct->u.regs,TCG_REG_R0 ); -break ; - -case '2' : - ct->ct |=TCG_CT_REG ; -tcg_regset_set32 (ct->u.regs,0 , (1 <<TCG_TARGET_NB_REGS ) -1 ); -tcg_regset_reset_reg (ct->u.regs,TCG_REG_R0 ); -tcg_regset_reset_reg (ct->u.regs,TCG_REG_R1 ); -break ; -default :return -1 ; }
@@ -819,75 +806,6 @@ static inline void tcg_out_goto_label(TCGContext *s, int cond, int label_index) } } -static void tcg_out_div_helper (TCGContext *s,int cond,const TCGArg *args, -void *helper_div,void *helper_rem,int shift) -{ -int div_reg = args[0 ]; -int rem_reg = args[1 ]; - -/* stmdb sp!, { r0 - r3, ip, lr } */ -/* (Note that we need an even number of registers as per EABI) */ -tcg_out32 (s, (cond <<28 ) |0x092d500f ); - -tcg_out_dat_reg (s, cond,ARITH_MOV ,0 ,0 , args[2 ],SHIFT_IMM_LSL (0 )); -tcg_out_dat_reg (s, cond,ARITH_MOV ,1 ,0 , args[3 ],SHIFT_IMM_LSL (0 )); -tcg_out_dat_reg (s, cond,ARITH_MOV ,2 ,0 , args[4 ],SHIFT_IMM_LSL (0 )); -tcg_out_dat_reg (s, cond,ARITH_MOV ,3 ,0 ,2 , shift); - -tcg_out_call (s, cond, (uint32_t ) helper_div); -tcg_out_dat_reg (s, cond,ARITH_MOV ,8 ,0 ,0 ,SHIFT_IMM_LSL (0 )); - -/* ldmia sp, { r0 - r3, fp, lr } */ -tcg_out32 (s, (cond <<28 ) |0x089d500f ); - -tcg_out_dat_reg (s, cond,ARITH_MOV ,0 ,0 , args[2 ],SHIFT_IMM_LSL (0 )); -tcg_out_dat_reg (s, cond,ARITH_MOV ,1 ,0 , args[3 ],SHIFT_IMM_LSL (0 )); -tcg_out_dat_reg (s, cond,ARITH_MOV ,2 ,0 , args[4 ],SHIFT_IMM_LSL (0 )); -tcg_out_dat_reg (s, cond,ARITH_MOV ,3 ,0 ,2 , shift); - -tcg_out_call (s, cond, (uint32_t ) helper_rem); - -tcg_out_dat_reg (s, cond,ARITH_MOV , rem_reg,0 ,0 ,SHIFT_IMM_LSL (0 )); -tcg_out_dat_reg (s, cond,ARITH_MOV , div_reg,0 ,8 ,SHIFT_IMM_LSL (0 )); - -/* ldr r0, [sp], #4 */ -if (rem_reg !=0 && div_reg !=0 ) { -tcg_out32 (s, (cond <<28 ) |0x04bd0004 ); - }else { -tcg_out_dat_imm (s, cond,ARITH_ADD ,13 ,13 ,4 ); - } -/* ldr r1, [sp], #4 */ -if (rem_reg !=1 && div_reg !=1 ) { -tcg_out32 (s, (cond <<28 ) |0x04bd1004 ); - }else { -tcg_out_dat_imm (s, cond,ARITH_ADD ,13 ,13 ,4 ); - } -/* ldr r2, [sp], #4 */ -if (rem_reg !=2 && div_reg !=2 ) { -tcg_out32 (s, (cond <<28 ) |0x04bd2004 ); - }else { -tcg_out_dat_imm (s, cond,ARITH_ADD ,13 ,13 ,4 ); - } -/* ldr r3, [sp], #4 */ -if (rem_reg !=3 && div_reg !=3 ) { -tcg_out32 (s, (cond <<28 ) |0x04bd3004 ); - }else { -tcg_out_dat_imm (s, cond,ARITH_ADD ,13 ,13 ,4 ); - } -/* ldr ip, [sp], #4 */ -if (rem_reg !=12 && div_reg !=12 ) { -tcg_out32 (s, (cond <<28 ) |0x04bdc004 ); - }else { -tcg_out_dat_imm (s, cond,ARITH_ADD ,13 ,13 ,4 ); - } -/* ldr lr, [sp], #4 */ -if (rem_reg !=14 && div_reg !=14 ) { -tcg_out32 (s, (cond <<28 ) |0x04bde004 ); - }else { -tcg_out_dat_imm (s, cond,ARITH_ADD ,13 ,13 ,4 ); - } -} -#ifdef CONFIG_SOFTMMU #include "../../softmmu_defs.h"
@@ -1487,16 +1405,6 @@ static inline void tcg_out_op(TCGContext *s, int opc,case INDEX_op_mulu2_i32:tcg_out_umull32 (s,COND_AL , args[0 ], args[1 ], args[2 ], args[3 ]);break ; -case INDEX_op_div2_i32: -tcg_out_div_helper (s,COND_AL , args, - tcg_helper_div_i64, tcg_helper_rem_i64, -SHIFT_IMM_ASR (31 )); -break ; -case INDEX_op_divu2_i32: -tcg_out_div_helper (s,COND_AL , args, - tcg_helper_divu_i64, tcg_helper_remu_i64, -SHIFT_IMM_LSR (31 )); -break ;/* XXX: Perhaps args[2] & 0x1f is wrong */ case INDEX_op_shl_i32: c = const_args[2] ?
@@ -1652,8 +1560,6 @@ static const TCGTargetOpDef arm_op_defs[] = { { INDEX_op_sub_i32, { "r", "r", "rI" } }, { INDEX_op_mul_i32, { "r", "r", "r" } }, { INDEX_op_mulu2_i32, { "r", "r", "r", "r" } }, - { INDEX_op_div2_i32, { "r", "r", "r", "1", "2" } }, - { INDEX_op_divu2_i32, { "r", "r", "r", "1", "2" } }, { INDEX_op_and_i32, { "r", "r", "rI" } }, { INDEX_op_andc_i32, { "r", "r", "rI" } }, { INDEX_op_or_i32, { "r", "r", "rI" } },
tcg/arm/tcg-target.h +0 -1
@@ -56,7 +56,6 @@ enum {#define TCG_TARGET_CALL_STACK_OFFSET 0/* optional instructions */ -#define TCG_TARGET_HAS_div2_i32#define TCG_TARGET_HAS_ext8s_i32#define TCG_TARGET_HAS_ext16s_i32// #define TCG_TARGET_HAS_ext8u_i32
Signature · 1 line
1.7.0
PATCH 3/3 tcg: declare internal helpers as const and pure
In reply to Aurelien JarnoTCG internal helpers only access to the values passed in arguments, and do not modify the CPU internal state. Thus they can be declared as const and pure.
--- tcg/tcg-op.h | 8 ++++---- 1 files changed, 4 insertions(+), 4 deletions(-)
tcg/tcg-op.h +4 -4
@@ -364,7 +364,6 @@ static inline void tcg_gen_helperN(void *func, int flags, int sizemask,tcg_temp_free_ptr (fn); } -/* FIXME: Should this be pure? */ static inline void tcg_gen_helper32 (void *func,TCGv_i32 ret,TCGv_i32 a,TCGv_i32 b) {
@@ -373,11 +372,11 @@ static inline void tcg_gen_helper32(void *func, TCGv_i32 ret, fn =tcg_const_ptr ((tcg_target_long )func); args[0 ] =GET_TCGV_I32 (a); args[1 ] =GET_TCGV_I32 (b); -tcg_gen_callN (&tcg_ctx, fn,0 ,0 ,GET_TCGV_I32 (ret),2 , args); +tcg_gen_callN (&tcg_ctx, fn,TCG_CALL_CONST |TCG_CALL_PURE , +0 ,GET_TCGV_I32 (ret),2 , args);tcg_temp_free_ptr (fn); } -/* FIXME: Should this be pure? */ static inline void tcg_gen_helper64 (void *func,TCGv_i64 ret,TCGv_i64 a,TCGv_i64 b) {
@@ -386,7 +385,8 @@ static inline void tcg_gen_helper64(void *func, TCGv_i64 ret, fn =tcg_const_ptr ((tcg_target_long )func); args[0 ] =GET_TCGV_I64 (a); args[1 ] =GET_TCGV_I64 (b); -tcg_gen_callN (&tcg_ctx, fn,0 ,7 ,GET_TCGV_I64 (ret),2 , args); +tcg_gen_callN (&tcg_ctx, fn,TCG_CALL_CONST |TCG_CALL_PURE , +7 ,GET_TCGV_I64 (ret),2 , args);tcg_temp_free_ptr (fn); }
Signature · 1 line
1.7.0
Re: [Qemu-devel] [PATCH 3/3] tcg: declare internal helpers as const and pure
Quoted 3 lines
TCG internal helpers only access to the values passed in arguments, and do not modify the CPU internal state. Thus they can be declared as const and pure.
I think this needs an explanatory comment. It's not immediately obvious that tcg_gen_helperN and tcg_gen_helper{32,64} have significantly different semantics.
tcg/README also needs updating, specifically:
"* Helpers:
Using the tcg_gen_helper_x_y it is possible to call any function taking i32, i64 or pointer types. Before calling an helper, all globals are stored at their canonical location and it is assumed that the function can modify them. In the future, function modifiers will be allowed to tell that the helper does not read or write some globals. "
Paul
Re: [Qemu-devel] [PATCH 3/3] tcg: declare internal helpers as const and pure
Quoted 7 lines · Paul Brook
Quoted 3 lines
TCG internal helpers only access to the values passed in arguments, and do not modify the CPU internal state. Thus they can be declared as const and pure.
I think this needs an explanatory comment. It's not immediately obvious that
tcg_gen_helperNand tcg_gen_helper{32,64} have significantly different semantics.
What do you mean exactly? Mentioning explicitly tcg_gen_helper{32,64} instead of "TCG internal helpers".
Quoted 11 lines
tcg/README also needs updating, specifically:
"* Helpers:
Using the
tcg_gen_helper_x_yit is possible to call any function taking i32, i64 or pointer types. Before calling an helper, all globals are stored at their canonical location and it is assumed that the function can modify them. In the future, function modifiers will be allowed to tell that the helper does not read or write some globals. "
Fully agreed, but it is not a change introduce by this patch series. I'll submit a patch later.
Signature · 2 lines
Aurelien Jarno GPG: 1024D/F1BCDB73 [email protected] http://www.aurel32.net
Re: [Qemu-devel] [PATCH 3/3] tcg: declare internal helpers as const and pure
Quoted 11 lines · Paul Brook
Quoted 7 lines
Quoted 3 lines
TCG internal helpers only access to the values passed in arguments, and do not modify the CPU internal state. Thus they can be declared as const and pure.
I think this needs an explanatory comment. It's not immediately obvious that
tcg_gen_helperNand tcg_gen_helper{32,64} have significantly different semantics.What do you mean exactly? Mentioning explicitly tcg_gen_helper{32,64} instead of "TCG internal helpers".
I think the difference between tcg_gen_helperN and tcg_gen_helper{32,64} is sufficiently subtle that it deserves documenting. It's not obvious that the latter may only be used for cont/pure helpers. My guess is that the FIXME you're removing was added precisely because there was uncertainty whether this assumption was reasonable, and under which circumstances they are used.
Paul