[llvm] [PowerPC]optimize the epilogue for restore non volatile cr fields (PR #202339)

Lei Huang via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 17 08:04:40 PDT 2026


================
@@ -2545,19 +2558,23 @@ static void restoreCRs(bool is31, bool CR2Spilled, bool CR3Spilled,
   MBB.insert(MI,
              addFrameReference(BuildMI(*MF, DL, TII.get(PPC::LWZ), MoveReg),
                                CSI[CSIIndex].getFrameIdx()));
-
-  unsigned RestoreOp = PPC::MTOCRF;
-  if (CR2Spilled)
-    MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR2)
-               .addReg(MoveReg, getKillRegState(!CR3Spilled && !CR4Spilled)));
-
-  if (CR3Spilled)
-    MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR3)
-               .addReg(MoveReg, getKillRegState(!CR4Spilled)));
-
-  if (CR4Spilled)
-    MBB.insert(MI, BuildMI(*MF, DL, TII.get(RestoreOp), PPC::CR4)
-               .addReg(MoveReg, getKillRegState(true)));
+  // Count how many CR fields need restoring
+  unsigned NumCRs =
+      (CR2Spilled ? 1 : 0) + (CR3Spilled ? 1 : 0) + (CR4Spilled ? 1 : 0);
+
+  assert(NumCRs >= 1 &&
+         "Requires at least one non-volatile CR field to be restored.");
+
+  if (NumCRs == 1) {
+    // Use MTOCRF for single field
+    unsigned CRReg = CR2Spilled ? PPC::CR2 : (CR3Spilled ? PPC::CR3 : PPC::CR4);
+    MBB.insert(MI, BuildMI(*MF, DL, TII.get(PPC::MTOCRF), CRReg)
+                       .addReg(MoveReg, getKillRegState(true)));
+  } else {
+    MBB.insert(MI, BuildMI(*MF, DL, TII.get(PPC::MTCRF))
+                       .addImm(255)
+                       .addReg(MoveReg, getKillRegState(true)));
+  }
----------------
lei137 wrote:

no worries.  was thinking we can use the same one, but I see the new update here makes the original comment no longer valid.

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


More information about the llvm-commits mailing list