[llvm-branch-commits] [llvm] [PowerPC] Model the CR defs of MTCRF used to restore callee-saved CRs (PR #242763)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Sat Nov 14 21:45:33 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-backend-powerpc
Author: actink (actinks)
<details>
<summary>Changes</summary>
The epilogue restores the non-volatile CR fields (CR2-CR4) with a single `mtcrf`. MTCRF takes only an FXM mask immediate and a GPR, and declares no CR operands, so the post-RA scheduler sees no dependency between the restore and instructions that read or write the restored CR bits. It can therefore hoist the restore above those reads, making the function use the caller's condition bits instead of its own.
Add the restored CR fields as implicit defs when building the MTCRF in `emitEpilogue()` and `restoreCRs()`, matching the way the prologue already adds implicit uses to MFCR. The scheduler now models the WAR/WAW dependencies and keeps the restore after the last read.
This is a regression from #<!-- -->202339 (3fc6711ccaf6), which replaced the per-field MTOCRF sequence -- each MTOCRF defines the CR field it restores -- with a single MTCRF, which declares no CR defs.
Fixes #<!-- -->242250.
Testing:
- `llvm/test/CodeGen/PowerPC/mtcrf-cr-restore.ll` passes with the fix and fails without it; CHECK lines are generated by `update_llc_test_checks.py`, and the test runs with `-verify-machineinstrs`.
- Related CR save/restore tests pass: `crsave.ll`, `aix-crspill.ll`, `2010-02-12-saveCR.ll`, `p10-spill-creq/crgt/crun.ll`, `ppc64-rop-protection{,-aix}.ll`, `ppc64-crsave.mir`, `aix32-crsave.mir`.
---
Full diff: https://github.com/llvm/llvm-project/pull/242763.diff
2 Files Affected:
- (modified) llvm/lib/Target/PowerPC/PPCFrameLowering.cpp (+21-6)
- (modified) llvm/test/CodeGen/PowerPC/mtcrf-cr-restore.ll (+2-5)
``````````diff
diff --git a/llvm/lib/Target/PowerPC/PPCFrameLowering.cpp b/llvm/lib/Target/PowerPC/PPCFrameLowering.cpp
index 6d8300fc8faa26..d59c02806997b2 100644
--- a/llvm/lib/Target/PowerPC/PPCFrameLowering.cpp
+++ b/llvm/lib/Target/PowerPC/PPCFrameLowering.cpp
@@ -1781,9 +1781,16 @@ void PPCFrameLowering::emitEpilogue(MachineFunction &MF,
for (unsigned CRField : MustSaveCRs) {
CRMask |= 0x80 >> (CRField - PPC::CR0);
}
- BuildMI(MBB, InsertPt, dl, TII.get(isPPC64 ? PPC::MTCRF8 : PPC::MTCRF))
- .addImm(CRMask)
- .addReg(SrcReg, getKillRegState(true));
+ MachineInstrBuilder MIB =
+ BuildMI(MBB, InsertPt, dl,
+ TII.get(isPPC64 ? PPC::MTCRF8 : PPC::MTCRF))
+ .addImm(CRMask)
+ .addReg(SrcReg, getKillRegState(true));
+ // MTCRF does not statically model which CR fields it writes. Add the
+ // restored fields as implicit defs so that the post-RA scheduler treats
+ // them as writes (and doesn't move MTCRF above reads of those fields).
+ for (unsigned CRField : MustSaveCRs)
+ MIB.addDef(CRField, RegState::Implicit);
}
};
@@ -2581,9 +2588,17 @@ static void restoreCRs(bool is31, bool CR2Spilled, bool CR3Spilled,
CRMask |= 0x10;
if (CR4Spilled)
CRMask |= 0x08;
- MBB.insert(MI, BuildMI(*MF, DL, TII.get(PPC::MTCRF))
- .addImm(CRMask)
- .addReg(MoveReg, getKillRegState(true)));
+ MachineInstrBuilder MIB = BuildMI(*MF, DL, TII.get(PPC::MTCRF))
+ .addImm(CRMask)
+ .addReg(MoveReg, getKillRegState(true));
+ // Add the restored CR fields as implicit defs (see BuildMoveToCR).
+ if (CR2Spilled)
+ MIB.addDef(PPC::CR2, RegState::Implicit);
+ if (CR3Spilled)
+ MIB.addDef(PPC::CR3, RegState::Implicit);
+ if (CR4Spilled)
+ MIB.addDef(PPC::CR4, RegState::Implicit);
+ MBB.insert(MI, MIB);
}
}
diff --git a/llvm/test/CodeGen/PowerPC/mtcrf-cr-restore.ll b/llvm/test/CodeGen/PowerPC/mtcrf-cr-restore.ll
index 8a0dd5d8e4e1da..8d2c859eed76fa 100644
--- a/llvm/test/CodeGen/PowerPC/mtcrf-cr-restore.ll
+++ b/llvm/test/CodeGen/PowerPC/mtcrf-cr-restore.ll
@@ -3,10 +3,7 @@
;
; The epilogue restores the non-volatile CR fields with mtcrf. The post-RA
; scheduler must not move that restore above reads of the CR bits it writes.
-;
-; FIXME: MTCRF does not model the CR fields it writes, so the restore is
-; currently hoisted above some CR2/CR3 reads. Update this test once MTCRF
-; models its defs. See https://github.com/llvm/llvm-project/issues/242250.
+; See https://github.com/llvm/llvm-project/issues/242250.
define signext i32 @restore_cr_after_uses(i128 %value, i1 %cond) {
; CHECK-LABEL: restore_cr_after_uses:
@@ -37,8 +34,8 @@ define signext i32 @restore_cr_after_uses(i128 %value, i1 %cond) {
; CHECK-NEXT: li 3, 1
; CHECK-NEXT: crandc 20, 8, 30
; CHECK-NEXT: lwz 12, 8(1)
-; CHECK-NEXT: mtcrf 48, 12
; CHECK-NEXT: crand 22, 30, 12
+; CHECK-NEXT: mtcrf 48, 12
; CHECK-NEXT: crnor 20, 22, 20
; CHECK-NEXT: isel 4, 0, 3, 20
; CHECK-NEXT: crandc 20, 24, 6
``````````
</details>
https://github.com/llvm/llvm-project/pull/242763
More information about the llvm-branch-commits
mailing list