public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/95126] New: Missed opportunity to turn static variables into immediates
@ 2020-05-14 10:16 pskocik at gmail dot com
  2020-05-14 11:47 ` [Bug c/95126] " rguenth at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: pskocik at gmail dot com @ 2020-05-14 10:16 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 95126
           Summary: Missed opportunity to turn static variables into
                    immediates
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pskocik at gmail dot com
  Target Milestone: ---

Example:

For:

struct small{ short a,b; signed char c; };

void call_func(void)
{
        extern int func(struct small X);
        static struct small const s = { 1,2,0 };
        func(s);
}

clang renders (x86_64):

0000000000000000 <call_func>:
   0:   bf 01 00 02 00          mov    edi,0x20001
   5:   e9 00 00 00 00          jmp    a <call_func+0xa>        6:
R_X86_64_PLT32       func-0x4

whereas gcc renders:

0000000000000000 <call_func>:
   0:   0f b7 3d 00 00 00 00    movzx  edi,WORD PTR [rip+0x0]        # 7
<call_func+0x7>        3: R_X86_64_PC32        .rodata-0x2
   7:   0f b7 05 00 00 00 00    movzx  eax,WORD PTR [rip+0x0]        # e
<call_func+0xe>        a: R_X86_64_PC32        .rodata-0x4
   e:   48 c1 e7 10             shl    rdi,0x10
  12:   48 09 f8                or     rax,rdi
  15:   0f b7 3d 00 00 00 00    movzx  edi,WORD PTR [rip+0x0]        # 1c
<call_func+0x1c>      18: R_X86_64_PC32       .rodata
  1c:   48 c1 e7 20             shl    rdi,0x20
  20:   48 09 c7                or     rdi,rax
  23:   e9 00 00 00 00          jmp    28 <call_func+0x28>      24:
R_X86_64_PLT32      func-0x4


https://gcc.godbolt.org/z/Qxq6Rh

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

* [Bug c/95126] Missed opportunity to turn static variables into immediates
  2020-05-14 10:16 [Bug c/95126] New: Missed opportunity to turn static variables into immediates pskocik at gmail dot com
@ 2020-05-14 11:47 ` rguenth at gcc dot gnu.org
  2021-12-13  4:56 ` [Bug middle-end/95126] [9/10/11/12 Regression] " pinskia at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-05-14 11:47 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
           Keywords|                            |missed-optimization
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2020-05-14

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
Confirmed.  Only RTL expansion sees the aggregate copy involved with the
function
call and this, when folded from a constant initializer, is not subject to
clever things such as merging of stores.

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

* [Bug middle-end/95126] [9/10/11/12 Regression] Missed opportunity to turn static variables into immediates
  2020-05-14 10:16 [Bug c/95126] New: Missed opportunity to turn static variables into immediates pskocik at gmail dot com
  2020-05-14 11:47 ` [Bug c/95126] " rguenth at gcc dot gnu.org
@ 2021-12-13  4:56 ` pinskia at gcc dot gnu.org
  2021-12-13  5:02 ` pinskia at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-12-13  4:56 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |needs-bisection
           Severity|normal                      |enhancement
            Summary|Missed opportunity to turn  |[9/10/11/12 Regression]
                   |static variables into       |Missed opportunity to turn
                   |immediates                  |static variables into
                   |                            |immediates
   Target Milestone|---                         |9.5
      Known to work|                            |5.1.0
      Known to fail|                            |5.2.0

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Hmm, in GCC 5.1.0 (and before) we used to produce:
        mov     rdi, QWORD PTR s.1839[rip]
        jmp     func

In GCC 5.2.0 and above GCC produces:

        movzx   edi, WORD PTR s.1839[rip+2]
        movzx   edx, WORD PTR s.1839[rip]
        sal     rdi, 16
        mov     rax, rdi
        movzx   edi, WORD PTR s.1839[rip+4]
        or      rax, rdx
        sal     rdi, 32
        or      rdi, rax
        jmp     func

In both cases, the expander sees:
  <bb 2>:
  func (s); [tail call]
  return;

But the expander goes bad.
So this has to be a patch which was backported to the GCC 5 branch which caused
to produce even worse code.

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

* [Bug middle-end/95126] [9/10/11/12 Regression] Missed opportunity to turn static variables into immediates
  2020-05-14 10:16 [Bug c/95126] New: Missed opportunity to turn static variables into immediates pskocik at gmail dot com
  2020-05-14 11:47 ` [Bug c/95126] " rguenth at gcc dot gnu.org
  2021-12-13  4:56 ` [Bug middle-end/95126] [9/10/11/12 Regression] " pinskia at gcc dot gnu.org
