This is an archive of the discontinued LLVM Phabricator instance.

[ARM] Save and restore CPSR around tMOVimm32
ClosedPublic

Authored by olista01 on Aug 21 2023, 3:43 AM.

Details

Summary

When resolving a frame index with a large offset for v6M execute-only,
we emit a tMOVimm32 pseudo-instruction, which later gets lowered to a
sequence of instructions, all of which are flag-setting. However, a
frame index may be generated for a register spill or reload instruction,
which can be inserted at a point where CPSR is live. This patch inserts
MRS and MSR instructions around the tMOVimm32 to save and restore the
value of CPSR, if CPSR is live at that point.

This may need up to two virtual registers (one to build the immediate
value, one to save CPSR) during frame index lowering, which happens
after register allocation, so we need to ensure two spill slots are
avilable to the register scavenger to ensure it can free up enough
registers for this.

There is no test for the emission (or not) of the MRS/MSR pair, because
it requires a spill or reload to be inserted at a point where CPSR is
live, which requires a large, complex function and is fragile enough
that any optimisation changes will break the test. This bug was easily
found by csmith with -verify-machineinstrs, which I now run regularly on
v6M execute-only (and many other combinations).

Patch by John Brawn and myself.

Diff Detail

Event Timeline

olista01 created this revision.Aug 21 2023, 3:43 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 21 2023, 3:43 AM
olista01 requested review of this revision.Aug 21 2023, 3:43 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 21 2023, 3:43 AM
stuij accepted this revision.Aug 24 2023, 4:23 AM

LGTM

This revision is now accepted and ready to land.Aug 24 2023, 4:23 AM
This revision was landed with ongoing or failed builds.Aug 24 2023, 6:15 AM
This revision was automatically updated to reflect the committed changes.