From acda42be1625b985351f9f33df9bb1094906262b Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Wed, 12 May 2021 02:26:58 +1000 Subject: [PATCH] CPU/Recompiler: Get rid of non-constant offsetofs --- src/core/cpu_code_cache.cpp | 2 +- src/core/cpu_core.h | 3 +++ src/core/cpu_recompiler_code_generator.cpp | 23 ++++++++----------- src/core/cpu_recompiler_code_generator.h | 1 - .../cpu_recompiler_code_generator_generic.cpp | 4 ++-- 5 files changed, 15 insertions(+), 18 deletions(-) diff --git a/src/core/cpu_code_cache.cpp b/src/core/cpu_code_cache.cpp index e0e4892fb..99d43f80d 100644 --- a/src/core/cpu_code_cache.cpp +++ b/src/core/cpu_code_cache.cpp @@ -500,7 +500,7 @@ recompile: { block->recompile_count++; - if (block->recompile_count >= RECOMPILE_COUNT_TO_FALL_BACK_TO_INTERPRETER) + if (block->recompile_count >= RECOMPILE_COUNT_TO_FALL_BACK_TO_INTERPRETER&&false) { Log_PerfPrintf("Block 0x%08X has been recompiled %u times in %u frames, falling back to interpreter", block->GetPC(), block->recompile_count, frame_diff); diff --git a/src/core/cpu_core.h b/src/core/cpu_core.h index 69b8b9518..288a899fa 100644 --- a/src/core/cpu_core.h +++ b/src/core/cpu_core.h @@ -84,6 +84,9 @@ struct State std::array dcache = {}; std::array icache_tags = {}; std::array icache_data = {}; + + static constexpr u32 GPRRegisterOffset(u32 index) { return offsetof(State, regs.r) + (sizeof(u32) * index); } + static constexpr u32 GTERegisterOffset(u32 index) { return offsetof(State, gte_regs.r32) + (sizeof(u32) * index); } }; extern State g_state; diff --git a/src/core/cpu_recompiler_code_generator.cpp b/src/core/cpu_recompiler_code_generator.cpp index f659b71f7..363fb87f7 100644 --- a/src/core/cpu_recompiler_code_generator.cpp +++ b/src/core/cpu_recompiler_code_generator.cpp @@ -14,11 +14,6 @@ Log_SetChannel(CPU::Recompiler); namespace CPU::Recompiler { -u32 CodeGenerator::CalculateRegisterOffset(Reg reg) -{ - return u32(offsetof(State, regs.r[0]) + (static_cast(reg) * sizeof(u32))); -} - bool CodeGenerator::CompileBlock(CodeBlock* block, CodeBlock::HostCodePointer* out_host_code, u32* out_host_code_size) { // TODO: Align code buffer. @@ -2627,7 +2622,7 @@ Value CodeGenerator::DoGTERegisterRead(u32 index) default: { - EmitLoadCPUStructField(value.host_reg, RegSize_32, offsetof(State, gte_regs.r32[index])); + EmitLoadCPUStructField(value.host_reg, RegSize_32, State::GTERegisterOffset(index)); } break; } @@ -2656,7 +2651,7 @@ void CodeGenerator::DoGTERegisterWrite(u32 index, const Value& value) { // sign-extend z component of vector registers Value temp = ConvertValueSize(value.ViewAsSize(RegSize_16), RegSize_32, true); - EmitStoreCPUStructField(offsetof(State, gte_regs.r32[index]), temp); + EmitStoreCPUStructField(State::GTERegisterOffset(index), temp); return; } break; @@ -2669,7 +2664,7 @@ void CodeGenerator::DoGTERegisterWrite(u32 index, const Value& value) { // zero-extend unsigned values Value temp = ConvertValueSize(value.ViewAsSize(RegSize_16), RegSize_32, false); - EmitStoreCPUStructField(offsetof(State, gte_regs.r32[index]), temp); + EmitStoreCPUStructField(State::GTERegisterOffset(index), temp); return; } break; @@ -2680,15 +2675,15 @@ void CodeGenerator::DoGTERegisterWrite(u32 index, const Value& value) Value temp = m_register_cache.AllocateScratch(RegSize_32); // SXY0 <- SXY1 - EmitLoadCPUStructField(temp.host_reg, RegSize_32, offsetof(State, gte_regs.r32[13])); - EmitStoreCPUStructField(offsetof(State, gte_regs.r32[12]), temp); + EmitLoadCPUStructField(temp.host_reg, RegSize_32, State::GTERegisterOffset(13)); + EmitStoreCPUStructField(State::GTERegisterOffset(12), temp); // SXY1 <- SXY2 - EmitLoadCPUStructField(temp.host_reg, RegSize_32, offsetof(State, gte_regs.r32[14])); - EmitStoreCPUStructField(offsetof(State, gte_regs.r32[13]), temp); + EmitLoadCPUStructField(temp.host_reg, RegSize_32, State::GTERegisterOffset(14)); + EmitStoreCPUStructField(State::GTERegisterOffset(13), temp); // SXY2 <- SXYP - EmitStoreCPUStructField(offsetof(State, gte_regs.r32[14]), value); + EmitStoreCPUStructField(State::GTERegisterOffset(14), value); return; } break; @@ -2711,7 +2706,7 @@ void CodeGenerator::DoGTERegisterWrite(u32 index, const Value& value) default: { // written as-is, 2x16 or 1x32 bits - EmitStoreCPUStructField(offsetof(State, gte_regs.r32[index]), value); + EmitStoreCPUStructField(State::GTERegisterOffset(index), value); return; } } diff --git a/src/core/cpu_recompiler_code_generator.h b/src/core/cpu_recompiler_code_generator.h index c7d03a8af..8d6210ee3 100644 --- a/src/core/cpu_recompiler_code_generator.h +++ b/src/core/cpu_recompiler_code_generator.h @@ -21,7 +21,6 @@ public: CodeGenerator(JitCodeBuffer* code_buffer); ~CodeGenerator(); - static u32 CalculateRegisterOffset(Reg reg); static const char* GetHostRegName(HostReg reg, RegSize size = HostPointerSize); static void AlignCodeBuffer(JitCodeBuffer* code_buffer); diff --git a/src/core/cpu_recompiler_code_generator_generic.cpp b/src/core/cpu_recompiler_code_generator_generic.cpp index 613dd14d3..73b146b62 100644 --- a/src/core/cpu_recompiler_code_generator_generic.cpp +++ b/src/core/cpu_recompiler_code_generator_generic.cpp @@ -9,13 +9,13 @@ namespace CPU::Recompiler { void CodeGenerator::EmitLoadGuestRegister(HostReg host_reg, Reg guest_reg) { - EmitLoadCPUStructField(host_reg, RegSize_32, CalculateRegisterOffset(guest_reg)); + EmitLoadCPUStructField(host_reg, RegSize_32, State::GPRRegisterOffset(static_cast(guest_reg))); } void CodeGenerator::EmitStoreGuestRegister(Reg guest_reg, const Value& value) { DebugAssert(value.size == RegSize_32); - EmitStoreCPUStructField(CalculateRegisterOffset(guest_reg), value); + EmitStoreCPUStructField(State::GPRRegisterOffset(static_cast(guest_reg)), value); } void CodeGenerator::EmitStoreInterpreterLoadDelay(Reg reg, const Value& value)