From cde3a3b44865d6654884f7c9be691fdf64bc9927 Mon Sep 17 00:00:00 2001 From: Sintendo Date: Sun, 12 Jan 2020 23:48:16 +0100 Subject: [PATCH] x64Emitter: Avoid 8-bit displacement when possible Due to the way the ModRM encoding works on x86, memory addressing combinations involving RBP or R13 need an additional byte for an 8-bit displacement of zero. However, this was also applied in cases where it is unnecessary, effectively wasting a byte. - MatR with RSP or R12 8B 44 24 00 mov eax,dword ptr [rsp] 8B 04 24 mov eax,dword ptr [rsp] - MRegSum with base != RBP or R13 46 8D 7C 37 00 lea r15d,[rdi+r14] 46 8D 3C 37 lea r15d,[rdi+r14] - MComplex without offset 8B 4C CA 00 mov ecx,dword ptr [rdx+rcx*8] 8B 0C CA mov ecx,dword ptr [rdx+rcx*8] --- Source/Core/Common/x64Emitter.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Source/Core/Common/x64Emitter.cpp b/Source/Core/Common/x64Emitter.cpp index 947e6e5614..169ffdc519 100644 --- a/Source/Core/Common/x64Emitter.cpp +++ b/Source/Core/Common/x64Emitter.cpp @@ -323,7 +323,11 @@ void OpArg::WriteRest(XEmitter* emit, int extraBytes, X64Reg _operandReg, // Okay, we're fine. Just disp encoding. // We need displacement. Which size? int ioff = (int)(s64)offset; - if (ioff < -128 || ioff > 127) + if (ioff == 0 && (_offsetOrBaseReg & 7) != 5) + { + mod = 0; + } + else if (ioff < -128 || ioff > 127) { mod = 2; // 32-bit displacement }