[llvm] [AMDGPU] Fix long-branch jump clobbers callee-saved registers (PR #229427)

Jan Patrick Lehr via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 6 11:47:13 PDT 2026


================
@@ -1824,17 +1824,21 @@ void SIFrameLowering::processFunctionBeforeFrameIndicesReplaced(
     }
   }
   // We initally reserved the highest available SGPR pair for long branches
-  // now, after RA, we shift down to a lower unused one if one exists
-  Register LongBranchReservedReg = FuncInfo->getLongBranchReservedReg();
-  Register UnusedLowSGPR =
-      TRI->findUnusedRegister(MRI, &AMDGPU::SGPR_64RegClass, MF);
+  // now, after RA, we shift down to a lower unused one if one exists.
+  // The prologue is already emitted here, do not use registers that need
+  // to be preserved.
   // If LongBranchReservedReg is null then we didn't find a long branch
   // and never reserved a register to begin with so there is nothing to
-  // shift down. Then if UnusedLowSGPR is null, there isn't available lower
-  // register to use so just keep the original one we set.
-  if (LongBranchReservedReg && UnusedLowSGPR) {
-    FuncInfo->setLongBranchReservedReg(UnusedLowSGPR);
-    MRI.reserveReg(UnusedLowSGPR, TRI);
+  // shift down.
+  if (Register LongBranchReservedReg = FuncInfo->getLongBranchReservedReg()) {
+    Register UnusedLowSGPR = TRI->findUnusedRegister(
+        MRI, &AMDGPU::SGPR_64RegClass, MF, /*ReserveHighestRegister=*/false,
+        /*ExcludeCalleeSaved=*/true);
+    if (UnusedLowSGPR && TRI->getHWRegIndex(UnusedLowSGPR) <
+                             TRI->getHWRegIndex(LongBranchReservedReg)) {
+      FuncInfo->setLongBranchReservedReg(UnusedLowSGPR);
+      MRI.reserveReg(UnusedLowSGPR, TRI);
----------------
jplehr wrote:

I don't know and I think this code is mostly unchanged, so I would guess that it is OK.

The main difference here is that the condition also makes sure that the register is actually shifted down and not up.

https://github.com/llvm/llvm-project/pull/229427


More information about the llvm-commits mailing list