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

            Bug ID: 93605
           Summary: GCC suboptimal tail call optimization in trivial
                    function forwarding with __attribute__((noinline))
           Product: gcc
           Version: 9.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mike.k at digitalcarbide dot com
  Target Milestone: ---

In a trivial function-forwarder where `__attribute__((noinline))` is specified
on the forwardee, an extra `movzx` instruction is generated (on x86-64) prior
to the tail call. This does not occur on Clang.

Observe (https://godbolt.org/z/kFGCpW):

```
namespace impl {
    __attribute__((noinline))
    static int func (bool v, int a, int b) {
        return v ? a/b : b/a;
    }
}

int func(bool v, int a, int b) {
    return impl::func(v, a, b);
}
```

On all tested versions (trunk (10) to GCC 4), this produces the following
assembly for `func`:

```
func(bool, int, int):
  movzx edi, dil
  jmp impl::func(bool, int, int)
```

On Clang trunk (10) until Clang 5.0.0, this produces the following assembly for
`func`:

```
func(bool, int, int): # @func(bool, int, int)
  jmp impl::func(bool, int, int) # TAILCALL
```

Clang 5.0.0 and below produce identical assembly to GCC:

```
func(bool, int, int): # @func(bool, int, int)
  movzx edi, dil
  jmp impl::func(bool, int, int) # TAILCALL
```

Reply via email to