On Tue, Nov 5, 2024 at 6:08 PM Jakub Jelinek <ja...@redhat.com> wrote: > > On Tue, Nov 05, 2024 at 05:56:09PM +0100, Uros Bizjak wrote: > > > Maybe never make functions having any volatile asm() "leaf"? Thus > > > require 'volatile' to be present - aka the asm has side-effects that > > > are not fully encoded in the constraints. > > > > I think this would work! > > > > Although this *might* be somehow a big hammer approach, disabling a > > I think it will unnecessarily punish lots of code in the wild. > Most asm volatile doesn't do calls in it, or if they do, they have learned > in the past 20 years that on x86_64 there is a red zone and they need to > first subtract red zone size from sp and restore it at the end.
The difference between redzoned and non-redzoned code is *two* instructions that decrease and increase stack pointer, which I think is an acceptable compromise between correctness and performance. Also important is that the approach works for all targets. My two eurocents... Attached RFC patch is all it is needed for the testcase to work correctly. Uros.
diff --git a/gcc/final.cc b/gcc/final.cc index 11141f2b56c..7d8733a2d7d 100644 --- a/gcc/final.cc +++ b/gcc/final.cc @@ -4044,7 +4044,8 @@ asm_fprintf (FILE *file, const char *p, ...) va_end (argptr); } -/* Return true if this function has no function calls. */ +/* Return true if this function has no function calls, + volatile asms and UNSPEC_VOLATILE instructions. */ bool leaf_function_p (void) @@ -4070,6 +4071,9 @@ leaf_function_p (void) && CALL_P (XVECEXP (PATTERN (insn), 0, 0)) && ! SIBLING_CALL_P (XVECEXP (PATTERN (insn), 0, 0))) return false; + if (NONJUMP_INSN_P (insn) + && volatile_insn_p (PATTERN(insn))) + return false; } return true;