https://gcc.gnu.org/bugzilla/show_bug.cgi?id=111107

--- Comment #26 from LIU Hao <lh_mouse at 126 dot com> ---
(In reply to Zeb Figura from comment #25)
> That's the difference between -mpreferred-stack-boundary and
> -mincoming-stack-boundary; I'm asking about -mstackrealign.

i386.opt:

   mstackrealign
   Target Var(ix86_force_align_arg_pointer)
   Realign stack in prologue.


... which is used here:

   static unsigned int
   ix86_minimum_incoming_stack_boundary (bool sibcall)
   {
     unsigned int incoming_stack_boundary;

     /* Stack of interrupt handler is aligned to 128 bits in 64bit mode.  */
     if (cfun->machine->func_type != TYPE_NORMAL)
       incoming_stack_boundary = TARGET_64BIT ? 128 : MIN_STACK_BOUNDARY;
     /* Prefer the one specified at command line. */
     else if (ix86_user_incoming_stack_boundary)
       incoming_stack_boundary = ix86_user_incoming_stack_boundary;
     /* In 32bit, use MIN_STACK_BOUNDARY for incoming stack boundary
        if -mstackrealign is used, it isn't used for sibcall check and
        estimated stack alignment is 128bit.  */
     else if (!sibcall
           && ix86_force_align_arg_pointer
           && crtl->stack_alignment_estimated == 128)
       incoming_stack_boundary = MIN_STACK_BOUNDARY;
     else
       incoming_stack_boundary = ix86_default_incoming_stack_boundary;


In this case, not an interrupt handler, no `-mincoming-stack-boundary` from
command line, no `-mstackrealign`; so `incoming_stack_boundary` is set to
`ix86_default_incoming_stack_boundary`, which is `PREFERRED_STACK_BOUNDARY`.

Thus, if we decrease default preferred stack boundary to 32,
`incoming_stack_boundary` will also be decreased accordingly, unless overridden
by some other options.


> > In i386-options.cc there's
> > 
> >    ix86_default_incoming_stack_boundary = PREFERRED_STACK_BOUNDARY;
> > 
> > Would it make any sense to decrease the preferred stack boundary to `32`
> > (like below)? Maybe it's too late, but Microsoft code has always been
> > assuming 4-byte alignment, thus a 16-byte alignment is always unpredictable.
> > 
> >    #undef PREFERRED_STACK_BOUNDARY_DEFAULT
> >    #define PREFERRED_STACK_BOUNDARY_DEFAULT \
> >      (TARGET_64BIT ? 128 : MIN_STACK_BOUNDARY)
> 
> Yes, that's what I would advocate for. gcc should assume 4-byte alignment
> and it should preserve 4-byte alignment.

After a little testing, I think `-mincoming-stack-boundary=2
-mpreferred-stack-boundary=2` should fix the issue. I'm gonna propose a patch
for such a change after my bootstrap completes.

Reply via email to