https://gcc.gnu.org/bugzilla/show_bug.cgi?id=117312
--- Comment #16 from Uroš Bizjak <ubizjak at gmail dot com> ---
(In reply to H. Peter Anvin from comment #15)
> Odd. When I added a read flag intrinsic to my test case, it prevented the
> red zone from being used. If it clobbers the redzone, then that's obviously
> a very serious problem.
The generic compiler part detects stack pointer modification (c.f.
notice_stack_pointer_modification_1 in stack-ptr-mod.cc) and clears
crtl->sp_is_unchanging.
Now, consider the following testcase, derived from x86 linux sources:
--cut here--
register unsigned long current_stack_pointer asm ("sp");
#define ASM_CALL_CONSTRAINT "+r" (current_stack_pointer)
void foo (void)
{
asm volatile ("#" : ASM_CALL_CONSTRAINT);
}
--cut here--
The asm writes to stack pointer, as can be seen from _.final dump:
(insn:TI 5 2 15 2 (parallel [
(set (reg/v:DI 7 sp [ current_stack_pointer ])
(asm_operands/v:DI ("#") ("=r") 0 [
(reg/v:DI 7 sp [ current_stack_pointer ])
]
[
(asm_input:DI ("0") rsp.c:6)
]
[] rsp.c:6))
(clobber (reg:CC 17 flags))
]) "rsp.c":6:3 -1
(expr_list:REG_UNUSED (reg:CC 17 flags)
(nil)))
but middle-end fails to detect this write. This issue can be observed by
putting a breakpoint at ix86_compute_frame_layout and:
Breakpoint 2, ix86_compute_frame_layout () at
../../git/gcc/gcc/config/i386/i386.cc:6876
6876 struct ix86_frame *frame = &cfun->machine->frame;
(gdb) p x_rtl->sp_is_unchanging
$1 = true
Which makes this PR a bug in the target-independent part of the compiler.
I will file a new PR that will obsolete this one. x86 redzone creation depends
on (c.f. ix86_compute_frame_layout):
if (ix86_using_red_zone ()
&& crtl->sp_is_unchanging
&& crtl->is_leaf
&& !ix86_pc_thunk_call_expanded
&& !ix86_current_function_calls_tls_descriptor)
And when crtl->sp_is_unchanging correctly detects stack pointer change in the
asm, the redzone creation (and other things that depend on
crtl->sp_is_unchanging) will be disabled. There is no need for "red-zone"
clobber, existing ASM_CALL_CONSTRAINT should do the trick.