public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug other/95781] New: Missing dead code elimination when a recursive function is inlined.
@ 2020-06-20  5:43 yyc1992 at gmail dot com
  2020-06-22  7:55 ` [Bug ipa/95781] " rguenth at gcc dot gnu.org
  2021-09-02  4:26 ` pinskia at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: yyc1992 at gmail dot com @ 2020-06-20  5:43 UTC (permalink / raw)
  To: gcc-bugs

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.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug ipa/95781] Missing dead code elimination when a recursive function is inlined.
  2020-06-20  5:43 [Bug other/95781] New: Missing dead code elimination when a recursive function is inlined yyc1992 at gmail dot com
@ 2020-06-22  7:55 ` rguenth at gcc dot gnu.org
  2021-09-02  4:26 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-06-22  7:55 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |hubicka at gcc dot gnu.org,
                   |                            |marxin at gcc dot gnu.org
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
           Keywords|                            |missed-optimization
          Component|other                       |ipa
   Last reconfirmed|                            |2020-06-22

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
Confirmed.  The recursion is only removed late via tail-recursion but then
cgraph processing order is unfortunate in the sense that we don't have a
IPA phase pass after optimizations and thus dead functions are only
removed reliably outside of (original) cgraph cycles.

For multiple reasons we want to be able to separate actual assembly
output from assembly production thus eventually a per symtab node
assembler fragment and late "IPA" assembling of the assembler file
might help here.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug ipa/95781] Missing dead code elimination when a recursive function is inlined.
  2020-06-20  5:43 [Bug other/95781] New: Missing dead code elimination when a recursive function is inlined yyc1992 at gmail dot com
  2020-06-22  7:55 ` [Bug ipa/95781] " rguenth at gcc dot gnu.org
@ 2021-09-02  4:26 ` pinskia at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-09-02  4:26 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
   Last reconfirmed|2020-06-22 00:00:00         |2021-9-1

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-09-02  4:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-20  5:43 [Bug other/95781] New: Missing dead code elimination when a recursive function is inlined yyc1992 at gmail dot com
2020-06-22  7:55 ` [Bug ipa/95781] " rguenth at gcc dot gnu.org
2021-09-02  4:26 ` pinskia at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).