public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/104110] New: AArch64 unnecessary use of call-preserved register
@ 2022-01-19  6:56 nate at thatsmathematics dot com
  2022-01-19  6:59 ` [Bug target/104110] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: nate at thatsmathematics dot com @ 2022-01-19  6:56 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 104110
           Summary: AArch64 unnecessary use of call-preserved register
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: nate at thatsmathematics dot com
  Target Milestone: ---

gcc misses an optimization (or in some sense deoptimizes) by using a
call-preserved register to save a trivial constant across a function call.

Source code:

void bar(unsigned);
unsigned foo(unsigned c) {
    bar(1U << c);
    return 1;
}

Output from gcc -O3 on AArch64:

foo:
        stp     x29, x30, [sp, -32]!
        mov     x29, sp
        str     x19, [sp, 16]
        mov     w19, 1
        lsl     w0, w19, w0
        bl      bar
        mov     w0, w19
        ldr     x19, [sp, 16]
        ldp     x29, x30, [sp], 32
        ret

Note that x19 is used unnecessarily to save the constant 1 across the function
call, causing an unnecessary push and pop.  It would have been better to just
use some call-clobbered register for the constant 1 before the function call,
and then a simple `mov w0, 1` afterward.\

Same behavior with -O, -O2, -Os.  Tested on godbolt, affects yesterday's trunk
and all the way back to 5.4.

Might be related to bug 70801 or bug 71768 but I am not sure.

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

* [Bug target/104110] AArch64 unnecessary use of call-preserved register
  2022-01-19  6:56 [Bug target/104110] New: AArch64 unnecessary use of call-preserved register nate at thatsmathematics dot com
@ 2022-01-19  6:59 ` pinskia at gcc dot gnu.org
  2022-01-19  7:20 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-19  6:59 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |ra

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This is interesting. The register allocation should have rematerlized the
constant on the other side of the call.

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

* [Bug target/104110] AArch64 unnecessary use of call-preserved register
  2022-01-19  6:56 [Bug target/104110] New: AArch64 unnecessary use of call-preserved register nate at thatsmathematics dot com
  2022-01-19  6:59 ` [Bug target/104110] " pinskia at gcc dot gnu.org
@ 2022-01-19  7:20 ` pinskia at gcc dot gnu.org
  2022-01-19  7:34 ` pinskia at gcc dot gnu.org
  2022-01-19  9:24 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-19  7:20 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Target|aarch64                     |aarch64*-*-* arm*-*-*

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Ok, this is interesting. On PowerPC and x86_64, rtl cse does not do the cse of
the constant but for aarch64 and arm it does ...
There has to be some cost issue.

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

* [Bug target/104110] AArch64 unnecessary use of call-preserved register
  2022-01-19  6:56 [Bug target/104110] New: AArch64 unnecessary use of call-preserved register nate at thatsmathematics dot com
  2022-01-19  6:59 ` [Bug target/104110] " pinskia at gcc dot gnu.org
  2022-01-19  7:20 ` pinskia at gcc dot gnu.org
@ 2022-01-19  7:34 ` pinskia at gcc dot gnu.org
  2022-01-19  9:24 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-19  7:34 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2022-01-19
           Keywords|ra                          |
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Rs6000 has:
      /* On the RS/6000, if it is valid in the insn, it is free.  */
    case CONST_INT:
      if (((outer_code == SET
            || outer_code == PLUS
            || outer_code == MINUS)
           && (satisfies_constraint_I (x)
               || satisfies_constraint_L (x)))


While aarch64 has:
      /* If an instruction can incorporate a constant within the
         instruction, the instruction's expression avoids calling
         rtx_cost() on the constant.  If rtx_cost() is called on a
         constant, then it is usually because the constant must be
         moved into a register by one or more instructions.

         The exception is constant 0, which can be expressed
         as XZR/WZR and is therefore free.  The exception to this is
         if we have (set (reg) (const0_rtx)) in which case we must cost
         the move.  However, we can catch that when we cost the SET, so
         we don't need to consider that here.  */
      if (x == const0_rtx)
        *cost = 0;
      else
        {
          /* To an approximation, building any other constant is
             proportionally expensive to the number of instructions
             required to build that constant.  This is true whether we
             are compiling for SPEED or otherwise.  */
          if (!is_a <scalar_int_mode> (mode, &int_mode))
            int_mode = word_mode;
          *cost = COSTS_N_INSNS (aarch64_internal_mov_immediate
                                 (NULL_RTX, x, false, int_mode));
        }


I think rs6000 definition should be used instead otherwise we get the case
where (set (reg) (reg)) is the same cost as (set (reg) (const_int)) so GCC
decides it is better to do the (set (reg) (reg)) instead of staying with the
(set (reg) (const_int)).

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

* [Bug target/104110] AArch64 unnecessary use of call-preserved register
  2022-01-19  6:56 [Bug target/104110] New: AArch64 unnecessary use of call-preserved register nate at thatsmathematics dot com
                   ` (2 preceding siblings ...)
  2022-01-19  7:34 ` pinskia at gcc dot gnu.org
@ 2022-01-19  9:24 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-19  9:24 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |DUPLICATE

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Dup of bug 86892 really.

*** This bug has been marked as a duplicate of bug 86892 ***

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

end of thread, other threads:[~2022-01-19  9:24 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-19  6:56 [Bug target/104110] New: AArch64 unnecessary use of call-preserved register nate at thatsmathematics dot com
2022-01-19  6:59 ` [Bug target/104110] " pinskia at gcc dot gnu.org
2022-01-19  7:20 ` pinskia at gcc dot gnu.org
2022-01-19  7:34 ` pinskia at gcc dot gnu.org
2022-01-19  9:24 ` 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).