@ 2021-12-13  5:02 ` pinskia at gcc dot gnu.org
  2022-01-21 13:34 ` rguenth at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-12-13  5:02 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=65408,
                   |                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=58744,
                   |                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=36043

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #2)
> So this has to be a patch which was backported to the GCC 5 branch which
> caused to produce even worse code.

The patch which made the worse code was r6-52-g984b20545fee1 which was
backported to the GCC 5 branch.

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

* [Bug middle-end/95126] [9/10/11/12 Regression] Missed opportunity to turn static variables into immediates
  2020-05-14 10:16 [Bug c/95126] New: Missed opportunity to turn static variables into immediates pskocik at gmail dot com
                   ` (2 preceding siblings ...)
  2021-12-13  5:02 ` pinskia at gcc dot gnu.org
@ 2022-01-21 13:34 ` rguenth at gcc dot gnu.org
  2022-02-08  5:38 ` amodra at gmail dot com
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-01-21 13:34 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|needs-bisection             |
                 CC|                            |amodra at gcc dot gnu.org
            Version|unknown                     |11.2.1
           Priority|P3                          |P2

--- Comment #4 from Richard Biener <rguenth at gcc dot gnu.org> ---
Indeed this seems to be the offending revision - Alan, can you have a look?

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

* [Bug middle-end/95126] [9/10/11/12 Regression] Missed opportunity to turn static variables into immediates
  2020-05-14 10:16 [Bug c/95126] New: Missed opportunity to turn static variables into immediates pskocik at gmail dot com
                   ` (3 preceding siblings ...)
  2022-01-21 13:34 ` rguenth at gcc dot gnu.org
@ 2022-02-08  5:38 ` amodra at gmail dot com
  2022-02-26 22:07 ` roger at nextmovesoftware dot com
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: amodra at gmail dot com @ 2022-02-08  5:38 UTC (permalink / raw)
  To: gcc-bugs

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

Alan Modra <amodra at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |amodra at gmail dot com

--- Comment #5 from Alan Modra <amodra at gmail dot com> ---
The gcc-5.1.0 code is incorrect.  It loads past the end of s, which is only
2-byte aligned.

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

* [Bug middle-end/95126] [9/10/11/12 Regression] Missed opportunity to turn static variables into immediates
  2020-05-14 10:16 [Bug c/95126] New: Missed opportunity to turn static variables into immediates pskocik at gmail dot com
                   ` (4 preceding siblings ...)
  2022-02-08  5:38 ` amodra at gmail dot com
