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

            Bug ID: 114262
           Summary: Over-inlining when optimizing for size?
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lh_mouse at 126 dot com
  Target Milestone: ---

(https://gcc.godbolt.org/z/a4ox6oEfT)
```
struct impl;
struct impl* get_impl(int key);
int get_value(struct impl* p);


extern __inline__ __attribute__((__gnu_inline__))
int get_value_by_key(int key)
  {
    struct impl* p = get_impl(key);
    if(!p)
      return -1;
    return get_value(p);
  }

int real_get_value_by_key(int key)
  {
    return get_value_by_key(key);
  }

```

This is actually two functions, one is `gnu_inline` and the other is a
non-inline one. It looks to me that if I mark a function `gnu_inline`, I assert
that 'somewhere I shall provide an external definition for you' so when
optimizing for size, GCC may generate a call instead of using the more complex
inline definition.

The `real_get_value_by_key` function is made a deliberate sibling call, so
ideally this should be
```
real_get_value_by_key:
        jmp     get_value_by_key
```
and not 
```
real_get_value_by_key:
        push    rsi
        call    get_impl
        test    rax, rax
        je      .L2
        mov     rdi, rax
        pop     rcx
        jmp     get_value
.L2:
        or      eax, -1
        pop     rdx
        ret
```

It still gets inlined with `-finline-limit=0` and can only be disabled by
`-fno-inline`. I have no idea how it is controlled.

---------------------------

# Trivia

These are two `gnu_inline` functions from the same library. Most of the time
they should both be inlined in user code. However, external definitions are
required when optimization is not turned on, or when their addresses are taken,
so they must still exist. As they are unlikely to be used  anyway, optimizing
for size makes much more sense.

Reply via email to