From b805223108d67ae57a4dd633f0d8da0079d0b037 Mon Sep 17 00:00:00 2001 From: Sintendo Date: Mon, 4 Jan 2021 23:02:22 +0100 Subject: [PATCH 1/5] Jit64: subfx - Optimize more constant a cases Consider the case where d and a refer to the same PowerPC register, which is known to hold an immediate value by the RegCache. We place a ReadWrite constraint on this register and bind it to an x86 register. The RegCache then allocates a new register, initializes it with the immediate, and returns a RCX64Reg for both d and a. At this point information about the immediate value becomes unreachable. In the case of subfx, this generates suboptimal code: Before 1: BF 1E 00 00 00 mov edi,1Eh <- done by RegCache 8B C7 mov eax,edi 8B FE mov edi,esi 2B F8 sub edi,eax Before 2: BE 00 AC 3F 80 mov esi,803FAC00h <- done by RegCache 8B C6 mov eax,esi 8B 75 EC mov esi,dword ptr [rbp-14h] 2B F0 sub esi,eax The solution is to explicitly handle the constant a case before having the RegCache allocate registers for us. After 1: 8D 7E E2 lea edi,[rsi-1Eh] After 2: 8B 75 EC mov esi,dword ptr [rbp-14h] 81 EE 00 AC 3F 80 sub esi,803FAC00h --- .../Core/Core/PowerPC/Jit64/Jit_Integer.cpp | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/Source/Core/Core/PowerPC/Jit64/Jit_Integer.cpp b/Source/Core/Core/PowerPC/Jit64/Jit_Integer.cpp index bd4b20a2d3..607c001bd7 100644 --- a/Source/Core/Core/PowerPC/Jit64/Jit_Integer.cpp +++ b/Source/Core/Core/PowerPC/Jit64/Jit_Integer.cpp @@ -948,6 +948,31 @@ void Jit64::subfx(UGeckoInstruction inst) if (inst.OE) GenerateConstantOverflow((s64)i - (s64)j); } + else if (gpr.IsImm(a)) + { + s32 j = gpr.SImm32(a); + RCOpArg Rb = gpr.Use(b, RCMode::Read); + RCX64Reg Rd = gpr.Bind(d, RCMode::Write); + RegCache::Realize(Rb, Rd); + + if (d == b) + { + SUB(32, Rd, Imm32(j)); + if (inst.OE) + GenerateOverflow(); + } + else if (Rb.IsSimpleReg() && !inst.OE) + { + LEA(32, Rd, MDisp(Rb.GetSimpleReg(), -j)); + } + else + { + MOV(32, Rd, Rb); + SUB(32, Rd, Imm32(j)); + if (inst.OE) + GenerateOverflow(); + } + } else { RCOpArg Ra = gpr.Use(a, RCMode::Read); @@ -965,10 +990,6 @@ void Jit64::subfx(UGeckoInstruction inst) MOV(32, Rd, Rb); SUB(32, Rd, R(RSCRATCH)); } - else if (Rb.IsSimpleReg() && Ra.IsImm() && !inst.OE) - { - LEA(32, Rd, MDisp(Rb.GetSimpleReg(), -Ra.SImm32())); - } else { MOV(32, Rd, Rb); From 57548b456bd4d551109d0591ad513ab949ca6092 Mon Sep 17 00:00:00 2001 From: Sintendo Date: Mon, 4 Jan 2021 23:54:15 +0100 Subject: [PATCH 2/5] Jit64: subfx - Special case a == 0 Occurs a bunch of times in Super Mario Sunshine. Before: 41 83 EE 00 sub r14d,0 After: Nothing! --- Source/Core/Core/PowerPC/Jit64/Jit_Integer.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Source/Core/Core/PowerPC/Jit64/Jit_Integer.cpp b/Source/Core/Core/PowerPC/Jit64/Jit_Integer.cpp index 607c001bd7..a654b4d776 100644 --- a/Source/Core/Core/PowerPC/Jit64/Jit_Integer.cpp +++ b/Source/Core/Core/PowerPC/Jit64/Jit_Integer.cpp @@ -955,7 +955,14 @@ void Jit64::subfx(UGeckoInstruction inst) RCX64Reg Rd = gpr.Bind(d, RCMode::Write); RegCache::Realize(Rb, Rd); - if (d == b) + if (j == 0) + { + if (d != b) + MOV(32, Rd, Rb); + if (inst.OE) + GenerateConstantOverflow(false); + } + else if (d == b) { SUB(32, Rd, Imm32(j)); if (inst.OE) From b0be20560f0ec41173ac4593aebbf6ef5f940536 Mon Sep 17 00:00:00 2001 From: Sintendo Date: Mon, 4 Jan 2021 23:56:58 +0100 Subject: [PATCH 3/5] Jit64: subfx - Special case b == 0 Happens in Super Mario Sunshine. You could probably do something similar for b == -1 (like we do for subfic), but I couldn't find any titles that do this. - Case 1: d == a Before: 41 8B C7 mov eax,r15d 41 BF 00 00 00 00 mov r15d,0 44 2B F8 sub r15d,eax After: 41 F7 DF neg r15d - Case 2: d != a Before: BF 00 00 00 00 mov edi,0 41 2B FD sub edi,r13d After: 41 8B FD mov edi,r13d F7 DF neg edi --- Source/Core/Core/PowerPC/Jit64/Jit_Integer.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Source/Core/Core/PowerPC/Jit64/Jit_Integer.cpp b/Source/Core/Core/PowerPC/Jit64/Jit_Integer.cpp index a654b4d776..e90859d270 100644 --- a/Source/Core/Core/PowerPC/Jit64/Jit_Integer.cpp +++ b/Source/Core/Core/PowerPC/Jit64/Jit_Integer.cpp @@ -980,6 +980,18 @@ void Jit64::subfx(UGeckoInstruction inst) GenerateOverflow(); } } + else if (gpr.IsImm(b) && gpr.Imm32(b) == 0) + { + RCOpArg Ra = gpr.Use(a, RCMode::Read); + RCX64Reg Rd = gpr.Bind(d, RCMode::Write); + RegCache::Realize(Ra, Rd); + + if (d != a) + MOV(32, Rd, Ra); + NEG(32, Rd); + if (inst.OE) + GenerateOverflow(); + } else { RCOpArg Ra = gpr.Use(a, RCMode::Read); From c0be34aa814cf6e3f4885c9c2a5e07aba6d7ea32 Mon Sep 17 00:00:00 2001 From: Sintendo Date: Tue, 5 Jan 2021 00:08:35 +0100 Subject: [PATCH 4/5] Jit64: subfx - Special case a == b Soul Calibur II does this. Before: 2B F6 sub esi,esi After: Nothing! --- Source/Core/Core/PowerPC/Jit64/Jit_Integer.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Source/Core/Core/PowerPC/Jit64/Jit_Integer.cpp b/Source/Core/Core/PowerPC/Jit64/Jit_Integer.cpp index e90859d270..d018f0604d 100644 --- a/Source/Core/Core/PowerPC/Jit64/Jit_Integer.cpp +++ b/Source/Core/Core/PowerPC/Jit64/Jit_Integer.cpp @@ -941,7 +941,13 @@ void Jit64::subfx(UGeckoInstruction inst) JITDISABLE(bJITIntegerOff); int a = inst.RA, b = inst.RB, d = inst.RD; - if (gpr.IsImm(a) && gpr.IsImm(b)) + if (a == b) + { + gpr.SetImmediate32(d, 0); + if (inst.OE) + GenerateConstantOverflow(false); + } + else if (gpr.IsImm(a, b)) { s32 i = gpr.SImm32(b), j = gpr.SImm32(a); gpr.SetImmediate32(d, i - j); From df70077e6b27608bfdc9812fa0d2bec6c1a4247b Mon Sep 17 00:00:00 2001 From: Sintendo Date: Tue, 5 Jan 2021 18:52:24 +0100 Subject: [PATCH 5/5] JitArm64: subfx - Special case a == b --- Source/Core/Core/PowerPC/JitArm64/JitArm64_Integer.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Source/Core/Core/PowerPC/JitArm64/JitArm64_Integer.cpp b/Source/Core/Core/PowerPC/JitArm64/JitArm64_Integer.cpp index 645ffc4af5..33e8f47450 100644 --- a/Source/Core/Core/PowerPC/JitArm64/JitArm64_Integer.cpp +++ b/Source/Core/Core/PowerPC/JitArm64/JitArm64_Integer.cpp @@ -856,7 +856,13 @@ void JitArm64::subfx(UGeckoInstruction inst) int a = inst.RA, b = inst.RB, d = inst.RD; - if (gpr.IsImm(a) && gpr.IsImm(b)) + if (a == b) + { + gpr.SetImmediate(d, 0); + if (inst.Rc) + ComputeRC0(gpr.GetImm(d)); + } + else if (gpr.IsImm(a) && gpr.IsImm(b)) { u32 i = gpr.GetImm(a), j = gpr.GetImm(b); gpr.SetImmediate(d, j - i);