public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/114185] New: Missed tail-call optimization due to an argument whose address is taken
@ 2024-03-01  6:25 dizhao at os dot amperecomputing.com
  2024-03-01  6:28 ` [Bug middle-end/114185] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: dizhao at os dot amperecomputing.com @ 2024-03-01  6:25 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 114185
           Summary: Missed tail-call optimization due to an argument whose
                    address is taken
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dizhao at os dot amperecomputing.com
  Target Milestone: ---

GCC (with "-Ofast") doesn't perform tail call optimization on a function like:

    long test_func (long n, unsigned long u64arg,
                    unsigned int u32arg, unsigned long *p)
    {
      if (n > 0)
        return test_func (n - 1, u64arg + (unsigned long) u32arg, u32arg, p);
      else
        return &u64arg - p;
    }

llvm can optimize the tail call.

GCC gave up tail call optimization, because the following check in
find_tail_calls() failed on argument "u64arg":

              /* The parameter should be a real operand, so that phi node
                 created for it at the start of the function has the meaning
                 of copying the value.  This test implies is_gimple_reg_type
                 from the previous condition, however this one could be
                 relaxed by being more careful with copying the new value
                 of the parameter (emitting appropriate GIMPLE_ASSIGN and
                 updating the virtual operands).  */
              if (!is_gimple_reg (param))
                break;

The check was to fix this ICE:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=93487 . But in this case,
"&u64arg" won't be used in PHI argument, so it seems ok for tail call
optimization. (BTW, I tried current GCC trunk on the example code in PR 93487,
but haven't encounter the ICE with that check removed, because the PHI
arguments are "&y" and "&x" now.)

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

* [Bug middle-end/114185] Missed tail-call optimization due to an argument whose address is taken
  2024-03-01  6:25 [Bug middle-end/114185] New: Missed tail-call optimization due to an argument whose address is taken dizhao at os dot amperecomputing.com
@ 2024-03-01  6:28 ` pinskia at gcc dot gnu.org
  2024-03-01  6:31 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-01  6:28 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This code is undefined if the if is not taken.

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

* [Bug middle-end/114185] Missed tail-call optimization due to an argument whose address is taken
  2024-03-01  6:25 [Bug middle-end/114185] New: Missed tail-call optimization due to an argument whose address is taken dizhao at os dot amperecomputing.com
  2024-03-01  6:28 ` [Bug middle-end/114185] " pinskia at gcc dot gnu.org
@ 2024-03-01  6:31 ` pinskia at gcc dot gnu.org
  2024-03-01  7:08 ` dizhao at os dot amperecomputing.com
  2024-03-01  7:15 ` rguenth at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-03-01  6:31 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Also the PR # you gave is wrong.

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

* [Bug middle-end/114185] Missed tail-call optimization due to an argument whose address is taken
  2024-03-01  6:25 [Bug middle-end/114185] New: Missed tail-call optimization due to an argument whose address is taken dizhao at os dot amperecomputing.com
  2024-03-01  6:28 ` [Bug middle-end/114185] " pinskia at gcc dot gnu.org
  2024-03-01  6:31 ` pinskia at gcc dot gnu.org
@ 2024-03-01  7:08 ` dizhao at os dot amperecomputing.com
  2024-03-01  7:15 ` rguenth at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: dizhao at os dot amperecomputing.com @ 2024-03-01  7:08 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Di Zhao <dizhao at os dot amperecomputing.com> ---
Sorry, the old tracker for the code is PR 17749 .

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

* [Bug middle-end/114185] Missed tail-call optimization due to an argument whose address is taken
  2024-03-01  6:25 [Bug middle-end/114185] New: Missed tail-call optimization due to an argument whose address is taken dizhao at os dot amperecomputing.com
                   ` (2 preceding siblings ...)
  2024-03-01  7:08 ` dizhao at os dot amperecomputing.com
@ 2024-03-01  7:15 ` rguenth at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: rguenth at gcc dot gnu.org @ 2024-03-01  7:15 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |missed-optimization

--- Comment #4 from Richard Biener <rguenth at gcc dot gnu.org> ---
The tail recursion optimization doesn't support non-register argument passing.

Of course the example is also broken as Andrew says.

A "better" example might be taking the address with like

  asm ("":: "g" (&u64arg));

Better make it meaningfully do sth with no invisible side-effects or escaping
of the address.

We should be pretty good these days with not setting TREE_ADDRESSABLE
unnecessarily (but I guess DECL_NOT_GIMPLE_REG should also inhibit
tail-recursion).

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

end of thread, other threads:[~2024-03-01  7:15 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-01  6:25 [Bug middle-end/114185] New: Missed tail-call optimization due to an argument whose address is taken dizhao at os dot amperecomputing.com
2024-03-01  6:28 ` [Bug middle-end/114185] " pinskia at gcc dot gnu.org
2024-03-01  6:31 ` pinskia at gcc dot gnu.org
2024-03-01  7:08 ` dizhao at os dot amperecomputing.com
2024-03-01  7:15 ` rguenth 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).