I think this pass was previously used under the assumption that most
functions would not need a frame pointer and it would be more efficient
to store the old stack pointer in a regular register pair.
Unfortunately, right now we're forced to always reserve the Y register
as a frame pointer: whether or not this is needed is only known after
regsiter allocation at which point it doesn't make sense anymore to mark
it as non-reserved. Therefore, it makes sense to use the Y register to
store the old stack pointer in functions with dynamic allocas (with a
variable size or not in the entry block). Knowing this can make the code
around dynamic allocas a lot simpler: simply save/restore the frame
pointer.
This is especially relevant in functions that have a frame pointer
anyway (for example, because they have stack spills). The stack restore
in the epilogue will implicitly restore the old stack pointer, so there
is no need to store the old stack pointer separately. It even reduces
register pressure as a side effect.
Warning: I haven't really tested this outside of the unit tests (and studying the output assembly). I'm not exactly sure how to test this, maybe by linking some test code with an existing program to check that it works as expected? However, the assembly looks good to me.
I checked compiler-rt and picolibc and both don't have any dynamic stack allocation (as I should have expected).
To be clear: this patch is meant as a refactor/cleanup, not as a codegen improvement (even though it is a codegen improvement).