How does the i386 backend optimise the stack slot assignment to minimize the displacement offset?
We don't. We just assign sequential addresses as we allocate stack slots.
; -O0 => large offset leal 8268(%esp), %eax incl (%eax) ; -O3 => small offset incl 40(%esp)
-O3 enables function inlining. With function inlining, we can see that the arrays are unused and we optimize them away. Without the arrays, the stack frames are small, and hence you get small offsets.
FRAME_GROWS_DOWNWARD has little effect on frame offsets. The internal frame layout has more of an effect. Almost all targets grow frames in the same direction as the stack, and since the stack grows downwards here, the frame grows downwards. That is just the natural direction of growth for the frame.
This thread has a stack slot assignment optimisation patch that has never been committed to GCC CVS, but the above indicats that there is some sort of mechanism in GCC already to mitigate this problem... http://gcc.gnu.org/ml/gcc-patches/2003-01/msg00019.html
Yes, this was a real attempt to optimize frame sizes. The existing scheme referred to in that thread is no stack slot assignment optimization at all.
--
Jim Wilson, GNU Tools Support, http://www.SpecifixInc.com