@ 2022-02-26 22:07 ` roger at nextmovesoftware dot com
  2022-05-27  9:42 ` [Bug middle-end/95126] [10/11/12/13 " rguenth at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: roger at nextmovesoftware dot com @ 2022-02-26 22:07 UTC (permalink / raw)
  To: gcc-bugs

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

Roger Sayle <roger at nextmovesoftware dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |roger at nextmovesoftware dot com
                 CC|                            |roger at nextmovesoftware dot com
             Status|NEW                         |ASSIGNED

--- Comment #6 from Roger Sayle <roger at nextmovesoftware dot com> ---
Patch proposed.
https://gcc.gnu.org/pipermail/gcc-patches/2022-February/590949.html

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

* [Bug middle-end/95126] [10/11/12/13 Regression] Missed opportunity to turn static variables into immediates
  2020-05-14 10:16 [Bug c/95126] New: Missed opportunity to turn static variables into immediates pskocik at gmail dot com
                   ` (5 preceding siblings ...)
  2022-02-26 22:07 ` roger at nextmovesoftware dot com
@ 2022-05-27  9:42 ` rguenth at gcc dot gnu.org
  2022-06-04 11:23 ` cvs-commit at gcc dot gnu.org
  2022-06-10 13:55 ` roger at nextmovesoftware dot com
  8 siblings, 0 replies; 10+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-05-27  9:42 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|9.5                         |10.4

--- Comment #7 from Richard Biener <rguenth at gcc dot gnu.org> ---
GCC 9 branch is being closed

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

* [Bug middle-end/95126] [10/11/12/13 Regression] Missed opportunity to turn static variables into immediates
  2020-05-14 10:16 [Bug c/95126] New: Missed opportunity to turn static variables into immediates pskocik at gmail dot com
                   ` (6 preceding siblings ...)
  2022-05-27  9:42 ` [Bug middle-end/95126] [10/11/12/13 " rguenth at gcc dot gnu.org
@ 2022-06-04 11:23 ` cvs-commit at gcc dot gnu.org
  2022-06-10 13:55 ` roger at nextmovesoftware dot com
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-06-04 11:23 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Roger Sayle <sayle@gcc.gnu.org>:

https://gcc.gnu.org/g:ed6fd2aed58f2cca99f15331bf68999c0e6df370

commit r13-990-ged6fd2aed58f2cca99f15331bf68999c0e6df370
Author: Roger Sayle <roger@nextmovesoftware.com>
Date:   Sat Jun 4 12:21:51 2022 +0100

    PR middle-end/95126: Expand small const structs as immediate constants.

    This patch resolves PR middle-end/95126 which is a code quality regression,
    by teaching the RTL expander to emit small const structs/unions as integer
    immediate constants.

    The motivating example from the bugzilla PR is:

    struct small{ short a,b; signed char c; };
    extern int func(struct small X);
    void call_func(void)
    {
        static struct small const s = { 1, 2, 0 };
        func(s);
    }

    which on x86_64 is currently compiled to:

    call_func:
            movzwl  s.0+2(%rip), %eax
            movzwl  s.0(%rip), %edx
            movzwl  s.0+4(%rip), %edi
            salq    $16, %rax
            orq     %rdx, %rax
            salq    $32, %rdi
            orq     %rax, %rdi
            jmp     func

    but with this patch is now optimized to:

    call_func:
            movl    $131073, %edi
            jmp     func

    2022-06-04  Roger Sayle  <roger@nextmovesoftware.com>

    gcc/ChangeLog
            PR middle-end/95126
            * calls.cc (load_register_parameters): When loading a suitable
            immediate_const_ctor_p VAR_DECL into a single word_mode register,
            construct it directly in a pseudo rather than read it (by parts)
            from memory.
            * expr.cc (int_expr_size): Make tree argument a const_tree.
            (immediate_const_ctor_p): Helper predicate.  Return true for
            simple constructors that may be materialized in a register.
            (expand_expr_real_1) [VAR_DECL]: When expanding a constant
            VAR_DECL with a suitable immediate_const_ctor_p constructor
            use store_constructor to materialize it directly in a pseudo.
            * expr.h (immediate_const_ctor_p): Prototype here.
            * varasm.cc (initializer_constant_valid_for_bitfield_p): Change
            VALUE argument from tree to const_tree.
            * varasm.h (initializer_constant_valid_for_bitfield_p): Update
            prototype.

    gcc/testsuite/ChangeLog
            PR middle-end/95126
            * gcc.target/i386/pr95126-m32-1.c: New test case.
            * gcc.target/i386/pr95126-m32-2.c: New test case.
            * gcc.target/i386/pr95126-m32-3.c: New test case.
            * gcc.target/i386/pr95126-m32-4.c: New test case.
            * gcc.target/i386/pr95126-m64-1.c: New test case.
            * gcc.target/i386/pr95126-m64-2.c: New test case.
            * gcc.target/i386/pr95126-m64-3.c: New test case.
            * gcc.target/i386/pr95126-m64-4.c: New test case.

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

* [Bug middle-end/95126] [10/11/12/13 Regression] Missed opportunity to turn static variables into immediates
  2020-05-14 10:16 [Bug c/95126] New: Missed opportunity to turn static variables into immediates pskocik at gmail dot com
                   ` (7 preceding siblings ...)
  2022-06-04 11:23 ` cvs-commit at gcc dot gnu.org
@ 2022-06-10 13:55 ` roger at nextmovesoftware dot com
  8 siblings, 0 replies; 10+ messages in thread
From: roger at nextmovesoftware dot com @ 2022-06-10 13:55 UTC (permalink / raw)
  To: gcc-bugs

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

Roger Sayle <roger at nextmovesoftware dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|10.4                        |13.0
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED

--- Comment #9 from Roger Sayle <roger at nextmovesoftware dot com> ---
This should now be fixed on mainline.

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

end of thread, other threads:[~2022-06-10 13:55 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-14 10:16 [Bug c/95126] New: Missed opportunity to turn static variables into immediates pskocik at gmail dot com
2020-05-14 11:47 ` [Bug c/95126] " rguenth at gcc dot gnu.org
2021-12-13  4:56 ` [Bug middle-end/95126] [9/10/11/12 Regression] " pinskia at gcc dot gnu.org
2021-12-13  5:02 ` pinskia at gcc dot gnu.org
2022-01-21 13:34 ` rguenth at gcc dot gnu.org
2022-02-08  5:38 ` amodra at gmail dot com
2022-02-26 22:07 ` roger at nextmovesoftware dot com
2022-05-27  9:42 ` [Bug middle-end/95126] [10/11/12/13 " rguenth at gcc dot gnu.org
2022-06-04 11:23 ` cvs-commit at gcc dot gnu.org
2022-06-10 13:55 ` roger at nextmovesoftware 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).