public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug rtl-optimization/116027] New: Redundant backup and restore of register with -flive-range-shrinkage
@ 2024-07-22  2:19 user202729 at protonmail dot com
  2024-07-22  5:34 ` [Bug rtl-optimization/116027] " pinskia at gcc dot gnu.org
  2024-07-22  6:20 ` user202729 at protonmail dot com
  0 siblings, 2 replies; 3+ messages in thread
From: user202729 at protonmail dot com @ 2024-07-22  2:19 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 116027
           Summary: Redundant backup and restore of register with
                    -flive-range-shrinkage
           Product: gcc
           Version: 15.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: user202729 at protonmail dot com
  Target Milestone: ---

Code:

        __attribute__((pure)) long f(long a, long b);

        long x;

        long g(){
                long y=x;
                long z=f(0, 0);
                return f(y, z);
        }

        long h(){
                long z=f(0, 0);
                long y=x;
                return f(y, z);
        }

Resulting assembly:

        g():
                        push    rbx
                        xor     esi, esi
                        xor     edi, edi
                        call    f(long, long)
                        mov     rdi, QWORD PTR x[rip]
                        pop     rbx
                        mov     rsi, rax
                        jmp     f(long, long)
        h():
                        xor     esi, esi
                        xor     edi, edi
                        call    f(long, long)
                        mov     rdi, QWORD PTR x[rip]
                        mov     rsi, rax
                        jmp     f(long, long)

In function g, the push rbx/pop rbx pair is redundant.

Godbolt: https://godbolt.org/z/os4EYP916

Without -flive-range-shrinkage then both generates optimal code.

The reason for the suboptimal code can be partially traced to the following
code in gcc/ira.cc:

          /* Don't move insns if live range shrinkage or register
                 pressure-sensitive scheduling were done because it will not
                 improve allocation but likely worsen insn scheduling.  */
          if (optimize
                  && !flag_live_range_shrinkage
                  && !(flag_sched_pressure && flag_schedule_insns))
                combine_and_move_insns ();

Without the flag specified, the load of x is moved to after the function call
(which is optimal).

With the flag specified, the move is not done in that pass (but it is optimized
in a subsequent pass anyway, that pass is probably after the pass that removes
redundant stack backup/restores).

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

* [Bug rtl-optimization/116027] Redundant backup and restore of register with -flive-range-shrinkage
  2024-07-22  2:19 [Bug rtl-optimization/116027] New: Redundant backup and restore of register with -flive-range-shrinkage user202729 at protonmail dot com
@ 2024-07-22  5:34 ` pinskia at gcc dot gnu.org
  2024-07-22  6:20 ` user202729 at protonmail dot com
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-07-22  5:34 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
-O2 -mpreferred-stack-boundary=3 -flive-range-shrinkage

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

* [Bug rtl-optimization/116027] Redundant backup and restore of register with -flive-range-shrinkage
  2024-07-22  2:19 [Bug rtl-optimization/116027] New: Redundant backup and restore of register with -flive-range-shrinkage user202729 at protonmail dot com
  2024-07-22  5:34 ` [Bug rtl-optimization/116027] " pinskia at gcc dot gnu.org
@ 2024-07-22  6:20 ` user202729 at protonmail dot com
  1 sibling, 0 replies; 3+ messages in thread
From: user202729 at protonmail dot com @ 2024-07-22  6:20 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from user202729 <user202729 at protonmail dot com> ---
Maybe this is a less artificial example.

__attribute__((pure)) long f(__int128 a, long b);

__int128 x;

long g(){
    __int128 y=x;
    long z=f(0, 0);
    x=1;
    return f(y, z);
}

long h(){
    long z=f(0, 0);
    __int128 y=x;
    x=1;
    return f(y, z);
}


Compile with -O2.

Assembly:

g():
        push    r14  // <-- redundant
        xor     edx, edx
        xor     edi, edi
        xor     esi, esi
        push    rbx  // <-- redundant
        sub     rsp, 8
        call    f(__int128, long)
        mov     rdi, QWORD PTR x[rip]
        mov     rsi, QWORD PTR x[rip+8]
        mov     QWORD PTR x[rip], 1
        mov     QWORD PTR x[rip+8], 0
        add     rsp, 8
        mov     rdx, rax
        pop     rbx  // <-- redundant
        pop     r14  // <-- redundant
        jmp     f(__int128, long)
h():
        sub     rsp, 8
        xor     edx, edx
        xor     edi, edi
        xor     esi, esi
        call    f(__int128, long)
        mov     rdi, QWORD PTR x[rip]
        mov     rsi, QWORD PTR x[rip+8]
        mov     QWORD PTR x[rip], 1
        mov     QWORD PTR x[rip+8], 0
        mov     rdx, rax
        add     rsp, 8
        jmp     f(__int128, long)

Godbolt: https://godbolt.org/z/E1cnrEWzs

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

end of thread, other threads:[~2024-07-22  6:20 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-07-22  2:19 [Bug rtl-optimization/116027] New: Redundant backup and restore of register with -flive-range-shrinkage user202729 at protonmail dot com
2024-07-22  5:34 ` [Bug rtl-optimization/116027] " pinskia at gcc dot gnu.org
2024-07-22  6:20 ` user202729 at protonmail dot com

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).