target/sh4: decode_gusa: recognize add#imm with prior mov Rm, Rn
PATCH 1/1 target/sh4: decode_gusa: recognize add#imm with prior mov Rm, Rn
The gUSA pattern matcher rejected add #imm, Rn whenever any prior mov Rm, Rn appeared (mv_src >= 0), forcing a fallback to cpu_exec_step_atomic for sequences like:
mov.l @r2, r3 ; load mov r3, r7 ; save old value (mv_src == ld_dst) add #1, r7 ; increment copy mov.l r7, @r2 ; store
When mv_src == ld_dst the move merely copies the loaded value to preserve it -- exactly the situation already accepted for the add Rm, Rn form. The immediate form can be handled identically with tcg_gen_atomic_fetch_add_i32 + tcg_gen_add_i32, so translate it inline instead of taking the slower single-step atomic fallback.
--- target/sh4/translate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
target/sh4/translate.c +1 -1
@@ -1974,7 +1974,7 @@ static void decode_gusa(DisasContext *ctx, CPUSH4State *env)break ; case 0x7000 ... 0x700f: /* add #imm,Rn */ -if (op_dst !=B11_8 || mv_src >=0 ) { +if (op_dst !=B11_8 || (mv_src >=0 && mv_src != ld_dst)) { goto fail; } op_opc = INDEX_op_add;
Signature · 1 line
2.53.0
Quoted 10 lines · Matt Turner
The gUSA pattern matcher rejected
add #imm, Rnwhenever any priormov Rm, Rnappeared (mv_src>= 0), forcing a fallback tocpu_exec_step_atomicfor sequences like:mov.l @r2, r3 ; load mov r3, r7 ; save old value (mv_src == ld_dst) add #1, r7 ; increment copy mov.l r7, @r2 ; store
Quoted 6 lines
When
mv_src==ld_dstthe move merely copies the loaded value to preserve it -- exactly the situation already accepted for theadd Rm, Rnform. The immediate form can be handled identically withtcg_gen_atomic_fetch_add_i32+tcg_gen_add_i32, so translate it inline instead of taking the slower single-step atomic fallback.
---
Re: [PATCH] target/sh4: decode_gusa: recognize add#imm with prior mov Rm,Rn
In reply to Matt TurnerQuoted 23 lines · Matt Turner
The gUSA pattern matcher rejected
add #imm, Rnwhenever any priormov Rm, Rnappeared (mv_src>= 0), forcing a fallback tocpu_exec_step_atomicfor sequences like:mov.l @r2, r3 ; load mov r3, r7 ; save old value (mv_src == ld_dst) add #1, r7 ; increment copy mov.l r7, @r2 ; storeWhen
mv_src==ld_dstthe move merely copies the loaded value to preserve it -- exactly the situation already accepted for theadd Rm, Rnform. The immediate form can be handled identically withtcg_gen_atomic_fetch_add_i32+tcg_gen_add_i32, so translate it inline instead of taking the slower single-step atomic fallback.--- target/sh4/translate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)target/sh4/translate.c +0
-0
target/sh4/translate.c static void decode_gusa(DisasContext *ctx, CPUSH4State *env) · line 1978
Quoted 6 lines
target/sh4/translate.c +1
-1@@ -1974,7 +1974,7 @@ static void decode_gusa(DisasContext *ctx, CPUSH4State *env)break ; case 0x7000 ... 0x700f: /* add #imm,Rn */ -if (op_dst !=B11_8 || mv_src >=0 ) { +if (op_dst !=B11_8 || (mv_src >=0 && mv_src != ld_dst)) { goto fail;
Hmm, but then you don't further constrain B11_8.
For the given example, afaics, B11_8 == op_dst, so we're failing only because mv_src >= 0.
Do we actually need the mv_src check? I'm guessing not.
r~
Quoted 33 lines · Richard Henderson
Quoted 23 lines · Matt Turner
The gUSA pattern matcher rejected
add #imm, Rnwhenever any priormov Rm, Rnappeared (mv_src>= 0), forcing a fallback tocpu_exec_step_atomicfor sequences like:mov.l @r2, r3 ; load mov r3, r7 ; save old value (mv_src == ld_dst) add #1, r7 ; increment copy mov.l r7, @r2 ; storeWhen
mv_src==ld_dstthe move merely copies the loaded value to preserve it -- exactly the situation already accepted for theadd Rm, Rnform. The immediate form can be handled identically withtcg_gen_atomic_fetch_add_i32+tcg_gen_add_i32, so translate it inline instead of taking the slower single-step atomic fallback.--- target/sh4/translate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)target/sh4/translate.c +0
-0target/sh4/translate.c static void decode_gusa(DisasContext *ctx, CPUSH4State *env) · line 1977
Quoted 5 lines
target/sh4/translate.c +1
-1@@ -1974,7 +1974,7 @@ static void decode_gusa(DisasContext *ctx, CPUSH4State *env)break ; case 0x7000 ... 0x700f: /* add #imm,Rn */ -if (op_dst !=B11_8 || mv_src >=0 ) { +if (op_dst !=B11_8 || (mv_src >=0 && mv_src != ld_dst)) { goto fail;Hmm, but then you don't further constrain
B11_8.
Quoted 6 lines
For the given example, afaics,
B11_8==op_dst, so we're failing only becausemv_src>= 0. Do we actually need themv_srccheck? I'm guessing not.r~
This check is necessary because the intention is to cause failure in the case of an unintended sequence of instructions.
If atomic addition results in such an instruction sequence, then it seems like a reasonable change.
Signature · 1 line
Yosinori Sato
Re: [PATCH] target/sh4: decode_gusa: recognize add#imm with prior mov Rm,Rn
Quoted 40 lines · On 5/27/26 06:27, [email protected] wrote:
Quoted 38 lines · Richard Henderson
Quoted 23 lines · Matt Turner
The gUSA pattern matcher rejected
add #imm, Rnwhenever any priormov Rm, Rnappeared (mv_src>= 0), forcing a fallback tocpu_exec_step_atomicfor sequences like:mov.l @r2, r3 ; load mov r3, r7 ; save old value (mv_src == ld_dst) add #1, r7 ; increment copy mov.l r7, @r2 ; storeWhen
mv_src==ld_dstthe move merely copies the loaded value to preserve it -- exactly the situation already accepted for theadd Rm, Rnform. The immediate form can be handled identically withtcg_gen_atomic_fetch_add_i32+tcg_gen_add_i32, so translate it inline instead of taking the slower single-step atomic fallback.--- target/sh4/translate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)target/sh4/translate.c +0
-0target/sh4/translate.c static void decode_gusa(DisasContext *ctx, CPUSH4State *env) · line 1977
Quoted 5 lines
target/sh4/translate.c +1
-1@@ -1974,7 +1974,7 @@ static void decode_gusa(DisasContext *ctx, CPUSH4State *env)break ; case 0x7000 ... 0x700f: /* add #imm,Rn */ -if (op_dst !=B11_8 || mv_src >=0 ) { +if (op_dst !=B11_8 || (mv_src >=0 && mv_src != ld_dst)) { goto fail;Hmm, but then you don't further constrain
B11_8.For the given example, afaics,
B11_8==op_dst, so we're failing only becausemv_src>= 0. Do we actually need themv_srccheck? I'm guessing not.
Quoted 4 lines
Quoted 1 line
r~
This check is necessary because the intention is to cause failure in the case of an unintended sequence of instructions.
Of course, but what's the unintended sequence prevented by mv_src >= 0? Matt has identified a sequence that *is* possible, which is prevented by that check.
r~
Quoted 44 lines · Richard Henderson
Quoted 42 lines · On 5/27/26 06:27, [email protected] wrote:
Quoted 39 lines · Richard Henderson
Quoted 23 lines · Matt Turner
The gUSA pattern matcher rejected
add #imm, Rnwhenever any priormov Rm, Rnappeared (mv_src>= 0), forcing a fallback tocpu_exec_step_atomicfor sequences like:mov.l @r2, r3 ; load mov r3, r7 ; save old value (mv_src == ld_dst) add #1, r7 ; increment copy mov.l r7, @r2 ; storeWhen
mv_src==ld_dstthe move merely copies the loaded value to preserve it -- exactly the situation already accepted for theadd Rm, Rnform. The immediate form can be handled identically withtcg_gen_atomic_fetch_add_i32+tcg_gen_add_i32, so translate it inline instead of taking the slower single-step atomic fallback.--- target/sh4/translate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)target/sh4/translate.c +0
-0target/sh4/translate.c static void decode_gusa(DisasContext *ctx, CPUSH4State *env) · line 1977
Quoted 5 lines
target/sh4/translate.c +1
-1@@ -1974,7 +1974,7 @@ static void decode_gusa(DisasContext *ctx, CPUSH4State *env)break ; case 0x7000 ... 0x700f: /* add #imm,Rn */ -if (op_dst !=B11_8 || mv_src >=0 ) { +if (op_dst !=B11_8 || (mv_src >=0 && mv_src != ld_dst)) { goto fail;Hmm, but then you don't further constrain
B11_8.For the given example, afaics,
B11_8==op_dst, so we're failing only becausemv_src>= 0. Do we actually need themv_srccheck? I'm guessing not.r~
Quoted 8 lines
Quoted 2 lines
This check is necessary because the intention is to cause failure in the case of an unintended sequence of instructions.
Of course, but what's the unintended sequence prevented by
mv_src>= 0? Matt has identified a sequence that *is* possible, which is prevented by that check.r~
Decoding "mov r3, r7" in the example code snippet results in mv_src being 3, which causes the current code to fail.
This is an excessive failure, so it should be made effective.
Signature · 1 line
Yosinori Sato
Quoted 34 lines · Richard Henderson
Quoted 23 lines · Matt Turner
The gUSA pattern matcher rejected
add #imm, Rnwhenever any priormov Rm, Rnappeared (mv_src>= 0), forcing a fallback tocpu_exec_step_atomicfor sequences like:mov.l @r2, r3 ; load mov r3, r7 ; save old value (mv_src == ld_dst) add #1, r7 ; increment copy mov.l r7, @r2 ; storeWhen
mv_src==ld_dstthe move merely copies the loaded value to preserve it -- exactly the situation already accepted for theadd Rm, Rnform. The immediate form can be handled identically withtcg_gen_atomic_fetch_add_i32+tcg_gen_add_i32, so translate it inline instead of taking the slower single-step atomic fallback.--- target/sh4/translate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)target/sh4/translate.c +0
-0target/sh4/translate.c static void decode_gusa(DisasContext *ctx, CPUSH4State *env) · line 1978
Quoted 6 lines
target/sh4/translate.c +1
-1@@ -1974,7 +1974,7 @@ static void decode_gusa(DisasContext *ctx, CPUSH4State *env)break ; case 0x7000 ... 0x700f: /* add #imm,Rn */ -if (op_dst !=B11_8 || mv_src >=0 ) { +if (op_dst !=B11_8 || (mv_src >=0 && mv_src != ld_dst)) { goto fail;Hmm, but then you don't further constrain
B11_8.
Quoted 2 lines
For the given example, afaics,
B11_8==op_dst, so we're failing only becausemv_src>= 0.
Do we actually need themv_srccheck? I'm guessing not.
I think you're exactly right that mv_src >= 0 doesn't do anything by itself. With this patch, the mv_src >= 0 check is needed to prevent a spurious failure for:
mov.l @r2, r3 ; ld_dst=3, op_dst=3 (set to ld_dst by default) add #1, r3 ; B11_8=3 mov.l r3, @r2
With just mv_src != ld_dst (no mv_src >= 0 guard):
- mv_src = -1, ld_dst = 3
- -1 != 3 -> true -> spurious fail
Quoted 44 lines · Matt Turner
Quoted 34 lines · Richard Henderson
Quoted 23 lines · Matt Turner
The gUSA pattern matcher rejected
add #imm, Rnwhenever any priormov Rm, Rnappeared (mv_src>= 0), forcing a fallback tocpu_exec_step_atomicfor sequences like:mov.l @r2, r3 ; load mov r3, r7 ; save old value (mv_src == ld_dst) add #1, r7 ; increment copy mov.l r7, @r2 ; storeWhen
mv_src==ld_dstthe move merely copies the loaded value to preserve it -- exactly the situation already accepted for theadd Rm, Rnform. The immediate form can be handled identically withtcg_gen_atomic_fetch_add_i32+tcg_gen_add_i32, so translate it inline instead of taking the slower single-step atomic fallback.--- target/sh4/translate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)target/sh4/translate.c +0
-0target/sh4/translate.c static void decode_gusa(DisasContext *ctx, CPUSH4State *env) · line 1978
Quoted 6 lines
target/sh4/translate.c +1
-1@@ -1974,7 +1974,7 @@ static void decode_gusa(DisasContext *ctx, CPUSH4State *env)break ; case 0x7000 ... 0x700f: /* add #imm,Rn */ -if (op_dst !=B11_8 || mv_src >=0 ) { +if (op_dst !=B11_8 || (mv_src >=0 && mv_src != ld_dst)) { goto fail;Hmm, but then you don't further constrain
B11_8.Quoted 2 lines
For the given example, afaics,
B11_8==op_dst, so we're failing only becausemv_src>= 0.
Do we actually need themv_srccheck? I'm guessing not.I think you're exactly right that
mv_src >= 0doesn't do anything by itself. With this patch, themv_src >= 0check is needed to prevent a spurious failure for:
Quoted 7 lines
mov.l @r2, r3 ; ld_dst=3, op_dst=3 (set to ld_dst by default) add #1, r3 ; B11_8=3 mov.l r3, @r2With just
mv_src!=ld_dst(nomv_src>= 0 guard):
-mv_src= -1,ld_dst= 3
- -1 != 3 -> true -> spurious fail
Unsure of the status on this patch. Will someone take it through their tree, or are there remaining concerns?
Quoted 50 lines · Matt Turner
Quoted 48 lines · Matt Turner
Quoted 34 lines · Richard Henderson
Quoted 23 lines · Matt Turner
The gUSA pattern matcher rejected
add #imm, Rnwhenever any priormov Rm, Rnappeared (mv_src>= 0), forcing a fallback tocpu_exec_step_atomicfor sequences like:mov.l @r2, r3 ; load mov r3, r7 ; save old value (mv_src == ld_dst) add #1, r7 ; increment copy mov.l r7, @r2 ; storeWhen
mv_src==ld_dstthe move merely copies the loaded value to preserve it -- exactly the situation already accepted for theadd Rm, Rnform. The immediate form can be handled identically withtcg_gen_atomic_fetch_add_i32+tcg_gen_add_i32, so translate it inline instead of taking the slower single-step atomic fallback.--- target/sh4/translate.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)target/sh4/translate.c +0
-0target/sh4/translate.c static void decode_gusa(DisasContext *ctx, CPUSH4State *env) · line 1978
Quoted 6 lines
target/sh4/translate.c +1
-1@@ -1974,7 +1974,7 @@ static void decode_gusa(DisasContext *ctx, CPUSH4State *env)break ; case 0x7000 ... 0x700f: /* add #imm,Rn */ -if (op_dst !=B11_8 || mv_src >=0 ) { +if (op_dst !=B11_8 || (mv_src >=0 && mv_src != ld_dst)) { goto fail;Hmm, but then you don't further constrain
B11_8.Quoted 2 lines
For the given example, afaics,
B11_8==op_dst, so we're failing only becausemv_src>= 0.
Do we actually need themv_srccheck? I'm guessing not.I think you're exactly right that
mv_src >= 0doesn't do anything by itself. With this patch, themv_src >= 0check is needed to prevent a spurious failure for:mov.l @r2, r3 ; ld_dst=3, op_dst=3 (set to ld_dst by default) add #1, r3 ; B11_8=3 mov.l r3, @r2
Quoted 6 lines
Quoted 3 lines
With just
mv_src!=ld_dst(nomv_src>= 0 guard):
-mv_src= -1,ld_dst= 3
- -1 != 3 -> true -> spurious failUnsure of the status on this patch. Will someone take it through their tree, or are there remaining concerns?
Sorry. I think this is fine.
Signature · 1 line
Yosinori Sato