[llvm] [AVR] No cli for SPWRITE on XMEGA (PR #147210)
Tom Vijlbrief via llvm-commits
llvm-commits at lists.llvm.org
Tue Jul 8 03:56:53 PDT 2025
================
@@ -2531,27 +2531,46 @@ bool AVRExpandPseudo::expand<AVR::SPWRITE>(Block &MBB, BlockIt MBBI) {
unsigned Flags = MI.getFlags();
TRI->splitReg(SrcReg, SrcLoReg, SrcHiReg);
- buildMI(MBB, MBBI, AVR::INRdA)
- .addReg(STI.getTmpRegister(), RegState::Define)
- .addImm(STI.getIORegSREG())
- .setMIFlags(Flags);
-
- buildMI(MBB, MBBI, AVR::BCLRs).addImm(0x07).setMIFlags(Flags);
-
- buildMI(MBB, MBBI, AVR::OUTARr)
- .addImm(0x3e)
- .addReg(SrcHiReg, getKillRegState(SrcIsKill))
- .setMIFlags(Flags);
-
- buildMI(MBB, MBBI, AVR::OUTARr)
- .addImm(STI.getIORegSREG())
- .addReg(STI.getTmpRegister(), RegState::Kill)
- .setMIFlags(Flags);
-
- buildMI(MBB, MBBI, AVR::OUTARr)
- .addImm(0x3d)
- .addReg(SrcLoReg, getKillRegState(SrcIsKill))
- .setMIFlags(Flags);
+ // From the XMEGA series manual:
+ // To prevent corruption when updating the stack pointer from software,
+ // a write to SPL will automatically disable interrupts
+ // for up to four instructions or until the next I/O memory write.
+ if (STI.getELFArch() >= 102) { // An XMEGA device
+
+ buildMI(MBB, MBBI, AVR::OUTARr)
+ .addImm(0x3d)
+ .addReg(SrcLoReg, getKillRegState(SrcIsKill))
+ .setMIFlags(Flags);
+
+ buildMI(MBB, MBBI, AVR::OUTARr)
+ .addImm(0x3e)
+ .addReg(SrcHiReg, getKillRegState(SrcIsKill))
+ .setMIFlags(Flags);
+
+ } else { // Disable interrupts for older devices (3 extra instructions)
+
+ buildMI(MBB, MBBI, AVR::INRdA)
----------------
tomtor wrote:
@benshi001 Reversing the SPL/SPH order is functionally correct, but it DOES now break some existing tests! Keeping the reverse order, will keep the tests happy, but will require the original code with has some duplicated code. My original PR was totally clean, introduced minimal changes and all tests did pass. I am really not happy how this PR is progressing, due to implementing some of your requested changes. I will not rewrite the existing now failing tests, my time is not free, and I would like to contribute to LLVM/AVR development, but it is hard to remain enthousiastic. So I suggest to leave the PR at is was, with optionally just your suggestion of replacing SPL/SPH constants with `STI.getIORegSPH/L()` added (NB not MY original code), for which I will write a new commit, after I get some positive feedback from you. FYI: @Patryk27 @aykevl
https://github.com/llvm/llvm-project/pull/147210
More information about the llvm-commits
mailing list