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

            Bug ID: 95781
           Summary: Missing dead code elimination when a recursive
                    function is inlined.
           Product: gcc
           Version: 10.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: other
          Assignee: unassigned at gcc dot gnu.org
          Reporter: yyc1992 at gmail dot com
  Target Milestone: ---

Code,

```
static int ffff2(int *p, int k)
{
    int res = 0;
    if (k > 0)
        res += ffff2(p, k - 1);
    return *p + res;
}

int g2(int *p)
{
    return ffff2(p, 3);
}
```

Compiling with -O3 the code produced for `g2` is

```
g2:
        movl    (%rdi), %eax
        sall    $2, %eax
        ret
```

i.e. `*p * 4` that doesn't need to call `ffff2`. However, the code for `ffff2`
is still generated even though it is never used.

It seems that this only happens when the recursive function is sufficiently
complex. Replacing `*p` with a constant or making the `k > 0` branch returning
directly produces code that does not have `ffff2` in it. Seems that there's
some smart late optimization pass that doesn't have a global DCE pass
afterwards?

Looks similar to https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80680 but I'm not
sure if they have the same root cause.

Reply via email to