This is an archive of the discontinued LLVM Phabricator instance.

[ARM] v6-M XO: save CPSR around LoadStackGuard
ClosedPublic

Authored by simonwallis2 on Aug 3 2023, 12:13 AM.

Details

Summary

For Thumb-1 Execute-Only, expandLoadStackGuardBase generates a tMOVimm32 pseudo when calculating the stack offset.
It does this in a context where the CSPR maybe be live. tMOVimm32 may corrupt CPSR.
To fix this, generate save/restore CPSR around the tMOVimm32 using MRS/MSR to/from a scratch register.

expandLoadStackGuardBase this runs after register allocation, so the scratch register needs to be a physical register.
Use R12 as a scratch register, as is usual when expanding a pseudo.
MSR/MRS are some of the few v6-M instructions which operate on a high register.

New stack-guard test case added which was generating incorrect code without the save/restore CPSR.

Diff Detail

Event Timeline

simonwallis2 created this revision.Aug 3 2023, 12:13 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 3 2023, 12:13 AM
simonwallis2 requested review of this revision.Aug 3 2023, 12:13 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 3 2023, 12:13 AM
stuij added a comment.Aug 3 2023, 3:22 AM

As a general FYI, when uploading a patch, please keep the whole context of the file, so people can move up and down the file in Phab, instead of being presented with Context not available.. I'm not sure what your process is of creating a review (I use Arc myself), but it'd be something like:
git format-patch -k -U999999 @{u}.

llvm/test/CodeGen/Thumb/stack-guard-xo-sub64.ll
1

I would put this test in stack-guard-xo.ll. One test per file seems a bit too granular to me.

Same patch, recreated correctly this time with the full diff context.

Moved the new test from its new file stack-guard-xo-sub64.ll into the existing test file stack-guard-xo.ll
Adjusted stack-guard-xo.ll to be slightly less sensitive to stack offsets.

simonwallis2 marked an inline comment as done.Aug 3 2023, 6:31 AM
stuij accepted this revision.Aug 8 2023, 2:31 AM

LGTM. Code generation isn't stellar, but the stack guard / XO combo is already quite horrible. Let's wait with attempting optimizations until we hear of an actual need.

This revision is now accepted and ready to land.Aug 8 2023, 2:31 AM
This revision was automatically updated to reflect the committed changes.