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

            Bug ID: 127322
           Summary: -fzero-call-used-regs suppressed on normal returns
                    when the function also has __builtin_eh_return
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: 220255623 at seu dot edu.cn
                CC: 220245569 at seu dot edu.cn, jianhao.xu at seu dot edu.cn
  Target Milestone: ---

`-fzero-call-used-regs=used-gpr` is silently skipped on an ordinary return
when the same function also contains `__builtin_eh_return`.

Reproducer:

void f(long secret, int normal, void *handler)
{
  register long r10 asm("r10") = secret;
  asm volatile ("" : "+r"(r10));

  if (normal)
    return;

  __builtin_eh_return(0, handler);
}

Command:

gcc -O2 -fzero-call-used-regs=used-gpr -S trigger.c -o trigger.s

Exit status is 0 with no diagnostics.

Observed normal-return path:

movq    %rdi, %r10
xorl    %ecx, %ecx
testl   %esi, %esi
je      .L7
addq    $16, %rsp
ret

There is no write of zero to %r10 before this ret. The xorl %ecx, %ecx
is part of the __builtin_eh_return stack adjustment, not the requested
scrub of %r10.

Expected:

The ordinary return should contain `xorl %r10d, %r10d` before ret.
Special handling of the __builtin_eh_return exit does not justify skipping
an independent ordinary return.

Unlike a tail-call exit, this ret is executed by f itself and returns
directly to f's caller. There is no subsequent function return that can
provide the missing cleanup. A control function without __builtin_eh_return
does emit `xorl %r10d, %r10d` with the same flags.

Compiler:

gcc (<build package label redacted>) 17.0.0 20260531 (experimental)

Target: x86_64-pc-linux-gnu
Configured with: <source-tree>/configure --prefix=<build-tree>
--disable-bootstrap --disable-multilib --disable-nls --enable-languages=c,c++
--enable-default-pie --enable-cet --disable-werror --enable-checking=release
--with-system-zlib --with-pkgversion=<redacted>
Thread model: posix
gcc version 17.0.0 20260531 (experimental)

Source snapshot: GCC master revision
f20bc4c2fe00928013c533e241b89ae3a6724ca1.

Also reproduced with GCC 13.3.0, 15.2.0, 16.1.0, and 17.0 snapshots
20260426 and 20260531 on x86_64. Other targets were not tested.

Reply via email to