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

zhijian lin via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 16 08:44:26 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)));
+  }
----------------
diggerlin wrote:

I do not  think creating a new local lambda function to replace the MBB.insert  will make the code more simple.

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


More information about the llvm-commits mailing list