From e8ac1fca801b9dfed3a6e22130bd9932023dd044 Mon Sep 17 00:00:00 2001
From: Connor McLaughlin <stenzek@gmail.com>
Date: Sun, 13 Jun 2021 18:06:28 +1000
Subject: [PATCH] CPU/Recompiler: Optimize bgez zero, addr to unconditional

---
 src/core/cpu_recompiler_code_generator.cpp | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/core/cpu_recompiler_code_generator.cpp b/src/core/cpu_recompiler_code_generator.cpp
index ee0832a04..233939474 100644
--- a/src/core/cpu_recompiler_code_generator.cpp
+++ b/src/core/cpu_recompiler_code_generator.cpp
@@ -2311,11 +2311,15 @@ bool CodeGenerator::Compile_Branch(const CodeBlockInstruction& cbi)
 
       const u8 rt = static_cast<u8>(cbi.instruction.i.rt.GetValue());
       const bool bgez = ConvertToBoolUnchecked(rt & u8(1));
-      const Condition condition = bgez ? Condition::PositiveOrZero : Condition::Negative;
+      const Condition condition = (bgez && cbi.instruction.r.rs == Reg::zero) ?
+                                    Condition::Always :
+                                    (bgez ? Condition::PositiveOrZero : Condition::Negative);
       const bool link = (rt & u8(0x1E)) == u8(0x10);
 
       // Read has to happen before the link as the compare can use ra.
-      Value lhs = m_register_cache.ReadGuestRegisterToScratch(cbi.instruction.i.rs);
+      Value lhs;
+      if (condition != Condition::Always)
+        lhs = m_register_cache.ReadGuestRegisterToScratch(cbi.instruction.i.rs);
 
       // The return address is always written if link is set, regardless of whether the branch is taken.
       if (link)